]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
9c9556071508499db171d6ab4df09d02bd975e83
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  */
24 package org.collectionspace.services.vocabulary.nuxeo;
25
26 import java.util.Iterator;
27 import java.util.List;
28
29 import org.collectionspace.services.VocabularyItemJAXBSchema;
30 import org.collectionspace.services.common.repository.DocumentWrapper;
31 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandler;
32 import org.collectionspace.services.vocabulary.VocabularyitemsCommon;
33 import org.collectionspace.services.vocabulary.VocabularyitemsCommonList;
34 import org.collectionspace.services.vocabulary.VocabularyitemsCommonList.VocabularyitemListItem;
35 import org.nuxeo.ecm.core.api.DocumentModel;
36 import org.nuxeo.ecm.core.api.DocumentModelList;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * VocabularyItemDocumentModelHandler
42  *
43  * $LastChangedRevision: $
44  * $LastChangedDate: $
45  */
46 public class VocabularyItemDocumentModelHandler
47         extends RemoteDocumentModelHandler<VocabularyitemsCommon, VocabularyitemsCommonList> {
48
49     private final Logger logger = LoggerFactory.getLogger(VocabularyItemDocumentModelHandler.class);
50     /**
51      * vocabularyItem is used to stash JAXB object to use when handle is called
52      * for Action.CREATE, Action.UPDATE or Action.GET
53      */
54     private VocabularyitemsCommon vocabularyItem;
55     /**
56      * vocabularyItemList is stashed when handle is called
57      * for ACTION.GET_ALL
58      */
59     private VocabularyitemsCommonList vocabularyItemList;
60     
61     /**
62      * inVocabulary is the parent vocabulary for this context
63      */
64     private String inVocabulary;
65
66     public String getInVocabulary() {
67                 return inVocabulary;
68         }
69
70         public void setInVocabulary(String inVocabulary) {
71                 this.inVocabulary = inVocabulary;
72         }
73
74         @Override
75     public void prepare(Action action) throws Exception {
76         //no specific action needed
77     }
78
79     /**
80      * getCommonPart get associated vocabularyItem
81      * @return
82      */
83     @Override
84     public VocabularyitemsCommon getCommonPart() {
85         return vocabularyItem;
86     }
87
88     /**
89      * setCommonPart set associated vocabularyItem
90      * @param vocabularyItem
91      */
92     @Override
93     public void setCommonPart(VocabularyitemsCommon vocabularyItem) {
94         this.vocabularyItem = vocabularyItem;
95     }
96
97     /**
98      * getCommonPartList get associated vocabularyItem (for index/GET_ALL)
99      * @return
100      */
101     @Override
102     public VocabularyitemsCommonList getCommonPartList() {
103         return vocabularyItemList;
104     }
105
106     @Override
107     public void setCommonPartList(VocabularyitemsCommonList vocabularyItemList) {
108         this.vocabularyItemList = vocabularyItemList;
109     }
110
111     @Override
112     public VocabularyitemsCommon extractCommonPart(DocumentWrapper wrapDoc)
113             throws Exception {
114         throw new UnsupportedOperationException();
115     }
116
117     @Override
118     public void fillCommonPart(VocabularyitemsCommon vocabularyItemObject, DocumentWrapper wrapDoc) throws Exception {
119         throw new UnsupportedOperationException();
120     }
121
122     @Override
123     public VocabularyitemsCommonList extractCommonPartList(DocumentWrapper wrapDoc) 
124         throws Exception {
125         VocabularyitemsCommonList coList = new VocabularyitemsCommonList();
126         try{
127                 DocumentModelList docList = (DocumentModelList) wrapDoc.getWrappedObject();
128         
129                 List<VocabularyitemsCommonList.VocabularyitemListItem> list = 
130                         coList.getVocabularyitemListItem();
131         
132                 //FIXME: iterating over a long list of documents is not a long term
133                 //strategy...need to change to more efficient iterating in future
134                 Iterator<DocumentModel> iter = docList.iterator();
135                 while(iter.hasNext()){
136                     DocumentModel docModel = iter.next();
137                     String parentVocab = (String)docModel.getProperty(getServiceContext().getCommonPartLabel("vocabularyItems"),
138                             VocabularyItemJAXBSchema.IN_VOCABULARY); 
139                     if( !inVocabulary.equals(parentVocab))
140                         continue;
141                     VocabularyitemListItem ilistItem = new VocabularyitemListItem();
142                     ilistItem.setDisplayName((String) docModel.getProperty(getServiceContext().getCommonPartLabel("vocabularyItems"),
143                             VocabularyItemJAXBSchema.DISPLAY_NAME));
144                     String id = docModel.getId();
145                     ilistItem.setUri("/vocabularies/"+inVocabulary+"/items/" + id);
146                     ilistItem.setCsid(id);
147                     list.add(ilistItem);
148                 }
149         }catch(Exception e){
150             if(logger.isDebugEnabled()){
151                 logger.debug("Caught exception in extractCommonPartList", e);
152             }
153             throw e;
154         }
155         return coList;
156     }
157
158
159     /* (non-Javadoc)
160      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#getDocumentType()
161      */
162     @Override
163     public String getDocumentType() {
164         return VocabularyItemConstants.NUXEO_DOCTYPE;
165     }
166
167     /**
168      * getQProperty converts the given property to qualified schema property
169      * @param prop
170      * @return
171      */
172     @Override
173     public String getQProperty(String prop) {
174         return VocabularyItemConstants.NUXEO_SCHEMA_NAME + ":" + prop;
175     }
176 }
177