KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > dav > server > property > domains > DAVDomain


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.domains;
21
22
23 import java.util.*;
24 import java.util.logging.*;
25
26 import org.openharmonise.commons.dsi.AbstractDataStoreInterface;
27 import org.openharmonise.commons.net.MimeTypeMapping;
28 import org.openharmonise.commons.xml.XMLUtils;
29 import org.openharmonise.commons.xml.namespace.NamespaceType;
30 import org.openharmonise.dav.server.managers.HarmonisePropertiesManager;
31 import org.openharmonise.dav.server.utils.*;
32 import org.openharmonise.rm.*;
33 import org.openharmonise.rm.metadata.Profile;
34 import org.openharmonise.rm.resources.*;
35 import org.openharmonise.rm.resources.content.*;
36 import org.openharmonise.rm.resources.metadata.properties.Property;
37 import org.openharmonise.rm.resources.metadata.properties.domains.Domain;
38 import org.openharmonise.rm.resources.metadata.values.Value;
39 import org.w3c.dom.*;
40 import org.w3c.dom.Document JavaDoc;
41
42 import com.ibm.webdav.*;
43
44 /**
45  * This class wraps up the functionality for publishing Harmonise domains to
46  * DAV XML and vice versa.
47  *
48  * @author Michael Bell
49  * @version $Revision: 1.2 $
50  * @since November 19, 2003
51  */

