Revision: 1986
Author: [email protected]
Date: Fri Jul 30 04:51:47 2010
Log: Added and implemented method to ICategoryService to return all categories as JSON (Issue 329)
http://code.google.com/p/simal/source/detail?r=1986

Modified:
/trunk/uk.ac.osswatch.simal.core/src/main/java/uk/ac/osswatch/simal/model/jena/Category.java /trunk/uk.ac.osswatch.simal.core/src/main/java/uk/ac/osswatch/simal/service/ICategoryService.java /trunk/uk.ac.osswatch.simal.core/src/main/java/uk/ac/osswatch/simal/service/jena/JenaCategoryService.java /trunk/uk.ac.osswatch.simal.core/src/test/java/uk/ac/osswatch/simal/integrationTest/service/TestCategoryService.java

=======================================
--- /trunk/uk.ac.osswatch.simal.core/src/main/java/uk/ac/osswatch/simal/model/jena/Category.java Wed Oct 28 17:44:10 2009 +++ /trunk/uk.ac.osswatch.simal.core/src/main/java/uk/ac/osswatch/simal/model/jena/Category.java Fri Jul 30 04:51:47 2010
@@ -23,6 +23,7 @@
 import java.util.Iterator;
 import java.util.Set;

+import org.apache.commons.lang.StringEscapeUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;

@@ -121,4 +122,15 @@
     }
     return new HashSet<IPerson>(allPeople.values());
   }
-}
+
+  public String toJSONRecord() throws SimalRepositoryException {
+    StringBuffer json = new StringBuffer();
+    json.append("{");
+    json.append("\"id\":\"" + getURI() + "\",");
+    json.append("\"label\":\""
+        + StringEscapeUtils.escapeJavaScript(getLabel().trim()) + "\"");
+    json.append("}");
+    return json.toString();
+  }
+
+}
=======================================
--- /trunk/uk.ac.osswatch.simal.core/src/main/java/uk/ac/osswatch/simal/service/ICategoryService.java Tue Nov 24 16:08:08 2009 +++ /trunk/uk.ac.osswatch.simal.core/src/main/java/uk/ac/osswatch/simal/service/ICategoryService.java Fri Jul 30 04:51:47 2010
@@ -47,6 +47,14 @@
         */
        public IDoapCategory get(String uri) throws SimalRepositoryException;

+
+       /**
+        * Return all categories in JSON format.
+        * @return
+        * @throws SimalRepositoryException
+        */
+       public String getAllCategoriesAsJSON() throws SimalRepositoryException;
+
        /**
         * Get a project from the repository. If the project does not yet exist 
it
         * will be created.
=======================================
--- /trunk/uk.ac.osswatch.simal.core/src/main/java/uk/ac/osswatch/simal/service/jena/JenaCategoryService.java Sat Oct 24 15:29:35 2009 +++ /trunk/uk.ac.osswatch.simal.core/src/main/java/uk/ac/osswatch/simal/service/jena/JenaCategoryService.java Fri Jul 30 04:51:47 2010
@@ -18,6 +18,7 @@
  */

 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.Set;

 import org.slf4j.Logger;
@@ -174,5 +175,22 @@
                        }
                }
        }
+
+  /**
+ * @see uk.ac.osswatch.simal.service.ICategoryService#getAllCategoriesAsJSON()
+   */
+  public String getAllCategoriesAsJSON() throws SimalRepositoryException {
+    StringBuffer json = new StringBuffer("{ \"items\": [");
+    Iterator<IDoapCategory> categories = getAll().iterator();
+    IDoapCategory category;
+    while (categories.hasNext()) {
+      category = categories.next();
+      json.append(category.toJSONRecord());
+      if (categories.hasNext()) {
+        json.append(",");
+      }
+    }
+    json.append("]}");
+    return json.toString();  }

 }
=======================================
--- /trunk/uk.ac.osswatch.simal.core/src/test/java/uk/ac/osswatch/simal/integrationTest/service/TestCategoryService.java Wed Nov 25 14:23:42 2009 +++ /trunk/uk.ac.osswatch.simal.core/src/test/java/uk/ac/osswatch/simal/integrationTest/service/TestCategoryService.java Fri Jul 30 04:51:47 2010
@@ -69,6 +69,15 @@

            assertTrue("Got too few categories", 50 < cats.size());
          }
+
+    @Test
+ public void testGetAllCategoriesAsJSON() throws SimalRepositoryException,
+        IOException {
+ String catsJSON = SimalRepositoryFactory.getCategoryService().getAllCategoriesAsJSON();
+
+      int nrCategories = catsJSON.split("\"id\"").length;
+      assertEquals("Got wrong number of categories", 56, nrCategories);
+    }

          @Test
          public void testFindCategoryById() throws SimalRepositoryException {

--
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