KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 import org.openharmonise.commons.dsi.AbstractDataStoreInterface;
24 import org.openharmonise.rm.*;
25 import org.openharmonise.rm.factory.*;
26 import org.openharmonise.rm.metadata.*;
27 import org.openharmonise.rm.publishing.*;
28 import org.openharmonise.rm.resources.AbstractParentObject;
29 import org.openharmonise.rm.resources.metadata.properties.PropertyGroup;
30 import org.w3c.dom.Element JavaDoc;
31
32
33 /**
34  * Class which represents a <code>Property</code> range which is
35  * restricted to <code>Profile</code> values.
36  *
37  * @author Michael Bell
38  * @version $Revision: 1.3 $
39  *
40  */

41 public class ProfileRange extends AbstractRange implements Range, Publishable {
42     
43     /**
44      * Profile range tag name
45      */

46     public final static String JavaDoc TAG_PROFILE_RANGE = "ProfileRange";
47     
48     /**
49      * Path restriction XML tag name
50      */

51     public final static String JavaDoc TAG_PATH_RESTRICTION = "PathRestriction";
52     
53     /**
54      * The separator character used to separate range restrictions when compiling
55      * the <code>String</code> representation for the database details field
56      */

57     static final private String JavaDoc SEPARATOR = "|";
58     
59     /**
60      * The list of paths of <code>PropertyGroup</code>s that have
61      * <code>Property</code> children whose instances can be added to the
62      * <code>Profile</code> values in this range
63      */

64     private List m_property_restrictions = null;
65
66     /**
67      * Constructs a new <code>ProfileRange</code>,
68      *
69      */

70     public ProfileRange() {
71         super(Profile.class.getName(), null);
72     }
73
74     /**
75      * Constructs a <code>ProfileRange</code> with the specified class type and
76      * other restrictions,
77      *
78      * @param sObjClassName the class name of the values of this range. This will
79      * normally be the class name of <code>Profile</code> but could
80      * concievably be the class name of a subclass of <code>Profile</code>
81      * @param sDetails a <code>String</code> representation of the non class
82      * type restrictions
83      */

84     public ProfileRange(String JavaDoc sObjClassName, String JavaDoc sDetails) {
85         super(sObjClassName, sDetails);
86     }
87
88     /* (non-Javadoc)
89      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#isValid(java.lang.Object)
90      */

91     public boolean isValid(Object JavaDoc obj) {
92         return (obj instanceof Profile);
93     }
94
95     /* (non-Javadoc)
96      * @see java.lang.Object#equals(java.lang.Object)
97      */

98     public boolean equals(Object JavaDoc obj) {
99         boolean bResult = false;
100
101         if (obj instanceof ProfileRange) {
102             bResult = super.equals(obj);
103         }
104
105         return bResult;
106     }
107
108     /**
109      * Returns a list of paths of <code>PropertyGroup</code>s whose <code>Property</code>
110      * children can be added to <code>Profile</code> values of this range,
111      *
112      * @return a list of paths of <code>PropertyGroup</code>s
113      */

114     public List getAllowedPropertyParents() {
115         return new ArrayList(m_property_restrictions);
116     }
117
118     /**
119      * Sets the list of paths of <code>PropertyGroup</code>s whose <code>Property</code>
120      * children can be added to <code>Profile</code> values of this range,
121      *
122      * @param allowedParents a list of paths of <code>PropertyGroup</code>s
123      */

124     public void setAllowedPropertyParents(List allowedParents) {
125         if ((m_property_restrictions == null && allowedParents != null)
126             || m_property_restrictions.equals(allowedParents) == false) {
127             m_property_restrictions = new ArrayList(allowedParents);
128             isChanged(true);
129         }
130
131     }
132     
133     /**
134      * Adds an individual path to the current list of <code>PropertyGroup</code>
135      * paths whose <code>Property</code> children can be added to the
136      * <code>Profile</code> values of this range.
137      *
138      * @param sPath the <code>PropertyGroup</code> path
139      */

140     public void addAllowedPropertyParent(String JavaDoc sPath) {
141         if(m_property_restrictions == null) {
142             m_property_restrictions = new ArrayList();
143         }
144         
145         if(m_property_restrictions.contains(sPath) == false) {
146             m_property_restrictions.add(sPath);
147             isChanged(true);
148         }
149     }
150
151     /* (non-Javadoc)
152      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#getDetails()
153      */

154     public String JavaDoc getDetails() {
155         if (isChanged()) {
156             StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
157             if (m_property_restrictions != null) {
158                 Iterator iter = m_property_restrictions.iterator();
159
160                 while (iter.hasNext()) {
161                     String JavaDoc sPath = (String JavaDoc) iter.next();
162                     strbuf.append(sPath);
163
164                     if (iter.hasNext()) {
165                         strbuf.append(SEPARATOR);
166                     }
167                 }
168
169             }
170             super.setDetails(strbuf.toString());
171         }
172
173         return super.getDetails();
174     }
175
176     /* (non-Javadoc)
177      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#setDetails(java.lang.String)
178      */

179     public void setDetails(String JavaDoc sDetails) {
180         if (m_property_restrictions == null) {
181             m_property_restrictions = new ArrayList();
182         }
183
184         if (sDetails != null) {
185             StringTokenizer tokenizer =
186                 new StringTokenizer(sDetails, SEPARATOR);
187
188             int i = 0;
189             int nTmp = 0;
190             while (tokenizer.hasMoreTokens()) {
191                 String JavaDoc token = tokenizer.nextToken();
192
193                 if (token != null && token.length() > 0) {
194                     m_property_restrictions.add(token);
195                 }
196             }
197         }
198         super.setDetails(sDetails);
199     }
200
201     /**
202      * Returns the properties whose instances can be added to the
203      * <code>Profile</code> values of this range.
204      *
205      * @param dsi the data store interface
206      * @return the properties whose instances can be added to the
207      * <code>Profile</code> values of this range
208      * @throws DataAccessException if an error occurs instantiating the
209      * <code>PropertyGroup</code>s or getting the children of those
210      * <code>PropertyGroup</code>s
211      */

212     public List getAllowedProperties(AbstractDataStoreInterface dsi)
213         throws DataAccessException {
214         List props = new Vector();
215
216         List propGroupPaths = this.getAllowedPropertyParents();
217
218         Iterator iter = propGroupPaths.iterator();
219
220         while (iter.hasNext()) {
221             String JavaDoc sPath = (String JavaDoc) iter.next();
222
223             try {
224                 PropertyGroup propGroup =
225                     (PropertyGroup) HarmoniseObjectFactory.instantiateHarmoniseObject(
226                         dsi,
227                         PropertyGroup.class.getName(),
228                         sPath);
229                 
230                 if(propGroup != null) {
231                     props.addAll(
232                             propGroup.getChildrenByType(
233                                 AbstractParentObject.LEAF_NODES));
234                 } else {
235                     handleInvalidParentRestriction(sPath);
236                 }
237                 
238             } catch (HarmoniseFactoryException e) {
239                 throw new DataAccessException(e.getLocalizedMessage(),e);
240             }
241
242         }
243
244         return props;
245     }
246
247     /**
248      * Handle an invalid path.
249      *
250      * @param path the invalid path
251      */

252     private void handleInvalidParentRestriction(String JavaDoc path) {
253         if(m_property_restrictions != null && path != null) {
254             m_property_restrictions.remove(path);
255         }
256     }
257
258     /* (non-Javadoc)
259      * @see org.openharmonise.rm.resources.metadata.properties.ranges.Range#getPropertyInstanceClass()
260      */

261     public Class JavaDoc getPropertyInstanceClass() throws ClassNotFoundException JavaDoc {
262         return ProfilePropertyInstance.class;
263     }
264     
265     /* (non-Javadoc)
266      * @see org.openharmonise.rm.publishing.Publishable#getTagName()
267      */

268     public String JavaDoc getTagName() {
269         return TAG_PROFILE_RANGE;
270     }
271     
272     /* (non-Javadoc)
273      * @see org.openharmonise.rm.publishing.Publishable#populate(org.w3c.dom.Element, org.openharmonise.rm.publishing.State)
274      */

275     public void populate(Element JavaDoc xmlElement, State state)
276         throws PopulateException {
277         String JavaDoc sTagname = xmlElement.getTagName();
278         
279         if(sTagname.equals(TAG_PATH_RESTRICTION)) {
280             String JavaDoc sPath = xmlElement.getFirstChild().getNodeValue();
281             addAllowedPropertyParent(sPath.trim());
282         } else {
283             super.populate(xmlElement, state);
284         }
285         
286     }
287
288 }
289
Popular Tags