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