1 16 package org.outerj.daisy.repository.commonimpl.schema; 17 18 import java.util.*; 19 20 import org.outerj.daisy.repository.ValueType; 21 import org.outerj.daisy.repository.VariantKey; 22 import org.outerj.daisy.repository.schema.ListItem; 23 import org.outerj.daisy.repository.schema.StaticListItem; 24 import org.outerj.daisy.repository.schema.StaticSelectionList; 25 import org.outerx.daisy.x10.ListItemDocument; 26 import org.outerx.daisy.x10.SelectionListDocument; 27 import org.outerx.daisy.x10.StaticSelectionListDocument; 28 29 33 public class StaticSelectionListImpl implements StaticSelectionList{ 34 38 private ArrayList listItems = new ArrayList(); 39 private SchemaStrategy creatingStrategy = null; 40 private boolean hasChanges=false; 42 private IntimateAccess intimateAccess = new IntimateAccess(); 43 private ValueType valueType; 44 private FieldTypeImpl owner; 45 private Map itemsByValue = new HashMap(); 46 47 public StaticSelectionListImpl(SchemaStrategy creatingStrategy, ValueType valueType, FieldTypeImpl owner) { 48 this.creatingStrategy = creatingStrategy; 49 this.valueType = valueType; 50 this.owner = owner; 51 } 52 53 public ListItem[] getItems() { 54 return (ListItem[])listItems.toArray(new ListItem[listItems.size()]); 55 } 56 57 public ListItem[] getItems(long branchId, long languageId, Locale locale) { 58 return getItems(); 59 } 60 61 private void index(ListItem listItem) { 62 itemsByValue.put(listItem.getValue(), listItem); 63 } 64 65 77 public void addItem(Object value) throws InvalidValueTypeException { 78 if (owner.isReadOnly()) 79 throw new RuntimeException (FieldTypeImpl.READ_ONLY_MESSAGE); 80 81 if (valueType.getTypeClass().isAssignableFrom(value.getClass())) { 82 StaticListItemImpl newItem = new StaticListItemImpl(value, valueType, creatingStrategy, owner); 83 listItems.add(newItem); 84 index(newItem); 85 hasChanges = true; 86 } else { 87 throw new InvalidValueTypeException(); 88 } 89 } 90 91 public void clear() { 92 if (owner.isReadOnly()) 93 throw new RuntimeException (FieldTypeImpl.READ_ONLY_MESSAGE); 94 95 listItems.clear(); 96 itemsByValue.clear(); 97 hasChanges=true; 98 } 99 100 public class IntimateAccess { 101 private IntimateAccess() {} 102 103 107 public void saved() { 108 hasChanges=false; 109 } 110 111 115 public boolean hasChanges() { 116 return hasChanges; 117 } 118 119 123 public void addItem(ListItem element) { 124 listItems.add(element); 125 index(element); 126 } 127 128 } 129 130 public IntimateAccess getIntimateAccess(SchemaStrategy strategy) { 131 if (strategy==creatingStrategy) return intimateAccess; 132 else return null; 133 } 134 135 public StaticListItem createStaticListItem(Object value) { 136 if (owner.isReadOnly()) 137 throw new RuntimeException (FieldTypeImpl.READ_ONLY_MESSAGE); 138 139 if (!valueType.getTypeClass().isAssignableFrom(value.getClass())) 140 throw new InvalidValueTypeException(); 141 142 StaticListItemImpl listItem = new StaticListItemImpl(value, valueType, this.creatingStrategy, owner); 143 listItems.add(listItem); 144 index(listItem); 145 hasChanges=true; 146 return listItem; 147 } 148 149 public String getLabel(Object value, Locale locale) { 150 ListItem listItem = (ListItem)itemsByValue.get(value); 151 if (listItem != null) { 152 String label = listItem.getLabel(locale); 153 if (label != null) 154 return label; 155 } 156 157 return null; 158 } 159 160 public StaticSelectionListDocument getXml() { 161 StaticSelectionListDocument selListDoc = StaticSelectionListDocument.Factory.newInstance(); 162 StaticSelectionListDocument.StaticSelectionList selList = selListDoc.addNewStaticSelectionList(); 163 164 ArrayList theItems = new ArrayList(); 165 166 for (Iterator iter = listItems.iterator(); iter.hasNext();) { 167 StaticListItem listItem = (StaticListItem) iter.next(); 168 ListItemDocument.ListItem listItemXml = listItem.getXml().getListItem(); 169 theItems.add(listItemXml); 170 } 171 172 selList.setListItemArray((ListItemDocument.ListItem[])theItems.toArray(new ListItemDocument.ListItem[theItems.size()])); 173 174 return selListDoc; 175 } 176 177 public void addToFieldTypeXml(SelectionListDocument.SelectionList selectionListXml) { 178 selectionListXml.setStaticSelectionList(getXml().getStaticSelectionList()); 179 } 180 181 public void setAllFromXml(StaticSelectionListDocument.StaticSelectionList sl) { 182 if (owner.isReadOnly()) 183 throw new RuntimeException (FieldTypeImpl.READ_ONLY_MESSAGE); 184 185 ListItemDocument.ListItem[] listItemArr = sl.getListItemArray(); 186 for (int i = 0; i < listItemArr.length; i++) { 187 ListItemDocument.ListItem item = listItemArr[i]; 188 Object value; 189 190 if (valueType == ValueType.STRING) { 191 value = item.getString(); 192 } else if (valueType == ValueType.DATE) { 193 value = item.getDate().getTime(); 195 } else if (valueType == ValueType.DATETIME) { 196 value = item.getDateTime().getTime(); 197 } else if (valueType == ValueType.DECIMAL) { 198 value = item.getDecimal(); 199 } else if (valueType == ValueType.DOUBLE) { 200 value = new Double (item.getDouble()); 201 } else if (valueType == ValueType.LONG) { 202 value = new Long (item.getLong()); 203 } else if (valueType == ValueType.BOOLEAN) { 204 value = item.getBoolean() ? Boolean.TRUE : Boolean.FALSE; 205 } else if (valueType == ValueType.LINK) { 206 ListItemDocument.ListItem.Link link = item.getLink(); 207 long branchId = link.isSetBranchId() ? link.getBranchId() : -1; 208 long languageId = link.isSetLanguageId() ? link.getLanguageId() : -1; 209 value = new VariantKey(link.getDocumentId(), branchId, languageId); 210 } else { 211 throw new RuntimeException ("Unsupported ValueType: " + valueType); 212 } 213 214 StaticListItemImpl li = (StaticListItemImpl)createStaticListItem(value); 215 216 StaticListItemImpl.IntimateAccess liInt = li.getIntimateAccess(creatingStrategy); 217 LocaleMap myMap = liInt.getLabels(); 218 myMap.readFromLabelsXml(item.getLabels()); 219 } 220 } 221 } 222 | Popular Tags |