KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > dataview > DataView


1 /* ====================================================================
2 *
3 * The ObjectStyle Group Software License, version 1.1
4 * ObjectStyle Group - http://objectstyle.org/
5 *
6 * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7 * of the software. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution, if any,
22 * must include the following acknowlegement:
23 * "This product includes software developed by independent contributors
24 * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25 * Alternately, this acknowlegement may appear in the software itself,
26 * if and wherever such third-party acknowlegements normally appear.
27 *
28 * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29 * or promote products derived from this software without prior written
30 * permission. For written permission, email
31 * "andrus at objectstyle dot org".
32 *
33 * 5. Products derived from this software may not be called "ObjectStyle"
34 * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35 * names without prior written permission.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals and hosted on ObjectStyle Group web site. For more
53 * information on the ObjectStyle Group, please see
54 * <http://objectstyle.org/>.
55 */

56 package org.objectstyle.cayenne.dataview;
57
58 import java.io.File JavaDoc;
59 import java.io.IOException JavaDoc;
60 import java.io.InputStream JavaDoc;
61 import java.io.Reader JavaDoc;
62 import java.net.URL JavaDoc;
63 import java.text.Format JavaDoc;
64 import java.util.Collection JavaDoc;
65 import java.util.Collections JavaDoc;
66 import java.util.HashMap JavaDoc;
67 import java.util.HashSet JavaDoc;
68 import java.util.Iterator JavaDoc;
69 import java.util.List JavaDoc;
70 import java.util.Locale JavaDoc;
71 import java.util.Map JavaDoc;
72 import java.util.Set JavaDoc;
73 import java.util.TreeMap JavaDoc;
74
75 import org.apache.commons.lang.BooleanUtils;
76 import org.apache.commons.lang.StringUtils;
77 import org.apache.commons.lang.Validate;
78 import org.apache.commons.lang.math.NumberUtils;
79 import org.jdom.Document;
80 import org.jdom.Element;
81 import org.jdom.JDOMException;
82 import org.jdom.input.SAXBuilder;
83 import org.objectstyle.cayenne.DataObject;
84 import org.objectstyle.cayenne.map.EntityResolver;
85 import org.objectstyle.cayenne.map.ObjAttribute;
86 import org.objectstyle.cayenne.map.ObjEntity;
87 import org.objectstyle.cayenne.map.ObjRelationship;
88
89 /**
90  * A root of the view configuration hierarchy. Contains a number of ObjEntityViews.
91  *
92  * @since 1.1
93  * @author Andriy Shapochka
94  */

