1 56 57 package org.objectstyle.cayenne.dataview.dvmodeler; 58 59 import java.util.*; 60 import org.jdom.*; 61 62 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 viewPath = "<b>" + dataView + "."; 82 String 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 o = itr.next(); 112 Element fieldElement = (Element)o; 113 ObjEntityViewField field = new ObjEntityViewField(this, fieldElement); 114 if (objEntityViewFieldsNames.add(field.getName()) == false){ 115 String 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 e){ 167 return null; 168 } 169 } 170 171 public List getObjEntityViewFields() { 172 return Collections.unmodifiableList(objEntityViewFields); 173 } 174 175 public ObjEntityViewField createObjEntityViewField(){ 176 String nameRoot = "field"; 177 String 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 name){ 204 Iterator itr = objEntityViewFields.iterator(); 205 while (itr.hasNext()){ 206 Object 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 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 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 o = itr.next(); 265 ObjEntityViewField field = (ObjEntityViewField)o; 266 Element objEntityViewFieldElement = 267 field.getObjEntityViewFieldElement(); 268 if (objEntityViewFieldsNames.add(field.getName()) == false){ 269 String 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 |