2 * This document is a part of the source code and related artifacts
\r
3 * for CollectionSpace, an open source collections management system
\r
4 * for museums and related institutions:
\r
6 * http://www.collectionspace.org
\r
7 * http://wiki.collectionspace.org
\r
9 * Copyright 2009 University of California at Berkeley
\r
11 * Licensed under the Educational Community License (ECL), Version 2.0.
\r
12 * You may not use this file except in compliance with this License.
\r
14 * You may obtain a copy of the ECL 2.0 License at
\r
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
\r
18 * Unless required by applicable law or agreed to in writing, software
\r
19 * distributed under the License is distributed on an "AS IS" BASIS,
\r
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
21 * See the License for the specific language governing permissions and
\r
22 * limitations under the License.
\r
24 package org.collectionspace.services.common.vocabulary;
\r
26 import java.util.ArrayList;
\r
27 import java.util.Collection;
\r
28 import java.util.HashMap;
\r
29 import java.util.Iterator;
\r
30 import java.util.List;
\r
31 import java.util.Map;
\r
33 import org.nuxeo.ecm.core.api.ClientException;
\r
34 import org.nuxeo.ecm.core.api.DocumentModel;
\r
35 import org.nuxeo.ecm.core.api.DocumentModelList;
\r
36 import org.nuxeo.ecm.core.api.model.Property;
\r
37 import org.nuxeo.ecm.core.api.model.PropertyException;
\r
38 import org.nuxeo.ecm.core.api.model.impl.primitives.StringProperty;
\r
39 import org.slf4j.Logger;
\r
40 import org.slf4j.LoggerFactory;
\r
42 import org.collectionspace.services.common.ServiceMain;
\r
43 import org.collectionspace.services.common.context.ServiceContext;
\r
44 import org.collectionspace.services.common.api.Tools;
\r
45 import org.collectionspace.services.common.authorityref.AuthorityRefDocList;
\r
46 import org.collectionspace.services.common.authorityref.AuthorityRefList;
\r
47 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
\r
48 import org.collectionspace.services.common.context.ServiceBindingUtils;
\r
49 import org.collectionspace.services.common.document.DocumentException;
\r
50 import org.collectionspace.services.common.document.DocumentNotFoundException;
\r
51 import org.collectionspace.services.common.document.DocumentUtils;
\r
52 import org.collectionspace.services.common.document.DocumentWrapper;
\r
53 import org.collectionspace.services.common.repository.RepositoryClient;
\r
54 import org.collectionspace.services.nuxeo.client.java.DocHandlerBase;
\r
55 import org.collectionspace.services.nuxeo.client.java.RepositoryJavaClientImpl;
\r
56 import org.collectionspace.services.common.service.ServiceBindingType;
\r
57 import org.collectionspace.services.jaxb.AbstractCommonList;
\r
58 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
\r
61 * RefNameServiceUtils is a collection of services utilities related to refName usage.
\r
63 * $LastChangedRevision: $
\r
64 * $LastChangedDate: $
\r
66 public class RefNameServiceUtils {
\r
68 private static final Logger logger = LoggerFactory.getLogger(RefNameServiceUtils.class);
\r
70 private static ArrayList<String> refNameServiceTypes = null;
\r
72 public static AuthorityRefDocList getAuthorityRefDocs(ServiceContext ctx,
\r
73 RepositoryClient repoClient,
\r
74 List<String> serviceTypes,
\r
77 int pageSize, int pageNum, boolean computeTotal) throws DocumentException, DocumentNotFoundException {
\r
78 AuthorityRefDocList wrapperList = new AuthorityRefDocList();
\r
79 AbstractCommonList commonList = (AbstractCommonList) wrapperList;
\r
80 commonList.setPageNum(pageNum);
\r
81 commonList.setPageSize(pageSize);
\r
82 List<AuthorityRefDocList.AuthorityRefDocItem> list =
\r
83 wrapperList.getAuthorityRefDocItem();
\r
85 Map<String, ServiceBindingType> queriedServiceBindings = new HashMap<String, ServiceBindingType>();
\r
86 Map<String, Map<String, String>> authRefFieldsByService = new HashMap<String, Map<String, String>>();
\r
88 DocumentModelList docList = findAuthorityRefDocs(ctx, repoClient, serviceTypes, refName, refPropName,
\r
89 queriedServiceBindings, authRefFieldsByService, pageSize, pageNum, computeTotal);
\r
91 if (docList == null) { // found no authRef fields - nothing to process
\r
94 // Set num of items in list. this is useful to our testing framework.
\r
95 commonList.setItemsInPage(docList.size());
\r
96 // set the total result size
\r
97 commonList.setTotalItems(docList.totalSize());
\r
99 int nRefsFound = processRefObjsDocList(docList, refName, queriedServiceBindings, authRefFieldsByService,
\r
101 if(logger.isDebugEnabled() && (nRefsFound < docList.size())) {
\r
102 logger.debug("Internal curiosity: got fewer matches of refs than # docs matched...");
\r
104 return wrapperList;
\r
107 private static ArrayList<String> getRefNameServiceTypes() {
\r
108 if(refNameServiceTypes == null) {
\r
109 refNameServiceTypes = new ArrayList<String>();
\r
110 refNameServiceTypes.add(ServiceBindingUtils.SERVICE_TYPE_AUTHORITY);
\r
111 refNameServiceTypes.add(ServiceBindingUtils.SERVICE_TYPE_OBJECT);
\r
112 refNameServiceTypes.add(ServiceBindingUtils.SERVICE_TYPE_PROCEDURE);
\r
114 return refNameServiceTypes;
\r
117 public static int updateAuthorityRefDocs(ServiceContext ctx,
\r
118 RepositoryClient repoClient,
\r
121 String refPropName ) {
\r
122 Map<String, ServiceBindingType> queriedServiceBindings = new HashMap<String, ServiceBindingType>();
\r
123 Map<String, Map<String, String>> authRefFieldsByService = new HashMap<String, Map<String, String>>();
\r
124 int nRefsFound = 0;
\r
125 if(!(repoClient instanceof RepositoryJavaClientImpl)) {
\r
126 throw new InternalError("updateAuthorityRefDocs() called with unknown repoClient type!");
\r
129 final int pageSize = 100; // Seems like a good value - no real data to set this well.
\r
130 int pageNumProcessed = 1;
\r
131 while(true) { // Keep looping until we find all the refs.
\r
132 logger.debug("updateAuthorityRefDocs working on page: "+pageNumProcessed);
\r
133 // Note that we always ask the Repo for the first page, since each page we process
\r
134 // should not be found in successive searches.
\r
135 DocumentModelList docList = findAuthorityRefDocs(ctx, repoClient, getRefNameServiceTypes(), oldRefName, refPropName,
\r
136 queriedServiceBindings, authRefFieldsByService, pageSize, 0, false);
\r
138 if((docList == null) // found no authRef fields - nothing to do
\r
139 || (docList.size() == 0)) { // No more to handle
\r
140 logger.debug("updateAuthorityRefDocs no more results");
\r
143 logger.debug("updateAuthorityRefDocs curr page result list size: "+docList.size());
\r
144 int nRefsFoundThisPage = processRefObjsDocList(docList, oldRefName, queriedServiceBindings, authRefFieldsByService,
\r
146 if(nRefsFoundThisPage>0) {
\r
147 ((RepositoryJavaClientImpl)repoClient).saveDocListWithoutHandlerProcessing(ctx, docList, true);
\r
148 nRefsFound += nRefsFoundThisPage;
\r
150 pageNumProcessed++;
\r
152 } catch(Exception e) {
\r
153 logger.error("Internal error updating the AuthorityRefDocs: " + e.getLocalizedMessage());
\r
154 logger.debug(Tools.errorToString(e, true));
\r
156 logger.debug("updateAuthorityRefDocs replaced a total of: "+nRefsFound);
\r
160 private static DocumentModelList findAuthorityRefDocs(ServiceContext ctx,
\r
161 RepositoryClient repoClient,
\r
162 List<String> serviceTypes,
\r
164 String refPropName,
\r
165 Map<String, ServiceBindingType> queriedServiceBindings,
\r
166 Map<String, Map<String, String>> authRefFieldsByService,
\r
167 int pageSize, int pageNum, boolean computeTotal) throws DocumentException, DocumentNotFoundException {
\r
169 // Get the service bindings for this tenant
\r
170 TenantBindingConfigReaderImpl tReader =
\r
171 ServiceMain.getInstance().getTenantBindingConfigReader();
\r
172 // We need to get all the procedures, authorities, and objects.
\r
173 List<ServiceBindingType> servicebindings = tReader.getServiceBindingsByType(ctx.getTenantId(), serviceTypes);
\r
174 if (servicebindings == null || servicebindings.isEmpty()) {
\r
175 logger.error("RefNameServiceUtils.getAuthorityRefDocs: No services bindings found, cannot proceed!");
\r
179 // Need to escape the quotes in the refName
\r
180 // TODO What if they are already escaped?
\r
181 String escapedRefName = refName.replaceAll("'", "\\\\'");
\r
182 ArrayList<String> docTypes = new ArrayList<String>();
\r
184 String query = computeWhereClauseForAuthorityRefDocs(escapedRefName, refPropName, docTypes, servicebindings,
\r
185 queriedServiceBindings, authRefFieldsByService );
\r
186 if (query == null) { // found no authRef fields - nothing to query
\r
189 // Now we have to issue the search
\r
190 DocumentWrapper<DocumentModelList> docListWrapper = repoClient.findDocs(ctx,
\r
191 docTypes, query, pageSize, pageNum, computeTotal);
\r
192 // Now we gather the info for each document into the list and return
\r
193 DocumentModelList docList = docListWrapper.getWrappedObject();
\r
197 private static String computeWhereClauseForAuthorityRefDocs(
\r
198 String escapedRefName,
\r
199 String refPropName,
\r
200 ArrayList<String> docTypes,
\r
201 List<ServiceBindingType> servicebindings,
\r
202 Map<String, ServiceBindingType> queriedServiceBindings,
\r
203 Map<String, Map<String, String>> authRefFieldsByService ) {
\r
204 StringBuilder whereClause = new StringBuilder();
\r
205 boolean fFirst = true;
\r
206 List<String> authRefFieldPaths = new ArrayList<String>();
\r
207 for (ServiceBindingType sb : servicebindings) {
\r
208 // Gets the property names for each part, qualified with the part label (which
\r
209 // is also the table name, the way that the repository works).
\r
210 authRefFieldPaths =
\r
211 ServiceBindingUtils.getAllPartsPropertyValues(sb,
\r
212 refPropName, ServiceBindingUtils.QUALIFIED_PROP_NAMES);
\r
213 if (authRefFieldPaths.isEmpty()) {
\r
216 String authRefPath = "";
\r
217 String ancestorAuthRefFieldName = "";
\r
218 Map<String, String> authRefFields = new HashMap<String, String>();
\r
219 for (int i = 0; i < authRefFieldPaths.size(); i++) {
\r
220 // fieldName = DocumentUtils.getDescendantOrAncestor(authRefFields.get(i));
\r
221 // For simple field values, we just search on the item.
\r
222 // For simple repeating scalars, we just search the group field
\r
223 // For repeating complex types, we will need to do more.
\r
224 authRefPath = authRefFieldPaths.get(i);
\r
225 ancestorAuthRefFieldName = DocumentUtils.getAncestorAuthRefFieldName(authRefFieldPaths.get(i));
\r
226 authRefFields.put(authRefPath, ancestorAuthRefFieldName);
\r
229 String docType = sb.getObject().getName();
\r
230 queriedServiceBindings.put(docType, sb);
\r
231 authRefFieldsByService.put(docType, authRefFields);
\r
232 docTypes.add(docType);
\r
233 Collection<String> fields = authRefFields.values();
\r
234 for (String field : fields) {
\r
235 // Build up the where clause for each authRef field
\r
239 whereClause.append(" OR ");
\r
241 //whereClause.append(prefix);
\r
242 whereClause.append(field);
\r
243 whereClause.append("='");
\r
244 whereClause.append(escapedRefName);
\r
245 whereClause.append("'");
\r
248 String whereClauseStr = whereClause.toString(); // for debugging
\r
249 if (fFirst) { // found no authRef fields - nothing to query
\r
252 return whereClause.toString();
\r
257 * Runs through the list of found docs, processing them.
\r
258 * If list is non-null, then processing means gather the info for items.
\r
259 * If list is null, and newRefName is non-null, then processing means replacing and updating.
\r
260 * If processing/updating, this must be called in teh context of an open session, and caller
\r
261 * must release Session after calling this.
\r
264 private static int processRefObjsDocList(
\r
265 DocumentModelList docList,
\r
267 Map<String, ServiceBindingType> queriedServiceBindings,
\r
268 Map<String, Map<String, String>> authRefFieldsByService,
\r
269 List<AuthorityRefDocList.AuthorityRefDocItem> list,
\r
270 String newAuthorityRefName) {
\r
271 if(newAuthorityRefName==null) {
\r
273 throw new InternalError("processRefObjsDocList() called with neither an itemList nor a new RefName!");
\r
275 } else if(list!=null) {
\r
276 throw new InternalError("processRefObjsDocList() called with both an itemList and a new RefName!");
\r
279 Iterator<DocumentModel> iter = docList.iterator();
\r
280 int nRefsFoundTotal = 0;
\r
281 while (iter.hasNext()) {
\r
282 DocumentModel docModel = iter.next();
\r
283 AuthorityRefDocList.AuthorityRefDocItem ilistItem;
\r
285 String docType = docModel.getDocumentType().getName();
\r
286 ServiceBindingType sb = queriedServiceBindings.get(docType);
\r
288 throw new RuntimeException(
\r
289 "getAuthorityRefDocs: No Service Binding for docType: " + docType);
\r
291 String serviceContextPath = "/" + sb.getName().toLowerCase() + "/";
\r
296 ilistItem = new AuthorityRefDocList.AuthorityRefDocItem();
\r
297 String csid = NuxeoUtils.getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
\r
298 ilistItem.setDocId(csid);
\r
299 ilistItem.setUri(serviceContextPath + csid);
\r
301 ilistItem.setUpdatedAt(DocHandlerBase.getUpdatedAtAsString(docModel));
\r
302 } catch(Exception e) {
\r
303 logger.error("Error getting udpatedAt value for doc ["+csid+"]: "+e.getLocalizedMessage());
\r
305 // The id and URI are the same on all doctypes
\r
306 ilistItem.setDocType(docType);
\r
307 ilistItem.setDocNumber(
\r
308 ServiceBindingUtils.getMappedFieldInDoc(sb, ServiceBindingUtils.OBJ_NUMBER_PROP, docModel));
\r
309 ilistItem.setDocName(
\r
310 ServiceBindingUtils.getMappedFieldInDoc(sb, ServiceBindingUtils.OBJ_NAME_PROP, docModel));
\r
312 // Now, we have to loop over the authRefFieldsByService to figure
\r
313 // out which field(s) matched this.
\r
314 Map<String,String> matchingAuthRefFields = authRefFieldsByService.get(docType);
\r
315 if (matchingAuthRefFields == null || matchingAuthRefFields.isEmpty()) {
\r
316 throw new RuntimeException(
\r
317 "getAuthorityRefDocs: internal logic error: can't fetch authRefFields for DocType.");
\r
319 String authRefAncestorField = "";
\r
320 String authRefDescendantField = "";
\r
321 String sourceField = "";
\r
322 int nRefsFoundInDoc = 0;
\r
323 // Use this if we go to qualified field names
\r
324 for (String path : matchingAuthRefFields.keySet()) {
\r
326 // This is the field name we show in the return info
\r
327 // Returned as a schema-qualified property path
\r
328 authRefAncestorField = (String) matchingAuthRefFields.get(path);
\r
329 // This is the qualified field we have to get from the doc model
\r
330 authRefDescendantField = DocumentUtils.getDescendantOrAncestor(path);
\r
331 // The ancestor field is part-schema (tablename) qualified
\r
332 //String[] strings = authRefAncestorField.split(":");
\r
333 //if (strings.length != 2) {
\r
334 // throw new RuntimeException(
\r
335 // "getAuthorityRefDocs: Bad configuration of path to authority reference field.");
\r
337 // strings[0] holds a schema name, such as "intakes_common"
\r
339 // strings[1] holds:
\r
340 // * The name of an authority reference field, such as "depositor";
\r
342 // * The name of an ancestor (e.g. parent, grandparent ...) field,
\r
343 // such as "fieldCollectors", of a repeatable authority reference
\r
344 // field, such as "fieldCollector".
\r
345 // TODO - if the value is not simple, or repeating scalar, need a more
\r
346 // sophisticated fetch.
\r
347 // Change this to an XPath model
\r
348 //Object fieldValue = docModel.getProperty(strings[0], strings[1]);
\r
349 // This will have to handle repeating complex fields by iterating over the possibilities
\r
350 // and finding the one that matches.
\r
351 Property fieldValue = docModel.getProperty(authRefAncestorField);
\r
352 // We know this doc should have a match somewhere, but it may not be in this field
\r
353 // If we are just building up the refItems, then it is enough to know we found a match.
\r
354 // If we are patching refName values, then we have to replace each match.
\r
355 int nRefsMatchedInField = refNameFoundInField(refName, fieldValue, newAuthorityRefName);
\r
356 if (nRefsMatchedInField > 0) {
\r
357 sourceField = authRefDescendantField;
\r
358 // Handle multiple fields matching in one Doc. See CSPACE-2863.
\r
359 if(nRefsFoundInDoc > 0) {
\r
360 // We already added ilistItem, so we need to clone that and add again
\r
361 if(ilistItem != null) {
\r
362 ilistItem = cloneAuthRefDocItem(ilistItem, sourceField);
\r
365 if(ilistItem != null) {
\r
366 ilistItem.setSourceField(sourceField);
\r
369 if(ilistItem != null) {
\r
370 list.add(ilistItem);
\r
372 nRefsFoundInDoc += nRefsMatchedInField;
\r
375 } catch (ClientException ce) {
\r
376 throw new RuntimeException(
\r
377 "getAuthorityRefDocs: Problem fetching: " + sourceField, ce);
\r
380 if (nRefsFoundInDoc == 0) {
\r
381 throw new RuntimeException(
\r
382 "getAuthorityRefDocs: Could not find refname in object:"
\r
383 + docType + ":" + NuxeoUtils.getCsid(docModel));
\r
385 nRefsFoundTotal += nRefsFoundInDoc;
\r
387 return nRefsFoundTotal;
\r
390 private static AuthorityRefDocList.AuthorityRefDocItem cloneAuthRefDocItem(
\r
391 AuthorityRefDocList.AuthorityRefDocItem ilistItem, String sourceField) {
\r
392 AuthorityRefDocList.AuthorityRefDocItem newlistItem = new AuthorityRefDocList.AuthorityRefDocItem();
\r
393 newlistItem.setDocId(ilistItem.getDocId());
\r
394 newlistItem.setDocName(ilistItem.getDocName());
\r
395 newlistItem.setDocNumber(ilistItem.getDocNumber());
\r
396 newlistItem.setDocType(ilistItem.getDocType());
\r
397 newlistItem.setUri(ilistItem.getUri());
\r
398 newlistItem.setSourceField(sourceField);
\r
399 return newlistItem;
\r
403 * Identifies whether the refName was found in the supplied field.
\r
404 * If passed a new RefName, will set that into fields in which the old one was found.
\r
408 * * Repeatable scalar fields (aka multi-valued fields)
\r
410 * Does not work for:
\r
411 * * Structured fields (complexTypes)
\r
412 * * Repeatable structured fields (repeatable complexTypes)
\r
414 private static int refNameFoundInField(String oldRefName, Property fieldValue, String newRefName) {
\r
416 if (fieldValue instanceof List) {
\r
417 List<Property> fieldValueList = (List) fieldValue;
\r
418 for (Property listItemValue : fieldValueList) {
\r
420 if ((listItemValue instanceof StringProperty)
\r
421 && oldRefName.equalsIgnoreCase((String)listItemValue.getValue())) {
\r
423 if(newRefName!=null) {
\r
424 fieldValue.setValue(newRefName);
\r
426 // We cannot quit after the first, if we are replacing values.
\r
427 // If we are just looking (not replacing), finding one is enough.
\r
431 } catch( PropertyException pe ) {}
\r
435 if ((fieldValue instanceof StringProperty)
\r
436 && oldRefName.equalsIgnoreCase((String)fieldValue.getValue())) {
\r
438 if(newRefName!=null) {
\r
439 fieldValue.setValue(newRefName);
\r
442 } catch( PropertyException pe ) {}
\r