KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > ejb > client > models > CustomFieldModel


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 cowsultants.itracker.ejb.client.models;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.*;
23
24 import cowsultants.itracker.ejb.client.resources.*;
25 import cowsultants.itracker.ejb.client.util.*;
26
27 public class CustomFieldModel extends GenericModel implements Comparator {
28     private int fieldType = -1;
29     private boolean required = false;
30     private String JavaDoc dateFormat = CustomFieldUtilities.DATE_FORMAT_DATEONLY;
31
32     private String JavaDoc dataScript = null;
33     private String JavaDoc validationScript = null;
34
35     private CustomFieldValueModel[] options = null;
36     private boolean sortOptionsByName = false;
37
38     private String JavaDoc name = "";
39     private String JavaDoc locale = "";
40
41     public CustomFieldModel() {
42         this.setId(new Integer JavaDoc(-1));
43     }
44
45     public CustomFieldModel(Integer JavaDoc id, int fieldType, boolean required) {
46         this.setId(id);
47         this.setFieldType(fieldType);
48         this.setRequired(required);
49     }
50
51     public CustomFieldModel(Integer JavaDoc id, int fieldType, boolean required, String JavaDoc dateFormat) {
52         this(id, fieldType, required);
53         this.dateFormat = (dateFormat == null ? CustomFieldUtilities.DATE_FORMAT_DATEONLY : dateFormat);
54     }
55
56     public CustomFieldModel(Integer JavaDoc id, int fieldType, boolean required, CustomFieldValueModel[] options, boolean sortOptions) {
57         this(id, fieldType, required);
58         this.options = options;
59         this.sortOptionsByName = sortOptions;
60     }
61
62     public int getFieldType() {
63         return fieldType;
64     }
65
66     public void setFieldType(int value) {
67         fieldType = value;
68     }
69
70     public boolean isRequired() {
71         return required;
72     }
73
74     public void setRequired(boolean value) {
75         required = value;
76     }
77
78     public String JavaDoc getDateFormat() {
79         return dateFormat;
80     }
81
82     public void setDateFormat(String JavaDoc value) {
83         dateFormat = value;
84     }
85
86     public boolean getSortOptionsByName() {
87         return sortOptionsByName;
88     }
89
90     public void setSortOptionsByName(boolean value) {
91         sortOptionsByName = value;
92     }
93
94     public CustomFieldValueModel[] getOptions() {
95         return (options == null ? new CustomFieldValueModel[0] : options);
96     }
97
98     public void setOptions(CustomFieldValueModel[] value) {
99         options = value;
100     }
101
102     public String JavaDoc getLocale() {
103         return locale;
104     }
105
106     public void setLocale(String JavaDoc value) {
107         locale = value;
108     }
109
110     public String JavaDoc getName() {
111         return name;
112     }
113
114     public void setName(String JavaDoc value) {
115         name = value;
116     }
117
118     /**
119       * Returns the name for a particular option value.
120       * @param optionValue the value to lookup the name for
121       * @return the localized name for the supplied value
122       */

123     public String JavaDoc getOptionNameByValue(String JavaDoc optionValue) {
124         if(optionValue != null && ! optionValue.equals("")) {
125             for(int i = 0; i < options.length; i++) {
126                 if(options[i] != null && options[i].getValue().equalsIgnoreCase(optionValue)) {
127                     return options[i].getName();
128                 }
129             }
130         }
131         return "";
132     }
133
134     /**
135       * Adds a new option value/name to the custom field. New options are put
136       * at the end of the list even if they should be sorted. This method is
137       * mainly used to build a new custom field so it can be saved later.
138       * @param value the option value
139       * @param label the label/name for the new option
140       */

141     public void addOption(String JavaDoc value, String JavaDoc label) {
142         CustomFieldValueModel[] newOptions = new CustomFieldValueModel[getOptions().length + 1];
143         if(getOptions().length > 0) {
144             System.arraycopy((Object JavaDoc) options, 0, (Object JavaDoc) newOptions, 0, options.length);
145         }
146         newOptions[getOptions().length] = new CustomFieldValueModel(this.getId(), value, label);
147
148         options = newOptions;
149     }
150
151     /**
152       * Sets this custom fields names based on the supplied local string.
153       * @param locale the name of the locale to use for the names
154       */

155     public void setLabels(String JavaDoc locale) {
156         Locale loc = ITrackerResources.getLocale(locale);
157         setLabels(loc);
158     }
159
160     /**
161       * Sets this custom fields names based on the supplied locale.
162       * @param locale the locale to use for the names
163       */

164     public void setLabels(Locale locale) {
165         this.locale = locale.toString();
166         this.name = CustomFieldUtilities.getCustomFieldName(getId(), locale);
167         for(int i = 0; i < options.length; i++) {
168             if(options[i] != null) {
169                 options[i].setName(CustomFieldUtilities.getCustomFieldOptionName(getId(), options[i].getId(), locale));
170             }
171         }
172         if(getSortOptionsByName()) {
173             try {
174                 Arrays.sort(options, new CustomFieldValueModel().new CompareByName());
175             } catch(Exception JavaDoc e) {
176             }
177         }
178     }
179
180     public int compare(Object JavaDoc a, Object JavaDoc b) {
181         return this.new CompareById().compare(a, b);
182     }
183
184     public String JavaDoc toString() {
185         return "CustomField[" + this.getId() + "] Type: " + this.getFieldType() + " Required: " + this.isRequired() +
186                " Date Format: " + this.getDateFormat() + " Num Options: " + this.getOptions().length;
187     }
188
189     public class CompareById implements Comparator {
190         protected boolean isAscending = true;
191
192         public CompareById() {
193         }
194
195         public CompareById(boolean isAscending) {
196             setAscending(isAscending);
197         }
198
199         public void setAscending(boolean value) {
200             this.isAscending = value;
201         }
202
203         public int compare(Object JavaDoc a, Object JavaDoc b) {
204             int result = 0;
205             if(! (a instanceof CustomFieldModel) || ! (b instanceof CustomFieldModel)) {
206                 throw new ClassCastException JavaDoc("Invalid class found during compare.");
207             }
208
209             CustomFieldModel ma = (CustomFieldModel) a;
210             CustomFieldModel mb = (CustomFieldModel) b;
211
212             if(ma.getId() == null && mb.getId() == null) {
213                 result = 0;
214             } else if(ma.getId() == null) {
215                 result = 1;
216             } else if(mb.getId() == null) {
217                 result = -1;
218             } else {
219                 result = ma.getId().compareTo(mb.getId());
220             }
221
222             return (isAscending ? result : result * -1);
223         }
224     }
225 }
226
Popular Tags