From 00f6ae4c97bf97c338a88cea14a2c5446bc0498e Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Thu, 29 Apr 2010 08:34:52 +0000
Subject: [PATCH] LMS-1445 Query API

SVN: 15667
---
 .../query/client/api/v1/QueryApiFacade.java   | 64 +++++++++++++
 .../query/client/api/v1/QueryApiTest.java     | 51 ++++++++++
 .../query/server/api/v1/QueryApiLogger.java   | 59 ++++++++++++
 .../query/server/api/v1/QueryApiServer.java   | 80 ++++++++++++++++
 .../server/api/v1/QueryServiceServer.java     | 47 ++++++++++
 .../query/server/api/v1/ResourceNames.java    | 30 ++++++
 .../query/shared/api/v1/IQueryApiServer.java  | 60 ++++++++++++
 .../shared/api/v1/dto/QueryDescription.java   | 93 +++++++++++++++++++
 .../shared/api/v1/dto/QueryTableModel.java    | 30 ++++++
 9 files changed, 514 insertions(+)
 create mode 100644 openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/client/api/v1/QueryApiFacade.java
 create mode 100644 openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/client/api/v1/QueryApiTest.java
 create mode 100644 openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/server/api/v1/QueryApiLogger.java
 create mode 100644 openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/server/api/v1/QueryApiServer.java
 create mode 100644 openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/server/api/v1/QueryServiceServer.java
 create mode 100644 openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/server/api/v1/ResourceNames.java
 create mode 100644 openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/shared/api/v1/IQueryApiServer.java
 create mode 100644 openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/shared/api/v1/dto/QueryDescription.java
 create mode 100644 openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/shared/api/v1/dto/QueryTableModel.java

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/client/api/v1/QueryApiFacade.java b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/client/api/v1/QueryApiFacade.java
new file mode 100644
index 00000000000..c246d32d28b
--- /dev/null
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/client/api/v1/QueryApiFacade.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2010 ETH Zuerich, CISD
+ *
+ * Licensed 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 ch.systemsx.cisd.openbis.plugin.query.client.api.v1;
+
+import java.util.List;
+
+import ch.systemsx.cisd.common.spring.HttpInvokerUtils;
+import ch.systemsx.cisd.openbis.plugin.query.server.api.v1.ResourceNames;
+import ch.systemsx.cisd.openbis.plugin.query.shared.api.v1.IQueryApiServer;
+import ch.systemsx.cisd.openbis.plugin.query.shared.api.v1.dto.QueryDescription;
+
+/**
+ * 
+ *
+ * @author Franz-Josef Elmer
+ */
+public class QueryApiFacade
+{
+    private static final int SERVER_TIMEOUT_MIN = 5;
+    
+    public static QueryApiFacade create(String serverURL, String userID, String password)
+    {
+        IQueryApiServer service = HttpInvokerUtils.createServiceStub(IQueryApiServer.class, serverURL + ResourceNames.QUERY_PLUGIN_SERVER_URL, SERVER_TIMEOUT_MIN);
+        String sessionToken = service.tryToAuthenticateAtQueryServer(userID, password);
+        if (sessionToken == null)
+        {
+            throw new IllegalArgumentException("User " + userID + "couldn't be authenticated");
+        }
+        return new QueryApiFacade(service, sessionToken);
+    }
+    
+    private final IQueryApiServer service;
+    private final String sessionToken;
+    
+    QueryApiFacade(IQueryApiServer service, String sessionToken)
+    {
+        this.service = service;
+        this.sessionToken = sessionToken;
+    }
+    
+    public void logout()
+    {
+        service.logout(sessionToken);
+    }
+    
+    public List<QueryDescription> listQueries()
+    {
+        return service.listQueries(sessionToken);
+    }
+}
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/client/api/v1/QueryApiTest.java b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/client/api/v1/QueryApiTest.java
new file mode 100644
index 00000000000..d111a5d5a84
--- /dev/null
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/client/api/v1/QueryApiTest.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2010 ETH Zuerich, CISD
+ *
+ * Licensed 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 ch.systemsx.cisd.openbis.plugin.query.client.api.v1;
+
+import java.util.List;
+
+import ch.systemsx.cisd.openbis.plugin.query.shared.api.v1.dto.QueryDescription;
+
+/**
+ * 
+ *
+ * @author Franz-Josef Elmer
+ */
+public class QueryApiTest
+{
+    public static void main(String[] args)
+    {
+        if (args.length != 3)
+        {
+            System.err.println("Usage: <openbis-server-url> <user> <password>");
+            return;
+        }
+
+        String serverURL = args[0];
+        String userID = args[1];
+        String password = args[2];
+        
+        QueryApiFacade facade = QueryApiFacade.create(serverURL, userID, password);
+        
+        List<QueryDescription> queries = facade.listQueries();
+        for (QueryDescription queryDescription : queries)
+        {
+            System.out.println(queryDescription.getName()+": "+queryDescription.getParameters());
+        }
+
+    }
+}
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/server/api/v1/QueryApiLogger.java b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/server/api/v1/QueryApiLogger.java
new file mode 100644
index 00000000000..cb34fabbcaf
--- /dev/null
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/server/api/v1/QueryApiLogger.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2010 ETH Zuerich, CISD
+ *
+ * Licensed 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 ch.systemsx.cisd.openbis.plugin.query.server.api.v1;
+
+import java.util.List;
+import java.util.Map;
+
+import ch.systemsx.cisd.authentication.ISessionManager;
+import ch.systemsx.cisd.common.spring.IInvocationLoggerContext;
+import ch.systemsx.cisd.openbis.generic.server.AbstractServerLogger;
+import ch.systemsx.cisd.openbis.generic.shared.dto.Session;
+import ch.systemsx.cisd.openbis.plugin.query.shared.api.v1.IQueryApiServer;
+import ch.systemsx.cisd.openbis.plugin.query.shared.api.v1.dto.QueryDescription;
+import ch.systemsx.cisd.openbis.plugin.query.shared.api.v1.dto.QueryTableModel;
+
+/**
+ * 
+ *
+ * @author Franz-Josef Elmer
+ */
+class QueryApiLogger extends AbstractServerLogger implements IQueryApiServer
+{
+
+    QueryApiLogger(ISessionManager<Session> sessionManager, IInvocationLoggerContext context)
+    {
+        super(sessionManager, context);
+    }
+
+    public QueryTableModel executeQuery(long queryID, Map<String, String> parameterBindings)
+    {
+        return null;
+    }
+
+    public List<QueryDescription> listQueries(String sessionToken)
+    {
+        logAccess(sessionToken, "list_queries");
+        return null;
+    }
+
+    public String tryToAuthenticateAtQueryServer(String userID, String userPassword)
+    {
+        return null;
+    }
+
+}
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/server/api/v1/QueryApiServer.java b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/server/api/v1/QueryApiServer.java
new file mode 100644
index 00000000000..49da8e31519
--- /dev/null
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/server/api/v1/QueryApiServer.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2010 ETH Zuerich, CISD
+ *
+ * Licensed 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 ch.systemsx.cisd.openbis.plugin.query.server.api.v1;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import javax.annotation.Resource;
+
+import org.springframework.stereotype.Component;
+
+import ch.systemsx.cisd.common.spring.IInvocationLoggerContext;
+import ch.systemsx.cisd.openbis.generic.server.AbstractServer;
+import ch.systemsx.cisd.openbis.generic.shared.dto.SessionContextDTO;
+import ch.systemsx.cisd.openbis.plugin.query.shared.IQueryServer;
+import ch.systemsx.cisd.openbis.plugin.query.shared.api.v1.IQueryApiServer;
+import ch.systemsx.cisd.openbis.plugin.query.shared.api.v1.dto.QueryDescription;
+import ch.systemsx.cisd.openbis.plugin.query.shared.api.v1.dto.QueryTableModel;
+import ch.systemsx.cisd.openbis.plugin.query.shared.basic.dto.QueryExpression;
+
+/**
+ * 
+ *
+ * @author Franz-Josef Elmer
+ */
+@Component(ResourceNames.QUERY_PLUGIN_SERVER)
+public class QueryApiServer extends AbstractServer<IQueryApiServer> implements IQueryApiServer
+{
+    @Resource(name = ch.systemsx.cisd.openbis.plugin.query.shared.ResourceNames.QUERY_PLUGIN_SERVER)
+    private IQueryServer queryServer;
+    
+    public IQueryApiServer createLogger(IInvocationLoggerContext context)
+    {
+        return new QueryApiLogger(sessionManager, context);
+    }
+
+    public String tryToAuthenticateAtQueryServer(String userID, String userPassword)
+    {
+        SessionContextDTO session = tryToAuthenticate(userID, userPassword);
+        return session == null ? null : session.getSessionToken();
+    }
+    
+    public List<QueryDescription> listQueries(String sessionToken)
+    {
+        List<QueryDescription> result = new ArrayList<QueryDescription>();
+        List<QueryExpression> queries = queryServer.listQueries(sessionToken);
+        for (QueryExpression queryExpression : queries)
+        {
+            QueryDescription queryDescription = new QueryDescription();
+            queryDescription.setId(queryExpression.getId());
+            queryDescription.setName(queryExpression.getName());
+            queryDescription.setDescription(queryExpression.getDescription());
+            queryDescription.setParameters(queryExpression.getParameters());
+            result.add(queryDescription);
+        }
+        return result;
+    }
+
+    public QueryTableModel executeQuery(long queryID, Map<String, String> parameterBindings)
+    {
+        // TODO Auto-generated method stub
+        return null;
+    }
+    
+}
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/server/api/v1/QueryServiceServer.java b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/server/api/v1/QueryServiceServer.java
new file mode 100644
index 00000000000..97e16d43a28
--- /dev/null
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/server/api/v1/QueryServiceServer.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2010 ETH Zuerich, CISD
+ *
+ * Licensed 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 ch.systemsx.cisd.openbis.plugin.query.server.api.v1;
+
+import javax.annotation.Resource;
+
+import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+import ch.systemsx.cisd.openbis.plugin.query.shared.api.v1.IQueryApiServer;
+
+/**
+ * 
+ *
+ * @author Franz-Josef Elmer
+ */
+@Controller
+@RequestMapping(
+    { ResourceNames.QUERY_PLUGIN_SERVER_URL, "/openbis" + ResourceNames.QUERY_PLUGIN_SERVER_URL })
+public class QueryServiceServer extends HttpInvokerServiceExporter
+{
+    @Resource(name = ResourceNames.QUERY_PLUGIN_SERVER)
+    private IQueryApiServer server;
+
+    @Override
+    public void afterPropertiesSet()
+    {
+        setServiceInterface(IQueryApiServer.class);
+        setService(server);
+        super.afterPropertiesSet();
+    }
+}
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/server/api/v1/ResourceNames.java b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/server/api/v1/ResourceNames.java
new file mode 100644
index 00000000000..91052e1adc1
--- /dev/null
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/server/api/v1/ResourceNames.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2010 ETH Zuerich, CISD
+ *
+ * Licensed 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 ch.systemsx.cisd.openbis.plugin.query.server.api.v1;
+
+/**
+ * 
+ *
+ * @author Franz-Josef Elmer
+ */
+public class ResourceNames
+{
+    public final static String QUERY_PLUGIN_SERVER = "query-api-server-v1";
+
+    public final static String QUERY_PLUGIN_SERVER_URL = "/rmi-query-v1";
+
+}
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/shared/api/v1/IQueryApiServer.java b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/shared/api/v1/IQueryApiServer.java
new file mode 100644
index 00000000000..729268f3ecd
--- /dev/null
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/shared/api/v1/IQueryApiServer.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2010 ETH Zuerich, CISD
+ *
+ * Licensed 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 ch.systemsx.cisd.openbis.plugin.query.shared.api.v1;
+
+import java.util.List;
+import java.util.Map;
+
+import org.springframework.transaction.annotation.Transactional;
+
+import ch.systemsx.cisd.openbis.plugin.query.shared.api.v1.dto.QueryDescription;
+import ch.systemsx.cisd.openbis.plugin.query.shared.api.v1.dto.QueryTableModel;
+
+/**
+ * Public API interface to query server (version 1). 
+ *
+ * @author Franz-Josef Elmer
+ */
+// DO NOT CHANGE THE INTERFACE CONTRACT IN A NON-BACKWARD COMPATIBLE WAY!
+public interface IQueryApiServer
+{
+    /**
+     * Tries to authenticate specified user with specified password. Returns session token if
+     * succeeded otherwise <code>null</code> is returned.
+     */
+    @Transactional    // this is not a readOnly transaction - it can create new users
+    public String tryToAuthenticateAtQueryServer(String userID, String userPassword);
+    
+    /**
+     * Logout the session with the specified session token.
+     */
+    @Transactional(readOnly = true)
+    public void logout(String sessionToken);
+    
+    /**
+     * Lists all queries available for the user of the specified session.
+     */
+    @Transactional(readOnly = true)
+    public List<QueryDescription> listQueries(String sessionToken);
+
+    /**
+     * Executes specified query using specified parameter bindings.
+     */
+    @Transactional(readOnly = true)
+    public QueryTableModel executeQuery(long queryID, Map<String, String> parameterBindings);
+
+}
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/shared/api/v1/dto/QueryDescription.java b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/shared/api/v1/dto/QueryDescription.java
new file mode 100644
index 00000000000..16761123916
--- /dev/null
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/shared/api/v1/dto/QueryDescription.java
@@ -0,0 +1,93 @@
+/*
+ * Copyright 2010 ETH Zuerich, CISD
+ *
+ * Licensed 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 ch.systemsx.cisd.openbis.plugin.query.shared.api.v1.dto;
+
+import java.io.Serializable;
+import java.util.List;
+
+/**
+ * Description of a query. Contains everything needed on client side to show to the user what
+ * queries are available, to specify parameter bindings, and to identify a query uniquely.
+ * 
+ * @author Franz-Josef Elmer
+ */
+public class QueryDescription implements Serializable
+{
+    private static final long serialVersionUID = 1L;
+
+    private long id;
+    
+    private String name;
+    
+    private String description;
+    
+    private List<String> parameters;
+
+    /**
+     * Returns the ID of the query. Will be used to identify the query to be executed.
+     */
+    public long getId()
+    {
+        return id;
+    }
+
+    public void setId(long id)
+    {
+        this.id = id;
+    }
+
+    /**
+     * Returns the name of the query.
+     */
+    public String getName()
+    {
+        return name;
+    }
+
+    public void setName(String name)
+    {
+        this.name = name;
+    }
+
+    /**
+     * Returns an optional description or empty string if undefined.
+     */
+    public String getDescription()
+    {
+        return description == null ? "" : description;
+    }
+
+    public void setDescription(String description)
+    {
+        this.description = description;
+    }
+
+    /**
+     * Returns the list of parameters to which values should be bound when executing the query.
+     * 
+     * @return an empty list if there are no parameters.
+     */
+    public List<String> getParameters()
+    {
+        return parameters;
+    }
+
+    public void setParameters(List<String> parameters)
+    {
+        this.parameters = parameters;
+    }
+}
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/shared/api/v1/dto/QueryTableModel.java b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/shared/api/v1/dto/QueryTableModel.java
new file mode 100644
index 00000000000..794617541d8
--- /dev/null
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/query/shared/api/v1/dto/QueryTableModel.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2010 ETH Zuerich, CISD
+ *
+ * Licensed 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 ch.systemsx.cisd.openbis.plugin.query.shared.api.v1.dto;
+
+import java.io.Serializable;
+
+/**
+ * 
+ *
+ * @author Franz-Josef Elmer
+ */
+public class QueryTableModel implements Serializable
+{
+    private static final long serialVersionUID = 1L;
+
+}
-- 
GitLab