Source code: com/gammastream/validity/GSVModel.java
1 package com.gammastream.validity;
2 //javadoc *.java -author -package com.gammastream.validity -d api
3
4 import com.webobjects.appserver.xml.*;
5 import com.webobjects.foundation.*;
6 import com.webobjects.eocontrol.*;
7 import com.webobjects.eoaccess.*;
8 import java.io.*;
9 import java.util.*;
10
11 /**
12 * GSVModel used for the mapping validation rules to an entities' attributes.<BR>
13 * The structure is very similar to Apple's EOModel.
14 *
15 * @author GammaStream Technologies, Inc.
16 */
17 public final class GSVModel extends Object implements WOXMLCoding {
18
19
20 /******************************** STATIC ********************************/
21
22 /**
23 * The name of the GSVModel file included in the '.eomodeld' wrapper.<BR>
24 * Currently the full name is: 'Validity.model'
25 */
26 public static final String MODEL_NAME = "Validity";
27
28 /**
29 * The extension of the GSVModel file included in the '.eomodeld' wrapper.<BR>
30 * Currently the full name is: 'Validity.model'
31 */
32 public static final String MODEL_EXTENSION = "model";
33
34
35 /******************************** INSTANCE ********************************/
36
37 //persistant model attributes
38 private NSMutableArray _entities = null;
39 private String _eomodelPath = null;
40 private String _eomodelName = null;
41
42 //helper for mapping the GSVModel to an EOModel
43 private EOModelGroup _eomodelGroup = null;
44
45 /**
46 * Creates a new GSVModel using the path to an '.eomodeld' file.<BR>
47 * <BR>
48 * Example of creating a GSVModel for the Movies example which ships with WebObjects.<BR>
49 * <BR>
50 * <blockquote>
51 * <code>
52 * EOModel eoModel = EOModelGroup.defaultGroup().modelNamed("Moview");<BR>
53 * GSVModel model = new GSVModel(eoModel.path());<BR>
54 * </code>
55 * </blockquote>
56 *
57 * @param path Path to an '.eomodeld' file.
58 * @exception java.lang.IllegalArgumentException Thrown if valid '.eomodeld' file does not exist at the specified path.
59 */
60 public GSVModel(String path) throws IllegalArgumentException {
61 if(this.validateEOModelForPath(path)){
62 _entities = new NSMutableArray();
63 _eomodelPath = path;
64 _eomodelName = NSPathUtilities.stringByDeletingPathExtension(NSPathUtilities.lastPathComponent(_eomodelPath));
65 _eomodelGroup = new EOModelGroup();
66 _eomodelGroup.addModelWithPath(_eomodelPath);
67 //this.saveModel();
68 } else {
69 throw new IllegalArgumentException("Could not find a valid EOModel at: " + path);
70 }
71 }
72
73 /**
74 * Determines that there is indeed a file located at the given path.
75 *
76 * @return <code>true</code> if an '.eomodeld' file is located at the given path; otherwise, we return <code>false</code>.
77 * @param path Path to the desired '.eomodeld' file.
78 *
79 */
80 public boolean validateEOModelForPath(String path){
81 try {
82 File f = new File(path);
83 return f.exists();
84 } catch(Exception e){
85 return false;
86 }
87 }
88
89 /**
90 * Saves the GSVModel to inside the '.eomodeld' file wrapper.
91 *
92 * @return <code>true</code> if save is successful; otherwise, returns <code>false</code>.
93 */
94 public boolean saveModel(){
95 String codedString = WOXMLCoder.coder().encodeRootObjectForKey(this, "Model");
96 String fullFileName = NSPathUtilities.stringByAppendingPathExtension(GSVModel.MODEL_NAME, GSVModel.MODEL_EXTENSION);
97 String xmlPath = NSPathUtilities.stringByAppendingPathComponent(_eomodelPath, fullFileName);
98 try {
99 File configurationFile = new File(xmlPath);
100
101 FileOutputStream fos = new FileOutputStream(configurationFile);
102 fos.write(codedString.getBytes());
103 fos.close();
104 NSLog.out.appendln("did save model to file");
105 return true;
106 } catch(IOException e) {
107 NSLog.out.appendln(e);
108 return false;
109 }
110 }
111
112 /**
113 * Returns the EOModelGroup that is being used to query information from the EOModel.
114 *
115 * @return EOModelGroup for the EOModel.
116 */
117 public EOModelGroup eomodelGroup(){
118 return _eomodelGroup;
119 }
120
121 /**
122 * Returns the EOModel Name for the associated GSVModel.
123 *
124 * @return Name of the EOModel
125 */
126 public String eomodelName(){
127 return _eomodelName;
128 }
129
130 /**
131 * Returns the path of the '.eomodeld' file.
132 *
133 * @return Path of the EOModel File.
134 * @see #setEomodelPath
135 */
136 public String eomodelPath(){
137 return _eomodelPath;
138 }
139
140 /**
141 * Set the path of the EOModel file.
142 *
143 * @param path Path of the EOModel File.
144 * @see #eomodelPath
145 */
146
147 public void setEomodelPath(String path){
148 _eomodelPath = path;
149 }
150
151 /**
152 * Returns the NSArray of GSVEntity Objects.
153 *
154 * @return NSArray of GSVEntity Objects.
155 * @see #addEntity
156 * @see #removeEntity
157 */
158
159 public NSArray entities(){
160 return _entities;
161 }
162
163 /**
164 * Adds a GSVEntity object to this GSVModel. The GSVEntity must not already exsit.<BR>
165 * If it does, IllegalArgumentException is thrown.
166 *
167 * @param newEntity GSVEntity object
168 * @exception java.lang.IllegalArgumentException GSVEnitity already exsits.
169 * @see #entities
170 * @see #removeEntity
171 */
172 public void addEntity(GSVEntity newEntity) throws IllegalArgumentException{
173 GSVEntity currentEntity = null;
174 for(int i=0;i<_entities.count();i++){
175 currentEntity = (GSVEntity)_entities.objectAtIndex(i);
176 if(currentEntity.name().equals(newEntity.name())){
177 throw new IllegalArgumentException("Entity for name '"+newEntity.name()+"' already exsits in model named "+this.eomodelName());
178 }
179 }
180 _entities.addObject(newEntity);
181 }
182
183 /**
184 * Removes a GSVEntity object from this GSVModel.
185 *
186 * @param oldEntity GSVEntity object to remove.
187 * @see #entities
188 * @see #addEntity
189 */
190 public void removeEntity(GSVEntity oldEntity){
191 _entities.removeObject(oldEntity);
192 }
193
194 /**
195 * Returns the GSVEntity object associated with the provided EOEnterpriseObject.
196 *
197 * @param object EOEnterpriseObject
198 * @return GSVEntity if the EOEnterpriseObject has an GSVEntity Null if not.
199 * @see #entityNamed
200 */
201 public GSVEntity entityForObject(Object object){
202 if(object instanceof EOEnterpriseObject){
203 GSVEntity currentEntity = null;
204 for(int i=0;i<_entities.count();i++){
205 currentEntity = (GSVEntity)_entities.objectAtIndex(i);
206 if(currentEntity.name().equals(((EOEnterpriseObject)object).entityName()))
207 return currentEntity;
208 }
209 }
210 return null;
211 }
212
213 /**
214 * Returns the GSVEntity object for the provided entity name.
215 *
216 * @param name name of entity
217 * @return GSVEntity if an GSVEntity is named name, <code>null</code> if it could not be found.
218 * @see #entityForObject
219 */
220 public GSVEntity entityNamed(String name){
221 GSVEntity currentEntity = null;
222 for(int i=0;i<_entities.count();i++){
223 currentEntity = (GSVEntity)_entities.objectAtIndex(i);
224 if(currentEntity.name().equals(name))
225 return currentEntity;
226 }
227 return null;
228 }
229
230 /**
231 * Returns an NSArray containing the GSVEntity names.
232 *
233 * @return NSArray names of GSVEntities.
234 */
235 public NSArray entityNames(){
236 NSMutableArray names = new NSMutableArray();
237 GSVEntity currentEntity = null;
238 for(int i=0;i<_entities.count();i++){
239 currentEntity = (GSVEntity)_entities.objectAtIndex(i);
240 names.addObject(currentEntity.name());
241 }
242 return names;
243 }
244
245 /**
246 * Internal method for saving paths
247 */
248 public void savePath(String s){
249 _eomodelPath = s;
250 _eomodelGroup.addModelWithPath(_eomodelPath);
251 this.saveModel();
252 }
253
254 /******************************** WOXMLCoding Impl ********************************/
255
256 /**
257 * WOXMLCoding Impl
258 *
259 * @param coder WOXMLCoder
260 *
261 * @see #GSVModel
262 */
263 public void encodeWithWOXMLCoder(WOXMLCoder coder) {
264 coder.encodeObjectForKey(_entities.immutableClone(), "Entities");
265 coder.encodeObjectForKey(_eomodelName, "EOModelName");
266 coder.encodeObjectForKey(_eomodelPath, "EOModelPath");
267 }
268
269 /**
270 * WOXMLCoding Impl
271 *
272 * @param decoder WOXMLDecoder
273 *
274 * @see #encodeWithWOXMLCoder
275 */
276 public GSVModel(WOXMLDecoder decoder) {
277 _entities = new NSMutableArray((NSArray)decoder.decodeObjectForKey("Entities"));
278 _eomodelName = (String)decoder.decodeObjectForKey("EOModelName");
279
280 //We changed "Name" to "EOModelName", so for backward compatibility, if "EOModelName"
281 //doesn't exist, we have to check for "Name"
282 if( _eomodelName == null ){
283 _eomodelName = (String)decoder.decodeObjectForKey("Name");
284 }
285 _eomodelGroup = new EOModelGroup();
286 _eomodelPath = (String)decoder.decodeObjectForKey("EOModelPath");
287 }
288
289
290 /**
291 * WOXMLCoding Impl
292 */
293 public Class classForCoder() {
294 try{
295 return Class.forName("com.gammastream.validity.GSVModel");
296 }catch(ClassNotFoundException e){
297 return null;
298 }
299 }
300
301 public void init(EOModel eomodel, EOEnterpriseObject eo) {
302 for (Enumeration e = entities().objectEnumerator(); e.hasMoreElements();) {
303 GSVEntity entity = (GSVEntity)e.nextElement();
304 EOEntity eoentity = eomodel.entityNamed(entity.name());
305 NSLog.debug.appendln("checking gsventity"+entity.name());
306 if ( eoentity == null) {
307 removeEntity(entity);
308 NSLog.debug.appendln("removed obsolete gsventity"+entity.name());
309 } else {
310 entity.init(this, eoentity);
311 }
312 }
313 }
314
315 }