KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > resources > metadata > properties > ranges > AbsoluteChildObjectRange


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.rm.resources.metadata.properties.ranges;
20
21 import java.util.*;
22 import java.util.logging.*;
23
24 import org.openharmonise.commons.dsi.*;
25 import org.openharmonise.rm.*;
26 import org.openharmonise.rm.dsi.DataStoreInterfaceFactory;
27 import org.openharmonise.rm.factory.*;
28 import org.openharmonise.rm.metadata.ChildObjectPropertyInstance;
29 import org.openharmonise.rm.publishing.*;
30 import org.openharmonise.rm.resources.*;
31 import org.openharmonise.rm.resources.publishing.Template;
32 import org.w3c.dom.*;
33
34
35 /**
36  * Class which represents a <code>Property</code> range which is
37  * restricted to <code>AbstractChildObject</code> values defined by an
38  * absolute path.
39  *
40  * @author Michael Bell
41  * @version $Revision: 1.2 $
42  */

43 public class AbsoluteChildObjectRange
44     extends AbstractRange
45     implements ChildObjectRange, Publishable {
46
47     /**
48      * Absolute child object range XML tag name
49      */

50     public final static String JavaDoc TAG_ABSOLUTECHILDOBJECT_RANGE = "AbsoluteChildObjectRange";
51     
52     /**
53      * The available values XML element name
54      */

55     public static final String JavaDoc TAG_AVAILABLEVALUES = "AvailableValues";
56     
57     /**
58      * The list of paths which specify the parent objects which define
59      * the collection of allowable values for this property range
60      */

61     private List m_parent_restrictions = null;
62     
63     /**
64      * The separator used to separate values in the string which is
65      * saved to the database representing the restrictions on this range
66      */

67     static final private String JavaDoc SEPARATOR = "|";
68     
69     private static Logger m_logger = Logger.getLogger(AbsoluteChildObjectRange.class.getName());
70
71     /**
72      * Constructs a new <code>AbsoluteChildObjectRange</code>.
73      *
74      */

75     public AbsoluteChildObjectRange() {
76         super();
77     }
78
79     /**
80      * Constructs an <code>AbsoluteChildObjectRange</code> with the specified
81      * class type and path restrictions.
82      *
83      * @param sObjClassName the class name of objects within this range
84      * @param sDetails the <code>String</code> representation of the path
85      * restrictions for objects within this range.
86      */

87     public AbsoluteChildObjectRange(String JavaDoc sObjClassName, String JavaDoc sDetails) {
88         super(sObjClassName, sDetails);
89     }
90
91     /**
92      * Constructs an <code>AbsoluteChildObjectRange</code> with the specified
93      * class type restriction.
94      *
95      * @param sObjClassName the class name of objects within this range
96      */

97     public AbsoluteChildObjectRange(String JavaDoc sObjClassName) {
98         super(sObjClassName);
99     }
100
101     /**
102      * Constructs an <code>AbsoluteChildObjectRange</code> with the specified
103      * class type and parent object restrictions.
104      *
105      * @param sObjClassName the class name of objects within this range
106      * @param parent the parent object which defines the path restriction
107      * for objects within this range
108      * @throws DataAccessException
109      */

110     public AbsoluteChildObjectRange(String JavaDoc sObjClassName, AbstractParentObject parent)
111         throws DataAccessException {
112         super(sObjClassName, parent.getFullPath());
113     }
114
115     /* (non-Javadoc)
116      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#isValid(java.lang.Object)
117      */

118     public boolean isValid(Object JavaDoc obj) {
119         boolean bIsValid = false;
120
121         if (obj instanceof AbstractChildObject) {
122
123             try {
124                 Class JavaDoc clss = Class.forName(m_sObjectClassName);
125
126                 if (clss.isInstance(obj) == true) {
127                     if (m_sDetails == null || m_sDetails.length() == 0) {
128                         bIsValid = true;
129                     } else {
130                         AbstractChildObject child = (AbstractChildObject) obj;
131
132                         Iterator iter = getAllowedParents().iterator();
133                         
134                         while (iter.hasNext() && bIsValid == false) {
135                             String JavaDoc sPath = (String JavaDoc) iter.next();
136                             bIsValid = child.getPath().startsWith(sPath);
137                         }
138                         
139                     }
140                 }
141             } catch (DataAccessException e) {
142                 //if we can't get the path from the child it's probably not valid
143
m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
144                 bIsValid = false;
145             } catch (ClassNotFoundException JavaDoc e) {
146                 //if class is not found it is definitely not valid
147
m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
148                 bIsValid = false;
149             }
150
151         }
152
153         return bIsValid;
154     }
155
156     /**
157      * Returns a list of available <code>AbstractChildObject</code> instances this
158      * range defines.
159      *
160      * @return a list of available <code>AbstractChildObject</code> instances this
161      * range defines
162      * @throws DataAccessException if an error occurs obtaining the instances
163      */

164     public List getAvailableValues() throws DataAccessException {
165         Vector result = new Vector();
166         try {
167             AbstractDataStoreInterface dsi =
168                 DataStoreInterfaceFactory.getDataStoreInterface();
169
170             String JavaDoc sParentClassName = AbstractChildObject.getParentObjectClassName(m_sObjectClassName);
171             
172             Iterator iter = getAllowedParents().iterator();
173
174             while (iter.hasNext()) {
175                 String JavaDoc sPath = (String JavaDoc) iter.next();
176                 
177                 AbstractParentObject parent =
178                                 (AbstractParentObject) HarmoniseObjectFactory.instantiateHarmoniseObject(
179                                     dsi,
180                                     sParentClassName,
181                                     sPath);
182
183                 addAllChildObjectsToList(result, parent);
184                 
185             }
186
187             
188         } catch (HarmoniseFactoryException e) {
189             throw new DataAccessException(
190                 "Error occured getting parent from factory",e);
191         } catch (DataStoreException e) {
192             throw new DataAccessException(
193                 "Error occured getting datastoreinterface",e);
194         }
195
196         return result;
197     }
198
199     /* (non-Javadoc)
200      * @see org.openharmonise.rm.publishing.Publishable#publish(org.w3c.dom.Element, org.openharmonise.rm.publishing.HarmoniseOutput, org.openharmonise.rm.publishing.State)
201      */

202     public Element publish(Element topEl, HarmoniseOutput output, State state)
203         throws PublishException {
204         Element el = null;
205
206         if (topEl.getTagName().equals(TAG_AVAILABLEVALUES) == true) {
207             el = output.createElement(TAG_AVAILABLEVALUES);
208
209             try {
210                 AbstractDataStoreInterface dsi =
211                     DataStoreInterfaceFactory.getDataStoreInterface();
212
213                 NodeList templates =
214                     topEl.getElementsByTagName(Template.TAG_TEMPLATE);
215
216                 if (templates.getLength() > 0) {
217                     Element templateEl = (Element) templates.item(0);
218
219                     Template template =
220                         (Template) HarmoniseObjectFactory.instantiateHarmoniseObject(
221                             dsi,
222                             templateEl,
223                             state);
224
225                     List values = getAvailableValues();
226
227                     Iterator iter = values.iterator();
228
229                     while (iter.hasNext()) {
230                         Publishable val = (Publishable) iter.next();
231
232                         el.appendChild(val.publish(template, output, state));
233                     }
234                 }
235             } catch (HarmoniseFactoryException e) {
236                 throw new PublishException(
237                     "Error occured getting template from factory",e);
238             } catch (DataAccessException e) {
239                 throw new PublishException(
240                     "Error occured getting available values",e);
241             } catch (DataStoreException e) {
242                 throw new PublishException(
243                     "Error occured getting data store interface",e);
244             }
245
246         } else {
247             el = super.publish(topEl, output, state);
248         }
249
250         return el;
251     }
252
253     /*----------------------------------------------------------------------------
254     Private Methods
255     -----------------------------------------------------------------------------*/

256
257     /**
258      * Adds all the non <code>AbstractParentObject</code> <code>AbstractChildObject</code>
259      * objects of the given <code>AbstractParentObject</code> to the given <code>List</code>.
260      *
261      * @param result the list which will hold the child objects
262      * @param parent the parent object whose descendants will be added to the
263      *
264      * @throws DataAccessException if there is an error getting the children of
265      * the given parent object
266      */

267     protected void addAllChildObjectsToList(
268         List result,
269         AbstractParentObject parent)
270         throws DataAccessException {
271         Iterator iter = parent.getChildren().iterator();
272
273         while (iter.hasNext()) {
274             AbstractChildObject child = (AbstractChildObject) iter.next();
275
276             if(isValid(child)) {
277                 result.add(child);
278             }
279
280             if (child instanceof AbstractParentObject) {
281                 addAllChildObjectsToList(result, (AbstractParentObject) child);
282             }
283         }
284     }
285
286     /* (non-Javadoc)
287      * @see java.lang.Object#equals(java.lang.Object)
288      */

289     public boolean equals(Object JavaDoc obj) {
290         boolean bResult = false;
291
292         if (obj instanceof AbsoluteChildObjectRange) {
293             bResult = super.equals(obj);
294         }
295
296         return bResult;
297     }
298
299     /**
300      * Returns a list of parent objects which define the path restrictions
301      * for this range.
302      *
303      * @return a list of parent objects which define the path restrictions
304      * for this range
305      * @throws DataAccessException
306      */

307     public List getAllowedParents() throws DataAccessException {
308         validateAllowedParents();
309         
310         List allowedParent = new ArrayList();
311         
312         if(m_parent_restrictions != null) {
313             allowedParent.addAll(m_parent_restrictions);
314         }
315         
316         return allowedParent;
317     }
318
319     /**
320      * Sets the list of path restrictions which determine the members of this
321      * range.
322      *
323      * @param allowedParents list of path restrictions
324      */

325     public void setAllowedParents(List allowedParents) {
326         if ((m_parent_restrictions == null && allowedParents != null) || m_parent_restrictions.equals(allowedParents) == false) {
327             m_parent_restrictions = new ArrayList(allowedParents);
328             isChanged(true);
329         }
330     }
331     
332     /**
333      * Adds the specified path restriction to the list of path restrictions.
334      *
335      * @param sPath the new path restriction
336      */

337     public void addAllowedParent(String JavaDoc sPath) {
338         if(m_parent_restrictions == null) {
339             m_parent_restrictions = new ArrayList();
340         }
341         
342         if(m_parent_restrictions.contains(sPath) == false) {
343             m_parent_restrictions.add(sPath);
344             isChanged(true);
345         }
346         
347     }
348
349     /* (non-Javadoc)
350      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#getDetails()
351      */

352     public String JavaDoc getDetails() {
353         if (isChanged()) {
354             StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
355             if (m_parent_restrictions != null) {
356                 try {
357                     validateAllowedParents();
358                 } catch (DataAccessException e) {
359                     m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
360                 }
361                 Iterator iter = m_parent_restrictions.iterator();
362
363                 while (iter.hasNext()) {
364                     String JavaDoc sPath = (String JavaDoc) iter.next();
365                     strbuf.append(sPath);
366
367                     if (iter.hasNext()) {
368                         strbuf.append(SEPARATOR);
369                     }
370                 }
371
372             }
373             super.setDetails(strbuf.toString());
374         }
375
376         return super.getDetails();
377     }
378
379     /* (non-Javadoc)
380      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#setDetails(java.lang.String)
381      */

382     public void setDetails(String JavaDoc sDetails) {
383         if (m_parent_restrictions == null) {
384             m_parent_restrictions = new ArrayList();
385         }
386         
387         if (sDetails != null) {
388             StringTokenizer tokenizer =
389                 new StringTokenizer(sDetails, SEPARATOR);
390
391
392             int i = 0;
393             int nTmp = 0;
394             while (tokenizer.hasMoreTokens()) {
395                 String JavaDoc token = tokenizer.nextToken();
396
397                 if (token != null && token.length() > 0) {
398                     m_parent_restrictions.add(token);
399                 }
400             }
401         }
402         super.setDetails(sDetails);
403     }
404     
405     /**
406      * Sets the object type restriction for this range.
407      *
408      * @param sClassname the class name of the objects within this range
409      */

410     public void setObjectRestriction(String JavaDoc sClassname) {
411         if((m_sObjectClassName == null && sClassname != null) || m_sObjectClassName.equals(sClassname) == false) {
412             m_sObjectClassName = sClassname;
413             isChanged(true);
414         }
415         
416     }
417
418     /* (non-Javadoc)
419      * @see org.openharmonise.rm.resources.metadata.properties.ranges.ChildObjectRange#getChildObjectValueClassName(org.openharmonise.rm.resources.AbstractChildObject)
420      */

421     public String JavaDoc getChildObjectValueClassName(AbstractChildObject child) {
422         return getObject();
423     }
424
425     /* (non-Javadoc)
426      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#getPropertyInstanceClass()
427      */

428     public Class JavaDoc getPropertyInstanceClass() throws ClassNotFoundException JavaDoc {
429         
430         return ChildObjectPropertyInstance.class;
431     }
432
433     /* (non-Javadoc)
434      * @see org.openharmonise.rm.publishing.Publishable#getTagName()
435      */

436     public String JavaDoc getTagName() {
437         return TAG_ABSOLUTECHILDOBJECT_RANGE;
438     }
439
440     /* (non-Javadoc)
441      * @see org.openharmonise.rm.publishing.Publishable#populate(org.w3c.dom.Element, org.openharmonise.rm.publishing.State)
442      */

443     public void populate(Element xmlElement, State state)
444         throws PopulateException {
445         String JavaDoc sTagname = xmlElement.getTagName();
446         
447         if(sTagname.equals(TAG_PATH_RESTRICTION)) {
448             String JavaDoc sPath = xmlElement.getFirstChild().getNodeValue();
449             addAllowedParent(sPath);
450         } else {
451             super.populate(xmlElement, state);
452         }
453         
454     }
455
456     /* (non-Javadoc)
457      * @see org.openharmonise.rm.resources.metadata.properties.ranges.ChildObjectRange#getChildObjectValueClassName(java.lang.Class)
458      */

459     public String JavaDoc getChildObjectValueClassName(Class JavaDoc clss) {
460         return getObject();
461     }
462     
463     private void validateAllowedParents() throws DataAccessException {
464         if(m_parent_restrictions != null && m_sObjectClassName != null) {
465             try {
466                 AbstractDataStoreInterface dsi =
467                     DataStoreInterfaceFactory.getDataStoreInterface();
468                 ListIterator iter = m_parent_restrictions.listIterator();
469                 String JavaDoc sParentClassName = AbstractChildObject.getParentObjectClassName(m_sObjectClassName);
470                 
471                 if(sParentClassName != null) {
472                     while (iter.hasNext()) {
473                         String JavaDoc sPath = (String JavaDoc) iter.next();
474                         
475                         AbstractObject obj = HarmoniseObjectFactory.instantiateHarmoniseObject(dsi, sParentClassName, sPath);
476                     
477                         if(obj == null || obj.exists() == false) {
478                             iter.remove();
479                         }
480                     }
481                 }
482                 
483             } catch (HarmoniseFactoryException e) {
484                 throw new DataAccessException(e);
485             } catch (DataStoreException e) {
486                 throw new DataAccessException(e);
487             }
488         }
489     }
490
491 }
492
Popular Tags