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 java.io.Serializable;
22  import java.util.Comparator;
23  import java.util.Locale;
24  
25  import org.apache.commons.lang.builder.CompareToBuilder;
26  import org.apache.commons.lang.builder.ToStringBuilder;
27  import org.itracker.core.resources.ITrackerResources;
28  import org.itracker.services.util.CustomFieldUtilities;
29  
30  /**
31   * An option for the value of a CustomField of type <code>LIST</code>.
32   * 
33   * @author ready
34   * @author johnny
35   */
36  public class CustomFieldValue extends AbstractEntity {
37  
38  	/**
39  	 * 
40  	 */
41  	private static final long serialVersionUID = 1L;
42  	public static final Comparator<CustomFieldValue> NAME_COMPARATOR = new NameComparator();
43  	public static final Comparator<CustomFieldValue> SORT_ORDER_COMPARATOR = new SortOrderComparator();
44  
45  	/** The custom field to which this option belongs. */
46  	private CustomField customField;
47  
48  
49  
50  	/** This option's value. */
51  	private String value;
52  
53  	/**
54  	 * This option's order among all available options for the
55  	 * <code>customField</code>.
56  	 */
57  	private int sortOrder;
58  
59  	/**
60  	 * Default constructor (required by Hibernate).
61  	 * 
62  	 * <p>
63  	 * PENDING: should be <code>private</code> so that it can only be used by
64  	 * Hibernate, to ensure that the fields which form an instance's identity
65  	 * are always initialized/never <tt>null</tt>.
66  	 * </p>
67  	 */
68  	public CustomFieldValue() {
69  	}
70  
71  	/**
72  	 * 
73  	 * @param customField
74  	 * @param value
75  	 */
76  	public CustomFieldValue(CustomField customField, String value) {
77  		setCustomField(customField);
78  		setValue(value);
79  	}
80  
81  	public CustomField getCustomField() {
82  		return (customField);
83  	}
84  
85  	public void setCustomField(CustomField customField) {
86  		if (customField == null) {
87  			throw new IllegalArgumentException("null customField");
88  		}
89  		this.customField = customField;
90  	}
91  
92  
93  	public String getValue() {
94  		return value;
95  	}
96  
97  	/**
98  	 * 
99  	 * @param value
100 	 */
101 	public void setValue(String value) {
102 		if (value == null) {
103 			throw new IllegalArgumentException("null value");
104 		}
105 		this.value = value;
106 	}
107 
108 	public int getSortOrder() {
109 		return sortOrder;
110 	}
111 
112 	public void setSortOrder(int sortOrder) {
113 		this.sortOrder = sortOrder;
114 	}
115 
116 	/**
117 	 * Returns a string with this instance's id and natural key.
118 	 */
119 	@Override
120 	public String toString() {
121 		return new ToStringBuilder(this).append("id", getId()).append(
122 				"customField", getCustomField()).append("value", getValue())
123 				.toString();
124 	}
125 
126 	/**
127 	 * Compares 2 CustomFieldValues by custom field and sort order.
128 	 * 
129 	 * <p>
130 	 * Note that it doesn't match the class' natural ordering because it doesn't
131 	 * take into account the custom field. <br>
132 	 * It should therefore only be used to compare options that belong to a
133 	 * single custom field.
134 	 * </p>
135 	 */
136 	private static class SortOrderComparator implements
137 			Comparator<CustomFieldValue>, Serializable {
138 		/**
139 		 * 
140 		 */
141 		private static final long serialVersionUID = 1L;
142 
143 		public int compare(CustomFieldValue a, CustomFieldValue b) {
144 			return new CompareToBuilder().append(a.getSortOrder(),
145 					b.getSortOrder())
146 					.toComparison();
147 		}
148 
149 	}
150 
151 	/**
152 	 * Compares 2 CustomFieldValues by name.
153 	 * 
154 	 * <p>
155 	 * If 2 instances have the same name, they are ordered by sortOrder.
156 	 * </p>
157 	 * 
158 	 * <p>
159 	 * It doesn't take into account the custom field. <br>
160 	 * It should therefore only be used to compare options that belong to a
161 	 * single custom field.
162 	 * </p>
163 	 */
164 	private static class NameComparator implements
165 			Comparator<CustomFieldValue>, Serializable {
166 		/**
167 		 * 
168 		 */
169 		private static final long serialVersionUID = 1L;
170 
171 		private final Locale locale;
172 		/**
173 		 * @deprecated should create a localized comparator
174 		 */
175 		private NameComparator() {
176 			this(new Locale(ITrackerResources.getDefaultLocale()));
177 		}
178 		private NameComparator(Locale locale) {
179 			this.locale = locale;
180 		}
181 		
182 		public int compare(CustomFieldValue a, CustomFieldValue b) {
183 			
184 			// 
185 			return new CompareToBuilder()
186 				.append(
187 					CustomFieldUtilities.getCustomFieldOptionName(a,this.locale), 
188 					CustomFieldUtilities.getCustomFieldOptionName(b,this.locale))
189 				.append(a.getSortOrder(), b.getSortOrder())
190 				.append(a.getId(), b.getId()).toComparison();
191 		}
192 
193 	}
194 
195 }