You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@chemistry.apache.org by fm...@apache.org on 2011/05/12 16:21:45 UTC
svn commit: r1102324 - in
/chemistry/opencmis/trunk/chemistry-opencmis-client:
chemistry-opencmis-client-api/src/main/java/org/apache/chemistry/opencmis/client/api/
chemistry-opencmis-client-impl/src/main/java/org/apache/chemistry/opencmis/client/runtime/
Author: fmui
Date: Thu May 12 14:21:45 2011
New Revision: 1102324
URL: http://svn.apache.org/viewvc?rev=1102324&view=rev
Log:
CMIS-374: added QueryStatement
Added:
chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/main/java/org/apache/chemistry/opencmis/client/api/QueryStatement.java (with props)
chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/main/java/org/apache/chemistry/opencmis/client/runtime/QueryStatementImpl.java (with props)
Modified:
chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/main/java/org/apache/chemistry/opencmis/client/api/Session.java
chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/main/java/org/apache/chemistry/opencmis/client/runtime/SessionImpl.java
Added: chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/main/java/org/apache/chemistry/opencmis/client/api/QueryStatement.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/main/java/org/apache/chemistry/opencmis/client/api/QueryStatement.java?rev=1102324&view=auto
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/main/java/org/apache/chemistry/opencmis/client/api/QueryStatement.java (added)
+++ chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/main/java/org/apache/chemistry/opencmis/client/api/QueryStatement.java Thu May 12 14:21:45 2011
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.chemistry.opencmis.client.api;
+
+import java.net.URI;
+import java.net.URL;
+import java.util.Calendar;
+import java.util.Date;
+
+import org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition;
+
+/**
+ * Query Statement.
+ *
+ * <p>
+ * Example: <blockquote>
+ *
+ * <pre>
+ * Calendar cal = ...
+ * Folder folder = ...
+ *
+ * QueryStatement qs = session.createQueryStatement("SELECT ?, ? FROM ? WHERE ? > ? AND IN_FOLDER(?) OR ? IN (?)");
+ *
+ * qs.setProperty(1, "cmis:document", "cmis:name");
+ * qs.setProperty(2, "cmis:document", "cmis:objectId");
+ * qs.setType(3, "cmis:document");
+ *
+ * qs.setProperty(4, "cmis:document", "cmis:creationDate");
+ * qs.setDateTime(5, cal);
+ *
+ * qs.setId(6, folder);
+ *
+ * qs.setProperty(7, "cmis:document", "cmis:createdBy");
+ * qs.setString(8, "bob", "tom", "lisa");
+ *
+ * String statement = qs.toQueryString();
+ * </pre>
+ *
+ * </blockquote>
+ * </p>
+ */
+public interface QueryStatement {
+
+ /**
+ * Sets the designated parameter to the query name of the given type id.
+ */
+ void setType(int parameterIndex, String typeId);
+
+ /**
+ * Sets the designated parameter to the query name of the given type.
+ */
+ void setType(int parameterIndex, ObjectType type);
+
+ /**
+ * Sets the designated parameter to the query name of the given property.
+ */
+ void setProperty(int parameterIndex, String typeId, String propertyId);
+
+ /**
+ * Sets the designated parameter to the query name of the given property.
+ */
+ void setProperty(int parameterIndex, PropertyDefinition<?> propertyDefinition);
+
+ /**
+ * Sets the designated parameter to the given number.
+ */
+ void setNumber(int parameterIndex, Number... num);
+
+ /**
+ * Sets the designated parameter to the given string.
+ */
+ void setString(int parameterIndex, String... str);
+
+ /**
+ * Sets the designated parameter to the given string. It does not escape
+ * backslashes ('\') in front of "%' and '_'.
+ */
+ void setStringLike(int parameterIndex, String str);
+
+ /**
+ * Sets the designated parameter to the given object id.
+ */
+ void setId(int parameterIndex, ObjectId... id);
+
+ /**
+ * Sets the designated parameter to the given URI.
+ */
+ void setUri(int parameterIndex, URI... uri);
+
+ /**
+ * Sets the designated parameter to the given URL.
+ */
+ void setUrl(int parameterIndex, URL... url);
+
+ /**
+ * Sets the designated parameter to the given boolean.
+ */
+ void setBoolean(int parameterIndex, boolean... bool);
+
+ /**
+ * Sets the designated parameter to the given timestamp.
+ */
+ void setDateTime(int parameterIndex, Calendar... cal);
+
+ /**
+ * Sets the designated parameter to the given timestamp.
+ */
+ void setDateTime(int parameterIndex, Date... date);
+
+ /**
+ * Sets the designated parameter to the given timestamp.
+ */
+ void setDateTime(int parameterIndex, long... ms);
+
+ /**
+ * Returns the query statement.
+ */
+ String toQueryString();
+
+ /**
+ * Executes the query.
+ *
+ * @see Session#query(String, boolean)
+ */
+ ItemIterable<QueryResult> query(boolean searchAllVersions);
+
+ /**
+ * Executes the query.
+ *
+ * @see Session#query(String, boolean, OperationContext)
+ */
+ ItemIterable<QueryResult> query(boolean searchAllVersions, OperationContext context);
+}
Propchange: chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/main/java/org/apache/chemistry/opencmis/client/api/QueryStatement.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/main/java/org/apache/chemistry/opencmis/client/api/Session.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/main/java/org/apache/chemistry/opencmis/client/api/Session.java?rev=1102324&r1=1102323&r2=1102324&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/main/java/org/apache/chemistry/opencmis/client/api/Session.java (original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-api/src/main/java/org/apache/chemistry/opencmis/client/api/Session.java Thu May 12 14:21:45 2011
@@ -290,6 +290,16 @@ public interface Session {
ItemIterable<QueryResult> query(String statement, boolean searchAllVersions, OperationContext context);
/**
+ * Creates a query statement.
+ *
+ * @param statement
+ * the query statement with placeholders ('?').
+ *
+ * @see QueryStatement
+ */
+ QueryStatement createQueryStatement(String statement);
+
+ /**
* Returns the content changes.
*
* @param changeLogToken
Added: chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/main/java/org/apache/chemistry/opencmis/client/runtime/QueryStatementImpl.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/main/java/org/apache/chemistry/opencmis/client/runtime/QueryStatementImpl.java?rev=1102324&view=auto
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/main/java/org/apache/chemistry/opencmis/client/runtime/QueryStatementImpl.java (added)
+++ chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/main/java/org/apache/chemistry/opencmis/client/runtime/QueryStatementImpl.java Thu May 12 14:21:45 2011
@@ -0,0 +1,378 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.chemistry.opencmis.client.runtime;
+
+import java.net.URI;
+import java.net.URL;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.TimeZone;
+
+import org.apache.chemistry.opencmis.client.api.ItemIterable;
+import org.apache.chemistry.opencmis.client.api.ObjectId;
+import org.apache.chemistry.opencmis.client.api.ObjectType;
+import org.apache.chemistry.opencmis.client.api.OperationContext;
+import org.apache.chemistry.opencmis.client.api.QueryResult;
+import org.apache.chemistry.opencmis.client.api.QueryStatement;
+import org.apache.chemistry.opencmis.client.api.Session;
+import org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition;
+
+/**
+ * QueryStatement implementation.
+ */
+public class QueryStatementImpl implements QueryStatement {
+
+ private final Session session;
+ private final String statement;
+ private final Map<Integer, String> parametersMap;
+
+ public QueryStatementImpl(Session session, String statement) {
+ if (session == null) {
+ throw new IllegalArgumentException("Session must be set!");
+ }
+
+ if (statement == null) {
+ throw new IllegalArgumentException("Statement must be set!");
+ }
+
+ this.session = session;
+ this.statement = statement.trim();
+
+ parametersMap = new HashMap<Integer, String>();
+ }
+
+ public void setType(int parameterIndex, String typeId) {
+ setType(parameterIndex, session.getTypeDefinition(typeId));
+ }
+
+ public void setType(int parameterIndex, ObjectType type) {
+ if (type == null) {
+ throw new IllegalArgumentException("Type must be set!");
+ }
+
+ String queryName = type.getQueryName();
+ if (queryName == null) {
+ throw new IllegalArgumentException("Type has no query name!");
+ }
+
+ parametersMap.put(parameterIndex, queryName);
+ }
+
+ public void setProperty(int parameterIndex, String typeId, String propertyId) {
+ ObjectType type = session.getTypeDefinition(typeId);
+
+ PropertyDefinition<?> propertyDefinition = type.getPropertyDefinitions().get(propertyId);
+ if (propertyDefinition == null) {
+ throw new IllegalArgumentException("Property does not exist!");
+ }
+
+ setProperty(parameterIndex, propertyDefinition);
+ }
+
+ public void setProperty(int parameterIndex, PropertyDefinition<?> propertyDefinition) {
+ if (propertyDefinition == null) {
+ throw new IllegalArgumentException("Property must be set!");
+ }
+
+ String queryName = propertyDefinition.getQueryName();
+ if (queryName == null) {
+ throw new IllegalArgumentException("Property has no query name!");
+ }
+
+ parametersMap.put(parameterIndex, queryName);
+ }
+
+ public void setNumber(int parameterIndex, Number... num) {
+ if (num == null || num.length == 0) {
+ throw new IllegalArgumentException("Number must be set!");
+ }
+
+ StringBuilder sb = new StringBuilder();
+ for (Number n : num) {
+ if (n == null) {
+ throw new IllegalArgumentException("Number is null!");
+ }
+
+ if (sb.length() > 0) {
+ sb.append(",");
+ }
+
+ sb.append(n.toString());
+ }
+
+ parametersMap.put(parameterIndex, sb.toString());
+ }
+
+ public void setString(int parameterIndex, String... str) {
+ if (str == null || str.length == 0) {
+ throw new IllegalArgumentException("String must be set!");
+ }
+
+ StringBuilder sb = new StringBuilder();
+ for (String s : str) {
+ if (s == null) {
+ throw new IllegalArgumentException("String is null!");
+ }
+
+ if (sb.length() > 0) {
+ sb.append(",");
+ }
+
+ sb.append(escape(s));
+ }
+
+ parametersMap.put(parameterIndex, sb.toString());
+ }
+
+ public void setStringLike(int parameterIndex, String str) {
+ if (str == null) {
+ throw new IllegalArgumentException("String must be set!");
+ }
+
+ parametersMap.put(parameterIndex, escapeLike(str));
+ }
+
+ public void setId(int parameterIndex, ObjectId... id) {
+ if (id == null || id.length == 0) {
+ throw new IllegalArgumentException("Id must be set!");
+ }
+
+ StringBuilder sb = new StringBuilder();
+ for (ObjectId oid : id) {
+ if (oid == null || oid.getId() == null) {
+ throw new IllegalArgumentException("Id is null!");
+ }
+
+ if (sb.length() > 0) {
+ sb.append(",");
+ }
+
+ sb.append(escape(oid.getId()));
+ }
+
+ parametersMap.put(parameterIndex, sb.toString());
+ }
+
+ public void setUri(int parameterIndex, URI... uri) {
+ if (uri == null) {
+ throw new IllegalArgumentException("URI must be set!");
+ }
+
+ StringBuilder sb = new StringBuilder();
+ for (URI u : uri) {
+ if (u == null) {
+ throw new IllegalArgumentException("URI is null!");
+ }
+
+ if (sb.length() > 0) {
+ sb.append(",");
+ }
+
+ sb.append(escape(u.toString()));
+ }
+
+ parametersMap.put(parameterIndex, sb.toString());
+ }
+
+ public void setUrl(int parameterIndex, URL... url) {
+ if (url == null) {
+ throw new IllegalArgumentException("URL must be set!");
+ }
+
+ StringBuilder sb = new StringBuilder();
+ for (URL u : url) {
+ if (u == null) {
+ throw new IllegalArgumentException("URI is null!");
+ }
+
+ if (sb.length() > 0) {
+ sb.append(",");
+ }
+
+ sb.append(escape(u.toString()));
+ }
+
+ parametersMap.put(parameterIndex, sb.toString());
+ }
+
+ public void setBoolean(int parameterIndex, boolean... bool) {
+ if (bool == null || bool.length == 0) {
+ throw new IllegalArgumentException("Boolean must not be set!");
+ }
+
+ StringBuilder sb = new StringBuilder();
+ for (boolean b : bool) {
+ if (sb.length() > 0) {
+ sb.append(",");
+ }
+
+ sb.append(b ? "TRUE" : "FALSE");
+ }
+
+ parametersMap.put(parameterIndex, sb.toString());
+ }
+
+ public void setDateTime(int parameterIndex, Calendar... cal) {
+ if (cal == null || cal.length == 0) {
+ throw new IllegalArgumentException("Calendar must be set!");
+ }
+
+ StringBuilder sb = new StringBuilder();
+ for (Calendar c : cal) {
+ if (c == null) {
+ throw new IllegalArgumentException("DateTime is null!");
+ }
+
+ if (sb.length() > 0) {
+ sb.append(",");
+ }
+
+ sb.append(convert(c.getTime()));
+ }
+
+ parametersMap.put(parameterIndex, sb.toString());
+ }
+
+ public void setDateTime(int parameterIndex, Date... date) {
+ if (date == null || date.length == 0) {
+ throw new IllegalArgumentException("Date must be set!");
+ }
+
+ StringBuilder sb = new StringBuilder();
+ for (Date d : date) {
+ if (d == null) {
+ throw new IllegalArgumentException("DateTime is null!");
+ }
+
+ if (sb.length() > 0) {
+ sb.append(",");
+ }
+
+ sb.append(convert(d));
+ }
+
+ parametersMap.put(parameterIndex, sb.toString());
+ }
+
+ public void setDateTime(int parameterIndex, long... ms) {
+ if (ms == null || ms.length == 0) {
+ throw new IllegalArgumentException("Timestamp must be set!");
+ }
+
+ StringBuilder sb = new StringBuilder();
+ for (long l : ms) {
+ if (sb.length() > 0) {
+ sb.append(",");
+ }
+
+ sb.append(convert(new Date(l)));
+ }
+
+ parametersMap.put(parameterIndex, sb.toString());
+ }
+
+ public String toQueryString() {
+ boolean inStr = false;
+ int parameterIndex = 0;
+
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < statement.length(); i++) {
+ char c = statement.charAt(i);
+
+ if (c == '\'') {
+ if (inStr && statement.charAt(i - 1) == '\\') {
+ inStr = true;
+ } else {
+ inStr = !inStr;
+ }
+ } else if (c == '?') {
+ parameterIndex++;
+ String s = parametersMap.get(parameterIndex);
+ if (s == null) {
+ sb.append(c);
+ } else {
+ sb.append(s);
+ }
+ } else {
+ sb.append(c);
+ }
+ }
+
+ return sb.toString();
+ }
+
+ public ItemIterable<QueryResult> query(boolean searchAllVersions) {
+ return session.query(toQueryString(), searchAllVersions);
+ }
+
+ public ItemIterable<QueryResult> query(boolean searchAllVersions, OperationContext context) {
+ return session.query(toQueryString(), searchAllVersions, context);
+ }
+
+ // --- internal ---
+
+ private static String escape(String str) {
+ StringBuilder sb = new StringBuilder("'");
+ for (int i = 0; i < str.length(); i++) {
+ char c = str.charAt(i);
+
+ if (c == '\'' || c == '\\') {
+ sb.append("\\");
+ }
+
+ sb.append(c);
+ }
+
+ sb.append("'");
+
+ return sb.toString();
+ }
+
+ private static String escapeLike(String str) {
+ StringBuilder sb = new StringBuilder("'");
+ for (int i = 0; i < str.length(); i++) {
+ char c = str.charAt(i);
+
+ if (c == '\'') {
+ sb.append("\\");
+ } else if (c == '\\') {
+ if (i + 1 < str.length() && (str.charAt(i + 1) == '%' || str.charAt(i + 1) == '_')) {
+ // no additional back slash
+ } else {
+ sb.append("\\");
+ }
+ }
+
+ sb.append(c);
+ }
+
+ sb.append("'");
+
+ return sb.toString();
+ }
+
+ private static String convert(Date date) {
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'");
+ sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
+
+ return "'" + sdf.format(date) + "'";
+ }
+}
Propchange: chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/main/java/org/apache/chemistry/opencmis/client/runtime/QueryStatementImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/main/java/org/apache/chemistry/opencmis/client/runtime/SessionImpl.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/main/java/org/apache/chemistry/opencmis/client/runtime/SessionImpl.java?rev=1102324&r1=1102323&r2=1102324&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/main/java/org/apache/chemistry/opencmis/client/runtime/SessionImpl.java (original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-impl/src/main/java/org/apache/chemistry/opencmis/client/runtime/SessionImpl.java Thu May 12 14:21:45 2011
@@ -40,6 +40,7 @@ import org.apache.chemistry.opencmis.cli
import org.apache.chemistry.opencmis.client.api.OperationContext;
import org.apache.chemistry.opencmis.client.api.Policy;
import org.apache.chemistry.opencmis.client.api.QueryResult;
+import org.apache.chemistry.opencmis.client.api.QueryStatement;
import org.apache.chemistry.opencmis.client.api.Relationship;
import org.apache.chemistry.opencmis.client.api.Session;
import org.apache.chemistry.opencmis.client.api.Tree;
@@ -575,6 +576,10 @@ public class SessionImpl implements Sess
});
}
+ public QueryStatement createQueryStatement(final String statement) {
+ return new QueryStatementImpl(this, statement);
+ }
+
public String setExtensionContext(String context) {
throw new CmisRuntimeException("not implemented");
}
@@ -697,9 +702,9 @@ public class SessionImpl implements Sess
}
String newId = getBinding().getObjectService().createFolder(getRepositoryId(),
- objectFactory.convertProperties(properties, null, CREATE_UPDATABILITY),
- folderId.getId(), objectFactory.convertPolicies(policies),
- objectFactory.convertAces(addAces), objectFactory.convertAces(removeAces), null);
+ objectFactory.convertProperties(properties, null, CREATE_UPDATABILITY), folderId.getId(),
+ objectFactory.convertPolicies(policies), objectFactory.convertAces(addAces),
+ objectFactory.convertAces(removeAces), null);
if (newId == null) {
return null;