KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > dav > server > property > ranges > DAVResourceRange


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
20 package org.openharmonise.dav.server.property.ranges;
21
22 import java.util.*;
23
24 import org.openharmonise.commons.dsi.AbstractDataStoreInterface;
25 import org.openharmonise.commons.xml.XMLUtils;
26 import org.openharmonise.commons.xml.namespace.NamespaceType;
27 import org.openharmonise.dav.server.managers.HarmonisePropertiesManager;
28 import org.openharmonise.dav.server.utils.*;
29 import org.openharmonise.rm.DataAccessException;
30 import org.openharmonise.rm.factory.*;
31 import org.openharmonise.rm.resources.*;
32 import org.openharmonise.rm.resources.content.*;
33 import org.openharmonise.rm.resources.metadata.properties.Property;
34 import org.openharmonise.rm.resources.metadata.properties.ranges.*;
35 import org.openharmonise.rm.resources.metadata.values.Value;
36 import org.w3c.dom.*;
37 import org.w3c.dom.Document JavaDoc;
38
39 import com.ibm.webdav.*;
40
41 /**
42  * Class to provide functionaliy to represent resource ranges.
43  *
44  * @author Michael Bell
45  * @version $Revision: 1.2 $
46  * @since November 20, 2003
47  */

48 public class DAVResourceRange extends DAVRange {
49
50     /**
51      * @param dsi
52      */

53     public DAVResourceRange(AbstractDataStoreInterface dsi) {
54         super(dsi, new AbsoluteChildObjectRange());
55
56     }
57
58     /**
59      * @param dsi
60      * @param range
61      */

62     public DAVResourceRange(AbstractDataStoreInterface dsi, Range range) {
63         super(dsi, range);
64     }
65
66     /**
67      * @param dsi
68      * @param davPropEl
69      */

70     public DAVResourceRange(AbstractDataStoreInterface dsi, Element davPropEl)
71         throws WebDAVException {
72         super(dsi, new AbsoluteChildObjectRange(), davPropEl);
73     }
74     
75     /**
76      * @param dsi
77      * @param davPropEl
78      */

79     public DAVResourceRange(AbstractDataStoreInterface dsi,Range range, Element davPropEl)
80         throws WebDAVException {
81         super(dsi, range, davPropEl);
82     }
83
84     /**
85      * Adds range details for to the given range element for child object ranges
86      *
87      * @param rangeEl
88      * @param range
89      * @param doc
90      * @throws WebDAVException
91      */

92     protected void addRangeDetails(Element rangeEl, Range range, Document JavaDoc doc)
93         throws WebDAVException {
94
95         try {
96
97             Element resourceTypeEl =
98                 doc.createElementNS(
99                     NamespaceType.DAV.getURI(),
100                     HarmonisePropertiesManager.TAG_RESOURCETYPE);
101             resourceTypeEl.setPrefix(NamespaceType.DAV.getPrefix());
102
103             String JavaDoc sRangeObj = ((ChildObjectRange)range).getChildObjectValueClassName((AbstractChildObject)null);
104             String JavaDoc sResourceType =
105                 HarmoniseNameResolver.getResourceTypeFromClass(sRangeObj);
106
107             Element valEl =
108                 doc.createElementNS(NamespaceType.DAV.getURI(), sResourceType);
109             valEl.setPrefix(NamespaceType.DAV.getPrefix());
110             resourceTypeEl.appendChild(valEl);
111             rangeEl.appendChild(resourceTypeEl);
112
113             Class JavaDoc clss = Class.forName(sRangeObj);
114
115             AbstractChildObject child =
116                 (AbstractChildObject) clss.newInstance();
117
118             String JavaDoc sParentClass = child.getParentObjectClassName();
119
120             List allowedParents = ((AbsoluteChildObjectRange) range).getAllowedParents();
121
122             Iterator iter = allowedParents.iterator();
123
124             while (iter.hasNext()) {
125                 String JavaDoc sPath = (String JavaDoc) iter.next();
126
127                 AbstractParentObject parent =
128                     (
129                         AbstractParentObject) HarmoniseObjectFactory
130                             .instantiateHarmoniseObject(
131                         m_dsi,
132                         sParentClass,
133                         sPath);
134
135                 if (parent != null) {
136                     String JavaDoc sDAVPath = HarmoniseNameResolver.getDAVPath(parent);
137                     Element hrefEl =
138                         doc.createElementNS(
139                             NamespaceType.DAV.getURI(),
140                             HarmonisePropertiesManager.TAG_HREF);
141                     hrefEl.setPrefix(NamespaceType.DAV.getPrefix());
142                     hrefEl.appendChild(doc.createTextNode(sDAVPath));
143                     rangeEl.appendChild(hrefEl);
144                 }
145             }
146
147         } catch (DataAccessException e) {
148             throw new WebDAVException(
149                     WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
150                     e.getLocalizedMessage());
151         } catch (HarmoniseFactoryException e) {
152             throw new WebDAVException(
153                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
154                 e.getLocalizedMessage());
155         } catch (NameResolverException e) {
156             throw new WebDAVException(
157                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
158                 e.getLocalizedMessage());
159         } catch (ClassNotFoundException JavaDoc e) {
160             throw new WebDAVException(
161                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
162                 e.getLocalizedMessage());
163         } catch (InstantiationException JavaDoc e) {
164             throw new WebDAVException(
165                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
166                 e.getLocalizedMessage());
167         } catch (IllegalAccessException JavaDoc e) {
168             throw new WebDAVException(
169                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
170                 e.getLocalizedMessage());
171         }
172     }
173
174     
175     /* (non-Javadoc)
176      * @see org.openharmonise.dav.server.property.ranges.DAVRange#populate(org.w3c.dom.Element)
177      */

178     public void populate(Element propEl) throws WebDAVException {
179         try {
180             //get resource type
181
String JavaDoc sResourceType = null;
182             
183             Element resourceEl = XMLUtils.getFirstNamedChild(propEl, HarmonisePropertiesManager.TAG_RESOURCETYPE);
184             
185             if(resourceEl != null) {
186                 Element resourceTypeEl = XMLUtils.getFirstElementChild(resourceEl);
187                 sResourceType = resourceTypeEl.getLocalName();
188             }
189             
190             //deal with hrefs
191
NodeList hrefNodes =
192                 propEl.getElementsByTagNameNS(
193                     NamespaceType.DAV.getURI(),
194                     HarmonisePropertiesManager.TAG_HREF);
195                     
196             List ohPaths = new ArrayList();
197             AbstractParentObject parent = null;
198             
199             String JavaDoc sParentClassName = null;
200             for (int i = 0; i < hrefNodes.getLength(); i++) {
201                 Element hrefEl = (Element) hrefNodes.item(i);
202             
203                 String JavaDoc hrefVal = hrefEl.getChildNodes().item(0).getNodeValue();
204             
205                 if(hrefVal != null && hrefVal.length() > 0) {
206                     AbstractChildObject child =
207                     HarmoniseNameResolver.getObjectFromURL(m_dsi, hrefVal);
208
209                     ohPaths.add(child.getFullPath());
210                     parent = (AbstractParentObject) child;
211                 
212                     //ensure that we're not mixing parent types in the hrefs
213
if(sParentClassName != null && parent.getClass().getName().equals(sParentClassName) == false) {
214                         throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Invalid set of paths");
215                     }
216                     
217                     sParentClassName = parent.getClass().getName();
218                 }
219                 
220             }
221             
222             ((AbsoluteChildObjectRange)m_range).setAllowedParents(ohPaths);
223             String JavaDoc sClassname = null;
224             //get object type
225
if(ohPaths.size()>0) {
226                 if(sResourceType.equals(HarmonisePropertiesManager.TAG_COLLECTION)) {
227                     sClassname = parent.getClass().getName();
228                 } else if(sResourceType.equals(TAG_VALUE)) {
229                     sClassname = Value.class.getName();
230                 } else if(sResourceType.equals(HarmonisePropertiesManager.TAG_PROPERTY_RESOURCE) == true) {
231                     sClassname = Property.class.getName();
232                 } else {
233                     List childClassTypes = parent.getChildClassNames();
234                     String JavaDoc parentName = parent.getClass().getName();
235                     Iterator iter = childClassTypes.iterator();
236                 
237                     while (iter.hasNext() && sClassname == null) {
238                         String JavaDoc sTmpClass = (String JavaDoc) iter.next();
239                         if(sTmpClass.equals(parentName) == false) {
240                         
241                             if(childClassTypes.size() == 2) {
242                                 sClassname = sTmpClass;
243                             } else {
244                                 //get class name by content type - should only be needed
245
//for Asset/Document distinction
246
if(parentName.equals(Section.class.getName()) == true) {
247                                     if(parent.getFullPath().startsWith(HarmoniseNameResolver.ASSETS_PATH)) {
248                                         sClassname = Asset.class.getName();
249                                     } else {
250                                         sClassname = org.openharmonise.rm.resources.content.Document.class.getName();
251                                     }
252                                 }
253                             }
254                         
255                         }
256                     }
257                 
258                 }
259             } else {
260                 sClassname = HarmoniseNameResolver.getClassFromResourceTypeDesc(sResourceType, null);
261             }
262             
263             
264             ((AbsoluteChildObjectRange)m_range).setObjectRestriction(sClassname);
265             
266             
267         } catch (DataAccessException e) {
268             throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR,e.getLocalizedMessage());
269         } catch (NameResolverException e) {
270             throw new WebDAVException(WebDAVStatus.SC_INTERNAL_SERVER_ERROR,e.getLocalizedMessage());
271         }
272
273
274     }
275
276 }
277
Popular Tags