Revision: 1987
Author: [email protected]
Date: Fri Jul 30 04:59:09 2010
Log: Added support for REST call /allCategories to return all categories as JSON (Issue 329)
http://code.google.com/p/simal/source/detail?r=1987

Added:
/trunk/uk.ac.osswatch.simal.rest/src/main/java/uk/ac/osswatch/simal/rest/CategoryAPIHandler.java
Modified:
/trunk/uk.ac.osswatch.simal.rest/src/main/java/uk/ac/osswatch/simal/rest/RESTCommand.java /trunk/uk.ac.osswatch.simal.rest/src/main/java/uk/ac/osswatch/simal/rest/SimalHandlerFactory.java

=======================================
--- /dev/null
+++ /trunk/uk.ac.osswatch.simal.rest/src/main/java/uk/ac/osswatch/simal/rest/CategoryAPIHandler.java Fri Jul 30 04:59:09 2010
@@ -0,0 +1,78 @@
+package uk.ac.osswatch.simal.rest;
+
+/*
+ * Copyright 2010 University of Oxford
+ *
+ * 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.                                                *
+ */
+
+import uk.ac.osswatch.simal.SimalRepositoryFactory;
+import uk.ac.osswatch.simal.rdf.SimalRepositoryException;
+
+/**
+ * A class for handling all API calls relating to categories.
+ *
+ */
+public class CategoryAPIHandler extends AbstractHandler {
+
+  /**
+   * Create a new CategoryAPI object to process the given command.
+   *
+   * @param cmd
+   * @throws SimalRepositoryException
+   */
+ protected CategoryAPIHandler(RESTCommand cmd) throws SimalRepositoryException {
+    super(cmd);
+  }
+
+  /**
+   * Execute a command.
+   *
+   * @param cmd
+   * @throws SimalAPIException
+   */
+  public String execute() throws SimalAPIException {
+    String execResult = null;
+
+    if (command.isGetAllCategories()) {
+      execResult = getAllCategories(command);
+    } else {
+      throw new SimalAPIException("Unknown command: " + command);
+    }
+
+    return execResult;
+  }
+
+/**
+   * Get all the categories from the repository.
+   *
+   * @param cmd
+   * @return
+   * @throws SimalAPIException
+   */
+ public String getAllCategories(RESTCommand cmd) throws SimalAPIException {
+    if (cmd.isJSON()) {
+      try {
+ return SimalRepositoryFactory.getCategoryService().getAllCategoriesAsJSON();
+      } catch (SimalRepositoryException e) {
+        throw new SimalAPIException(
+ "Unable to get JSON representation of all categories from the repository",
+            e);
+      }
+    } else {
+      throw new SimalAPIException("Unkown format requested - " + cmd);
+    }
+  }
+
+}
=======================================
--- /trunk/uk.ac.osswatch.simal.rest/src/main/java/uk/ac/osswatch/simal/rest/RESTCommand.java Fri Jul 23 15:48:15 2010 +++ /trunk/uk.ac.osswatch.simal.rest/src/main/java/uk/ac/osswatch/simal/rest/RESTCommand.java Fri Jul 30 04:59:09 2010
@@ -41,9 +41,12 @@
   private static final Logger LOGGER = LoggerFactory
       .getLogger(RESTCommand.class);

+  public static final String ALL_CATEGORIES = "/allCategories";
+
   public static final String ALL_PROJECTS = "/allProjects";
   public static final String GET_PROJECT = "/project";
   public static final String PROJECT_ADD = "/addProject";
+
   public static final String ALL_PEOPLE = "/allPeople";
   public static final String PERSON = "/person";
   public static final String ALL_COLLEAGUES = "/allColleagues";
@@ -487,6 +490,15 @@
   private void setProjectID(String id) {
     params.put(PARAM_PROJECT_ID, id);
   }
+
+  /**
+   * Return true if this command is a get all categories command.
+   *
+   * @return
+   */
+  public boolean isGetAllCategories() {
+    return (params.get(PARAM_METHOD).equals(ALL_CATEGORIES));
+  }

   /**
    * Return true if this command is a get all projects command.
@@ -628,5 +640,12 @@
   public boolean isGet() {
     return !isPost();
   }
+
+  /**
+   * @return
+   */
+  public boolean isCategoryCommand() {
+    return (isGetAllCategories());
+  }

 }
=======================================
--- /trunk/uk.ac.osswatch.simal.rest/src/main/java/uk/ac/osswatch/simal/rest/SimalHandlerFactory.java Thu Sep 18 14:58:49 2008 +++ /trunk/uk.ac.osswatch.simal.rest/src/main/java/uk/ac/osswatch/simal/rest/SimalHandlerFactory.java Fri Jul 30 04:59:09 2010
@@ -45,6 +45,8 @@
         handler = new ProjectAPI(command);
       } else if (command.isPersonCommand()) {
         handler = new PersonAPI(command);
+      } else if (command.isCategoryCommand()) {
+        handler = new CategoryAPIHandler(command);
       }
     } catch (SimalRepositoryException e) {
       throw new SimalAPIException("Unable to create handler", e);

--
You received this message because you are subscribed to the Google Groups "Simal 
Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/simal-commits?hl=en.

Reply via email to