95 public class DataView {
96
97     private Locale JavaDoc locale = Locale.US;
98     
99     //Data type definitions
100
private DataTypeSpec dataTypeSpec = new DataTypeSpec();
101     
102     //Format definitions
103
private FormatFactory formatFactory = new FormatFactory();
104     
105     //ObjEntity lookup
106
private EntityResolver entityResolver;
107
108     private Map JavaDoc objEntityViews = new TreeMap JavaDoc();
109     private Set JavaDoc lookupEntityViewFields;
110     private LookupCache lookupCache = new LookupCache();
111     private EventDispatcher fieldValueChangeDispatcher;
112     
113     //used internally to resolve field lookup dependencies
114
private Map JavaDoc lookupReferenceTable;
115
116     public DataView() {
117     }
118
119     public EntityResolver getEntityResolver() {
120         return entityResolver;
121     }
122
123     public void setEntityResolver(EntityResolver entityResolver) {
124         Validate.notNull(entityResolver);
125         this.entityResolver = entityResolver;
126     }
127
128     public void load(File JavaDoc[] xmlSources) throws IOException JavaDoc {
129         Validate.noNullElements(xmlSources);
130         SAXBuilder builder = new SAXBuilder();
131         Document[] documents = new Document[xmlSources.length];
132         for (int i = 0; i < xmlSources.length; i++) {
133             try {
134                 documents[i] = builder.build(xmlSources[i]);
135             }
136             catch (JDOMException ex) {
137                 ex.printStackTrace();
138             }
139         }
140         load(documents);
141     }
142
143     public void load(URL JavaDoc[] xmlSources) throws IOException JavaDoc {
144         Validate.noNullElements(xmlSources);
145         SAXBuilder builder = new SAXBuilder();
146         Document[] documents = new Document[xmlSources.length];
147         for (int i = 0; i < xmlSources.length; i++) {
148             try {
149                 documents[i] = builder.build(xmlSources[i]);
150             }
151             catch (JDOMException ex) {
152                 ex.printStackTrace();
153             }
154         }
155         load(documents);
156     }
157
158     public void load(Reader JavaDoc[] xmlSources) throws IOException JavaDoc {
159         Validate.noNullElements(xmlSources);
160         SAXBuilder builder = new SAXBuilder();
161         Document[] documents = new Document[xmlSources.length];
162         for (int i = 0; i < xmlSources.length; i++) {
163             try {
164                 documents[i] = builder.build(xmlSources[i]);
165             }
166             catch (JDOMException ex) {
167                 ex.printStackTrace();
168             }
169         }
170         load(documents);
171     }
172
173     public void load(InputStream JavaDoc[] xmlSources) throws IOException JavaDoc {
174         Validate.noNullElements(xmlSources);
175         SAXBuilder builder = new SAXBuilder();
176         Document[] documents = new Document[xmlSources.length];
177         for (int i = 0; i < xmlSources.length; i++) {
178             try {
179                 documents[i] = builder.build(xmlSources[i]);
180             }
181             catch (JDOMException ex) {
182                 ex.printStackTrace();
183             }
184         }
185         load(documents);
186     }
187
188     public void load(Document[] views) {
189         Validate.noNullElements(views);
190         lookupReferenceTable = new HashMap JavaDoc();
191         for (int i = 0; i < views.length; i++) {
192             Element root = views[i].getRootElement();
193             List JavaDoc entityViews = root.getChildren("obj-entity-view");
194             for (Iterator JavaDoc j = entityViews.iterator(); j.hasNext();) {
195                 Element entityViewElement = (Element) j.next();
196                 loadEntityView(entityViewElement);
197             }
198         }
199         resolveLookupReferences();
200         lookupReferenceTable = null;
201     }
202
203     private void resolveLookupReferences() {
204         lookupEntityViewFields = new HashSet JavaDoc();
205         for (Iterator JavaDoc i = lookupReferenceTable.entrySet().iterator(); i.hasNext();) {
206             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
207             ObjEntityViewField field = (ObjEntityViewField) entry.getKey();
208             String JavaDoc[] lookupDescriptor = (String JavaDoc[]) entry.getValue();
209             ObjEntityView lookupEntityView = getObjEntityView(lookupDescriptor[0]);
210             ObjEntityViewField lookupField = lookupEntityView
211                     .getField(lookupDescriptor[1]);
212             field.setLookupField(lookupField);
213             lookupEntityViewFields.add(lookupField);
214         }
215     }
216
217     private void loadEntityView(Element element) {
218         String JavaDoc name = element.getAttributeValue("name");
219         Validate.isTrue(name != null && !objEntityViews.containsKey(name));
220         String JavaDoc objEntityName = element.getAttributeValue("obj-entity-name");
221         Validate.notNull(objEntityName);
222         ObjEntity objEntity = entityResolver.getObjEntity(objEntityName);
223         ObjEntityView entityView = new ObjEntityView();
224         entityView.setName(name);
225         entityView.setObjEntity(objEntity);
226         objEntityViews.put(name, entityView);
227         entityView.setOwner(this);
228         List JavaDoc fields = element.getChildren("field");
229         for (Iterator JavaDoc i = fields.iterator(); i.hasNext();) {
230             Element fieldElement = (Element) i.next();
231             loadField(entityView, fieldElement);
232         }
233     }
234
235     private void loadField(ObjEntityView entityView, Element element) {
236         String JavaDoc name = element.getAttributeValue("name");
237         ObjEntityViewField field = new ObjEntityViewField();
238         field.setName(name);
239         String JavaDoc prefIndex = element.getAttributeValue("pref-index");
240         field.setPreferredIndex(NumberUtils.toInt(prefIndex, -1));
241         entityView.insertField(field);
242
243         String JavaDoc calcType = element.getAttributeValue("calc-type");
244         Validate.notNull(calcType);
245         CalcTypeEnum fieldCalcType = CalcTypeEnum.getEnum(calcType);
246         Validate.isTrue(
247                 CalcTypeEnum.NO_CALC_TYPE.equals(fieldCalcType)
248                         || CalcTypeEnum.LOOKUP_TYPE.equals(fieldCalcType),
249                 "Calc Type not supported yet: ",
250                 fieldCalcType);
251         field.setCalcType(fieldCalcType);
252
253         ObjEntity objEntity = entityView.getObjEntity();
254
255         if (CalcTypeEnum.NO_CALC_TYPE.equals(fieldCalcType)) {
256             String JavaDoc objAttributeName = element.getAttributeValue("obj-attribute-name");
257             Validate.notNull(objAttributeName);
258             ObjAttribute objAttribute = (ObjAttribute) objEntity
259                     .getAttribute(objAttributeName);
260             field.setObjAttribute(objAttribute);
261         }
262         else if (CalcTypeEnum.LOOKUP_TYPE.equals(fieldCalcType)) {
263             String JavaDoc objRelationshipName = element
264                     .getAttributeValue("obj-relationship-name");
265             Validate.notNull(objRelationshipName);
266             ObjRelationship objRelationship = (ObjRelationship) objEntity
267                     .getRelationship(objRelationshipName);
268             field.setObjRelationship(objRelationship);
269             Element lookupElement = element.getChild("lookup");
270             Validate.notNull(lookupElement);
271             String JavaDoc lookupEntityView = lookupElement
272                     .getAttributeValue("obj-entity-view-name");
273             Validate.notNull(lookupEntityView);
274             String JavaDoc lookupEntityField = lookupElement.getAttributeValue("field-name");
275             Validate.notNull(lookupEntityField);
276             String JavaDoc[] lookupDescriptor = new String JavaDoc[] {
277                     lookupEntityView, lookupEntityField
278             };
279             lookupReferenceTable.put(field, lookupDescriptor);
280         }
281
282         String JavaDoc dataType = element.getAttributeValue("data-type");
283         Validate.notNull(dataType);
284         field.setDataType(dataTypeSpec.getDataType(dataType));
285
286         String JavaDoc editable = element.getAttributeValue("editable");
287         field.setEditable(BooleanUtils.toBoolean(editable));
288
289         String JavaDoc visible = element.getAttributeValue("visible");
290         field.setVisible(BooleanUtils.toBoolean(visible));
291
292         Element captionElement = element.getChild("caption");
293         if (captionElement != null)
294             field.setCaption(StringUtils.stripToEmpty(captionElement.getText()));
295
296         Element editFormatElement = element.getChild("edit-format");
297         if (editFormatElement != null) {
298             String JavaDoc formatClassName = editFormatElement.getAttributeValue("class");
299             Validate.notNull(formatClassName);
300             Class JavaDoc formatClass;
301             try {
302                 formatClass = Class.forName(formatClassName);
303                 Map JavaDoc parameters = DataView.childrenToMap(editFormatElement);
304                 Format JavaDoc format = formatFactory.createFormat(
305                         formatClass,
306                         locale,
307                         parameters);
308                 field.setEditFormat(format);
309             }
310             catch (ClassNotFoundException JavaDoc ex) {
311             }
312         }
313
314         Element displayFormatElement = element.getChild("display-format");
315         if (displayFormatElement != null) {
316             String JavaDoc formatClassName = displayFormatElement.getAttributeValue("class");
317             Validate.notNull(formatClassName);
318             Class JavaDoc formatClass;
319             try {
320                 formatClass = Class.forName(formatClassName);
321                 Map JavaDoc parameters = DataView.childrenToMap(displayFormatElement);
322                 Format JavaDoc format = formatFactory.createFormat(
323                         formatClass,
324                         locale,
325                         parameters);
326                 field.setDisplayFormat(format);
327             }
328             catch (ClassNotFoundException JavaDoc ex) {
329             }
330         }
331
332         Element defaultValueElement = element.getChild("default-value");
333         if (defaultValueElement != null) {
334             String JavaDoc defaultValueStr = StringUtils.stripToEmpty(defaultValueElement
335                     .getText());
336             Object JavaDoc defaultValue = dataTypeSpec.create(
337                     field.getDataType(),
338                     defaultValueStr);
339             field.setDefaultValue(defaultValue);
340         }
341     }
342
343     public Set JavaDoc getObjEntityViewNames() {
344         return Collections.unmodifiableSet(objEntityViews.keySet());
345     }
346
347     public Collection JavaDoc getObjEntityViews() {
348         return Collections.unmodifiableCollection(objEntityViews.values());
349     }
350
351     public ObjEntityView getObjEntityView(String JavaDoc viewName) {
352         return (ObjEntityView) objEntityViews.get(viewName);
353     }
354
355     public LookupCache getLookupCache() {
356         return lookupCache;
357     }
358
359     public void setLookupCache(LookupCache lookupCache) {
360         Validate.notNull(lookupCache);
361         this.lookupCache = lookupCache;
362     }
363
364     public Set JavaDoc getLookupObjEntityViewFields() {
365         return Collections.unmodifiableSet(lookupEntityViewFields);
366     }
367
368     public Locale JavaDoc getLocale() {
369         return locale;
370     }
371
372     public void setLocale(Locale JavaDoc locale) {
373         this.locale = locale;
374     }
375
376     public DataTypeSpec getDataTypeSpec() {
377         return dataTypeSpec;
378     }
379
380     public void setDataTypeSpec(DataTypeSpec dataTypeSpec) {
381         this.dataTypeSpec = dataTypeSpec;
382     }
383
384     public FormatFactory getFormatFactory() {
385         return formatFactory;
386     }
387
388     public void setFormatFactory(FormatFactory formatFactory) {
389         this.formatFactory = formatFactory;
390     }
391
392     private static Map JavaDoc childrenToMap(Element element) {
393         List JavaDoc children = element.getChildren();
394         if (children.isEmpty())
395             return Collections.EMPTY_MAP;
396         else {
397             Map JavaDoc map = new HashMap JavaDoc(children.size());
398             for (Iterator JavaDoc i = children.iterator(); i.hasNext();) {
399                 Element child = (Element) i.next();
400                 map.put(child.getName(), StringUtils.stripToNull(child.getText()));
401             }
402             return map;
403         }
404     }
405
406     public void addFieldValueChangeListener(FieldValueChangeListener listener) {
407         fieldValueChangeDispatcher = EventDispatcher.add(
408                 fieldValueChangeDispatcher,
409                 listener);
410     }
411
412     public void removeFieldValueChangeListener(FieldValueChangeListener listener) {
413         fieldValueChangeDispatcher = EventDispatcher.remove(
414                 fieldValueChangeDispatcher,
415                 listener);
416     }
417
418     public void clearFieldValueChangeListeners() {
419         if (fieldValueChangeDispatcher != null) {
420             fieldValueChangeDispatcher.clear();
421             fieldValueChangeDispatcher = null;
422         }
423     }
424
425     public void fireFieldValueChangeEvent(
426             ObjEntityViewField source,
427             DataObject modifiedObject,
428             Object JavaDoc oldValue,
429             Object JavaDoc newValue) {
430         if (fieldValueChangeDispatcher != null && source.getRootOwner() == this) {
431             fieldValueChangeDispatcher.dispatch(new FieldValueChangeEvent(
432                     source,
433                     modifiedObject,
434                     oldValue,
435                     newValue));
436         }
437     }
438 }
Popular Tags