KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > dataview > dvmodeler > ObjEntityViewField


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
57 package org.objectstyle.cayenne.dataview.dvmodeler;
58
59 import java.util.*;
60 import org.jdom.*;
61
62 /**
63  *
64  * @author Nataliya Kholodna
65  * @version 1.0
66  */

67
68 public class ObjEntityViewField extends DVObject {
69   private ObjEntityView objEntityView;
70   private ObjRelationship objRelationship;//implied
71
private ObjAttribute objAttribute;//implied
72
private int prefIndex;
73   private boolean editable;//required
74
private String JavaDoc calcType = "";//required
75
private String JavaDoc dataType = ""; //required
76
private boolean visible; //required
77

78   private String JavaDoc caption = "";
79   private String JavaDoc defaultValue = "";
80   private Lookup lookup;
81   private EditFormat editFormat;
82   private DisplayFormat displayFormat;
83
84   private List loadErrors = new ArrayList();
85   private List saveErrors = new ArrayList();
86
87   public ObjEntityViewField(ObjEntityView objEntityView, Element element) {
88     this.objEntityView = objEntityView;
89     DataView dataView = objEntityView.getDataView();
90     String JavaDoc fieldPath = "<b>" + dataView + "." + objEntityView + ".";
91     String JavaDoc attributeValue = element.getAttributeValue("name");
92     if ((attributeValue == null) || (attributeValue.length() == 0)){
93       setName("");
94       fieldPath += "</b><br>";
95       loadErrors.add(fieldPath + "field has no name<br><br>");
96     } else {
97       fieldPath += attributeValue + "</b><br>";
98       setName(attributeValue);
99     }
100     attributeValue = element.getAttributeValue("editable");
101     if ((attributeValue == null) || (attributeValue.length() == 0)){
102       editable = false;
103       loadErrors.add(fieldPath + "field has no attribute \"editable\"<br><br>");
104
105     } else {
106       if ((attributeValue.equalsIgnoreCase("true"))
107          || (attributeValue.equalsIgnoreCase("false"))){
108         editable = Boolean.valueOf(attributeValue).booleanValue();
109       } else {
110         editable = false;
111         loadErrors.add(fieldPath + "\"editable\" attribute value is not valid<br><br>");
112
113       }
114     }
115     attributeValue = element.getAttributeValue("calc-type");
116     if ((attributeValue == null) || (attributeValue.length() == 0)){
117       Attribute relationship = element.getAttribute("obj-relationship-name");
118       Element lookupChild = element.getChild("lookup");
119       if ((relationship != null) || (lookupChild != null)){
120         calcType = "lookup";
121       }else {
122         calcType = "nocalc";
123       }
124       loadErrors.add(fieldPath + "field has no attribute \"calc-type\"<br><br>");
125
126     } else {
127       if (attributeValue.equals("nocalc") || attributeValue.equals("lookup")){
128         calcType = attributeValue;
129       } else {
130         Attribute relationship = element.getAttribute("obj-relationship-name");
131         Element lookupChild = element.getChild("lookup");
132         Attribute attribute = element.getAttribute("obj-attribute-name");
133         if (attribute != null){
134           calcType = "nocalc";
135         } else if ((relationship != null) || (lookupChild != null)){
136           calcType = "lookup";
137         }else {
138           calcType = "nocalc";
139         }
140         loadErrors.add(fieldPath + "\"calc-type\" attribute value is not valid<br><br>");
141       }
142     }
143
144     attributeValue = element.getAttributeValue("data-type");
145     if ((attributeValue == null) || (attributeValue.length() == 0)){
146       dataType = "String";
147       loadErrors.add(fieldPath + "field has no attribute \"data-type\"<br><br>");
148
149     } else {
150       if (attributeValue.equals("String") || attributeValue.equals("Money")
151         || attributeValue.equals("Integer") || attributeValue.equals("Double")
152         || attributeValue.equals("Percent") || attributeValue.equals("Date")
153         || attributeValue.equals("Datetime") || attributeValue.equals("Boolean")
154         || attributeValue.equals("Object")){
155
156         dataType = attributeValue;
157       } else {
158         dataType = "String";
159         loadErrors.add(fieldPath + "Attribute \"data-type\" is not valid<br><br>");
160       }
161     }
162     attributeValue = element.getAttributeValue("visible");
163     if (attributeValue == null){
164       visible = true;
165       loadErrors.add(fieldPath + "field has no attribute \"visible\"<br><br>");
166
167     } else {
168       if ((attributeValue.equalsIgnoreCase("false"))
169          || (attributeValue.equalsIgnoreCase("true"))){
170         visible = new Boolean JavaDoc(attributeValue).booleanValue();
171       } else {
172         visible = false;
173         loadErrors.add(fieldPath + "Attribute \"visible\" is not valid. <br> It must be true or false<br><br>");
174
175       }
176     }
177  // -- Implied attributes---
178
if (calcType.equals("nocalc")){
179       attributeValue = element.getAttributeValue("obj-attribute-name");
180       lookup = new Lookup(this);
181       if ((attributeValue == null) || (attributeValue.length() == 0)){
182         objAttribute = null;
183         loadErrors.add(fieldPath + "field attribute \"calc-type\" has value \"nocalc\", " +
184           "but field has no attribute \"obj-attribute-name\"<br><br>");
185       } else{
186         ObjEntity entity = objEntityView.getObjEntity();
187         if (entity != null){
188           objAttribute = entity.getObjAttribute(attributeValue);
189           if (objAttribute == null){
190           loadErrors.add(fieldPath + "ObjEntity \"" + entity + "\" has no attribute \""
191              + attributeValue + "\"<br><br>");
192           }
193         }else{
194           objAttribute = null;
195           loadErrors.add(fieldPath + "ObjEntity for ObjEntity View \""
196             + objEntityView + "\" is null<br><br>");
197         }
198
199       }
200     }else{
201       if (calcType.equals("lookup")){
202         attributeValue = element.getAttributeValue("obj-relationship-name");
203         if ((attributeValue == null) || (attributeValue.length() == 0)){
204           objRelationship = null;
205           lookup = new Lookup(this);
206           loadErrors.add(fieldPath + "field has no attribute \"obj-relationship-name\"<br><br>");
207         } else{
208           ObjEntity objEntity = objEntityView.getObjEntity();
209           List relationships = objEntity.getDataMap().getObjRelationshipsBySourceToOne(objEntity);
210           for (Iterator j = relationships.iterator(); j.hasNext();){
211             ObjRelationship relationship = (ObjRelationship)j.next();
212             if (relationship.getName().equals(attributeValue)){
213               objRelationship = relationship;
214             }
215           }
216           if (objRelationship == null){
217             lookup = new Lookup(this);
218             loadErrors.add(fieldPath + "ObjRelationship "
219             + attributeValue + " with source " + objEntity + " and toMany = false does not exist in the Data Map "
220             + objEntity.getDataMap() + "<br><br>");
221           } else {
222             Element lookupChild = element.getChild("lookup");
223             if (lookupChild != null){
224               lookup = new Lookup(this);
225               String JavaDoc lookupViewName = lookupChild.getAttributeValue("obj-entity-view-name");
226               String JavaDoc lookupFieldName = lookupChild.getAttributeValue("field-name");
227               if ((lookupViewName == null) || (lookupViewName.length() == 0)){
228                 loadErrors.add(fieldPath + "Calc Type of this field is \"lookup\", but its lookup " +
229                                "has no attribute \"obj-entity-view\" <br><br>");
230               }else {
231                 if (lookupFieldName == null){
232                   loadErrors.add(fieldPath + "Field lookup has no attribute \"field\"<br><br>");
233                 }
234                 dataView.getCayenneProject().putFieldLookup(this, lookupViewName, lookupFieldName);
235               }
236
237             } else {
238               lookup = new Lookup(this);
239             }
240           }
241         }
242       }
243     }
244
245     attributeValue = element.getAttributeValue("pref-index");
246     if (attributeValue == null) {
247       prefIndex = -1;
248     } else if (attributeValue.length() == 0){
249       prefIndex = -1;
250       loadErrors.add(fieldPath + "Attribute \"pref-index\" has no value<br><br>");
251     } else {
252       try{
253         prefIndex = Integer.parseInt(attributeValue);
254       } catch (Exception JavaDoc e){
255         prefIndex = -1;
256         loadErrors.add(fieldPath + "Attribute \"pref-index\" is not valid<br><br>");
257       }
258       if (prefIndex < -1) {
259         prefIndex = -1;
260         loadErrors.add(fieldPath + "Attribute \"pref-index\" has value &lt -1<br><br>");
261       }
262     }
263
264  // -- child elements (don't required)
265

266     if (element.getChild("caption") != null){
267       caption = element.getChild("caption").getText().trim();
268     }
269
270     if (element.getChild("edit-format") != null){
271       editFormat = new EditFormat(element.getChild("edit-format"));
272     } else {
273       editFormat = new EditFormat();
274     }
275     if (element.getChild("display-format") != null){
276       displayFormat = new DisplayFormat(element.getChild("display-format"));
277     } else {
278       displayFormat = new DisplayFormat();
279     }
280     if (element.getChild("default-value") != null){
281       defaultValue = element.getChildText("default-value").trim();
282     }
283     this.objEntityView.addObjEntityViewField(this);
284   }
285
286   public ObjEntityViewField(ObjEntityView objEntityView){
287     setName("Field");
288     this.objEntityView = objEntityView;
289     calcType = "nocalc";
290     dataType = "String";
291     visible = true;
292     editable = true;
293     prefIndex = objEntityView.getObjEntityViewFieldCount();
294     lookup = new Lookup(this);
295     editFormat = new EditFormat();
296     displayFormat = new DisplayFormat();
297     objEntityView.addObjEntityViewField(this);
298   }
299
300   public List getLoadErrors(){
301     return Collections.unmodifiableList(loadErrors);
302   }
303   public List getSaveErrors(){
304     return Collections.unmodifiableList(saveErrors);
305   }
306
307   public void setObjEntityView(ObjEntityView objEntityView){
308     this.objEntityView = objEntityView;
309   }
310
311   public ObjEntityView getObjEntityView(){
312     return objEntityView;
313   }
314
315   //get-set methods for attributes
316

317   public void setDataType(String JavaDoc dataType){
318     String JavaDoc oldDataType = this.dataType;
319     this.dataType = dataType;
320     propertyChangeListeners.firePropertyChange("dataType", oldDataType, this.dataType);
321   }
322   public String JavaDoc getDataType(){
323     return dataType;
324   }
325
326   public void setCalcType(String JavaDoc calcType){
327     String JavaDoc oldCalcType = this.calcType;
328     this.calcType = calcType;
329     propertyChangeListeners.firePropertyChange("calcType", oldCalcType, calcType);
330   }
331
332   public String JavaDoc getCalcType(){
333     return calcType;
334   }
335
336   public void setObjRelationship(ObjRelationship relationship){
337     ObjRelationship oldRelationship = objRelationship;
338     objRelationship = relationship;
339
340     propertyChangeListeners.firePropertyChange("objRelationship", oldRelationship, objRelationship);
341
342   }
343   public ObjRelationship getObjRelationship(){
344     return objRelationship;
345   }
346
347
348   public void setObjAttribute(ObjAttribute attribute){
349     ObjAttribute oldObjAttribute = objAttribute;
350     objAttribute = attribute;
351     propertyChangeListeners.firePropertyChange("objAttribute", oldObjAttribute, objAttribute);
352
353   }
354   public ObjAttribute getObjAttribute(){
355     return objAttribute;
356   }
357
358   public void setPrefIndex(int index){
359     prefIndex = index;
360   }
361
362   public int getPrefIndex(){
363     return prefIndex;
364   }
365
366   public void setEditable(boolean editable){
367     this.editable = editable;
368   }
369   public boolean getEditable(){
370     return editable;
371   }
372
373   public void setVisible(boolean visible){
374     this.visible = visible;
375   }
376   public boolean getVisible(){
377     return visible;
378   }
379
380   public void setCaption(String JavaDoc caption){
381     this.caption = caption;
382   }
383   public String JavaDoc getCaption(){
384     return caption;
385   }
386
387   public void setDefaultValue(String JavaDoc defaultValue){
388     this.defaultValue = defaultValue;
389   }
390
391   public String JavaDoc getDefaultValue(){
392     return defaultValue;
393   }
394
395   public void setLookup(Lookup lookup){
396     Lookup oldLookup = this.lookup;
397     if (lookup == null){
398       this.lookup.setLookupObjEntityView(null);
399       this.lookup.setLookupField(null);
400     }
401     this.lookup = lookup;
402     propertyChangeListeners.firePropertyChange("lookup", oldLookup, lookup);
403   }
404
405   public Lookup getLookup(){
406     return lookup;
407   }
408
409   public void setEditFormat(EditFormat editFormat){
410     this.editFormat = editFormat;
411   }
412
413   public EditFormat getEditFormat(){
414     return editFormat;
415   }
416
417   public void setDisplayFormat(DisplayFormat displayFormat){
418     this.displayFormat = displayFormat;
419   }
420   public DisplayFormat getDisplayFormat(){
421     return displayFormat;
422   }
423
424 //-- toString
425
public String JavaDoc toString(){
426     return getName();
427   }
428 // -- get element
429
public Element getObjEntityViewFieldElement(){
430     Element fieldElement = new Element("field");
431     if (saveErrors.size() != 0){
432       saveErrors.clear();
433     }
434     DataView dataView = objEntityView.getDataView();
435     String JavaDoc fieldPath = "<b>" + dataView.getName() + "."
436        + objEntityView.getName() + "." + getName() + "</b><br>";
437     if (getName().length() == 0){
438       saveErrors.add(fieldPath + "Field has no name;<br><br>");
439     }
440     fieldElement.setAttribute("name", getName());
441
442     fieldElement.setAttribute("calc-type", calcType);
443     if (calcType.equals("nocalc")){
444       if (objAttribute == null){
445         saveErrors.add(fieldPath + "field attribute \"calc-type\" value is \"nocalc\", but field has no \"obj-attribute-name\" attribute value<br><br>");
446         fieldElement.setAttribute("obj-attribute-name", "");
447       } else {
448         fieldElement.setAttribute("obj-attribute-name", objAttribute.getName());
449       }
450     }else{
451       if (calcType.equals("lookup")){
452         if (objRelationship == null){
453           fieldElement.setAttribute("obj-relationship-name", "");
454           saveErrors.add(fieldPath +
455             "field attribute \"calc-type\" value is \"lookup\", but field has no \"obj-relationship-name\" attribute value<br><br>");
456         } else {
457           fieldElement.setAttribute("obj-relationship-name", objRelationship.getName());
458         }
459         fieldElement.addContent(lookup.getLookupElement());
460         saveErrors.addAll(lookup.getSaveErrors());
461       }
462     }
463     fieldElement.setAttribute("editable", String.valueOf(editable));
464     fieldElement.setAttribute("pref-index", String.valueOf(prefIndex));
465     fieldElement.setAttribute("data-type", dataType);
466     fieldElement.setAttribute("visible", String.valueOf(visible));
467     Element captionElement = new Element("caption");
468     if (caption.length() != 0){
469       fieldElement.addContent(captionElement.addContent(caption));
470     }
471
472     if (!editFormat.isEmpty()){
473       fieldElement.addContent(editFormat.getEditFormatElement());
474     }
475     if (!displayFormat.isEmpty()){
476       fieldElement.addContent(displayFormat.getDisplayFormatElement());
477     }
478     Element defaultValueElement = new Element("default-value");
479     if (defaultValue.length() != 0){
480       fieldElement.addContent(defaultValueElement.addContent(defaultValue));
481     }
482     return fieldElement;
483   }
484 }
485
Popular Tags