KickJava   Java API By Example, From Geeks To Geeks.

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


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 ObjEntityView extends DVObject {
69   private DataView dataView;
70   private ObjEntity objEntity;
71
72   private List objEntityViewFields = new ArrayList();
73
74   private List loadErrors = new ArrayList();
75   private List saveErrors = new ArrayList();
76
77   private Set objEntityViewFieldsNames = new HashSet();
78
79   public ObjEntityView(CayenneProject project, DataView dataView, Element element) {
80     this.dataView = dataView;
81     String JavaDoc viewPath = "<b>" + dataView + ".";
82     String JavaDoc attributeValue = element.getAttributeValue("name");
83     if ((attributeValue == null) || (attributeValue.length() == 0)){
84       setName("");
85       viewPath += "</b><br>";
86       loadErrors.add(viewPath + "ObjEntity View has no attribute \"name\"<br><br>");
87     } else {
88       setName(attributeValue);
89       viewPath += attributeValue + "</b><br>";
90     }
91
92     attributeValue = element.getAttributeValue("obj-entity-name");
93     if ((attributeValue == null) || (attributeValue.length() == 0)){
94       objEntity = null;
95       loadErrors.add(viewPath + "ObjEntity View has no attribute \"obj-entity-name\"<br><br>");
96     } else {
97       ObjEntity entity = project.getObjEntity(attributeValue);
98       if (entity != null){
99         objEntity = entity;
100         objEntity.addObjEntityView(this);
101       } else {
102         objEntity = null;
103         loadErrors.add(viewPath + "ObjEntity \""
104         + attributeValue + "\" does not exist in the project<br><br>");
105       }
106
107     }
108     List objEntityViewFieldElements = element.getChildren("field");
109     Iterator itr = objEntityViewFieldElements.iterator();
110     while(itr.hasNext()){
111       Object JavaDoc o = itr.next();
112       Element fieldElement = (Element)o;
113       ObjEntityViewField field = new ObjEntityViewField(this, fieldElement);
114       if (objEntityViewFieldsNames.add(field.getName()) == false){
115         String JavaDoc path = "<b>" + dataView.getName() + "." + getName()
116             + "." + field.getName() + "</b><br>";
117         loadErrors.add(path + "ObjEntityView Field \"" + field.getName()
118             + "\" already exists in the ObjEntity View\"" + getName() + "\"<br><br>");
119       }
120       loadErrors.addAll(field.getLoadErrors());
121     }
122     objEntityViewFieldsNames.clear();
123     dataView.addObjEntityView(this);
124   }
125
126   public ObjEntityView(DataView dataView){
127     this.dataView = dataView;
128     setName("ObjEntityView");
129     dataView.addObjEntityView(this);
130   }
131
132   public List getLoadErrors(){
133     return Collections.unmodifiableList(loadErrors);
134   }
135
136   public List getSaveErrors(){
137     return Collections.unmodifiableList(saveErrors);
138   }
139
140   public void setDataView(DataView dataView){
141     this.dataView = dataView;
142   }
143
144   public DataView getDataView(){
145     return dataView;
146   }
147   public void clearObjEntity(){
148     objEntity = null;
149   }
150
151   public void setObjEntity(ObjEntity objEntity){
152     if(this.objEntity != null){
153       this.objEntity.removeObjEntityView(this);
154     }
155     if (objEntity != null){
156       this.objEntity = objEntity;
157       objEntity.getDataMap().addObjEntityView(this);
158     }else{
159       this.objEntity = null;
160     }
161   }
162
163   public ObjEntity getObjEntity(){
164     try{
165       return objEntity;
166     } catch(Exception JavaDoc e){
167       return null;
168     }
169   }
170
171   public List getObjEntityViewFields() {
172     return Collections.unmodifiableList(objEntityViewFields);
173   }
174
175   public ObjEntityViewField createObjEntityViewField(){
176     String JavaDoc nameRoot = "field";
177     String JavaDoc name = nameRoot;
178     for (int i = 0; i < Integer.MAX_VALUE; i++) {
179       name = nameRoot + i;
180       boolean nameExists = false;
181       for (int j = 0; j < objEntityViewFields.size(); j++) {
182         ObjEntityViewField objEntityViewField = (ObjEntityViewField)objEntityViewFields.get(j);
183         nameExists = name.equalsIgnoreCase(objEntityViewField.getName());
184         if (nameExists)
185           break;
186       }
187       if (!nameExists)
188         break;
189     }
190     ObjEntityViewField objEntityViewField = new ObjEntityViewField(this);
191     objEntityViewField.setName(name);
192     return objEntityViewField;
193   }
194
195   public void addObjEntityViewField(ObjEntityViewField objEntityViewField){
196     objEntityViewFields.add(objEntityViewField);
197   }
198
199   public void removeObjEntityViewField(ObjEntityViewField objEntityViewField){
200     objEntityViewFields.remove(objEntityViewField);
201   }
202
203   public ObjEntityViewField getObjEntityViewField(String JavaDoc name){
204     Iterator itr = objEntityViewFields.iterator();
205     while (itr.hasNext()){
206       Object JavaDoc o = itr.next();
207       ObjEntityViewField objEntField = (ObjEntityViewField)o;
208       if (objEntField.getName().equals(name)){
209         return objEntField;
210       }
211     }
212     return null;
213   }
214
215   public ObjEntityViewField getObjEntityViewField(int index){
216     return (ObjEntityViewField)(objEntityViewFields.get(index));
217   }
218
219   public int getIndexOfObjEntityViewField(ObjEntityViewField objEntityViewField){
220     return objEntityViewFields.indexOf(objEntityViewField);
221   }
222
223
224   public int getObjEntityViewFieldCount(){
225     return objEntityViewFields.size();
226   }
227
228   public void clear(){
229     objEntityViewFields.clear();
230   }
231
232   public String JavaDoc toString(){
233     return getName();
234   }
235
236   public Element getObjEntityViewElement(){
237     Element objEntityViewElement = new Element("obj-entity-view");
238
239     if (saveErrors.size() != 0){
240       saveErrors.clear();
241     }
242
243     String JavaDoc viewPath = "<b>" + dataView.getName() + "."
244                       + getName() + "</b><br>";
245
246     objEntityViewElement.setAttribute("name", getName());
247     if (getName().length() == 0){
248       saveErrors.add(viewPath + "ObjEntity View has no name<br><br>");
249     }
250
251     if (objEntity != null){
252       objEntityViewElement.setAttribute("obj-entity-name", objEntity.getName());
253     }else{
254       objEntityViewElement.setAttribute("obj-entity-name", "");
255       saveErrors.add(viewPath + "ObjEntity View has no attribute obj-entity-name<br><br>");
256     }
257
258     List objEntityViewFieldElements = new ArrayList();
259     Set objEntityViewFieldsSort = new TreeSet(new ObjEntityViewFieldComparator());
260     objEntityViewFieldsSort.addAll(objEntityViewFields);
261
262     Iterator itr = objEntityViewFieldsSort.iterator();
263     while (itr.hasNext()){
264       Object JavaDoc o = itr.next();
265       ObjEntityViewField field = (ObjEntityViewField)o;
266       Element objEntityViewFieldElement =
267                      field.getObjEntityViewFieldElement();
268       if (objEntityViewFieldsNames.add(field.getName()) == false){
269         String JavaDoc path = "<b>" + dataView.getName() + "." + getName()
270             + "." + field.getName() + "</b><br>";
271         saveErrors.add(path + "ObjEntityView Field \"" + field.getName()
272             + "\" already exists in the ObjEntity View \"" + getName() + "\"<br><br>");
273       }
274
275       saveErrors.addAll(field.getSaveErrors());
276       objEntityViewFieldElements.add(objEntityViewFieldElement);
277     }
278     objEntityViewFieldsNames.clear();
279     objEntityViewElement.setContent(objEntityViewFieldElements);
280     return objEntityViewElement;
281   }
282 }
283
Popular Tags