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 CustomFieldValueModel extends GenericModel implements Comparator { 28 private Integer customFieldId; 29 private String optionValue; 30 private String name; 31 private int sortOrder; 32 33 public CustomFieldValueModel() { 34 } 35 36 public CustomFieldValueModel(Integer customFieldId, String value, String name) { 37 setCustomFieldId(customFieldId); 38 setValue(value); 39 setName(name); 40 } 41 42 public String getValue() { 43 return (optionValue == null ? "" : optionValue); 44 } 45 46 public void setValue(String value) { 47 optionValue = value; 48 } 49 50 public int getSortOrder() { 51 return sortOrder; 52 } 53 54 public void setSortOrder(int value) { 55 sortOrder = value; 56 } 57 58 public Integer getCustomFieldId() { 59 return (customFieldId == null ? new Integer (-1) : customFieldId); 60 } 61 62 public void setCustomFieldId(Integer value) { 63 customFieldId = value; 64 } 65 66 public String getName() { 67 return (name == null ? "" : name); 68 } 69 70 public void setName(String value) { 71 name = value; 72 } 73 74 public int compare(Object a, Object b) { 75 return this.new CompareBySortOrder().compare(a, b); 76 } 77 78 public boolean equals(Object obj) { 79 return this.new CompareBySortOrder().equals(obj); 80 } 81 82 public int hashCode() { 83 return this.new CompareBySortOrder().hashCode(); 84 } 85 86 public String toString() { 87 return "CustomFieldValue [" + getId() + "] Value: " + getValue() + " CustomField: " + getCustomFieldId(); 88 } 89 90 public abstract class CustomFieldValueModelComparator implements Comparator { 91 protected boolean isAscending = true; 92 93 public CustomFieldValueModelComparator() { 94 } 95 96 public CustomFieldValueModelComparator(boolean isAscending) { 97 setAscending(isAscending); 98 } 99 100 public void setAscending(boolean value) { 101 this.isAscending = value; 102 } 103 104 protected abstract int doComparison(CustomFieldValueModel ma, CustomFieldValueModel mb); 105 106 public final boolean equals(Object obj) { 107 if(! (obj instanceof CustomFieldValueModel)) { 108 return false; 109 } 110 111 try { 112 CustomFieldValueModel mo = (CustomFieldValueModel) obj; 113 if(CustomFieldValueModel.this.getId() == mo.getId()) { 114 return true; 115 } 116 } catch(ClassCastException cce) { 117 } 118 119 return false; 120 } 121 122 public int hashCode() { 123 return (CustomFieldValueModel.this.getId() == null ? 1 : CustomFieldValueModel.this.getId().intValue()); 124 } 125 126 public final int compare(Object a, Object b) { 127 if(! (a instanceof CustomFieldValueModel) || ! (b instanceof CustomFieldValueModel)) { 128 throw new ClassCastException (); 129 } 130 131 CustomFieldValueModel ma = (CustomFieldValueModel) a; 132 CustomFieldValueModel mb = (CustomFieldValueModel) b; 133 134 int result = doComparison(ma, mb); 135 if(! isAscending) { 136 result = result * -1; 137 } 138 return result; 139 } 140 } 141 142 public class CompareBySortOrder extends CustomFieldValueModelComparator { 143 public CompareBySortOrder(){ 144 super(); 145 } 146 147 public CompareBySortOrder(boolean isAscending) { 148 super(isAscending); 149 } 150 151 protected int doComparison(CustomFieldValueModel ma, CustomFieldValueModel mb) { 152 if(ma.getSortOrder() == mb.getSortOrder()) { 153 return 0; 154 } else if(ma.getSortOrder() > mb.getSortOrder()) { 155 return 1; 156 } else if(mb.getSortOrder() < mb.getSortOrder()) { 157 return -1; 158 } 159 160 return 0; 161 } 162 } 163 164 public class CompareByName extends CustomFieldValueModelComparator { 165 public CompareByName(){ 166 super(); 167 } 168 169 public CompareByName(boolean isAscending) { 170 super(isAscending); 171 } 172 173 protected int doComparison(CustomFieldValueModel ma, CustomFieldValueModel mb) { 174 if(ma.getName().equals(mb.getName())) { 175 if(ma.getSortOrder() > mb.getSortOrder()) { 176 return 1; 177 } else if(mb.getSortOrder() < mb.getSortOrder()) { 178 return -1; 179 } else { 180 return 0; 181 } 182 } else { 183 return ma.getName().compareTo(mb.getName()); 184 } 185 } 186 } 187 } 188 | Popular Tags |