1 package net.sf.mavenhist.persistency;
2
3 import java.math.BigDecimal;
4 import java.text.ParseException;
5 import java.text.SimpleDateFormat;
6 import java.util.Calendar;
7 import java.util.Date;
8
9 import org.apache.commons.lang.StringUtils;
10
11 /**
12 * This class allows to set the values only with <code>String</code>s. This is needed to wrap the
13 * beans with jelly.
14 */
15 public class StringMetric extends Metric {
16
17 /**
18 * Date format to load {@link String} as dates.
19 */
20 private SimpleDateFormat datePattern = new SimpleDateFormat("yyyy-MM-dd");
21
22 /**
23 * Default constructor for creation as bean.
24 */
25 public StringMetric() {
26 }
27
28 /**
29 * Creates a new metric.
30 *
31 * @param metricName name.
32 * @param metricValue value as <code>String</code>.
33 * @param date date as <code>String</code>.
34 * @param groupId group id
35 * @param artifactName artifcat name
36 * @param scope scope
37 * @param buildNumber build number
38 */
39 public StringMetric(
40 String metricName,
41 String metricValue,
42 String date,
43 String groupId,
44 String artifactName,
45 String scope,
46 String buildNumber) {
47 super(metricName, null, null, groupId, artifactName, scope, buildNumber);
48 setDateString(date);
49 setValue(metricValue);
50 }
51
52 /**
53 * Sets the date and parses it with the pattern "yyyy-MM-dd". If the date is
54 * not valid, it will be set to the current date.
55 *
56 * @param date to set
57 */
58 public void setDateString(String date) {
59 Date newDate = Calendar.getInstance().getTime();
60 if (!StringUtils.isEmpty(date)) {
61 try {
62 newDate = datePattern.parse(date);
63 } catch (ParseException e) {
64 System.out.println("Could not parse date: '" + date + "'");
65 }
66 }
67 setDate(newDate);
68 }
69
70 /**
71 * Preprocesses string value.
72 * @param s input string
73 * @return preprocessed value.
74 */
75 private String preprocessValue(String s) {
76
77 s = s.trim();
78
79 s = StringUtils.stripEnd(s, "%");
80
81 if ("NaN".equalsIgnoreCase(s)) {
82 s = "";
83 }
84 if ("-".equals(s)) {
85 s = "";
86 }
87 return s;
88 }
89
90 /**
91 * Sets the metric value by converting the given string argument to a big
92 * decimal, calls {@link #setMetricValue(BigDecimal)}. If the string value
93 * is not parseable it will simply log it and set <code>null</code> as
94 * value.
95 *
96 * @param s number to be set as <code>String</code>
97 */
98 public void setValue(String s) {
99
100 String parsedS = preprocessValue(s);
101 if (StringUtils.isEmpty(parsedS)) {
102 System.out.println("Not setting value: " + s);
103 } else {
104 BigDecimal value = null;
105 try {
106 value = new BigDecimal(parsedS);
107 setMetricValue(value);
108 } catch (NumberFormatException e) {
109 System.out.println("Could not parse value: " + s);
110 }
111 }
112 }
113
114 /**
115 * Sets the date pattern to parse the date string from the <code>setDateString</code>-method.
116 *
117 * @param pattern patterns.
118 */
119 public void setDatePattern(String pattern) {
120 if (!StringUtils.isEmpty(pattern)) {
121 datePattern = new SimpleDateFormat(pattern);
122 }
123 }
124 }