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