]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
1f93a349bd96ba12a475576d5874381aa600b095
[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         //
62         // (subectCsid = ${csid} OR objectCsid = ${csid}) overrides the individual subject or object query params
63         // (Example,    ((rel.subjectcsid = subject AND rel.objectcsid = target)
64         //                                      OR 
65         //                              (rel.subjectcsid = target AND rel.objectcsid = subject))
66         //
67         if (subjectOrObject != null) {
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
81         } else {
82                 if (subject != null) {
83                         if (stringBuilder.length() > 0) {
84                                 stringBuilder.append(IQueryManager.SEARCH_QUALIFIER_AND);
85                         }
86
87                         stringBuilder.append(RelationConstants.NUXEO_SCHEMA_NAME + ":" +
88                                         RelationJAXBSchema.SUBJECT_CSID + " = " + "'" + subject + "'");
89                 }
90                 
91                 if (object != null) {
92                         if (stringBuilder.length() > 0) {
93                                 stringBuilder.append(IQueryManager.SEARCH_QUALIFIER_AND);
94                         }
95                         stringBuilder.append(RelationConstants.NUXEO_SCHEMA_NAME + ":" +
96                                         RelationJAXBSchema.OBJECT_CSID + " = " + "'" + object + "'");
97                 }               
98         }
99         
100         //
101         // Check for the other possible query params
102         //
103         if (subjectType != null) {
104                 if (stringBuilder.length() > 0) {
105                         stringBuilder.append(IQueryManager.SEARCH_QUALIFIER_AND);
106                 }
107                 stringBuilder.append(RelationConstants.NUXEO_SCHEMA_NAME + ":" +
108                                 RelationJAXBSchema.SUBJECT_DOCTYPE + " = " + "'" + subjectType + "'");
109         }
110         
111         if (predicate != null) {
112                 if (stringBuilder.length() > 0) {
113                         stringBuilder.append(IQueryManager.SEARCH_QUALIFIER_AND);
114                 }
115                 stringBuilder.append(RelationConstants.NUXEO_SCHEMA_NAME + ":" +
116                                 RelationJAXBSchema.RELATIONSHIP_TYPE + " = " + "'" + predicate + "'");
117         }
118                 
119         if (objectType != null) {
120                 if (stringBuilder.length() > 0) {
121                         stringBuilder.append(IQueryManager.SEARCH_QUALIFIER_AND);
122                 }
123                 // BUG - this should use the new field RelationJAXBSchema.OBJECT_DOCTYPE
124                 stringBuilder.append(RelationConstants.NUXEO_SCHEMA_NAME + ":" +
125                                 RelationJAXBSchema.OBJECT_DOCTYPE + " = " + "'" + objectType + "'");
126         }
127         
128         if (stringBuilder.length() > 0) {
129                 result = stringBuilder.toString();
130         }
131         
132         return result;
133     }
134 }
135