]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
15b7c873cd73038afb61e33e486cf1fc02c3bf46
[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.intake.nuxeo;
25
26 import java.util.Iterator;
27 import java.util.List;
28
29 import javax.ws.rs.PathParam;
30 import javax.ws.rs.WebApplicationException;
31 import javax.ws.rs.core.Context;
32 import javax.ws.rs.core.Response;
33 import javax.ws.rs.core.UriInfo;
34
35 import org.collectionspace.services.IntakeJAXBSchema;
36 import org.collectionspace.services.common.authorityref.AuthorityRefList;
37 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
38 import org.collectionspace.services.common.context.MultipartServiceContextImpl;
39 import org.collectionspace.services.common.context.ServiceContext;
40 import org.collectionspace.services.common.document.DocumentHandler.Action;
41 import org.collectionspace.services.common.document.DocumentWrapper;
42 import org.collectionspace.services.common.security.UnauthorizedException;
43 import org.collectionspace.services.common.vocabulary.RefNameUtils;
44 import org.collectionspace.services.intake.IntakesCommon;
45 import org.collectionspace.services.intake.IntakesCommonList;
46 import org.collectionspace.services.intake.IntakesCommonList.IntakeListItem;
47
48 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
49 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
50 import org.nuxeo.ecm.core.api.DocumentModel;
51 import org.nuxeo.ecm.core.api.DocumentModelList;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 /**
56  * IntakeDocumentModelHandler
57  *
58  * $LastChangedRevision: $
59  * $LastChangedDate: $
60  */
61 public class IntakeDocumentModelHandler
62         extends RemoteDocumentModelHandlerImpl<IntakesCommon, IntakesCommonList> {
63
64     private final Logger logger = LoggerFactory.getLogger(IntakeDocumentModelHandler.class);
65     /**
66      * intake is used to stash JAXB object to use when handle is called
67      * for Action.CREATE, Action.UPDATE or Action.GET
68      */
69     private IntakesCommon intake;
70     /**
71      * intakeList is stashed when handle is called
72      * for ACTION.GET_ALL
73      */
74     private IntakesCommonList intakeList;
75
76
77     /**
78      * getCommonPart get associated intake
79      * @return
80      */
81     @Override
82     public IntakesCommon getCommonPart() {
83         return intake;
84     }
85
86     /**
87      * setCommonPart set associated intake
88      * @param intake
89      */
90     @Override
91     public void setCommonPart(IntakesCommon intake) {
92         this.intake = intake;
93     }
94
95     /**
96      * getCommonPartList get associated intake (for index/GET_ALL)
97      * @return
98      */
99     @Override
100     public IntakesCommonList getCommonPartList() {
101         return intakeList;
102     }
103
104     @Override
105     public void setCommonPartList(IntakesCommonList intakeList) {
106         this.intakeList = intakeList;
107     }
108
109     @Override
110     public IntakesCommon extractCommonPart(DocumentWrapper<DocumentModel> wrapDoc)
111             throws Exception {
112         throw new UnsupportedOperationException();
113     }
114
115     @Override
116     public void fillCommonPart(IntakesCommon intakeObject, DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
117         throw new UnsupportedOperationException();
118     }
119
120     @Override
121     public IntakesCommonList extractCommonPartList(DocumentWrapper<DocumentModelList> wrapDoc) throws Exception {
122         DocumentModelList docList = wrapDoc.getWrappedObject();
123
124         IntakesCommonList coList = new IntakesCommonList();
125         List<IntakesCommonList.IntakeListItem> list = coList.getIntakeListItem();
126
127         //FIXME: iterating over a long list of documents is not a long term
128         //strategy...need to change to more efficient iterating in future
129         Iterator<DocumentModel> iter = docList.iterator();
130         while(iter.hasNext()){
131             DocumentModel docModel = iter.next();
132             IntakeListItem ilistItem = new IntakeListItem();
133             ilistItem.setEntryNumber((String) docModel.getProperty(getServiceContext().getCommonPartLabel(),
134                     IntakeJAXBSchema.ENTRY_NUMBER));
135             ilistItem.setDepositor((String) docModel.getProperty(getServiceContext().getCommonPartLabel(),
136                     IntakeJAXBSchema.DEPOSITOR));
137             String id = NuxeoUtils.extractId(docModel.getPathAsString());
138             ilistItem.setUri(getServiceContextPath() + id);
139             ilistItem.setCsid(id);
140             list.add(ilistItem);
141         }
142
143         return coList;
144     }
145
146     /**
147      * getQProperty converts the given property to qualified schema property
148      * @param prop
149      * @return
150      */
151     @Override
152     public String getQProperty(String prop) {
153         return IntakeConstants.NUXEO_SCHEMA_NAME + ":" + prop;
154     }
155     
156     public AuthorityRefList getAuthorityRefs(
157                 DocumentWrapper<DocumentModel> docWrapper,
158                 String pathPrefix,
159                 List<String> authRefFields) {
160         AuthorityRefList authRefList = new AuthorityRefList();
161         try {
162             DocumentModel docModel = docWrapper.getWrappedObject();
163             List<AuthorityRefList.AuthorityRefItem> list = 
164                 authRefList.getAuthorityRefItem();
165
166             for(String field:authRefFields){
167                         String refName = (String)docModel.getPropertyValue(pathPrefix+field);
168                         if(refName==null)
169                                 continue;
170                 try{
171                         RefNameUtils.AuthorityTermInfo termInfo =
172                                 RefNameUtils.parseAuthorityTermInfo(refName);
173                         AuthorityRefList.AuthorityRefItem ilistItem = 
174                                 new AuthorityRefList.AuthorityRefItem();
175                         ilistItem.setRefName(refName);
176                         ilistItem.setAuthDisplayName(termInfo.inAuthority.displayName);
177                         ilistItem.setItemDisplayName(termInfo.displayName);
178                         ilistItem.setSourceField(field);
179                         ilistItem.setUri(termInfo.getRelativeUri());
180                     list.add(ilistItem);
181                 } catch( Exception e ) {
182                     if (logger.isDebugEnabled()) {
183                         logger.debug("Caught exception in getAuthorityRefs", e);
184                     }
185                 }
186             }
187         } catch (Exception e) {
188             if (logger.isDebugEnabled()) {
189                 logger.debug("Caught exception in getIntakeList", e);
190             }
191             Response response = Response.status(
192                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
193             throw new WebApplicationException(response);
194         }
195         return authRefList;
196     }
197
198 }
199