]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
98b0d1d7c12cc922c7a9aee74261a93ba0ab001c
[tmp/jakarta-migration.git] /
1 /**\r
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
5 \r
6  *  http://www.collectionspace.org\r
7  *  http://wiki.collectionspace.org\r
8 \r
9  *  Copyright 2009 University of California at Berkeley\r
10 \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
13 \r
14  *  You may obtain a copy of the ECL 2.0 License at\r
15 \r
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt\r
17 \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
23  */\r
24 package org.collectionspace.services.common.vocabulary;\r
25 \r
26 import java.util.ArrayList;\r
27 import java.util.HashMap;\r
28 import java.util.Iterator;\r
29 import java.util.List;\r
30 \r
31 import org.nuxeo.ecm.core.api.ClientException;\r
32 import org.nuxeo.ecm.core.api.DocumentModel;\r
33 import org.nuxeo.ecm.core.api.DocumentModelList;\r
34 import org.slf4j.Logger;\r
35 import org.slf4j.LoggerFactory;\r
36 import org.collectionspace.services.common.ServiceMain;\r
37 import org.collectionspace.services.common.authorityref.AuthorityRefDocList;\r
38 import org.collectionspace.services.common.authorityref.AuthorityRefList;\r
39 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;\r
40 import org.collectionspace.services.common.context.ServiceBindingUtils;\r
41 import org.collectionspace.services.common.document.DocumentException;\r
42 import org.collectionspace.services.common.document.DocumentNotFoundException;\r
43 import org.collectionspace.services.common.document.DocumentWrapper;\r
44 import org.collectionspace.services.common.repository.RepositoryClient;\r
45 import org.collectionspace.services.common.service.ServiceBindingType;\r
46 import org.collectionspace.services.nuxeo.util.NuxeoUtils;\r
47 \r
48 /**\r
49  * RefNameServiceUtils is a collection of services utilities related to refName usage.\r
50  *\r
51  * $LastChangedRevision: $\r
52  * $LastChangedDate: $\r
53  */\r
54 public class RefNameServiceUtils {\r
55 \r
56     private final Logger logger = LoggerFactory.getLogger(RefNameServiceUtils.class);\r
57     \r
58     public static AuthorityRefDocList getAuthorityRefDocs(RepositoryClient repoClient, \r
59                 String tenantId, String serviceType, String refName,\r
60                 int pageSize, int pageNum, boolean computeTotal )\r
61                 throws DocumentException, DocumentNotFoundException {\r
62         AuthorityRefDocList wrapperList = new AuthorityRefDocList();\r
63         List<AuthorityRefDocList.AuthorityRefDocItem> list = \r
64                 wrapperList.getAuthorityRefDocItem();\r
65         TenantBindingConfigReaderImpl tReader =\r
66             ServiceMain.getInstance().getTenantBindingConfigReader();\r
67         List<ServiceBindingType> servicebindings = tReader.getServiceBindingsByType(tenantId, serviceType);\r
68         if(servicebindings==null || servicebindings.isEmpty())\r
69                 return null;\r
70         // Need to escape the quotes in the refName\r
71         // TODO What if they are already escaped?\r
72         String escapedRefName = refName.replaceAll("'", "\\\\'");\r
73         String domain = tReader.getTenantBinding(tenantId).getRepositoryDomain();\r
74         ArrayList<String> docTypes = new ArrayList<String>(); \r
75         HashMap<String, ServiceBindingType> queriedServiceBindings = new HashMap<String, ServiceBindingType>(); \r
76         HashMap<String, List<String>> authRefFieldsByService = new HashMap<String, List<String>>(); \r
77         StringBuilder whereClause = new StringBuilder();\r
78         boolean fFirst = true;\r
79         for(ServiceBindingType sb:servicebindings) {\r
80                 List<String> authRefFields = \r
81                         ServiceBindingUtils.getAllPartsPropertyValues(sb, \r
82                                 ServiceBindingUtils.AUTH_REF_PROP, ServiceBindingUtils.QUALIFIED_PROP_NAMES);\r
83                 if(authRefFields.isEmpty())\r
84                         continue;\r
85                 String docType = sb.getObject().getName();\r
86                 queriedServiceBindings.put(docType, sb);\r
87                 authRefFieldsByService.put(docType, authRefFields);\r
88                 docTypes.add(docType);\r
89                 /*\r
90                 // HACK - need to get qualified properties from the ServiceBinding\r
91                 String prefix = "";\r
92                 if(docType.equalsIgnoreCase("Intake"))\r
93                         prefix = "intakes_common:";\r
94                 else if(docType.equalsIgnoreCase("Loanin"))\r
95                         prefix = "loansin_common:";\r
96                 else if(docType.equalsIgnoreCase("Acquisition"))\r
97                         prefix = "acquisitions_common:";\r
98                 */\r
99                 for(String field:authRefFields) {\r
100                         // Build up the where clause for each authRef field\r
101                         if(fFirst) {\r
102                                 fFirst = false;\r
103                         } else {\r
104                                 whereClause.append(" OR ");\r
105                         }\r
106                         //whereClause.append(prefix);\r
107                         whereClause.append(field);\r
108                         whereClause.append("='");\r
109                         whereClause.append(escapedRefName);\r
110                         whereClause.append("'");\r
111                 }\r
112         }\r
113         if(fFirst) // found no authRef fields - nothing to query\r
114                 return wrapperList;\r
115         String fullQuery = whereClause.toString(); // for debug\r
116                 // Now we have to issue the search\r
117                 DocumentWrapper<DocumentModelList> docListWrapper = repoClient.findDocs(\r
118                         docTypes, whereClause.toString(), domain, pageSize, pageNum, computeTotal );\r
119                 // Now we gather the info for each document into the list and return\r
120         DocumentModelList docList = docListWrapper.getWrappedObject();\r
121         Iterator<DocumentModel> iter = docList.iterator();\r
122         while(iter.hasNext()){\r
123             DocumentModel docModel = iter.next();\r
124             AuthorityRefDocList.AuthorityRefDocItem ilistItem = new AuthorityRefDocList.AuthorityRefDocItem();\r
125             String csid = NuxeoUtils.extractId(docModel.getPathAsString());\r
126             String docType = docModel.getDocumentType().getName();\r
127             ServiceBindingType sb = queriedServiceBindings.get(docType);\r
128             if(sb==null) {\r
129                 throw new RuntimeException(\r
130                                 "getAuthorityRefDocs: No Service Binding for docType: "+docType);\r
131             }\r
132             String serviceContextPath = "/" + sb.getName().toLowerCase() + "/";\r
133             // The id and URI are the same on all doctypes\r
134             ilistItem.setDocId(csid);\r
135             ilistItem.setUri(serviceContextPath+csid);\r
136             ilistItem.setDocType(docType);\r
137             ilistItem.setDocNumber(\r
138                         ServiceBindingUtils.getMappedFieldInDoc(sb, ServiceBindingUtils.OBJ_NUMBER_PROP, docModel));\r
139             ilistItem.setDocName(\r
140                         ServiceBindingUtils.getMappedFieldInDoc(sb, ServiceBindingUtils.OBJ_NAME_PROP, docModel));\r
141             // Now, we have to loop over the authRefFieldsByService to figure\r
142             // out which field matched this. Ignore multiple matches.\r
143             List<String> authRefFields = authRefFieldsByService.get(docType);\r
144             if(authRefFields==null || authRefFields.isEmpty()){\r
145                 throw new RuntimeException(\r
146                                 "getAuthorityRefDocs: internal logic error: can't fetch authRefFields for DocType." );\r
147             }\r
148             boolean fRefFound = false;\r
149             /* Use this if we go to qualified field names\r
150             for(String field:authRefFields){\r
151                 String[] strings = field.split(":");\r
152                 if(strings.length!=2) {\r
153                         throw new RuntimeException(\r
154                                         "getAuthorityRefDocs: Bad configuration of authRefField.");\r
155                 }\r
156                 try {\r
157                     if(refName.equals(docModel.getProperty(strings[0], strings[1]))) {\r
158                         ilistItem.setSourceField(field);\r
159                         fRefFound = true;\r
160                         break;\r
161                     }\r
162                 } catch(ClientException ce) {\r
163                         throw new RuntimeException(\r
164                                         "getAuthorityRefDocs: Problem fetching: "+field, ce);\r
165                 }\r
166             }\r
167             */\r
168             for(String field:authRefFields){\r
169                 try {\r
170                     if(refName.equals(docModel.getPropertyValue(field))) {\r
171                         ilistItem.setSourceField(field);\r
172                         fRefFound = true;\r
173                         break;\r
174                     }\r
175                 } catch(ClientException ce) {\r
176                         throw new RuntimeException(\r
177                                         "getAuthorityRefDocs: Problem fetching: "+field, ce);\r
178                 }\r
179             }\r
180             if(!fRefFound) {\r
181                 throw new RuntimeException(\r
182                                 "getAuthorityRefDocs: Could not find refname in object:"\r
183                                 +docType+":"+csid);\r
184             }\r
185             list.add(ilistItem);\r
186         }\r
187         return wrapperList;\r
188     }\r
189 \r
190 }\r
191 \r