52 public class DAVDomain {
53
54     public static final String JavaDoc TAG_DOMAIN = "domain";
55     public static final String JavaDoc TAG_MIN_OCCURS = "minOccurs";
56     public static final String JavaDoc TAG_MAX_OCCURS = "maxOccurs";
57     public static final String JavaDoc TYPE_DOMAIN_DATA = "domainData";
58     public static final String JavaDoc TAG_CONTENT_TYPE = "contenttype";
59
60     private AbstractDataStoreInterface m_dsi = null;
61     
62     /**
63      * Logger for this class
64      */

65     private static final Logger m_logger = Logger.getLogger(DAVDomain.class.getName());
66
67     /**
68      * List of domains associated to a particular property
69      *
70      */

71     private List m_domain_list = null;
72
73     /**
74      * Creates an object with a data store interface
75      *
76      * @param dsi
77      */

78     public DAVDomain(AbstractDataStoreInterface dsi) {
79         m_dsi = dsi;
80     }
81
82     /**
83      * Creates an object with a data store interface and a list of Harmonise domains
84      * which are associated with a property.
85      */

86     public DAVDomain(AbstractDataStoreInterface dsi, List domains) {
87         m_domain_list = domains;
88         m_dsi = dsi;
89
90     }
91
92     /**
93      * Returns the current the domain specification of a resource as XML.
94      *
95      * @param doc
96      * @return
97      */

98     public Element asXML(Document JavaDoc doc) throws WebDAVException {
99         Element domainEl =
100             doc.createElementNS(NamespaceType.DAV.getURI(), TAG_DOMAIN);
101         domainEl.setPrefix(NamespaceType.DAV.getPrefix());
102
103         if (m_domain_list.size() > 1) {
104
105             domainEl.setAttributeNS(
106                 NamespaceType.SOAP_ENCODING.getURI(),
107                 NamespaceType.SOAP_ENCODING.getPrefix()
108                     + ":"
109                     + HarmonisePropertiesManager.ATTRIB_ARRAY_SIZE,
110                 String.valueOf(m_domain_list.size()));
111
112             domainEl.setAttributeNS(
113                 NamespaceType.SOAP_ENCODING.getURI(),
114                 NamespaceType.SOAP_ENCODING.getPrefix()
115                     + ":"
116                     + HarmonisePropertiesManager.ATTRIB_ITEM_TYPE,
117                 TYPE_DOMAIN_DATA);
118             domainEl.setAttribute(
119                 "xmlns:" + NamespaceType.SOAP_ENCODING.getPrefix(),
120                 NamespaceType.SOAP_ENCODING.getURI());
121
122             Iterator iter = m_domain_list.iterator();
123
124             while (iter.hasNext()) {
125                 Domain domain = (Domain) iter.next();
126
127                 Element domainDataEl =
128                     doc.createElementNS(
129                         NamespaceType.DAV.getURI(),
130                         TYPE_DOMAIN_DATA);
131                 
132                 domainDataEl.setPrefix(NamespaceType.DAV.getPrefix());
133                 addDomainDetails(domainDataEl, domain, doc);
134
135                 domainEl.appendChild(domainDataEl);
136             }
137
138         } else if (m_domain_list.size() == 1) {
139
140             addDomainDetails(domainEl, (Domain) m_domain_list.get(0), doc);
141         }
142
143         return domainEl;
144     }
145
146     /**
147      * Adds the domain details to the given domain element.
148      *
149      * @param domainEl
150      * @param doc
151      * @throws WebDAVException
152      */

153     private void addDomainDetails(
154         Element domainEl,
155         Domain domain,
156         Document JavaDoc doc)
157         throws WebDAVException {
158
159         try {
160             String JavaDoc sDomainObj = domain.getDomainClass();
161
162             Element resourceTypeEl =
163                 doc.createElementNS(
164                     NamespaceType.DAV.getURI(),
165                     HarmonisePropertiesManager.TAG_RESOURCETYPE);
166             resourceTypeEl.setPrefix(NamespaceType.DAV.getPrefix());
167
168             String JavaDoc sResourceType =
169                 HarmoniseNameResolver.getResourceTypeFromClass(sDomainObj);
170
171             if(m_logger.isLoggable(Level.FINE)) {
172                 m_logger.logp(Level.FINE, this.getClass().getName(), "addDomainDetails", "domain type - " + sResourceType + ", object - " + sDomainObj);
173             }
174             
175             Element resourceEl =
176                 doc.createElementNS(NamespaceType.DAV.getURI(), sResourceType);
177             resourceEl.setPrefix(NamespaceType.DAV.getPrefix());
178             
179             resourceTypeEl.appendChild(resourceEl);
180
181             domainEl.appendChild(resourceTypeEl);
182
183             int nMinOccurs = domain.getMinOccurs();
184             int nMaxOccurs = domain.getMaxOccurs();
185
186             if (nMinOccurs > 0) {
187                 Element minOccEl = doc.createElementNS(NamespaceType.DAV.getURI(), TAG_MIN_OCCURS);
188                 minOccEl.setPrefix(NamespaceType.DAV.getPrefix());
189                 minOccEl.appendChild(
190                     doc.createTextNode(String.valueOf(nMinOccurs)));
191                 domainEl.appendChild(minOccEl);
192             }
193
194             if (nMaxOccurs > 0) {
195                 Element maxOccEl = doc.createElementNS(NamespaceType.DAV.getURI(), TAG_MAX_OCCURS);
196                 maxOccEl.setPrefix(NamespaceType.DAV.getPrefix());
197                 maxOccEl.appendChild(
198                     doc.createTextNode(String.valueOf(nMaxOccurs)));
199                 domainEl.appendChild(maxOccEl);
200             }
201
202             List domainDetails = domain.getDetails();
203
204             Class JavaDoc domainClass = Class.forName(sDomainObj);
205             
206             if(Profile.class.isAssignableFrom(domainClass)) {
207                 domainClass = Property.class;
208             }
209             
210             if(domainDetails.size() > 0) {
211                 Iterator detailIter = domainDetails.iterator();
212
213                 while (detailIter.hasNext()) {
214                     String JavaDoc sDomainDetails = (String JavaDoc) detailIter.next();
215                     
216                     String JavaDoc sDAVpath =
217                         HarmoniseNameResolver.getDAVPath(domainClass, sDomainDetails);
218
219                     Element hrefEl =
220                         doc.createElementNS(
221                             NamespaceType.DAV.getURI(),
222                             HarmonisePropertiesManager.TAG_HREF);
223                     hrefEl.setPrefix(NamespaceType.DAV.getPrefix());
224                     hrefEl.appendChild(doc.createTextNode(sDAVpath));
225                     domainEl.appendChild(hrefEl);
226                 }
227                 
228             }
229
230             List contentRestrictions = domain.getContentRestrictions();
231
232             Iterator contentIter = contentRestrictions.iterator();
233
234             while (contentIter.hasNext()) {
235                 String JavaDoc sContentType = (String JavaDoc) contentIter.next();
236
237                 Element contentEl =
238                     doc.createElementNS(
239                         NamespaceType.DAV.getURI(),
240                         TAG_CONTENT_TYPE);
241                 contentEl.setPrefix(NamespaceType.DAV.getPrefix());
242                 contentEl.appendChild(doc.createTextNode(sContentType));
243                 domainEl.appendChild(contentEl);
244             }
245
246         } catch (DataAccessException e) {
247             m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
248             throw new WebDAVException(
249                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
250                 e.getLocalizedMessage());
251         } catch (ClassNotFoundException JavaDoc e) {
252             throw new WebDAVException(
253                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
254                 e.getLocalizedMessage());
255         } catch (NameResolverException e) {
256             throw new WebDAVException(
257                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
258                 e.getLocalizedMessage());
259         }
260
261     }
262
263     /**
264      * @param propEl
265      */

266     public void populate(Element propEl) throws WebDAVException {
267
268         if(m_domain_list == null) {
269             m_domain_list = new ArrayList();
270         }
271
272         NodeList nodes = propEl.getChildNodes();
273         
274         if(nodes.getLength() > 0) {
275             String JavaDoc sArraySize =
276                 propEl.getAttributeNS(NamespaceType.SOAP_ENCODING.getURI(),HarmonisePropertiesManager.ATTRIB_ARRAY_SIZE);
277     
278             String JavaDoc sItemType = propEl.getAttributeNS(NamespaceType.SOAP_ENCODING.getURI(), HarmonisePropertiesManager.ATTRIB_ITEM_TYPE);
279     
280             if (sArraySize != null && sArraySize.trim().length() > 0) {
281                 NodeList itemNodes = propEl.getElementsByTagName(sItemType);
282     
283                 for (int i = 0; i < itemNodes.getLength(); i++) {
284                     if (itemNodes.item(i).getNodeType() != Node.ELEMENT_NODE) {
285                         continue;
286                     }
287                     Element tmpEl = (Element) itemNodes.item(i);
288                     
289                     Domain domain = instantiateDomain(tmpEl);
290     
291                     if(domain != null) {
292                         m_domain_list.add(domain);
293                     }else {
294                         throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Invalid domain spec");
295                     }
296                 }
297     
298             } else {
299                 Domain domain = instantiateDomain(propEl);
300     
301                 if(domain != null) {
302                     m_domain_list.add(domain);
303                 } else {
304                     throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Invalid domain spec");
305                 }
306                 
307             }
308         }
309     }
310
311     /**
312      * @return
313      */

314     public List getHarmoniseDomains() {
315         return m_domain_list;
316     }
317
318     /**
319      * Instantiates a Harmonise domain from the given element.
320      *
321      * @param dataEl
322      * @return
323      * @throws WebDAVException
324      */

325     private Domain instantiateDomain(Element dataEl) throws WebDAVException {
326
327         
328         Domain domain = new Domain(m_dsi);
329
330         try {
331             
332             //deal with min and max occurs
333
Element minEl = XMLUtils.getFirstNamedChild(dataEl, TAG_MIN_OCCURS);
334
335             if (minEl != null) {
336                 String JavaDoc sMin = minEl.getChildNodes().item(0).getNodeValue();
337
338                 domain.setMinOccurs(Integer.parseInt(sMin));
339             }
340
341             Element maxEl = XMLUtils.getFirstNamedChild(dataEl, TAG_MAX_OCCURS);
342
343             if (maxEl != null) {
344                 String JavaDoc sMax = maxEl.getChildNodes().item(0).getNodeValue();
345                 
346                 if(sMax.equalsIgnoreCase("unbounded") == true) {
347                     domain.setMaxOccurs(Domain.UNBOUNDED);
348                 } else {
349                     domain.setMaxOccurs(Integer.parseInt(sMax));
350                 }
351             }
352             
353             //get resource type
354
String JavaDoc sResourceType = null;
355             
356             Element resourceEl = XMLUtils.getFirstNamedChild(dataEl, HarmonisePropertiesManager.TAG_RESOURCETYPE);
357             
358             if(resourceEl != null) {
359                 Element resourceTypeEl = XMLUtils.getFirstElementChild(resourceEl);
360                 sResourceType = resourceTypeEl.getLocalName();
361             }
362
363             //deal with hrefs
364
NodeList hrefNodes =
365                 dataEl.getElementsByTagNameNS(
366                     NamespaceType.DAV.getURI(),
367                     HarmonisePropertiesManager.TAG_HREF);
368                     
369             AbstractChildObject child = null;
370
371             for (int i = 0; i < hrefNodes.getLength(); i++) {
372                 Element hrefEl = (Element) hrefNodes.item(i);
373
374                 String JavaDoc hrefVal = hrefEl.getChildNodes().item(0).getNodeValue();
375                 
376                 
377                 
378                 if(m_logger.isLoggable(Level.FINE)) {
379                     m_logger.logp(Level.FINE, this.getClass().getName(), "instantiateDomain","domain href - " + hrefVal);
380                 }
381
382                 child =
383                     HarmoniseNameResolver.getObjectFromURL(m_dsi, hrefVal);
384                     
385                 
386                 if(child != null) {
387                     domain.addDetails(child.getFullPath());
388                 } else {
389                     throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,"No resource found matching domain");
390                 }
391                 
392
393             }
394
395             //deal with content types
396
NodeList contentNodes =
397                 dataEl.getElementsByTagNameNS(
398                     NamespaceType.DAV.getURI(),
399                     TAG_CONTENT_TYPE);
400             List contentTypes = new Vector();
401
402             for (int i = 0; i < contentNodes.getLength(); i++) {
403                 Element contentEl = (Element) contentNodes.item(i);
404
405                 String JavaDoc contentVal =
406                     contentEl.getChildNodes().item(0).getNodeValue();
407
408                 contentTypes.add(contentVal);
409                 domain.addContentTypeRestriction(contentVal);
410
411             }
412             
413             //set domain obj
414
String JavaDoc sDomainObj = this.getObjectType((AbstractParentObject) child, sResourceType, contentTypes);
415             
416             
417             Class JavaDoc domainClass = Class.forName(sDomainObj);
418             
419             if(AbstractParentObject.class.isAssignableFrom(domainClass)
420                     && domain.getMinOccurs() > 0) {
421                 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Collection domains must have a minoccurs of 0");
422             }
423             
424             if(sDomainObj != null) {
425                 domain.setDomainClass(sDomainObj);
426             } else {
427                 domain = null;
428             }
429
430         } catch (ClassNotFoundException JavaDoc e) {
431             throw new WebDAVException(
432                     WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
433                     e.getLocalizedMessage());
434         } catch (NumberFormatException JavaDoc e) {
435             throw new WebDAVException(
436                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
437                 e.getLocalizedMessage());
438         } catch (PopulateException e) {
439             throw new WebDAVException(
440                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
441                 e.getLocalizedMessage());
442         } catch (DataAccessException e) {
443             throw new WebDAVException(
444                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
445                 e.getLocalizedMessage());
446         } catch (NameResolverException e) {
447             throw new WebDAVException(
448                 WebDAVStatus.SC_INTERNAL_SERVER_ERROR,
449                 e.getLocalizedMessage());
450         }
451
452         return domain;
453
454     }
455     
456     /**
457      * Returns the correct Harmonise object class name given the child object referenced
458      * by the href and the resource type.
459      *
460      * @param child
461      * @param sResourceType
462      * @return
463      */

