1 18 19 package cowsultants.itracker.ejb.client.models; 20 21 import java.io.Serializable ; 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 dateFormat = CustomFieldUtilities.DATE_FORMAT_DATEONLY; 31 32 private String dataScript = null; 33 private String validationScript = null; 34 35 private CustomFieldValueModel[] options = null; 36 private boolean sortOptionsByName = false; 37 38 private String name = ""; 39 private String locale = ""; 40 41 public CustomFieldModel() { 42 this.setId(new Integer (-1)); 43 } 44 45 public CustomFieldModel(Integer id, int fieldType, boolean required) { 46 this.setId(id); 47 this.setFieldType(fieldType); 48 this.setRequired(required); 49 } 50 51 public CustomFieldModel(Integer id, int fieldType, boolean required, String dateFormat) { 52 this(id, fieldType, required); 53 this.dateFormat = (dateFormat == null ? CustomFieldUtilities.DATE_FORMAT_DATEONLY : dateFormat); 54 } 55 56 public CustomFieldModel(Integer 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 getDateFormat() { 79 return dateFormat; 80 } 81 82 public void setDateFormat(String 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 getLocale() { 103 return locale; 104 } 105 106 public void setLocale(String value) { 107 locale = value; 108 } 109 110 public String getName() { 111 return name; 112 } 113 114 public void setName(String value) { 115 name = value; 116 } 117 118 123 public String getOptionNameByValue(String 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 141 public void addOption(String value, String label) { 142 CustomFieldValueModel[] newOptions = new CustomFieldValueModel[getOptions().length + 1]; 143 if(getOptions().length > 0) { 144 System.arraycopy((Object ) options, 0, (Object ) newOptions, 0, options.length); 145 } 146 newOptions[getOptions().length] = new CustomFieldValueModel(this.getId(), value, label); 147 148 options = newOptions; 149 } 150 151 155 public void setLabels(String locale) { 156 Locale loc = ITrackerResources.getLocale(locale); 157 setLabels(loc); 158 } 159 160 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 e) { 176 } 177 } 178 } 179 180 public int compare(Object a, Object b) { 181 return this.new CompareById().compare(a, b); 182 } 183 184 public String 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 a, Object b) { 204 int result = 0; 205 if(! (a instanceof CustomFieldModel) || ! (b instanceof CustomFieldModel)) { 206 throw new ClassCastException ("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 |