]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
3000a18737358196dbf049c5e5dff25f489234ea
[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.servicegroup.nuxeo;
25
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Map;
31
32 import javax.ws.rs.WebApplicationException;
33 import javax.ws.rs.core.Response;
34
35 import org.collectionspace.services.ServiceGroupListItemJAXBSchema;
36 import org.collectionspace.services.nuxeo.client.java.CommonList;
37 import org.collectionspace.services.nuxeo.client.java.DocHandlerBase;
38 import org.collectionspace.services.nuxeo.client.java.RepositoryJavaClientImpl;
39 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
40 import org.collectionspace.services.jaxb.AbstractCommonList;
41 import org.collectionspace.services.client.IQueryManager;
42 import org.collectionspace.services.client.PoxPayloadIn;
43 import org.collectionspace.services.client.PoxPayloadOut;
44 import org.collectionspace.services.common.ServiceMain;
45 import org.collectionspace.services.common.ServiceMessages;
46 import org.collectionspace.services.common.api.Tools;
47 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
48 import org.collectionspace.services.common.context.ServiceBindingUtils;
49 import org.collectionspace.services.common.context.ServiceContext;
50 import org.collectionspace.services.common.document.DocumentException;
51 import org.collectionspace.services.common.document.DocumentFilter;
52 import org.collectionspace.services.common.document.DocumentNotFoundException;
53 import org.collectionspace.services.common.document.DocumentWrapper;
54 import org.collectionspace.services.common.query.QueryManager;
55 import org.collectionspace.services.common.repository.RepositoryClient;
56 import org.collectionspace.services.common.security.SecurityUtils;
57 import org.collectionspace.services.config.service.ServiceBindingType;
58 import org.collectionspace.services.config.service.ServiceObjectType;
59 import org.collectionspace.services.servicegroup.ServicegroupsCommon;
60 import org.nuxeo.ecm.core.api.DocumentModel;
61 import org.nuxeo.ecm.core.api.DocumentModelList;
62 import org.nuxeo.ecm.core.api.model.PropertyException;
63 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
66
67 public class ServiceGroupDocumentModelHandler 
68         extends DocHandlerBase<ServicegroupsCommon> {
69         
70     protected final Logger logger = LoggerFactory.getLogger(this.getClass());
71     
72     protected static final int NUM_META_FIELDS = 3;
73     protected static final String DOC_TYPE_FIELD = "docType";
74     protected static final String DOC_NUMBER_FIELD = "docNumber";
75     protected static final String DOC_NAME_FIELD = "docName";
76
77     public AbstractCommonList getItemsForGroup(
78                 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
79                 List<String> serviceGroupNames) throws Exception {
80         CommonList commonList = new CommonList();
81         AbstractCommonList list = (AbstractCommonList)commonList;
82         RepositoryInstance repoSession = null;
83         boolean releaseRepoSession = false;
84         
85         try { 
86                 RepositoryJavaClientImpl repoClient = (RepositoryJavaClientImpl)this.getRepositoryClient(ctx);
87                 repoSession = this.getRepositorySession();
88                 if (repoSession == null) {
89                         repoSession = repoClient.getRepositorySession(ctx);
90                         releaseRepoSession = true;
91                 }
92             DocumentFilter myFilter = getDocumentFilter();
93                 // Make sure we pick up workflow state, etc. 
94                 int pageSize = myFilter.getPageSize();
95                 int pageNum = myFilter.getStartPage();
96                 list.setPageNum(pageNum);
97                 list.setPageSize(pageSize);
98
99                 try {
100                 Map<String, ServiceBindingType> queriedServiceBindings = new HashMap<String, ServiceBindingType>();
101                 RepositoryJavaClientImpl nuxeoRepoClient = (RepositoryJavaClientImpl)repoClient;
102                 // Get the service bindings for this tenant
103                 TenantBindingConfigReaderImpl tReader =
104                         ServiceMain.getInstance().getTenantBindingConfigReader();
105                 // We need to get all the procedures, authorities, and objects.
106                 List<ServiceBindingType> servicebindings = 
107                                 tReader.getServiceBindingsByType(ctx.getTenantId(), serviceGroupNames);
108                 if (servicebindings == null || servicebindings.isEmpty()) {
109                     Response response = Response.status(Response.Status.NOT_FOUND).entity(
110                             ServiceMessages.READ_FAILED + 
111                             ServiceMessages.resourceNotFoundMsg(implode(serviceGroupNames, ","))).type("text/plain").build();
112                     throw new WebApplicationException(response);
113                 }
114                 servicebindings = SecurityUtils.getReadableServiceBindingsForCurrentUser(servicebindings);
115                 // Build the list of docTypes for allowed serviceBindings
116                 ArrayList<String> docTypes = new ArrayList<String>();
117                 for(ServiceBindingType binding:servicebindings) {
118                         ServiceObjectType serviceObj = binding.getObject();
119                         if(serviceObj!=null) {
120                         String docType = serviceObj.getName();
121                                 docTypes.add(docType);
122                         queriedServiceBindings.put(docType, binding);
123                         }
124                 }
125                 
126                 // This should be "Document" but CMIS is gagging on that right now.
127                 ctx.getQueryParams().add(IQueryManager.SELECT_DOC_TYPE_FIELD, "cmis:document");
128                 
129                 // Now we have to issue the search
130                 // findDocs qill build a QueryContext, which wants to see a docType for our context
131                 ctx.setDocumentType("Document");
132                 DocumentWrapper<DocumentModelList> docListWrapper = 
133                                 nuxeoRepoClient.findDocs(ctx, this, repoSession, docTypes );
134                 // Now we gather the info for each document into the list and return
135                 DocumentModelList docList = docListWrapper.getWrappedObject();
136                 
137                 if (docList == null) { // found no authRef fields - nothing to process
138                     return list;
139                 }
140                 processDocList(docList, queriedServiceBindings, commonList);
141                 list.setItemsInPage(docList.size());
142                 list.setTotalItems(docList.totalSize());
143                 } catch (DocumentException de) {
144                         throw de;
145                 } catch (Exception e) {
146                         if (logger.isDebugEnabled()) {
147                                 logger.debug("Caught exception ", e);
148                         }
149                         throw new DocumentException(e);
150                 } finally {
151                         if (releaseRepoSession && repoSession != null) {
152                                 repoClient.releaseRepositorySession(ctx, repoSession);
153                         }
154                 }
155         } catch (Exception e) {
156                 if (logger.isDebugEnabled()) {
157                         logger.debug("Caught exception ", e);
158                 }
159                 throw new DocumentException(e);
160         }               
161         return list;
162     }
163     
164     // Move this to a Utils class!
165     public static String implode(List<String> stringList, String sep) {
166         StringBuilder sb = new StringBuilder();
167         boolean fFirst = false;
168         for(String name:stringList) {
169                 if(fFirst) {
170                         fFirst = false;
171                 } else {
172                         sb.append(sep);
173                 }
174                 sb.append(name);
175         }
176         return sb.toString();
177     }
178     
179     private String getUriFromServiceBinding(ServiceBindingType sb, String csid) {
180         return "/" + sb.getName().toLowerCase() + "/" + csid;
181     }
182     
183     private void processDocList(        
184                 DocumentModelList docList,
185                 Map<String, ServiceBindingType> queriedServiceBindings,
186                 CommonList list ) {
187         int nFields = NUM_META_FIELDS+NUM_STANDARD_LIST_RESULT_FIELDS;
188         String fields[] = new String[nFields];
189         fields[0] = STANDARD_LIST_CSID_FIELD;
190         fields[1] = STANDARD_LIST_URI_FIELD;
191         fields[2] = STANDARD_LIST_UPDATED_AT_FIELD;
192         fields[3] = STANDARD_LIST_WORKFLOW_FIELD;
193         fields[4] = DOC_NAME_FIELD;
194         fields[5] = DOC_NUMBER_FIELD;
195         fields[6] = DOC_TYPE_FIELD;
196         list.setFieldsReturned(fields);
197         Iterator<DocumentModel> iter = docList.iterator();
198                 HashMap<String, Object> item = new HashMap<String, Object>();
199         while (iter.hasNext()) {
200             DocumentModel docModel = iter.next();
201             String docType = docModel.getDocumentType().getName();
202             docType = ServiceBindingUtils.getUnqualifiedTenantDocType(docType);
203             ServiceBindingType sb = queriedServiceBindings.get(docType);
204             if (sb == null) {
205                 throw new RuntimeException(
206                         "processDocList: No Service Binding for docType: " + docType);
207             }
208             String csid = NuxeoUtils.getCsid(docModel);
209             item.put(STANDARD_LIST_CSID_FIELD, csid);
210             // Need to get the URI for the document, by it's type.
211             item.put(STANDARD_LIST_URI_FIELD, getUriFromServiceBinding(sb, csid));
212             try {
213                 item.put(STANDARD_LIST_UPDATED_AT_FIELD, getUpdatedAtAsString(docModel));
214                 item.put(STANDARD_LIST_WORKFLOW_FIELD, docModel.getCurrentLifeCycleState());
215             } catch(Exception e) {
216                 logger.error("Error getting core values for doc ["+csid+"]: "+e.getLocalizedMessage());
217             }
218
219             String value = ServiceBindingUtils.getMappedFieldInDoc(sb, 
220                                                         ServiceBindingUtils.OBJ_NUMBER_PROP, docModel);
221             item.put(DOC_NUMBER_FIELD, value);
222             value = ServiceBindingUtils.getMappedFieldInDoc(sb, 
223                                                         ServiceBindingUtils.OBJ_NAME_PROP, docModel);
224             item.put(DOC_NAME_FIELD, value);
225             item.put(DOC_TYPE_FIELD, docType);
226             
227             list.addItem(item);
228             item.clear();
229         }
230
231     }
232     
233
234 }
235