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.slf4j.Logger;
\r
37 import org.slf4j.LoggerFactory;
\r
39 import org.collectionspace.services.common.ServiceMain;
\r
40 import org.collectionspace.services.common.context.ServiceContext;
\r
41 import org.collectionspace.services.common.authorityref.AuthorityRefDocList;
\r
42 import org.collectionspace.services.common.authorityref.AuthorityRefList;
\r
43 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
\r
44 import org.collectionspace.services.common.context.ServiceBindingUtils;
\r
45 import org.collectionspace.services.common.document.DocumentException;
\r
46 import org.collectionspace.services.common.document.DocumentNotFoundException;
\r
47 import org.collectionspace.services.common.document.DocumentUtils;
\r
48 import org.collectionspace.services.common.document.DocumentWrapper;
\r
49 import org.collectionspace.services.common.repository.RepositoryClient;
\r
50 import org.collectionspace.services.common.service.ServiceBindingType;
\r
51 import org.collectionspace.services.jaxb.AbstractCommonList;
\r
52 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
\r
55 * RefNameServiceUtils is a collection of services utilities related to refName usage.
\r
57 * $LastChangedRevision: $
\r
58 * $LastChangedDate: $
\r
60 public class RefNameServiceUtils {
\r
62 private static final Logger logger = LoggerFactory.getLogger(RefNameServiceUtils.class);
\r
64 public static AuthorityRefDocList getAuthorityRefDocs(ServiceContext ctx,
\r
65 RepositoryClient repoClient,
\r
68 int pageSize, int pageNum, boolean computeTotal) throws DocumentException, DocumentNotFoundException {
\r
69 AuthorityRefDocList wrapperList = new AuthorityRefDocList();
\r
70 AbstractCommonList commonList = (AbstractCommonList) wrapperList;
\r
71 commonList.setPageNum(pageNum);
\r
72 commonList.setPageSize(pageSize);
\r
73 List<AuthorityRefDocList.AuthorityRefDocItem> list =
\r
74 wrapperList.getAuthorityRefDocItem();
\r
76 // Get the service bindings for this tenant
\r
77 TenantBindingConfigReaderImpl tReader =
\r
78 ServiceMain.getInstance().getTenantBindingConfigReader();
\r
79 List<ServiceBindingType> servicebindings = tReader.getServiceBindingsByType(ctx.getTenantId(), serviceType);
\r
80 if (servicebindings == null || servicebindings.isEmpty()) {
\r
81 logger.error("RefNameServiceUtils.getAuthorityRefDocs: No services bindings found, cannot proceed!");
\r
85 // Need to escape the quotes in the refName
\r
86 // TODO What if they are already escaped?
\r
87 String escapedRefName = refName.replaceAll("'", "\\\\'");
\r
88 ArrayList<String> docTypes = new ArrayList<String>();
\r
89 Map<String, ServiceBindingType> queriedServiceBindings = new HashMap<String, ServiceBindingType>();
\r
90 Map<String, Map<String, String>> authRefFieldsByService = new HashMap<String, Map<String, String>>();
\r
92 String query = computeWhereClauseForAuthorityRefDocs(escapedRefName, docTypes, servicebindings,
\r
93 queriedServiceBindings, authRefFieldsByService );
\r
94 if (query == null) { // found no authRef fields - nothing to query
\r
97 // Now we have to issue the search
\r
98 DocumentWrapper<DocumentModelList> docListWrapper = repoClient.findDocs(ctx,
\r
99 docTypes, query, pageSize, pageNum, computeTotal);
\r
100 // Now we gather the info for each document into the list and return
\r
101 DocumentModelList docList = docListWrapper.getWrappedObject();
\r
102 // Set num of items in list. this is useful to our testing framework.
\r
103 commonList.setItemsInPage(docList.size());
\r
104 // set the total result size
\r
105 commonList.setTotalItems(docList.totalSize());
\r
107 processRefObjsDocList(docList, refName, servicebindings,
\r
108 queriedServiceBindings, authRefFieldsByService,
\r
110 return wrapperList;
\r
113 private static String computeWhereClauseForAuthorityRefDocs(
\r
114 String escapedRefName,
\r
115 ArrayList<String> docTypes,
\r
116 List<ServiceBindingType> servicebindings,
\r
117 Map<String, ServiceBindingType> queriedServiceBindings,
\r
118 Map<String, Map<String, String>> authRefFieldsByService ) {
\r
119 StringBuilder whereClause = new StringBuilder();
\r
120 boolean fFirst = true;
\r
121 List<String> authRefFieldPaths = new ArrayList<String>();
\r
122 for (ServiceBindingType sb : servicebindings) {
\r
123 // Gets the property names for each part, qualified with the part label (which
\r
124 // is also the table name, the way that the repository works).
\r
125 authRefFieldPaths =
\r
126 ServiceBindingUtils.getAllPartsPropertyValues(sb,
\r
127 ServiceBindingUtils.AUTH_REF_PROP, ServiceBindingUtils.QUALIFIED_PROP_NAMES);
\r
128 if (authRefFieldPaths.isEmpty()) {
\r
131 String authRefPath = "";
\r
132 String ancestorAuthRefFieldName = "";
\r
133 Map<String, String> authRefFields = new HashMap<String, String>();
\r
134 for (int i = 0; i < authRefFieldPaths.size(); i++) {
\r
135 // fieldName = DocumentUtils.getDescendantOrAncestor(authRefFields.get(i));
\r
136 // For simple field values, we just search on the item.
\r
137 // For simple repeating scalars, we just search the group field
\r
138 // For repeating complex types, we will need to do more.
\r
139 authRefPath = authRefFieldPaths.get(i);
\r
140 ancestorAuthRefFieldName = DocumentUtils.getAncestorAuthRefFieldName(authRefFieldPaths.get(i));
\r
141 authRefFields.put(authRefPath, ancestorAuthRefFieldName);
\r
144 String docType = sb.getObject().getName();
\r
145 queriedServiceBindings.put(docType, sb);
\r
146 authRefFieldsByService.put(docType, authRefFields);
\r
147 docTypes.add(docType);
\r
148 Collection<String> fields = authRefFields.values();
\r
149 for (String field : fields) {
\r
150 // Build up the where clause for each authRef field
\r
154 whereClause.append(" OR ");
\r
156 //whereClause.append(prefix);
\r
157 whereClause.append(field);
\r
158 whereClause.append("='");
\r
159 whereClause.append(escapedRefName);
\r
160 whereClause.append("'");
\r
163 String whereClauseStr = whereClause.toString(); // for debugging
\r
164 if (fFirst) { // found no authRef fields - nothing to query
\r
167 return whereClause.toString();
\r
172 * Runs through the list of found docs, processing them.
\r
173 * If list is non-null, then processing means gather the info for items.
\r
174 * If list is null, and newRefName is non-null, then processing means replacing and updating.
\r
176 private static void processRefObjsDocList(DocumentModelList docList,
\r
178 List<ServiceBindingType> servicebindings,
\r
179 Map<String, ServiceBindingType> queriedServiceBindings,
\r
180 Map<String, Map<String, String>> authRefFieldsByService,
\r
181 List<AuthorityRefDocList.AuthorityRefDocItem> list,
\r
182 String newAuthorityRefName) {
\r
183 Iterator<DocumentModel> iter = docList.iterator();
\r
184 while (iter.hasNext()) {
\r
185 DocumentModel docModel = iter.next();
\r
186 AuthorityRefDocList.AuthorityRefDocItem ilistItem;
\r
188 String docType = docModel.getDocumentType().getName();
\r
189 ServiceBindingType sb = queriedServiceBindings.get(docType);
\r
191 throw new RuntimeException(
\r
192 "getAuthorityRefDocs: No Service Binding for docType: " + docType);
\r
194 String serviceContextPath = "/" + sb.getName().toLowerCase() + "/";
\r
199 ilistItem = new AuthorityRefDocList.AuthorityRefDocItem();
\r
200 String csid = NuxeoUtils.getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
\r
201 ilistItem.setDocId(csid);
\r
202 ilistItem.setUri(serviceContextPath + csid);
\r
203 // The id and URI are the same on all doctypes
\r
204 ilistItem.setDocType(docType);
\r
205 ilistItem.setDocNumber(
\r
206 ServiceBindingUtils.getMappedFieldInDoc(sb, ServiceBindingUtils.OBJ_NUMBER_PROP, docModel));
\r
207 ilistItem.setDocName(
\r
208 ServiceBindingUtils.getMappedFieldInDoc(sb, ServiceBindingUtils.OBJ_NAME_PROP, docModel));
\r
210 // Now, we have to loop over the authRefFieldsByService to figure
\r
211 // out which field matched this. Ignore multiple matches.
\r
212 Map<String,String> matchingAuthRefFields = authRefFieldsByService.get(docType);
\r
213 if (matchingAuthRefFields == null || matchingAuthRefFields.isEmpty()) {
\r
214 throw new RuntimeException(
\r
215 "getAuthorityRefDocs: internal logic error: can't fetch authRefFields for DocType.");
\r
217 String authRefAncestorField = "";
\r
218 String authRefDescendantField = "";
\r
219 String sourceField = "";
\r
220 boolean fRefFound = false;
\r
221 // Use this if we go to qualified field names
\r
222 for (String path : matchingAuthRefFields.keySet()) {
\r
224 // This is the field name we show in the return info
\r
225 authRefAncestorField = (String) matchingAuthRefFields.get(path);
\r
226 // This is the qualified field we have to get from the doc model
\r
227 authRefDescendantField = DocumentUtils.getDescendantOrAncestor(path);
\r
228 // The ancestor field is part-schema (tablename) qualified
\r
229 String[] strings = authRefAncestorField.split(":");
\r
230 if (strings.length != 2) {
\r
231 throw new RuntimeException(
\r
232 "getAuthorityRefDocs: Bad configuration of path to authority reference field.");
\r
234 // strings[0] holds a schema name, such as "intakes_common"
\r
236 // strings[1] holds:
\r
237 // * The name of an authority reference field, such as "depositor";
\r
239 // * The name of an ancestor (e.g. parent, grandparent ...) field,
\r
240 // such as "fieldCollectors", of a repeatable authority reference
\r
241 // field, such as "fieldCollector".
\r
242 // TODO - if the value is not simple, or repeating scalar, need a more
\r
243 // sophisticated fetch.
\r
244 Object fieldValue = docModel.getProperty(strings[0], strings[1]);
\r
245 // We cannot be sure why we have this doc, so look for matches
\r
246 boolean fRefMatches = refNameFoundInField(refName, fieldValue);
\r
248 sourceField = authRefDescendantField;
\r
249 // Handle multiple fields matching in one Doc. See CSPACE-2863.
\r
251 // We already added ilistItem, so we need to clone that and add again
\r
252 if(ilistItem != null) {
\r
253 ilistItem = cloneAuthRefDocItem(ilistItem, sourceField);
\r
256 if(ilistItem != null) {
\r
257 ilistItem.setSourceField(sourceField);
\r
261 if(ilistItem != null) {
\r
262 list.add(ilistItem);
\r
266 } catch (ClientException ce) {
\r
267 throw new RuntimeException(
\r
268 "getAuthorityRefDocs: Problem fetching: " + sourceField, ce);
\r
272 throw new RuntimeException(
\r
273 "getAuthorityRefDocs: Could not find refname in object:"
\r
274 + docType + ":" + NuxeoUtils.getCsid(docModel));
\r
280 private static AuthorityRefDocList.AuthorityRefDocItem cloneAuthRefDocItem(
\r
281 AuthorityRefDocList.AuthorityRefDocItem ilistItem, String sourceField) {
\r
282 AuthorityRefDocList.AuthorityRefDocItem newlistItem = new AuthorityRefDocList.AuthorityRefDocItem();
\r
283 newlistItem.setDocId(ilistItem.getDocId());
\r
284 newlistItem.setDocName(ilistItem.getDocName());
\r
285 newlistItem.setDocNumber(ilistItem.getDocNumber());
\r
286 newlistItem.setDocType(ilistItem.getDocType());
\r
287 newlistItem.setUri(ilistItem.getUri());
\r
288 newlistItem.setSourceField(sourceField);
\r
289 return newlistItem;
\r
293 * Identifies whether the refName was found in the supplied field.
\r
297 * * Repeatable scalar fields (aka multi-valued fields)
\r
299 * Does not work for:
\r
300 * * Structured fields (complexTypes)
\r
301 * * Repeatable structured fields (repeatable complexTypes)
\r
303 private static boolean refNameFoundInField(String refName, Object fieldValue) {
\r
305 boolean result = false;
\r
306 if (fieldValue instanceof List) {
\r
307 List<String> fieldValueList = (List) fieldValue;
\r
308 for (String listItemValue : fieldValueList) {
\r
309 if (refName.equalsIgnoreCase(listItemValue)) {
\r
315 } else if (fieldValue instanceof String){
\r
316 if (refName.equalsIgnoreCase((String)fieldValue)) {
\r