| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
package org.itracker.model; |
| 20 |
|
|
| 21 |
|
import java.io.Serializable; |
| 22 |
|
import java.text.ParseException; |
| 23 |
|
import java.text.SimpleDateFormat; |
| 24 |
|
import java.util.ArrayList; |
| 25 |
|
import java.util.Comparator; |
| 26 |
|
import java.util.Iterator; |
| 27 |
|
import java.util.List; |
| 28 |
|
import java.util.Locale; |
| 29 |
|
import java.util.ResourceBundle; |
| 30 |
|
|
| 31 |
|
import org.apache.commons.lang.builder.CompareToBuilder; |
| 32 |
|
import org.apache.commons.lang.builder.ToStringBuilder; |
| 33 |
|
import org.apache.log4j.Logger; |
| 34 |
|
import org.itracker.services.exceptions.IssueException; |
| 35 |
|
import org.itracker.services.util.CustomFieldUtilities; |
| 36 |
|
import org.itracker.web.taglib.FormatCustomFieldTag; |
| 37 |
|
import org.jfree.util.Log; |
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
@author |
| 70 |
|
@see |
| 71 |
|
@see |
| 72 |
|
|
|
|
|
| 16.7% |
Uncovered Elements: 65 (78) |
Complexity: 31 |
Complexity Density: 0.67 |
|
| 73 |
|
public class CustomField extends AbstractEntity implements Comparable<Entity> { |
| 74 |
|
|
| 75 |
|
private static final Logger logger = Logger.getLogger(CustomField.class); |
| 76 |
|
|
| 77 |
|
public static final Comparator<CustomField> NAME_COMPARATOR = new NameComparator(); |
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
public static final SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("dd/MM/yyyy"); |
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
private static final long serialVersionUID = 1L; |
| 86 |
|
|
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
private Type type; |
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
|
private String dateFormat; |
| 101 |
|
|
| 102 |
|
|
| 103 |
|
|
| 104 |
|
|
| 105 |
|
|
| 106 |
|
private boolean required; |
| 107 |
|
|
| 108 |
|
|
| 109 |
|
|
| 110 |
|
|
| 111 |
|
|
| 112 |
|
|
| 113 |
|
|
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 121 |
|
|
| 122 |
|
private List<CustomFieldValue> options = new ArrayList<CustomFieldValue>(); |
| 123 |
|
|
| 124 |
|
|
| 125 |
|
|
| 126 |
|
@link |
| 127 |
|
|
| 128 |
|
private boolean sortOptionsByName; |
| 129 |
|
|
| 130 |
|
|
| 131 |
|
|
| 132 |
|
|
| 133 |
|
|
| 134 |
|
|
| 135 |
|
|
| 136 |
|
|
| 137 |
|
|
| 138 |
|
|
| 139 |
|
|
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
| 143 |
|
|
| 144 |
|
|
| 145 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 146 |
3
|
public CustomField() {... |
| 147 |
|
} |
| 148 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 149 |
0
|
@Deprecated... |
| 150 |
|
public CustomField(String name, Type type) { |
| 151 |
|
|
| 152 |
0
|
setFieldType(type); |
| 153 |
|
} |
| 154 |
|
|
| 155 |
|
|
| 156 |
|
|
| 157 |
|
|
| 158 |
|
|
| 159 |
|
|
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
|
|
| 164 |
|
|
| 165 |
|
|
| 166 |
|
|
| 167 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 168 |
0
|
public Type getFieldType() {... |
| 169 |
0
|
return type; |
| 170 |
|
} |
| 171 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 172 |
1
|
public void setFieldType(Type type) {... |
| 173 |
1
|
this.type = type; |
| 174 |
|
} |
| 175 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 176 |
0
|
public String getDateFormat() {... |
| 177 |
0
|
return dateFormat; |
| 178 |
|
} |
| 179 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 180 |
1
|
public void setDateFormat(String dateFormat) {... |
| 181 |
1
|
this.dateFormat = dateFormat; |
| 182 |
|
} |
| 183 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 184 |
0
|
public boolean isRequired() {... |
| 185 |
0
|
return required; |
| 186 |
|
} |
| 187 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 188 |
1
|
public void setRequired(boolean required) {... |
| 189 |
1
|
this.required = required; |
| 190 |
|
} |
| 191 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 192 |
1
|
public List<CustomFieldValue> getOptions() {... |
| 193 |
1
|
return options; |
| 194 |
|
} |
| 195 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 196 |
0
|
public void setOptions(List<CustomFieldValue> options) {... |
| 197 |
0
|
this.options = options; |
| 198 |
|
} |
| 199 |
|
|
| 200 |
|
|
| 201 |
|
|
| 202 |
|
|
| 203 |
|
|
| 204 |
|
|
| 205 |
|
|
| 206 |
|
|
| 207 |
|
|
| 208 |
|
|
| 209 |
|
|
| 210 |
|
@param |
| 211 |
|
|
| 212 |
|
@param |
| 213 |
|
|
| 214 |
|
|
| 215 |
|
@deprecated |
| 216 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 217 |
2
|
public void addOption(String value, String label) {... |
| 218 |
2
|
this.options.add(new CustomFieldValue(this, value)); |
| 219 |
|
} |
| 220 |
|
|
| 221 |
|
|
| 222 |
|
|
| 223 |
|
|
| 224 |
|
@deprecated |
| 225 |
|
@link |
| 226 |
|
@param |
| 227 |
|
|
| 228 |
|
@return |
| 229 |
|
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 4 |
Complexity Density: 0.5 |
|
| 230 |
0
|
public String getOptionNameByValue(String optionValue) {... |
| 231 |
0
|
final Iterator<CustomFieldValue> iter = this.options.iterator(); |
| 232 |
|
|
| 233 |
0
|
if (logger.isDebugEnabled()) { |
| 234 |
0
|
logger.warn("getOptionNameByValue: called deprecated api", new RuntimeException()); |
| 235 |
|
} |
| 236 |
0
|
while (iter.hasNext()) { |
| 237 |
0
|
CustomFieldValue option = iter.next(); |
| 238 |
|
|
| 239 |
0
|
if (option.getValue().equalsIgnoreCase(optionValue)) { |
| 240 |
0
|
return CustomFieldUtilities.getCustomFieldOptionName(option, null); |
| 241 |
|
} |
| 242 |
|
} |
| 243 |
0
|
return ""; |
| 244 |
|
} |
| 245 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 246 |
0
|
public boolean isSortOptionsByName() {... |
| 247 |
0
|
return sortOptionsByName; |
| 248 |
|
} |
| 249 |
|
|
| 250 |
|
|
| 251 |
|
@param |
| 252 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 253 |
1
|
public void setSortOptionsByName(boolean sortOptionsByName) {... |
| 254 |
1
|
this.sortOptionsByName = sortOptionsByName; |
| 255 |
|
} |
| 256 |
|
|
| 257 |
|
|
| 258 |
|
|
| 259 |
|
|
| 260 |
|
|
| 261 |
|
|
| 262 |
|
|
| 263 |
|
|
| 264 |
|
|
| 265 |
|
|
| 266 |
|
|
| 267 |
|
|
| 268 |
|
|
| 269 |
|
|
| 270 |
|
|
| 271 |
|
|
| 272 |
|
|
| 273 |
|
|
| 274 |
|
|
| 275 |
|
|
| 276 |
|
|
| 277 |
|
|
| 278 |
|
|
| 279 |
|
|
| 280 |
|
|
| 281 |
|
|
| 282 |
|
|
| 283 |
|
|
| 284 |
|
|
| 285 |
|
|
| 286 |
|
|
| 287 |
|
|
| 288 |
|
|
| 289 |
|
|
| 290 |
|
|
| 291 |
|
|
| 292 |
|
|
| 293 |
|
|
| 294 |
|
|
| 295 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 296 |
0
|
@Override... |
| 297 |
|
public String toString() { |
| 298 |
|
|
| 299 |
0
|
return new ToStringBuilder(this) |
| 300 |
|
.append("id", getId()) |
| 301 |
|
.append("type", getFieldType()) |
| 302 |
|
.append("sortOptionsByName", isSortOptionsByName()).toString(); |
| 303 |
|
} |
| 304 |
|
|
| 305 |
|
|
| 306 |
|
|
| 307 |
|
|
| 308 |
|
|
| 309 |
|
|
| 310 |
|
|
| 311 |
|
|
| 312 |
|
|
| 313 |
|
|
| 314 |
|
|
| 315 |
|
|
| 316 |
|
|
| 317 |
|
|
| 318 |
|
|
| 319 |
|
|
| 320 |
|
|
| 321 |
|
|
| 322 |
|
|
| 323 |
|
|
| 324 |
|
|
| 325 |
|
|
| 326 |
|
|
| 327 |
|
|
| 328 |
|
|
| 329 |
|
|
| 330 |
|
|
| 331 |
|
|
| 332 |
|
@param |
| 333 |
|
|
| 334 |
|
@param |
| 335 |
|
@param |
| 336 |
|
@throws |
| 337 |
|
|
| 338 |
|
@see |
| 339 |
|
|
|
|
|
| 0% |
Uncovered Elements: 35 (35) |
Complexity: 13 |
Complexity Density: 0.52 |
|
| 340 |
0
|
public void checkAssignable(String value, Locale locale,... |
| 341 |
|
ResourceBundle bundle) throws IssueException { |
| 342 |
|
|
| 343 |
|
|
| 344 |
0
|
if (this.isRequired() && (value == null || value.trim().length() == 0)) { |
| 345 |
0
|
throw new IssueException("Value is required.", IssueException.TYPE_CF_REQ_FIELD); |
| 346 |
|
} |
| 347 |
|
|
| 348 |
0
|
switch (this.type) { |
| 349 |
|
|
| 350 |
0
|
case INTEGER: |
| 351 |
0
|
try { |
| 352 |
0
|
Integer.parseInt(value); |
| 353 |
|
} catch (NumberFormatException nfe) { |
| 354 |
0
|
throw new IssueException("Invalid integer.", |
| 355 |
|
IssueException.TYPE_CF_PARSE_NUM); |
| 356 |
|
} |
| 357 |
0
|
break; |
| 358 |
|
|
| 359 |
0
|
case DATE: |
| 360 |
0
|
if (this.dateFormat != CustomFieldUtilities.DATE_FORMAT_UNKNOWN) { |
| 361 |
0
|
SimpleDateFormat format = |
| 362 |
|
|
| 363 |
|
new SimpleDateFormat(bundle |
| 364 |
|
.getString("itracker.dateformat." + this.dateFormat), |
| 365 |
|
locale); |
| 366 |
|
|
| 367 |
0
|
try { |
| 368 |
0
|
format.parse(value); |
| 369 |
|
} catch (ParseException ex) { |
| 370 |
0
|
throw new IssueException("Invalid date format.", |
| 371 |
|
IssueException.TYPE_CF_PARSE_DATE); |
| 372 |
|
} |
| 373 |
|
} |
| 374 |
0
|
break; |
| 375 |
|
|
| 376 |
0
|
case LIST: |
| 377 |
0
|
Iterator<CustomFieldValue> it = getOptions().iterator(); |
| 378 |
0
|
while (it.hasNext()) { |
| 379 |
0
|
CustomFieldValue customFieldValue = (CustomFieldValue) it |
| 380 |
|
.next(); |
| 381 |
0
|
if (customFieldValue.getValue().equalsIgnoreCase(value)) { |
| 382 |
0
|
return; |
| 383 |
|
} |
| 384 |
|
} |
| 385 |
0
|
if (Log.isDebugEnabled()) { |
| 386 |
0
|
Log.debug("checkAssignable: could not assign value to custom field values: " + value + ", " + getOptions()); |
| 387 |
|
} |
| 388 |
0
|
throw new IssueException("Invalid value.", IssueException.TYPE_CF_INVALID_LIST_OPTION); |
| 389 |
0
|
default: |
| 390 |
|
|
| 391 |
|
} |
| 392 |
|
} |
| 393 |
|
|
| 394 |
|
|
| 395 |
|
|
| 396 |
|
|
|
|
|
| 33.3% |
Uncovered Elements: 12 (18) |
Complexity: 8 |
Complexity Density: 0.57 |
|
| 397 |
|
public static enum Type implements IntCodeEnum<Type> { |
| 398 |
|
|
| 399 |
|
STRING(1), INTEGER(2), DATE(3), LIST(4); |
| 400 |
|
|
| 401 |
|
private final int code; |
| 402 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 403 |
4
|
private Type(int code) {... |
| 404 |
4
|
this.code = code; |
| 405 |
|
} |
| 406 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 407 |
0
|
public int getCode() {... |
| 408 |
0
|
return code; |
| 409 |
|
} |
| 410 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 411 |
0
|
public Type fromCode(int code) {... |
| 412 |
0
|
return Type.valueOf(code); |
| 413 |
|
} |
| 414 |
|
|
|
|
|
| 27.3% |
Uncovered Elements: 8 (11) |
Complexity: 5 |
Complexity Density: 0.45 |
|
| 415 |
1
|
public static Type valueOf(int code) {... |
| 416 |
1
|
switch (code) { |
| 417 |
0
|
case 1: |
| 418 |
0
|
return STRING; |
| 419 |
1
|
case 2: |
| 420 |
1
|
return INTEGER; |
| 421 |
0
|
case 3: |
| 422 |
0
|
return DATE; |
| 423 |
0
|
case 4: |
| 424 |
0
|
return LIST; |
| 425 |
0
|
default: |
| 426 |
0
|
throw new IllegalArgumentException("Unknown code : " + code); |
| 427 |
|
} |
| 428 |
|
} |
| 429 |
|
|
| 430 |
|
} |
| 431 |
|
|
| 432 |
|
|
| 433 |
|
|
| 434 |
|
|
| 435 |
|
|
| 436 |
|
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
| 437 |
|
public static enum DateFormat { |
| 438 |
|
|
| 439 |
|
DATE_TIME("full"), DATE("dateonly"), TIME("timeonly"); |
| 440 |
|
|
| 441 |
|
final String code; |
| 442 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 443 |
0
|
DateFormat(String code) {... |
| 444 |
0
|
this.code = code; |
| 445 |
|
} |
| 446 |
|
|
| 447 |
|
} |
| 448 |
|
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 3 |
Complexity Density: 1 |
|
| 449 |
|
public static final class NameComparator implements Comparator<CustomField>, Serializable { |
| 450 |
|
|
| 451 |
|
|
| 452 |
|
|
| 453 |
|
private static final long serialVersionUID = 1L; |
| 454 |
|
|
| 455 |
|
private final Locale locale; |
| 456 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 457 |
1
|
public NameComparator() {... |
| 458 |
1
|
this(null); |
| 459 |
|
} |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 460 |
1
|
public NameComparator(Locale locale) {... |
| 461 |
1
|
this.locale = locale; |
| 462 |
|
} |
| 463 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 464 |
0
|
public int compare(CustomField o1, CustomField o2) {... |
| 465 |
0
|
return new CompareToBuilder().append( |
| 466 |
|
CustomFieldUtilities.getCustomFieldName(o1.getId(), locale), |
| 467 |
|
CustomFieldUtilities.getCustomFieldName(o2.getId(), locale)) |
| 468 |
|
.append(o1.getId(), o2.getId()) |
| 469 |
|
.toComparison(); |
| 470 |
|
} |
| 471 |
|
|
| 472 |
|
} |
| 473 |
|
|
| 474 |
|
} |