View Javadoc
1   /*
2    * This software was designed and created by Jason Carroll.
3    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4    * The author can be reached at jcarroll@cowsultants.com
5    * ITracker website: http://www.cowsultants.com
6    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7    *
8    * This program is free software; you can redistribute it and/or modify
9    * it only under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17   */
18  
19  package org.itracker.model;
20  
21  import org.apache.commons.lang.builder.CompareToBuilder;
22  import org.apache.commons.lang.builder.ToStringBuilder;
23  
24  import java.io.Serializable;
25  import java.util.Comparator;
26  
27  /**
28   * An option for the value of a CustomField of type <code>LIST</code>.
29   *
30   * @author ready
31   * @author johnny
32   */
33  public class CustomFieldValue extends AbstractEntity {
34  
35      /**
36       *
37       */
38      private static final long serialVersionUID = 1L;
39      public static final Comparator<CustomFieldValue> SORT_ORDER_COMPARATOR = new SortOrderComparator();
40  
41      /**
42       * The custom field to which this option belongs.
43       */
44      private CustomField customField;
45  
46  
47      /**
48       * This option's value.
49       */
50      private String value;
51  
52      /**
53       * This option's order among all available options for the
54       * <code>customField</code>.
55       */
56      private int sortOrder;
57  
58      /**
59       * Default constructor (required by Hibernate).
60       * <p/>
61       * <p>
62       * PENDING: should be <code>private</code> so that it can only be used by
63       * Hibernate, to ensure that the fields which form an instance's identity
64       * are always initialized/never <tt>null</tt>.
65       * </p>
66       */
67      public CustomFieldValue() {
68      }
69  
70      public CustomFieldValue(CustomField customField, String value) {
71          setCustomField(customField);
72          setValue(value);
73      }
74  
75      public CustomField getCustomField() {
76          return (customField);
77      }
78  
79      public void setCustomField(CustomField customField) {
80          if (customField == null) {
81              throw new IllegalArgumentException("null customField");
82          }
83          this.customField = customField;
84      }
85  
86  
87      public String getValue() {
88          return value;
89      }
90  
91      /**
92       * @param value
93       */
94      public void setValue(String value) {
95          if (value == null) {
96              throw new IllegalArgumentException("null value");
97          }
98          this.value = value;
99      }
100 
101     public int getSortOrder() {
102         return sortOrder;
103     }
104 
105     public void setSortOrder(int sortOrder) {
106         this.sortOrder = sortOrder;
107     }
108 
109     /**
110      * Returns a string with this instance's id and natural key.
111      */
112     @Override
113     public String toString() {
114         return new ToStringBuilder(this).append("id", getId()).append(
115                 "customField", getCustomField()).append("value", getValue())
116                 .toString();
117     }
118 
119     /**
120      * Compares 2 CustomFieldValues by custom field and sort order.
121      * <p/>
122      * <p>
123      * Note that it doesn't match the class' natural ordering because it doesn't
124      * take into account the custom field. <br>
125      * It should therefore only be used to compare options that belong to a
126      * single custom field.
127      * </p>
128      */
129     private static class SortOrderComparator implements
130             Comparator<CustomFieldValue>, Serializable {
131         /**
132          *
133          */
134         private static final long serialVersionUID = 1L;
135 
136         public int compare(CustomFieldValue../../org/itracker/model/CustomFieldValue.html#CustomFieldValue">CustomFieldValue a, CustomFieldValue b) {
137             return new CompareToBuilder().append(a.getSortOrder(),
138                     b.getSortOrder())
139                     .toComparison();
140         }
141 
142     }
143 
144 
145 }