KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > resources > metadata > properties > domains > Domain


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.domains;
20
21 import java.sql.*;
22 import java.util.*;
23 import java.util.logging.*;
24
25 import org.openharmonise.commons.dsi.*;
26 import org.openharmonise.commons.dsi.dml.*;
27 import org.openharmonise.commons.dsi.dml.functions.*;
28 import org.openharmonise.commons.xml.XMLUtils;
29 import org.openharmonise.rm.*;
30 import org.openharmonise.rm.factory.*;
31 import org.openharmonise.rm.publishing.*;
32 import org.openharmonise.rm.resources.*;
33 import org.openharmonise.rm.resources.lifecycle.EditException;
34 import org.openharmonise.rm.resources.metadata.properties.Property;
35 import org.openharmonise.rm.resources.publishing.Template;
36 import org.w3c.dom.*;
37
38
39 /**
40  * This class represents a property domain which can be associated to
41  * a <code>Property</code> can apply to within Harmonise. A
42  * property domain defines the set of objects that may have an instance
43  * of the associated property. The domain may be restricted by object type, path
44  * and content type.
45  *
46  * @author Michael Bell
47  * @version $Revision: 1.3 $
48  *
49  */

50 public class Domain implements Publishable, Cloneable JavaDoc {
51     
52     /**
53      * Path restriction XML tag name
54      */

55     public String JavaDoc TAG_PATHRESTRICTION = "PathRestriction";
56
57     /**
58      * <code>int</code> constant for representing an unbounded value
59      */

60     public static final int UNBOUNDED = -1;
61
62     //XML constants
63
/**
64      * Domain tag name
65      */

66     public static final String JavaDoc TAG_DOMAIN = "Domain";
67     
68     /**
69      * Domain object tag name
70      */

71     public static final String JavaDoc TAG_DOMAIN_OBJECT = "DomainObject";
72     
73     /**
74      * Domain details tag name
75      */

76     public static final String JavaDoc TAG_DOMAIN_DETAILS = "DomainDetails";
77     /**
78      * Domain details tag name
79      */

80     public static final String JavaDoc TAG_PATH = "Path";
81     /**
82      * Domain details tag name
83      */

84     public static final String JavaDoc TAG_CONTENT_RESTRICTIONS = "ContentRestrictions";
85     /**
86      * Minimum occurance attribute name
87      */

88     public static final String JavaDoc ATTRIB_MIN_OCCURS = "minOccurs";
89     
90     /**
91      * Maximum occurance attribute name
92      */

93     public static final String JavaDoc ATTRIB_MAX_OCCURS = "maxOccurs";
94     
95     /**
96      * Depth attribute name
97      */

98     public static final String JavaDoc ATTRIB_DEPTH = "depth";
99
100     //DB constants
101
/**
102      * Domain id database sequence name
103      */

104     public static final String JavaDoc SEQ_DOMAIN = "seq_property_domain";
105     
106     /**
107      * Domain database table name
108      */

109     public static final String JavaDoc TBL_NAME = "property_domain";
110     
111     /**
112      * Domain details database column name
113      */

114     public static final String JavaDoc TBL_DOMAIN_DETAILS = "domain_details";
115     
116     /**
117      * Domain content restrictions database table name
118      */

119     public static final String JavaDoc TBL_DOMAIN_CONTENT_RESTRICTIONS = "domain_content_details";
120     
121     /**
122      * Domain id database column name
123      */

124     public static final String JavaDoc CLMN_DOMAIN_ID = "id";
125     
126     /**
127      * Domain id database column name in the domain detail table
128      */

129     public static final String JavaDoc CLMN_DETAILS_DOMAIN_ID = "domain_id";
130     
131     /**
132      * Content type database column name
133      */

134     public static final String JavaDoc CLMN_CONTENT_TYPE = "content_type";
135     
136     /**
137      * Domain object database column name
138      */

139     public static final String JavaDoc CLMN_DOMAIN_OBJECT = "domain_object";
140     
141     /**
142      * Domain details database column name.
143      */

144     public static final String JavaDoc CLMN_DOMAIN_DETAILS = "domain_details";
145     
146     /**
147      * Property key database column name
148      */

149     public static final String JavaDoc CLMN_PROPERTY_KEY = "property_key";
150     
151     /**
152      * Minimum occurance database column name
153      */

154     public static final String JavaDoc CLMN_MIN_OCCURS = "minOccurs";
155     
156     /**
157      * Maximum occurance databse column name
158      */

159     public static final String JavaDoc CLMN_MAX_OCCURS = "maxOccurs";
160     
161     /**
162      * Depth database column name
163      */

164     public static final String JavaDoc CLMN_DEPTH = "depth";
165     
166     /**
167      * Map of child class to parent classes. Used to cache info.
168      */

169     public static Map m_child_parent_mappings = new Hashtable();
170
171     /**
172      * Id of this domain
173      */

174     private int m_nId = -1;
175     
176     /**
177      * Class name for objects in this domain
178      */

179     private String JavaDoc m_sObjectName = null;
180     
181     /**
182      * List of domain details. Specifies the paths to the parent
183      * objects which are the ancestors of all objects included in this
184      * domain
185      */

186     private List m_details = null;
187     
188     /**
189      * List of content restrictions, mime types, for this domain. For example,
190      * a domain may specify only resources which are 'image/jpeg'
191      */

192     private List m_contentRestrictions = null;
193     
194     /**
195      * Minimum occurance of the property associated to this domain on a resource
196      * in this domain.
197      */

198     private int m_nMinOccurs = 0;
199     
200     /**
201      * Maximum occurance of the property associated to this domain on a resource
202      * in this domain.
203      *
204      * Note: A value of -1 signifies an unbounded limit
205      */

206     private int m_nMaxOccurs = -1;
207     
208     /**
209      * <code>boolean</code> indicating whether the list of domain details has been
210      * populated.
211      */

212     private boolean m_bIsDetailsPopulated = false;
213     
214     /**
215      * <code>boolean</code> indicating whether the list of content restrictions
216      * has been populated.
217      */

218     private boolean m_bIsContentRestrictPopulated = false;
219     
220     /**
221      * <code>boolean</code> indicating whether this domain is a historical version
222      */

223     private boolean m_bIsHistorical = false;
224     
225     /**
226      * Data store interface for this domain
227      */

228     private AbstractDataStoreInterface m_dsi = null;
229     
230     /**
231      * The depth restriction from the collections defined by the domain details
232      * that determine the scope of this domain
233      *
234      */

235     private int m_nDepth = -1;
236     
237     /**
238      * <code>boolean</code> indicating whether this domain has been changed since
239      * being populated from the database
240      */

241     private boolean m_bIsChanged = false;
242     
243     /**
244      * Logger for this class
245      */

246     private static final Logger m_logger = Logger.getLogger(Domain.class.getName());
247
248     /**
249      * Constructs a <code>Domain</code> with no details and no interface to the
250      * database.
251      *
252      */

253     public Domain() {
254     }
255
256     /**
257      * Constructs a <code>Domain</code> with an interface to the database.
258      *
259      * @param dsi the data store interface
260      */

261     public Domain(AbstractDataStoreInterface dsi) {
262         m_dsi = dsi;
263     }
264
265     /**
266      * Constructs a <code>Domain</code> with an interface to the database with a
267      * class name restriction.
268      *
269      * @param dsi the data store interface
270      * @param sClassName the class name restriction
271      */

272     public Domain(AbstractDataStoreInterface dsi, String JavaDoc sClassName) {
273         m_dsi = dsi;
274         m_sObjectName = sClassName;
275     }
276
277     /**
278      * Constructs a known <code>Domain</code> from an id.
279      *
280      * @param dsi the data store interface
281      * @param nId the domain id
282      *
283      */

284     public Domain(AbstractDataStoreInterface dsi, int nId) {
285         m_dsi = dsi;
286         m_nId = nId;
287     }
288
289     /**
290      * Marks this domain as new.
291      *
292      */

293     public void markAsNew() {
294         m_nId = -1;
295     }
296
297     /**
298      * Returns the id of this domain.
299      *
300      * @return the id of this domain
301      */

302     public int getId() {
303         return m_nId;
304     }
305
306     /**
307      * Sets the id of this domain.
308      *
309      * @param nId the id of this domain
310      */

311     public void setId(int nId) {
312         m_nId = nId;
313     }
314
315     /**
316      * Returns the depth restiction on this domain.
317      *
318      * @return the depth restiction on this domain
319      */

320     public int getDepth() {
321         return m_nDepth;
322     }
323
324     /**
325      * Sets the depth of this domain.
326      *
327      * @param nId the depth of this domain
328      */

329     public void setDepth(int nDepth) {
330         m_nDepth = nDepth;
331     }
332
333     /**
334      * Returns the name of the <code>Class</code> restriction for this domain.
335      *
336      * @return the name of the <code>Class</code> restriction for this domain
337      */

338     public String JavaDoc getDomainClass() {
339         return m_sObjectName;
340     }
341
342     /**
343      * Sets the <code>Class</code> restriction for this domain.
344      *
345      * @param the name of the <code>Class</code> restriction for this domain
346      */

347     public void setDomainClass(String JavaDoc objectName) {
348         this.m_sObjectName = objectName;
349     }
350
351     /**
352      * Returns the list of path restrictions for this domain.
353      *
354      * @return the list of path restrictions for this domain.
355      * @throws DataAccessException if an error occurs populating
356      * the data from the database
357      */

358     public List getDetails() throws DataAccessException {
359         if (m_bIsDetailsPopulated == false) {
360             try {
361                 populateDetails();
362             } catch (DataStoreException e) {
363                 throw new DataAccessException("Error populating details", e);
364             }
365         }
366         
367         try {
368             List detailsToRemove = new ArrayList();
369             
370             ListIterator iter = m_details.listIterator();
371             
372             String JavaDoc domainClassName = getDomainClass();
373             String JavaDoc parentClassName = domainClassName;
374             
375             Class JavaDoc domainClass = Class.forName(domainClassName);
376             
377             if(AbstractParentObject.class.isAssignableFrom(domainClass) == false) {
378                 
379                 parentClassName = (String JavaDoc) m_child_parent_mappings.get(domainClass);
380                 
381                 if(parentClassName == null) {
382                     parentClassName = ((AbstractChildObject)domainClass.newInstance()).getParentObjectClassName();
383                     m_child_parent_mappings.put(domainClass, parentClassName);
384                 }
385                 
386             }
387             
388             while (iter.hasNext()) {
389                 String JavaDoc sDetail = (String JavaDoc) iter.next();
390                 AbstractObject obj = HarmoniseObjectFactory.instantiateHarmoniseObject(m_dsi,parentClassName, sDetail);
391                 
392                 if(obj == null || obj.exists() == false) {
393                     detailsToRemove.add(sDetail);
394                     iter.remove();
395                 }
396             }
397             
398             if(detailsToRemove.size() > 0) {
399                 DeleteStatement delete = new DeleteStatement();
400                 
401                 delete.addWhereCondition(getDetailsColumnRef(CLMN_DOMAIN_DETAILS, m_bIsHistorical), "IN", detailsToRemove);
402             
403                 m_dsi.execute(delete);
404             }
405         } catch (HarmoniseFactoryException e) {
406             throw new DataAccessException(e);
407         } catch (DataStoreException e) {
408             throw new DataAccessException(e);
409         } catch (ClassNotFoundException JavaDoc e) {
410             throw new DataAccessException(e);
411         } catch (InstantiationException JavaDoc e) {
412             throw new DataAccessException(e);
413         } catch (IllegalAccessException JavaDoc e) {
414             throw new DataAccessException(e);
415         }
416
417         return m_details;
418     }
419
420     /**
421      * Adds a path restriction to this domain.
422      *
423      * @param sDetails the path restriction to add
424      * @throws if an error occurs populating this domain
425      */

426     public void addDetails(String JavaDoc sDetails) throws PopulateException {
427         if (m_bIsDetailsPopulated == false) {
428             try {
429                 populateDetails();
430             } catch (DataStoreException e) {
431                 throw new PopulateException("Error populating details", e);
432             }
433         }
434
435         if (m_details.contains(sDetails) == false) {
436             m_details.add(sDetails);
437             m_bIsChanged = true;
438         }
439     }
440
441     /**
442      * Returns <code>true</code> if this domain has changed since being
443      * populated from the database.
444      *
445      * @return <code>true</code> if this domain has changed
446      */

447     public boolean isChanged() {
448         return m_bIsChanged;
449     }
450
451     /**
452      * Returns the list of content restrictions on this domain.
453      *
454      * @return the list of content restrictions on this domain
455      * @throws DataAccessException if an error occurs populating this
456      * domain
457      */

458     public List getContentRestrictions() throws DataAccessException {
459         if (m_bIsContentRestrictPopulated == false) {
460             try {
461                 populateContentRestrictions();
462             } catch (DataStoreException e) {
463                 throw new DataAccessException("Error populating details", e);
464             }
465         }
466
467         return this.m_contentRestrictions;
468     }
469
470     /**
471      * Adds a content type restriction to this domain.
472      *
473      * @param sDetails the content type restriction to add
474      * @throws PopulateException if an error occurs populating this object
475      */

476     public void addContentTypeRestriction(String JavaDoc sContentType)
477         throws PopulateException {
478         if (m_bIsDetailsPopulated == false) {
479             try {
480                 populateContentRestrictions();
481             } catch (DataStoreException e) {
482                 throw new PopulateException("Error populating details", e);
483             }
484         }
485
486         if (m_contentRestrictions.contains(sContentType) == false) {
487             m_contentRestrictions.add(sContentType);
488             m_bIsChanged = true;
489         }
490
491     }
492
493     /**
494      * Populates this domain with content restrictions from the DB.
495      *
496      * @throws DataStoreException if an error occurs quering the database
497      */

498     private void populateContentRestrictions() throws DataStoreException {
499         ResultSet rs = null;
500         
501         if (m_bIsContentRestrictPopulated == false) {
502
503             if (m_contentRestrictions == null) {
504                 m_contentRestrictions = new Vector();
505             }
506
507             SelectStatement select = new SelectStatement();
508
509             select.addSelectColumn(
510                 getContentRestrictionsColumnRef(
511                     CLMN_CONTENT_TYPE,
512                     m_bIsHistorical));
513
514             select.addWhereCondition(
515                 getContentRestrictionsColumnRef(
516                     CLMN_DETAILS_DOMAIN_ID,
517                     m_bIsHistorical),
518                 "=",
519                 this.m_nId);
520
521             rs = m_dsi.execute(select);
522
523             try {
524                 while (rs.next()) {
525                     String JavaDoc sDetail = rs.getString(1);
526
527                     m_contentRestrictions.add(sDetail);
528                 }
529             } catch (SQLException e) {
530                 throw new DataStoreException(
531                     "Error populating domain details",
532                     e);
533             } finally {
534                 if(rs!=null) {
535                     try {
536                         rs.close();
537                     } catch (SQLException e1) {
538                         throw new DataStoreException(
539                         "Error occured closing result set",e1);
540                     }
541                 }
542             }
543
544             m_bIsContentRestrictPopulated = true;
545         }
546
547     }
548
549     /**
550      * Returns the minimum occurance limit for the property on this domain.
551      *
552      * @return the minimum occurance limit for the property on this domain
553      */

554     public int getMinOccurs() {
555         return m_nMinOccurs;
556     }
557
558     /**
559      * Returns the maximum occurance limit for the property on this domain.
560      *
561      * Note: A value of -1 signifies an unbounded limit
562      *
563      * @return the maximum occurance limit for the property on this domain
564      */

565     public int getMaxOccurs() {
566         return m_nMaxOccurs;
567     }
568
569     /**
570      * Sets the minimum occurance limit for the property on this domain.
571      *
572      * @param nMinOccurs the minimum occurance limit
573      */

574     public void setMinOccurs(int nMinOccurs) {
575         m_nMinOccurs = nMinOccurs;
576     }
577
578     /**
579      * Sets the maximum occurance limit for the property on this domain.
580      *
581      * @param nMinOccurs the maximum occurance limit
582      */

583     public void setMaxOccurs(int nMaxOccurs) {
584         m_nMaxOccurs = nMaxOccurs;
585     }
586
587     /**
588      * Sets whether this domain is historical.
589      *
590      * @param bIsHist <code>true</code> if this domain is a historical version,
591      * otherwise <code>false</code>
592      */

593     public void setHistorical(boolean bIsHist) {
594         m_bIsHistorical = bIsHist;
595     }
596
597     /* (non-Javadoc)
598      * @see org.openharmonise.rm.publishing.Publishable#publish(org.openharmonise.rm.resources.publishing.Template, org.openharmonise.rm.publishing.HarmoniseOutput, org.openharmonise.rm.publishing.State)
599      */

600     public Element publish(Template template, HarmoniseOutput output, State state)
601         throws PublishException {
602         Element resultEl = null;
603
604             try {
605                 resultEl =
606                     publish(template.getTemplateRootElement(), output, state);
607             } catch (DataAccessException e) {
608                 throw new PublishException(e);
609             }
610         
611
612         return resultEl;
613     }
614
615     /* (non-Javadoc)
616      * @see org.openharmonise.rm.publishing.Publishable#publish(org.w3c.dom.Element, org.openharmonise.rm.publishing.HarmoniseOutput, org.openharmonise.rm.publishing.State)
617      */

618     public Element publish(Element topEl, HarmoniseOutput output, State state)
619         throws PublishException {
620         Element docEl = null;
621         NodeList nodes = null;
622         Text txt = null;
623         String JavaDoc sTagName = topEl.getTagName();
624
625         try {
626
627             if (topEl.getTagName().equals(getTagName())) {
628                 docEl = output.createElement(getTagName());
629
630                 if (m_nMinOccurs > 0) {
631                     docEl.setAttribute(
632                         ATTRIB_MIN_OCCURS,
633                         String.valueOf(m_nMinOccurs));
634                 }
635
636                 if (m_nMaxOccurs > 0) {
637                     docEl.setAttribute(
638                         ATTRIB_MAX_OCCURS,
639                         String.valueOf(m_nMaxOccurs));
640                 }
641
642                 nodes = topEl.getChildNodes();
643             } else if (sTagName.equals(TAG_DOMAIN_DETAILS)) {
644                 docEl = output.createElement(sTagName);
645                 
646                     if (m_bIsDetailsPopulated == false) {
647                         populateDetails();
648                     }
649
650                     Iterator iter = m_details.iterator();
651
652                     while (iter.hasNext()) {
653                         String JavaDoc sDetail = (String JavaDoc) iter.next();
654                     Element pathEl =
655                         output.createElement(TAG_PATH);
656                         txt = output.createTextNode(sDetail);
657                     pathEl.appendChild(txt);
658                     docEl.appendChild(pathEl);
659                     }
660             } else if (sTagName.equals(TAG_CONTENT_RESTRICTIONS)) {
661                 docEl = output.createElement(sTagName);
662                 if (m_bIsContentRestrictPopulated == false) {
663                     populateContentRestrictions();
664                 }
665                 Iterator iter = m_contentRestrictions.iterator();
666                 while (iter.hasNext()) {
667                     String JavaDoc sDetail = (String JavaDoc) iter.next();
668                     Element pathEl =
669                         output.createElement(TAG_PATH);
670                     txt = output.createTextNode(sDetail);
671                     pathEl.appendChild(txt);
672                     docEl.appendChild(pathEl);
673                 }
674             } else if (sTagName.equals(TAG_DOMAIN_OBJECT)) {
675                 docEl = output.createElement(sTagName);
676                 txt = output.createTextNode(getDomainClass());
677                 docEl.appendChild(txt);
678             }
679
680             // recurse through the children if there are any
681
Element formEl;
682             Element el;
683
684             if (nodes != null) {
685                 for (int i = 0; i < nodes.getLength(); i++) {
686                     if (nodes.item(i).getNodeType() != Node.ELEMENT_NODE) {
687                         continue;
688                     }
689
690                     formEl = (Element) nodes.item(i);
691                     el = publish(formEl, output, state);
692
693                     if (el != null) {
694                         docEl.appendChild(el);
695                     }
696                 }
697             }
698
699         } catch (DataStoreException e) {
700             throw new PublishException(e);
701         }
702
703         return docEl;
704     }
705
706     /* (non-Javadoc)
707      * @see org.openharmonise.rm.publishing.Publishable#populate(org.w3c.dom.Element, org.openharmonise.rm.publishing.State)
708      */

709     public void populate(Element xmlElement, State state)
710         throws PopulateException {
711         String JavaDoc sTagName = xmlElement.getTagName();
712         Text txt = null;
713         String JavaDoc sTemp = null;
714
715         if (sTagName.equals(getTagName()) == true) {
716
717             String JavaDoc sMinOccurs = xmlElement.getAttribute(ATTRIB_MIN_OCCURS);
718
719             if (sMinOccurs != null && sMinOccurs.length() > 0) {
720                 m_nMinOccurs = Integer.parseInt(sMinOccurs);
721             }
722
723             String JavaDoc sMaxOccurs = xmlElement.getAttribute(ATTRIB_MAX_OCCURS);
724
725             if (sMaxOccurs != null && sMaxOccurs.length() > 0) {
726                 m_nMaxOccurs = Integer.parseInt(sMaxOccurs);
727             }
728
729             NodeList nodes = xmlElement.getChildNodes();
730
731             for (int i = 0; i < nodes.getLength(); i++) {
732                 if (nodes.item(i).getNodeType() != Node.ELEMENT_NODE) {
733                     continue;
734                 }
735
736                 Element el = (Element) nodes.item(i);
737                 populate(el, state);
738             }
739         } else if (sTagName.equals(TAG_DOMAIN_OBJECT)) {
740             NodeList nodes = xmlElement.getChildNodes();
741             
742             for(int i=0;i<nodes.getLength();i++) {
743                 Node node = nodes.item(i);
744                 
745                 if(node.getNodeType() == Node.TEXT_NODE) {
746                     txt = (Text) node;
747                     String JavaDoc sVal = txt.getNodeValue().trim();
748                     
749                     if(sVal.length()>0) {
750                         setDomainClass(sVal);
751                         break;
752                     }
753                     
754                 } else if(node.getNodeType() == Node.ELEMENT_NODE) {
755                     try {
756                         String JavaDoc sClassname = HarmoniseObjectFactory.getClassName(m_dsi, (Element)node);
757                         setDomainClass(sClassname);
758                         break;
759                     } catch (HarmoniseFactoryException e) {
760                         throw new PopulateException(e);
761                     }
762                 }
763             }
764             
765             
766             
767         } else if (sTagName.equals(TAG_DOMAIN_DETAILS)) {
768             
769             List nodes = XMLUtils.getChildrenByName(xmlElement, TAG_PATHRESTRICTION);
770             
771             for (Iterator iter = nodes.iterator(); iter.hasNext();) {
772                 Element pathEl = (Element) iter.next();
773                 txt = (Text) pathEl.getFirstChild();
774                 addDetails(txt.getNodeValue());
775             }
776             
777         }
778
779     }
780
781     /**
782      * Returns a <code>List</code> of <code>Property</code> definitions which can be applied
783      * to objects of the specified <code>Class</code>.
784      *
785      * @param dsi the data store interface
786      * @param clss the <code>Class</code>
787      * @return a <code>List</code> of <code>Property</code> objects
788      * @throws DataAccessException if an error occurs querying the data store
789      */

790     static public List getAvailableProperties(
791         AbstractDataStoreInterface dsi,
792         Class JavaDoc clss)
793         throws DataAccessException {
794         return getAvailableProperties(dsi, clss, (String JavaDoc) null);
795     }
796
797     /**
798      * Returns a <code>List</code> of <code>Property</code> definitions which can be applied
799      * to the object of the specified <code>Class</code> and path.
800      *
801      * @param dsi the data store interface
802      * @param clss the <code>Class</code>
803      * @param sPath the path of an object of the specified <code>Class</code>
804      * @return a <code>List</code> of <code>Property</code> objects
805      * @throws DataAccessException if an error occurs querying the data store
806      */

807     static public List getAvailableProperties(
808         AbstractDataStoreInterface dsi,
809         Class JavaDoc clss,
810         String JavaDoc sPath)
811         throws DataAccessException {
812         Vector result = new Vector();
813         ResultSet rs = null;
814         SelectStatement select = new SelectStatement();
815
816         try {
817             select.addSelectColumn(
818                 AbstractObject.getColumnRef(
819                     Property.class.getName(),
820                     AbstractObject.ATTRIB_ID));
821             select.addSelectColumn(
822                             AbstractObject.getColumnRef(
823                                 Property.class.getName(),
824                                 AbstractObject.ATTRIB_TYPE));
825             
826             ColumnRef colDomDetails = new ColumnRef(
827             TBL_DOMAIN_DETAILS,
828             CLMN_DOMAIN_DETAILS,
829             ColumnRef.TEXT);
830             
831             select.addSelectColumn(colDomDetails);
832
833             select.addJoinCondition(
834                 AbstractObject.getColumnRef(
835                     Property.class.getName(),
836                     AbstractObject.ATTRIB_KEY),
837                 new ColumnRef(TBL_NAME, CLMN_PROPERTY_KEY, ColumnRef.NUMBER));
838
839             select.addJoinCondition(
840                 getDetailsColumnRef(CLMN_DETAILS_DOMAIN_ID, false),
841                 getColumnRef(CLMN_DOMAIN_ID, false));
842
843             select.addWhereCondition(
844                 new ColumnRef(TBL_NAME, CLMN_DOMAIN_OBJECT, ColumnRef.TEXT),
845                 "=",
846                 clss.getName());
847                 
848             if(sPath != null) {
849                 Substring substr = new Substring(sPath,new Integer JavaDoc(1),new Length(colDomDetails));
850                 
851                 select.addWhereCondition(colDomDetails, "=", substr);
852             }
853             
854
855         } catch (DataStoreException e) {
856             throw new DataAccessException("Error occured building query", e);
857         }
858
859         try {
860             
861             rs = dsi.execute(select);
862
863             while (rs.next()) {
864                 int nPropId = rs.getInt(1);
865                 String JavaDoc sPropType = rs.getString(2);
866                 
867                 Property prop =
868                     (
869                         org.openharmonise.rm.resources.metadata.properties.Property) HarmoniseObjectFactory
870                             .instantiateHarmoniseObject(
871                         dsi,
872                         sPropType,
873                         nPropId);
874
875                 result.add(prop);
876
877             }
878         } catch (HarmoniseFactoryException e) {
879             throw new DataAccessException(
880                 "Error occured getting property from factory",e);
881         } catch (DataStoreException e) {
882             throw new DataAccessException(
883                 "Error occured executing query",e);
884         } catch (SQLException e) {
885             throw new DataAccessException(
886                 "Error occured executing query",e);
887         } finally {
888             if(rs!=null) {
889                 try {
890                     rs.close();
891                 } catch (SQLException e) {
892                     m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
893                 }
894             }
895         }
896
897         return result;
898     }
899
900     /**
901      * Returns a <code>List</code> of <code>Property</code> definitions which can be applied
902      * to the objects of the specified <code>Class</code> and paths.
903      *
904      * @param dsi the data store interface
905      * @param clss the <code>Class</code>
906      * @param paths the list of paths
907      * @return a <code>List</code> of <code>Property</code> objects
908      * @throws DataAccessException if an error occurs querying the data store
909      */

910     static public List getAvailableProperties(
911         AbstractDataStoreInterface dsi,
912         Class JavaDoc clss,
913         List paths)
914         throws DataAccessException {
915         Vector result = new Vector();
916         ResultSet rs = null;
917         SelectStatement select = new SelectStatement();
918
919         try {
920             select.addSelectColumn(
921                 AbstractObject.getColumnRef(
922                     Property.class.getName(),
923                     AbstractObject.ATTRIB_ID));
924             select.addSelectColumn(
925                             AbstractObject.getColumnRef(
926                                 Property.class.getName(),
927                                 AbstractObject.ATTRIB_TYPE));
928             
929             ColumnRef colDomDetails = new ColumnRef(
930                         TBL_DOMAIN_DETAILS,
931                         CLMN_DOMAIN_DETAILS,
932                         ColumnRef.TEXT);
933             
934             select.addSelectColumn(colDomDetails);
935
936             select.addJoinCondition(
937                 AbstractObject.getColumnRef(
938                     Property.class.getName(),
939                     AbstractObject.ATTRIB_KEY),
940                 new ColumnRef(TBL_NAME, CLMN_PROPERTY_KEY, ColumnRef.NUMBER));
941                 
942             select.addJoinCondition(
943                             getDetailsColumnRef(CLMN_DETAILS_DOMAIN_ID, false),
944                             getColumnRef(CLMN_DOMAIN_ID, false));
945
946             select.addWhereCondition(
947                 new ColumnRef(TBL_NAME, CLMN_DOMAIN_OBJECT, ColumnRef.TEXT),
948                 "=",
949                 clss.getName());
950                 
951             if(paths != null) {
952                 
953                 if(paths.size() == 1) {
954                     Substring substr = new Substring((String JavaDoc)paths.get(0),new Integer JavaDoc(1),new Length(colDomDetails));
955                         
956                     select.addWhereCondition(colDomDetails, "=", substr);
957                         
958                 } else if(paths.size() > 1) {
959                     WhereConditionGroup wheres = new WhereConditionGroup();
960                     wheres.setStringingOperator("or");
961                     
962                     Iterator iter = paths.iterator();
963
964                     while (iter.hasNext()) {
965                         String JavaDoc sPath = (String JavaDoc) iter.next();
966     
967                         Substring substr = new Substring(sPath,new Integer JavaDoc(1),new Length(colDomDetails));
968                         wheres.addCondition(colDomDetails, "=", substr);
969
970                     }
971                     select.addWhereCondition(wheres);
972                 }
973                 
974             }
975
976         } catch (DataStoreException e) {
977             throw new DataAccessException(
978                 "Error occured building query",e);
979         }
980
981         try {
982
983             rs = dsi.execute(select);
984
985             while (rs.next()) {
986                 int nPropId = rs.getInt(1);
987                 String JavaDoc sPropType = rs.getString(2);
988                 Property prop =
989                     (
990                         org.openharmonise.rm.resources.metadata.properties.Property) HarmoniseObjectFactory
991                             .instantiateHarmoniseObject(
992                         dsi,
993                         sPropType,
994                         nPropId);
995
996                 result.add(prop);
997             }
998         } catch (HarmoniseFactoryException e) {
999             throw new DataAccessException(
1000                "Error occured getting property from factory",e);
1001        } catch (DataStoreException e) {
1002            throw new DataAccessException(
1003                "Error occured executing query",e);
1004        } catch (SQLException e) {
1005            throw new DataAccessException(
1006                "Error occured executing query",e);
1007        } finally {
1008        if(rs!=null) {
1009            try {
1010                rs.close();
1011            } catch (SQLException e) {
1012                m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
1013            }
1014        }
1015    }
1016
1017        return result;
1018    }
1019    
1020    /**
1021     * Returns a <code>List</code> of <code>Property</code> objects which are allowed
1022     * to be applied to the specified <code>AbstractProfiledObject</code>.
1023     *
1024     * @param dsi the data store interface
1025     * @param profObj the profiled object
1026     * @return a applicable <code>List</code> of <code>Property</code> objects
1027     * @throws DataAccessException if an error occurs querying the data store
1028     */

1029    static public List getAvailableProperties(
1030        AbstractDataStoreInterface dsi,
1031        AbstractProfiledObject profObj)
1032        throws DataAccessException {
1033        List result = null;
1034        if(profObj instanceof AbstractChildObject) {
1035            result = getAvailableProperties(
1036                        dsi,
1037            profObj.getClass(),
1038            ((AbstractChildObject)profObj).getAllFullPaths());
1039        } else {
1040            result = getAvailableProperties(
1041                        dsi,
1042                        profObj.getClass());
1043        }
1044        
1045        return result;
1046    }
1047
1048    /* (non-Javadoc)
1049     * @see org.openharmonise.rm.publishing.Publishable#getTagName()
1050     */

1051    public String JavaDoc getTagName() {
1052        return TAG_DOMAIN;
1053    }
1054
1055    /* (non-Javadoc)
1056     * @see java.lang.Object#equals(java.lang.Object)
1057     */

1058    public boolean equals(Object JavaDoc obj) {
1059        boolean bResult = false;
1060
1061        if (obj instanceof Domain) {
1062            Domain domain = (Domain) obj;
1063
1064            if (this == domain) {
1065                bResult = true;
1066            } else {
1067                try {
1068                    if (this.m_bIsContentRestrictPopulated == false) {
1069                        this.populateContentRestrictions();
1070                    }
1071                    if (this.m_bIsDetailsPopulated == false) {
1072                        this.populateDetails();
1073                    }
1074
1075                    if (m_nDepth == domain.getDepth()) {
1076                        if (m_nMaxOccurs == domain.getMaxOccurs()) {
1077                            if (m_nMinOccurs == domain.getMinOccurs()) {
1078                                if (m_sObjectName.equals(domain.getDomainClass())) {
1079                                    if (m_contentRestrictions
1080                                        .equals(
1081                                            domain.getContentRestrictions())) {
1082                                        if (m_details
1083                                            .equals(domain.getDetails())) {
1084                                            bResult = true;
1085                                        }
1086                                    }
1087                                }
1088                            }
1089                        }
1090                    }
1091
1092                } catch (DataStoreException e) {
1093                    m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
1094                } catch (DataAccessException e) {
1095                    m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
1096                }
1097
1098            }
1099
1100        }
1101
1102        return bResult;
1103    }
1104
1105    /**
1106     * Saves the domain data for this property to the database.
1107     *
1108     * @param prop the <code>Property</code> assocaited to this domain
1109     * @throws EditException if an error occurs saving this domain to the
1110     * database
1111     */

1112    public void save(Property prop) throws EditException {
1113        try {
1114            InsertStatement insert = new InsertStatement();
1115
1116            if (m_nId < 0) {
1117                m_nId = m_dsi.getSequenceNextValue(SEQ_DOMAIN);
1118            }
1119
1120            insert.addColumnValue(
1121                getColumnRef(CLMN_DOMAIN_ID, m_bIsHistorical),
1122                m_nId);
1123
1124            insert.addColumnValue(
1125                getColumnRef(Domain.CLMN_PROPERTY_KEY, m_bIsHistorical),
1126                prop.getKey());
1127            insert.addColumnValue(
1128                getColumnRef(Domain.CLMN_DOMAIN_OBJECT, m_bIsHistorical),
1129                this.m_sObjectName);
1130
1131            insert.addColumnValue(
1132                getColumnRef(Domain.CLMN_MIN_OCCURS, m_bIsHistorical),
1133                this.m_nMinOccurs);
1134
1135            insert.addColumnValue(
1136                getColumnRef(Domain.CLMN_MAX_OCCURS, m_bIsHistorical),
1137                this.m_nMaxOccurs);
1138
1139            m_dsi.execute(insert);
1140
1141            if (m_details != null) {
1142
1143                Iterator iter = m_details.iterator();
1144
1145                while (iter.hasNext()) {
1146                    String JavaDoc sDetail = (String JavaDoc) iter.next();
1147
1148                    insert.clear();
1149
1150                    insert.addColumnValue(
1151                        getDetailsColumnRef(
1152                            Domain.CLMN_DETAILS_DOMAIN_ID,
1153                            m_bIsHistorical),
1154                        m_nId);
1155
1156                    insert.addColumnValue(
1157                        getDetailsColumnRef(
1158                            Domain.CLMN_DOMAIN_DETAILS,
1159                            m_bIsHistorical),
1160                        sDetail);
1161
1162                    m_dsi.execute(insert);
1163                }
1164            }
1165
1166            if (m_contentRestrictions != null) {
1167
1168                Iterator iter = m_contentRestrictions.iterator();
1169
1170                while (iter.hasNext()) {
1171                    String JavaDoc contentType = (String JavaDoc) iter.next();
1172
1173                    insert.clear();
1174
1175                    insert.addColumnValue(
1176                        getContentRestrictionsColumnRef(
1177                            Domain.CLMN_DETAILS_DOMAIN_ID,
1178                            m_bIsHistorical),
1179                        m_nId);
1180
1181                    insert.addColumnValue(
1182          getContentRestrictionsColumnRef(
1183                            Domain.CLMN_CONTENT_TYPE,
1184                            m_bIsHistorical),
1185                        contentType);
1186
1187                    m_dsi.execute(insert);
1188                }
1189            }
1190        } catch (DataAccessException e) {
1191            throw new EditException("Error saving domain", e);
1192        } catch (DataStoreException e) {
1193            throw new EditException("Error saving domain", e);
1194        } catch (SQLException e) {
1195            throw new EditException("Error saving domain", e);
1196        }
1197    }
1198
1199    /**
1200     * Deletes this domain from the database.
1201     *
1202     * @throws EditException if an error occurs deleting this domain from
1203     * the database
1204     */

1205    public void delete() throws EditException {
1206        try {
1207            DeleteStatement delete = new DeleteStatement();
1208
1209            delete.addWhereCondition(
1210                getDetailsColumnRef(
1211                    Domain.CLMN_DETAILS_DOMAIN_ID,
1212                    m_bIsHistorical),
1213                "=",
1214                m_nId);
1215
1216
1217            m_dsi.execute(delete);
1218            
1219            delete.clear();
1220
1221            delete.addWhereCondition(
1222                this.getContentRestrictionsColumnRef(
1223                    CLMN_DETAILS_DOMAIN_ID,
1224                    this.m_bIsHistorical),
1225                "=",
1226                m_nId);
1227
1228            m_dsi.execute(delete);
1229
1230            delete.clear();
1231
1232            delete.addWhereCondition(
1233                getColumnRef(CLMN_DOMAIN_ID, this.m_bIsHistorical),
1234                "=",
1235                m_nId);
1236
1237            m_dsi.execute(delete);
1238
1239        } catch (DataStoreException e) {
1240            throw new EditException("Error deleting domain", e);
1241        }
1242    }
1243
1244    /**
1245     * Populates the restrictions for this domain from the database.
1246     *
1247     * @throws DataStoreException if an error occurs querying the database
1248     */

1249    private synchronized void populateDetails() throws DataStoreException {
1250        if (m_bIsDetailsPopulated == false) {
1251
1252            if (m_details == null) {
1253                m_details = new Vector();
1254            }
1255
1256            SelectStatement select = new SelectStatement();
1257
1258            select.addSelectColumn(
1259                getDetailsColumnRef(CLMN_DOMAIN_DETAILS, m_bIsHistorical));
1260
1261            select.addWhereCondition(
1262                getDetailsColumnRef(CLMN_DETAILS_DOMAIN_ID, m_bIsHistorical),
1263                "=",
1264                this.m_nId);
1265
1266            ResultSet rs = m_dsi.execute(select);
1267
1268            try {
1269                while (rs.next()) {
1270                    String JavaDoc sDetail = rs.getString(1);
1271
1272                    m_details.add(sDetail);
1273                }
1274            } catch (SQLException e) {
1275                throw new DataStoreException(
1276                    "Error populating domain details",
1277                    e);
1278            } finally {
1279                if(rs!=null) {
1280                    try {
1281                        rs.close();
1282                    } catch (SQLException e1) {
1283                        throw new DataStoreException(
1284                        "Error occured closing result set:"
1285                            + e1.getLocalizedMessage());
1286                    }
1287                }
1288            }
1289
1290            m_bIsDetailsPopulated = true;
1291        }
1292    }
1293
1294    /**
1295     * Returns the appropriate <code>ColumnRef</code> for the given column name
1296     * from the <code>Domain</code> domain details database table.
1297     *
1298     * @param sCol the column name
1299     * @param bIsHist <code>true</code> if the column references the historical
1300     * database table
1301     * @return the appropriate <code>ColumnRef</code>
1302     * @throws DataStoreException if the referenced column is invalid
1303     */

1304    protected static ColumnRef getDetailsColumnRef(String JavaDoc sCol, boolean bIsHist)
1305        throws DataStoreException {
1306        String JavaDoc sTable = Domain.CLMN_DOMAIN_DETAILS;
1307        ColumnRef colref = null;
1308
1309        if (bIsHist == true) {
1310            sTable = sTable + AbstractObject.EXT_HIST;
1311        }
1312
1313        if (sCol.equals(Domain.CLMN_DOMAIN_DETAILS) == true) {
1314            colref = new ColumnRef(sTable, sCol, ColumnRef.TEXT);
1315        } else if (sCol.equals(Domain.CLMN_DETAILS_DOMAIN_ID) == true) {
1316            colref = new ColumnRef(sTable, sCol, ColumnRef.NUMBER);
1317        } else {
1318            throw new InvalidColumnReferenceException();
1319        }
1320
1321        return colref;
1322    }
1323
1324    /**
1325     * Returns the appropriate <code>ColumnRef</code> for the given column name
1326     * from the <code>Domain</code> content restrictions database table.
1327     *
1328     * @param sCol the column name
1329     * @param bIsHist <code>true</code> if the column references the historical
1330     * @return the appropriate <code>ColumnRef</code>
1331     * @throws DataStoreException if the referenced column is invalid
1332     */

1333    private ColumnRef getContentRestrictionsColumnRef(
1334        String JavaDoc sCol,
1335        boolean bIsHist)
1336        throws DataStoreException {
1337        String JavaDoc sTable = Domain.TBL_DOMAIN_CONTENT_RESTRICTIONS;
1338        ColumnRef colref = null;
1339
1340        if (bIsHist == true) {
1341            sTable = sTable + AbstractObject.EXT_HIST;
1342        }
1343
1344        if (sCol.equals(Domain.CLMN_CONTENT_TYPE) == true) {
1345            colref = new ColumnRef(sTable, sCol, ColumnRef.TEXT);
1346        } else if (sCol.equals(Domain.CLMN_DETAILS_DOMAIN_ID) == true) {
1347            colref = new ColumnRef(sTable, sCol, ColumnRef.NUMBER);
1348        } else {
1349            throw new InvalidColumnReferenceException();
1350        }
1351
1352        return colref;
1353    }
1354
1355    /**
1356     * Returns the appropriate <code>ColumnRef</code> for the given column name
1357     * from the <code>Domain</code> database table.
1358     *
1359     * @param sCol the column name
1360     * @param bIsHist <code>true</code> if the column references the historical
1361     * @return the appropriate <code>ColumnRef</code>
1362     * @throws DataStoreException if the referenced column is invalid
1363     */

1364    protected static ColumnRef getColumnRef(String JavaDoc sCol, boolean bIsHist)
1365        throws DataStoreException {
1366        String JavaDoc sTable = Domain.TBL_NAME;
1367        ColumnRef colref = null;
1368
1369        if (bIsHist == true) {
1370            sTable = sTable + AbstractObject.EXT_HIST;
1371        }
1372
1373        if (sCol.equals(Domain.CLMN_DOMAIN_DETAILS) == true
1374            || sCol.equals(Domain.CLMN_DOMAIN_OBJECT) == true) {
1375            colref = new ColumnRef(sTable, sCol, ColumnRef.TEXT);
1376        } else if (sCol.equals(Domain.CLMN_PROPERTY_KEY) == true) {
1377            colref = new ColumnRef(sTable, sCol, ColumnRef.NUMBER);
1378        } else if (sCol.equals(Domain.CLMN_MIN_OCCURS) == true) {
1379            colref = new ColumnRef(sTable, sCol, ColumnRef.NUMBER);
1380        } else if (sCol.equals(Domain.CLMN_MAX_OCCURS) == true) {
1381            colref = new ColumnRef(sTable, sCol, ColumnRef.NUMBER);
1382        } else if (sCol.equals(Domain.CLMN_DEPTH) == true) {
1383            colref = new ColumnRef(sTable, sCol, ColumnRef.NUMBER);
1384        } else if (sCol.equals(Domain.CLMN_DOMAIN_ID) == true) {
1385            colref = new ColumnRef(sTable, sCol, ColumnRef.NUMBER);
1386        } else {
1387            throw new InvalidColumnReferenceException();
1388        }
1389
1390        return colref;
1391    }
1392
1393    /**
1394     * Returns <code>true</code> if the specified object is a member of this
1395     * domain and therefore can have an instance of the <code>Property</code>
1396     * associated to this domain applied to it, otherwise <code>false</code>.
1397     *
1398     * @param obj the profiled object
1399     * @return <code>true</code> if the specified object is a member of this domain
1400     */

1401    public boolean isValid(AbstractProfiledObject obj)
1402        throws DataAccessException {
1403        boolean bIsValid = true;
1404
1405        if (obj.getClass().getName().equals(getDomainClass()) == true) {
1406            if (obj instanceof AbstractChildObject) {
1407                AbstractChildObject child = (AbstractChildObject) obj;
1408                List details = getDetails();
1409                String JavaDoc sChildPath = child.getPath();
1410                
1411                //if there are domain details and the child has
1412
//a parent then check object path
1413
if (details.size() > 0
1414                        && sChildPath != null
1415                        && sChildPath.length() > 0) {
1416                    
1417                    bIsValid = false;
1418
1419                    Iterator iter = details.iterator();
1420
1421                    while (iter.hasNext()) {
1422                        String JavaDoc sPath = (String JavaDoc) iter.next();
1423                        
1424                        if (sChildPath.startsWith(sPath)) {
1425                            bIsValid = true;
1426                        }
1427                    }
1428                }
1429
1430            }
1431        } else {
1432            bIsValid = false;
1433        }
1434
1435        return bIsValid;
1436    }
1437    
1438    /* (non-Javadoc)
1439     * @see java.lang.Object#clone()
1440     */

1441    public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
1442    try {
1443      populateContentRestrictions();
1444      populateDetails();
1445      
1446      Domain domain = (Domain)super.clone();
1447      domain.m_details = (List)((Vector)m_details).clone();
1448      domain.m_contentRestrictions = (List)((Vector) m_contentRestrictions).clone();
1449      
1450      return domain;
1451    } catch (CloneNotSupportedException JavaDoc e) {
1452      throw new IllegalStateException JavaDoc(
1453        "Clone failed:" + e.getLocalizedMessage());
1454    } catch (DataStoreException e) {
1455            throw new IllegalStateException JavaDoc("Clone failed:" + e.getLocalizedMessage());
1456        }
1457    }
1458
1459}
1460
Popular Tags