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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.services.common.relation.nuxeo;
26 import java.lang.StringBuilder;
28 import org.collectionspace.services.client.IQueryManager;
29 import org.collectionspace.services.common.relation.RelationJAXBSchema;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
37 * $LastChangedRevision: $
40 public class RelationsUtils {
42 /** The Constant logger. */
43 private static final Logger logger = LoggerFactory.getLogger(RelationsUtils.class);
46 * Builds the where clause.
48 * @param subject the subject
49 * @param predicate the predicate
50 * @param object the object
53 public static String buildWhereClause(String subject, String subjectType,
55 String object, String objectType,
56 String subjectOrObject) {
59 StringBuilder stringBuilder = new StringBuilder();
62 // (subectCsid = ${csid} OR objectCsid = ${csid}) overrides the individual subject or object query params
63 // (Example, ((rel.subjectcsid = subject AND rel.objectcsid = target)
65 // (rel.subjectcsid = target AND rel.objectcsid = subject))
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(")");
82 if (subject != null) {
83 if (stringBuilder.length() > 0) {
84 stringBuilder.append(IQueryManager.SEARCH_QUALIFIER_AND);
87 stringBuilder.append(RelationConstants.NUXEO_SCHEMA_NAME + ":" +
88 RelationJAXBSchema.SUBJECT_CSID + " = " + "'" + subject + "'");
92 if (stringBuilder.length() > 0) {
93 stringBuilder.append(IQueryManager.SEARCH_QUALIFIER_AND);
95 stringBuilder.append(RelationConstants.NUXEO_SCHEMA_NAME + ":" +
96 RelationJAXBSchema.OBJECT_CSID + " = " + "'" + object + "'");
101 // Check for the other possible query params
103 if (subjectType != null) {
104 if (stringBuilder.length() > 0) {
105 stringBuilder.append(IQueryManager.SEARCH_QUALIFIER_AND);
107 stringBuilder.append(RelationConstants.NUXEO_SCHEMA_NAME + ":" +
108 RelationJAXBSchema.SUBJECT_DOCTYPE + " = " + "'" + subjectType + "'");
111 if (predicate != null) {
112 if (stringBuilder.length() > 0) {
113 stringBuilder.append(IQueryManager.SEARCH_QUALIFIER_AND);
115 stringBuilder.append(RelationConstants.NUXEO_SCHEMA_NAME + ":" +
116 RelationJAXBSchema.RELATIONSHIP_TYPE + " = " + "'" + predicate + "'");
119 if (objectType != null) {
120 if (stringBuilder.length() > 0) {
121 stringBuilder.append(IQueryManager.SEARCH_QUALIFIER_AND);
123 // BUG - this should use the new field RelationJAXBSchema.OBJECT_DOCTYPE
124 stringBuilder.append(RelationConstants.NUXEO_SCHEMA_NAME + ":" +
125 RelationJAXBSchema.OBJECT_DOCTYPE + " = " + "'" + objectType + "'");
128 if (stringBuilder.length() > 0) {
129 result = stringBuilder.toString();