]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
b82bbdde1150a838cf24d2e1d653aeeb7af409c0
[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.common.service.ServiceBindingType;
58 import org.collectionspace.services.common.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,
80                 String keywords) throws Exception {
81         CommonList commonList = new CommonList();
82         AbstractCommonList list = (AbstractCommonList)commonList;
83         RepositoryInstance repoSession = null;
84         boolean releaseRepoSession = false;
85         
86         try { 
87                 RepositoryJavaClientImpl repoClient = (RepositoryJavaClientImpl)this.getRepositoryClient(ctx);
88                 repoSession = this.getRepositorySession();
89                 if (repoSession == null) {
90                         repoSession = repoClient.getRepositorySession();
91                         releaseRepoSession = true;
92                 }
93             DocumentFilter myFilter = getDocumentFilter();
94             String whereClause = null;
95                 if (keywords != null) {
96                     whereClause = QueryManager.createWhereClauseFromKeywords(keywords);
97                     if(Tools.isEmpty(whereClause)) {
98                         if (logger.isDebugEnabled()) {
99                                 logger.debug("The WHERE clause is empty for keywords: ["+keywords+"]");
100                         }
101                     } else {
102                             myFilter.appendWhereClause(whereClause, IQueryManager.SEARCH_QUALIFIER_AND);
103                             if (logger.isDebugEnabled()) {
104                                 logger.debug("The WHERE clause is: " + myFilter.getWhereClause());
105                             }
106                     }
107                 }
108                 int pageSize = myFilter.getPageSize();
109                 int pageNum = myFilter.getStartPage();
110                 final boolean computeTotal = true; 
111                 list.setPageNum(pageSize);
112                 list.setPageSize(pageNum);
113
114                 try {
115                 Map<String, ServiceBindingType> queriedServiceBindings = new HashMap<String, ServiceBindingType>();
116                 RepositoryJavaClientImpl nuxeoRepoClient = (RepositoryJavaClientImpl)repoClient;
117                 // Get the service bindings for this tenant
118                 TenantBindingConfigReaderImpl tReader =
119                         ServiceMain.getInstance().getTenantBindingConfigReader();
120                 // We need to get all the procedures, authorities, and objects.
121                 List<ServiceBindingType> servicebindings = 
122                                 tReader.getServiceBindingsByType(ctx.getTenantId(), serviceGroupNames);
123                 if (servicebindings == null || servicebindings.isEmpty()) {
124                     Response response = Response.status(Response.Status.NOT_FOUND).entity(
125                             ServiceMessages.READ_FAILED + 
126                             ServiceMessages.resourceNotFoundMsg(implode(serviceGroupNames, ","))).type("text/plain").build();
127                     throw new WebApplicationException(response);
128                 }
129                 servicebindings = SecurityUtils.getReadableServiceBindingsForCurrentUser(servicebindings);
130                 // Build the list of docTypes for allowed serviceBindings
131                 ArrayList<String> docTypes = new ArrayList<String>();
132                 for(ServiceBindingType binding:servicebindings) {
133                         ServiceObjectType serviceObj = binding.getObject();
134                         if(serviceObj!=null) {
135                         String docType = serviceObj.getName();
136                                 docTypes.add(docType);
137                         queriedServiceBindings.put(docType, binding);
138                         }
139                 }
140                 
141                 // Now we have to issue the search
142                 // findDocs qill build a QueryContext, which wants to see a docType for our context
143                 ctx.setDocumentType("Document");
144                 DocumentWrapper<DocumentModelList> docListWrapper = nuxeoRepoClient.findDocs(ctx, repoSession,
145                         docTypes, whereClause, pageSize, pageNum, computeTotal);
146                 // Now we gather the info for each document into the list and return
147                 DocumentModelList docList = docListWrapper.getWrappedObject();
148                 
149                 if (docList == null) { // found no authRef fields - nothing to process
150                     return list;
151                 }
152                 processDocList(docList, queriedServiceBindings, commonList);
153                 list.setItemsInPage(docList.size());
154                 list.setTotalItems(docList.totalSize());
155                 } catch (DocumentException de) {
156                         throw de;
157                 } catch (Exception e) {
158                         if (logger.isDebugEnabled()) {
159                                 logger.debug("Caught exception ", e);
160                         }
161                         throw new DocumentException(e);
162                 } finally {
163                         if (releaseRepoSession && repoSession != null) {
164                                 repoClient.releaseRepositorySession(repoSession);
165                         }
166                 }
167         } catch (Exception e) {
168                 if (logger.isDebugEnabled()) {
169                         logger.debug("Caught exception ", e);
170                 }
171                 throw new DocumentException(e);
172         }               
173         return list;
174     }
175     
176     // Move this to a Utils class!
177     public static String implode(List<String> stringList, String sep) {
178         StringBuilder sb = new StringBuilder();
179         boolean fFirst = false;
180         for(String name:stringList) {
181                 if(fFirst) {
182                         fFirst = false;
183                 } else {
184                         sb.append(sep);
185                 }
186                 sb.append(name);
187         }
188         return sb.toString();
189     }
190     
191     private String getUriFromServiceBinding(ServiceBindingType sb, String csid) {
192         return "/" + sb.getName().toLowerCase() + "/" + csid;
193     }
194     
195     private void processDocList(        
196                 DocumentModelList docList,
197                 Map<String, ServiceBindingType> queriedServiceBindings,
198                 CommonList list ) {
199         int nFields = NUM_META_FIELDS+NUM_STANDARD_LIST_RESULT_FIELDS;
200         String fields[] = new String[nFields];
201         fields[0] = "csid";
202         fields[1] = "uri";
203         fields[2] = "updatedAt";
204         fields[3] = DOC_NAME_FIELD;
205         fields[4] = DOC_NUMBER_FIELD;
206         fields[5] = DOC_TYPE_FIELD;
207         list.setFieldsReturned(fields);
208         Iterator<DocumentModel> iter = docList.iterator();
209                 HashMap<String,String> item = new HashMap<String,String>();
210         while (iter.hasNext()) {
211             DocumentModel docModel = iter.next();
212             String docType = docModel.getDocumentType().getName();
213             ServiceBindingType sb = queriedServiceBindings.get(docType);
214             if (sb == null) {
215                 throw new RuntimeException(
216                         "processDocList: No Service Binding for docType: " + docType);
217             }
218             String csid = NuxeoUtils.getCsid(docModel);
219             item.put(STANDARD_LIST_CSID_FIELD, csid);
220             // Need to get the URI for the document, by it's type.
221             item.put(STANDARD_LIST_URI_FIELD, getUriFromServiceBinding(sb, csid));
222             try {
223                 item.put(STANDARD_LIST_UPDATED_AT_FIELD, getUpdatedAtAsString(docModel));
224             } catch(Exception e) {
225                 logger.error("Error getting udpatedAt value for doc ["+csid+"]: "+e.getLocalizedMessage());
226             }
227
228             String value = ServiceBindingUtils.getMappedFieldInDoc(sb, 
229                                                         ServiceBindingUtils.OBJ_NUMBER_PROP, docModel);
230             item.put(DOC_NUMBER_FIELD, value);
231             value = ServiceBindingUtils.getMappedFieldInDoc(sb, 
232                                                         ServiceBindingUtils.OBJ_NAME_PROP, docModel);
233             item.put(DOC_NAME_FIELD, value);
234             item.put(DOC_TYPE_FIELD, docType);
235             
236             list.addItem(item);
237             item.clear();
238         }
239
240     }
241     
242
243 }
244