KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > commonimpl > schema > StaticSelectionListImpl


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 /**
30  * A static selection list, manually created
31  * by a daisy administrator for a specific FieldType.
32  */

33 public class StaticSelectionListImpl implements StaticSelectionList{
34     /* we're using an ArrayList because it will store the
35      * ListItem objects in the order in which we add them
36      * to it, which is the behaviour we desire
37      */

38     private ArrayList listItems = new ArrayList();
39     private SchemaStrategy creatingStrategy = null;
40     //marks whether or not the object was changed since last storage
41
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     /**
66      * Adds a value to this StaticSelectionListImpl.
67      *
68      * <p>This value will be wrapped in a ListItem object,
69      * i.e. this method is actually a shortcut for the
70      * addItem(ListItem listItem) method, relieving
71      * the outside world from the task of creating
72      * a ListItem object.
73      *
74      * @param value the object containing the value to be
75      * stored in the StaticSelectionListImpl
76      */

77     public void addItem(Object JavaDoc value) throws InvalidValueTypeException {
78         if (owner.isReadOnly())
79             throw new RuntimeException JavaDoc(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 JavaDoc(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         /**
104          * method to be called by the strategy after the object
105          * has been persisted.
106          */

107         public void saved() {
108             hasChanges=false;
109         }
110         
111         /**
112          * marks whether items have been added and/or cleared after
113          * the last time the object was persisted
114          */

115         public boolean hasChanges() {
116             return hasChanges;
117         }
118
119         /**
120          * adds listitem without marking the fact that the selection list
121          * has been changed
122          */

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 JavaDoc value) {
136         if (owner.isReadOnly())
137             throw new RuntimeException JavaDoc(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 JavaDoc getLabel(Object JavaDoc value, Locale locale) {
150         ListItem listItem = (ListItem)itemsByValue.get(value);
151         if (listItem != null) {
152             String JavaDoc 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 JavaDoc(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 JavaDoc value;
189
190             if (valueType == ValueType.STRING) {
191                 value = item.getString();
192             } else if (valueType == ValueType.DATE) {
193                 //not sure about this getTime()... check later
194
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 JavaDoc(item.getDouble());
201             } else if (valueType == ValueType.LONG) {
202                 value = new Long JavaDoc(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 JavaDoc("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