464     private String JavaDoc getObjectType(AbstractParentObject parent, String JavaDoc sResourceType, List contentTypes) throws DataAccessException {
465         String JavaDoc sObjType = null;
466         
467         String JavaDoc parentName = parent.getClass().getName();
468         
469         if(sResourceType.equals("collection") == true) {
470             sObjType = parentName;
471         } else if(sResourceType.equals("value") == true) {
472             sObjType = Value.class.getName();
473         } else if(sResourceType.equals("property") == true) {
474             sObjType = Profile.class.getName();
475         } else if(sResourceType.equals("property-resource") == true) {
476             sObjType = Property.class.getName();
477         } else {
478             List childClassTypes = parent.getChildClassNames();
479             
480             Iterator iter = childClassTypes.iterator();
481             
482             while (iter.hasNext() && sObjType == null) {
483                 String JavaDoc className = (String JavaDoc) iter.next();
484                 if(className.equals(parentName) == false) {
485                     
486                     if(contentTypes == null || contentTypes.size() == 0) {
487                         if(parentName.equals(Section.class.getName()) == true) {
488                             if(parent.getFullPath().startsWith(HarmoniseNameResolver.ASSETS_PATH)) {
489                                 sObjType = Asset.class.getName();
490                             } else {
491                                 sObjType = org.openharmonise.rm.resources.content.Document.class.getName();
492                             }
493                         } else {
494                             sObjType = className;
495                         }
496                         
497                     } else {
498                         //get class name by content type - should only be needed
499
//for Asset/Document distinction
500
if(parentName.equals(Section.class.getName()) == true) {
501                             if(contentTypes.get(0).equals(MimeTypeMapping.XML.getMimeType()) == false || parent.getFullPath().startsWith(HarmoniseNameResolver.ASSETS_PATH)) {
502                                 sObjType = Asset.class.getName();
503                             } else {
504                                 sObjType = org.openharmonise.rm.resources.content.Document.class.getName();
505                             }
506                         }
507                     }
508                     
509                 }
510             }
511             
512         }
513         
514         
515         return sObjType;
516     }
517
518 }
519
Popular Tags