]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
30211583d22b7269c920e29eb22365cb94fc066d
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  */
24 package org.collectionspace.services.common.relation.nuxeo;
25
26 import java.lang.StringBuilder;
27
28 import org.collectionspace.services.client.IQueryManager;
29 import org.collectionspace.services.common.relation.RelationJAXBSchema;
30
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * RelationsUtils
36  *
37  * $LastChangedRevision: $
38  * $LastChangedDate: $
39  */
40 public class RelationsUtils {
41
42     /** The Constant logger. */
43     private static final Logger logger = LoggerFactory.getLogger(RelationsUtils.class);
44
45     /**
46      * Builds the where clause.
47      *
48      * @param subject the subject
49      * @param predicate the predicate
50      * @param object the object
51      * @return the string
52      */
53     public static String buildWhereClause(String subject, String subjectType,
54                 String predicate,
55                 String object, String objectType,
56                 String subjectOrObject) {
57         String result = null;
58         
59         StringBuilder stringBuilder = new StringBuilder();
60         
61         if (subjectOrObject != null && object != null) {
62                 // Used for GET requests like: cspace-services/collectionobjects?mkRtSbjOrObj=cf5db000-4e65-42d5-8117
63                 //
64                 // (Example,    ((rel.subjectcsid = subject AND rel.objectcsid = target)
65                 //                                      OR 
66                 //                              (rel.subjectcsid = target AND rel.objectcsid = subject))
67                 //
68                 String target = object;
69                 stringBuilder.append("(");
70                 stringBuilder.append("(" + RelationConstants.NUXEO_SCHEMA_NAME + ":" +
71                                 RelationJAXBSchema.SUBJECT_CSID + " = " + "'" + subjectOrObject + "'");
72                 stringBuilder.append(" AND " + RelationConstants.NUXEO_SCHEMA_NAME + ":" +
73                                 RelationJAXBSchema.OBJECT_CSID + " = " + "'" + target + "'" + ")");
74                 stringBuilder.append(" OR ");
75                 stringBuilder.append("(" + RelationConstants.NUXEO_SCHEMA_NAME + ":" +
76                                 RelationJAXBSchema.SUBJECT_CSID + " = " + "'" + target + "'");
77                 stringBuilder.append(" AND " + RelationConstants.NUXEO_SCHEMA_NAME + ":" +
78                                 RelationJAXBSchema.OBJECT_CSID + " = " + "'" + subjectOrObject + "'" + ")");
79                 stringBuilder.append(")");
80         } else if (subjectOrObject != null) {
81                 // Used for GET requests like: cspace-services/relations?sbjOrObj=cf5db000-4e65-42d5-8117
82                 //
83                 // (subectCsid = ${csid} OR objectCsid = ${csid}) overrides the individual subject or object query params
84                 // (Example,    (rel.subjectcsid = subjectOrObject      OR rel.objectcsid = subjectOrObject)
85                 //
86                 stringBuilder.append("(" + RelationConstants.NUXEO_SCHEMA_NAME + ":" +
87                                 RelationJAXBSchema.SUBJECT_CSID + " = " + "'" + subjectOrObject + "'");
88                 stringBuilder.append(" OR ");
89                 stringBuilder.append(RelationConstants.NUXEO_SCHEMA_NAME + ":" +
90                                 RelationJAXBSchema.OBJECT_CSID + " = " + "'" + subjectOrObject + "')");
91         } else {
92                 // Used for GET requests like: cspace-services/relations?sbj=cf5db000-4e65-42d5-8117
93                 // and cspace-services/relations?obj=cf5db000-4e65-42d5-8117
94                 //
95                 if (subject != null) {
96                         if (stringBuilder.length() > 0) {
97                                 stringBuilder.append(IQueryManager.SEARCH_QUALIFIER_AND);
98                         }
99
100                         stringBuilder.append(RelationConstants.NUXEO_SCHEMA_NAME + ":" +
101                                         RelationJAXBSchema.SUBJECT_CSID + " = " + "'" + subject + "'");
102                 }
103                 
104                 if (object != null) {
105                         if (stringBuilder.length() > 0) {
106                                 stringBuilder.append(IQueryManager.SEARCH_QUALIFIER_AND);
107                         }
108                         stringBuilder.append(RelationConstants.NUXEO_SCHEMA_NAME + ":" +
109                                         RelationJAXBSchema.OBJECT_CSID + " = " + "'" + object + "'");
110                 }               
111         }
112         
113         //
114         // Check for the other possible query params
115         //
116         if (subjectType != null) {
117                 if (stringBuilder.length() > 0) {
118                         stringBuilder.append(IQueryManager.SEARCH_QUALIFIER_AND);
119                 }
120                 stringBuilder.append(RelationConstants.NUXEO_SCHEMA_NAME + ":" +
121                                 RelationJAXBSchema.SUBJECT_DOCTYPE + " = " + "'" + subjectType + "'");
122         }
123         
124         if (predicate != null) {
125                 if (stringBuilder.length() > 0) {
126                         stringBuilder.append(IQueryManager.SEARCH_QUALIFIER_AND);
127                 }
128                 stringBuilder.append(RelationConstants.NUXEO_SCHEMA_NAME + ":" +
129                                 RelationJAXBSchema.RELATIONSHIP_TYPE + " = " + "'" + predicate + "'");
130         }
131                 
132         if (objectType != null) {
133                 if (stringBuilder.length() > 0) {
134                         stringBuilder.append(IQueryManager.SEARCH_QUALIFIER_AND);
135                 }
136                 // BUG - this should use the new field RelationJAXBSchema.OBJECT_DOCTYPE
137                 stringBuilder.append(RelationConstants.NUXEO_SCHEMA_NAME + ":" +
138                                 RelationJAXBSchema.OBJECT_DOCTYPE + " = " + "'" + objectType + "'");
139         }
140         
141         if (stringBuilder.length() > 0) {
142                 result = stringBuilder.toString();
143         }
144         
145         return result;
146     }
147 }
148