Author: cgeer
Date: Fri Mar 29 07:59:06 2013
New Revision: 1462425
URL: http://svn.apache.org/r1462425
Log:
RAVE-934 - Categories Resource
Added:
rave/trunk/rave-components/rave-core-api/src/main/java/org/apache/rave/rest/CategoriesResource.java
rave/trunk/rave-components/rave-core-api/src/main/java/org/apache/rave/rest/model/Category.java
rave/trunk/rave-components/rave-core-api/src/main/java/org/apache/rave/rest/model/CategoryList.java
rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/rest/impl/DefaultCategoriesResource.java
Modified:
rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/service/impl/DefaultCategoryService.java
rave/trunk/rave-portal-resources/src/main/webapp/WEB-INF/cxf-applicationContext.xml
Added:
rave/trunk/rave-components/rave-core-api/src/main/java/org/apache/rave/rest/CategoriesResource.java
URL:
http://svn.apache.org/viewvc/rave/trunk/rave-components/rave-core-api/src/main/java/org/apache/rave/rest/CategoriesResource.java?rev=1462425&view=auto
==============================================================================
---
rave/trunk/rave-components/rave-core-api/src/main/java/org/apache/rave/rest/CategoriesResource.java
(added)
+++
rave/trunk/rave-components/rave-core-api/src/main/java/org/apache/rave/rest/CategoriesResource.java
Fri Mar 29 07:59:06 2013
@@ -0,0 +1,50 @@
+/*
+ * 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.rave.rest;
+
+import org.apache.rave.rest.model.Category;
+
+import javax.ws.rs.*;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
+
+@Path("/categories")
+public interface CategoriesResource {
+ @GET
+ @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
+ public Response getCategories();
+
+ @GET
+ @Path("/{id}")
+ @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
+ public Response getCategory(@PathParam("id") String id);
+
+ @PUT
+ @Path("/{id}")
+ @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
+ @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
+ public Response updateCategory(@PathParam("id") String id, Category
category, @Context UriInfo uri);
+
+ @POST
+ @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
+ @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
+ public Response createCategory(Category category);
+}
Added:
rave/trunk/rave-components/rave-core-api/src/main/java/org/apache/rave/rest/model/Category.java
URL:
http://svn.apache.org/viewvc/rave/trunk/rave-components/rave-core-api/src/main/java/org/apache/rave/rest/model/Category.java?rev=1462425&view=auto
==============================================================================
---
rave/trunk/rave-components/rave-core-api/src/main/java/org/apache/rave/rest/model/Category.java
(added)
+++
rave/trunk/rave-components/rave-core-api/src/main/java/org/apache/rave/rest/model/Category.java
Fri Mar 29 07:59:06 2013
@@ -0,0 +1,64 @@
+/*
+ * 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.rave.rest.model;
+
+import javax.xml.bind.annotation.*;
+
+@XmlAccessorType(XmlAccessType.FIELD)
+@XmlType(name = "Category", propOrder = {
+ "id", "text"
+})
+@XmlRootElement(name = "Category")
+public class Category {
+ @XmlAttribute(name = "id")
+ private String id;
+ @XmlElement(name = "Text")
+ private String text;
+
+ public Category() {}
+
+ public Category(org.apache.rave.model.Category category) {
+ this.id = category.getId();
+ this.text = category.getText();
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public void setId(String id) {
+ this.id = id;
+ }
+
+ public String getText() {
+ return text;
+ }
+
+ public void setText(String text) {
+ this.text = text;
+ }
+
+ public void saveToCategory(org.apache.rave.model.Category category) {
+ if (category.getId() != null && !category.getId().equals(id)) {
+ throw new RuntimeException("You cannot change the ID of a Category
object");
+ }
+
+ if (text != null) { category.setText(text); }
+ }
+}
Added:
rave/trunk/rave-components/rave-core-api/src/main/java/org/apache/rave/rest/model/CategoryList.java
URL:
http://svn.apache.org/viewvc/rave/trunk/rave-components/rave-core-api/src/main/java/org/apache/rave/rest/model/CategoryList.java?rev=1462425&view=auto
==============================================================================
---
rave/trunk/rave-components/rave-core-api/src/main/java/org/apache/rave/rest/model/CategoryList.java
(added)
+++
rave/trunk/rave-components/rave-core-api/src/main/java/org/apache/rave/rest/model/CategoryList.java
Fri Mar 29 07:59:06 2013
@@ -0,0 +1,40 @@
+/*
+ * 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.rave.rest.model;
+
+import javax.xml.bind.annotation.*;
+import java.util.ArrayList;
+import java.util.List;
+
+@XmlAccessorType(XmlAccessType.FIELD)
+@XmlType(name = "Categories", propOrder = {
+ "categories"
+})
+@XmlRootElement(name = "Categories")
+public class CategoryList {
+ @XmlElement(name = "Category")
+ protected List<Category> categories;
+
+ public List<Category> getCategories() {
+ if (categories == null) {
+ categories = new ArrayList<Category>();
+ }
+ return this.categories;
+ }
+}
Modified:
rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/service/impl/DefaultCategoryService.java
URL:
http://svn.apache.org/viewvc/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/service/impl/DefaultCategoryService.java?rev=1462425&r1=1462424&r2=1462425&view=diff
==============================================================================
---
rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/service/impl/DefaultCategoryService.java
(original)
+++
rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/portal/service/impl/DefaultCategoryService.java
Fri Mar 29 07:59:06 2013
@@ -60,8 +60,7 @@ public class DefaultCategoryService impl
category.setCreatedUserId(createdUser.getId());
category.setLastModifiedDate(now);
category.setLastModifiedUserId(createdUser.getId());
- categoryRepository.save(category);
- return category;
+ return categoryRepository.save(category);
}
@Override
Added:
rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/rest/impl/DefaultCategoriesResource.java
URL:
http://svn.apache.org/viewvc/rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/rest/impl/DefaultCategoriesResource.java?rev=1462425&view=auto
==============================================================================
---
rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/rest/impl/DefaultCategoriesResource.java
(added)
+++
rave/trunk/rave-components/rave-core/src/main/java/org/apache/rave/rest/impl/DefaultCategoriesResource.java
Fri Mar 29 07:59:06 2013
@@ -0,0 +1,82 @@
+/*
+ * 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.rave.rest.impl;
+
+import org.apache.rave.model.User;
+import org.apache.rave.portal.service.CategoryService;
+import org.apache.rave.portal.service.UserService;
+import org.apache.rave.rest.CategoriesResource;
+import org.apache.rave.rest.model.Category;
+import org.apache.rave.rest.model.CategoryList;
+
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriBuilder;
+import javax.ws.rs.core.UriInfo;
+
+public class DefaultCategoriesResource implements CategoriesResource {
+ private CategoryService categoryService;
+ private UserService userService;
+
+ @Override
+ public Response getCategories() {
+ CategoryList categoryList = new CategoryList();
+ for (org.apache.rave.model.Category category :
categoryService.getAll()) {
+ categoryList.getCategories().add(new Category((category)));
+ }
+
+ return Response.ok(categoryList).build();
+ }
+
+ @Override
+ public Response getCategory(String id) {
+ org.apache.rave.model.Category category = categoryService.get(id);
+ if (category == null) {
+ return Response.status(Response.Status.NOT_FOUND).build();
+ } else {
+ return Response.ok(new Category(category)).build();
+ }
+ }
+
+ @Override
+ public Response updateCategory(String id, Category category,UriInfo uri) {
+ User user = userService.getAuthenticatedUser();
+
+ org.apache.rave.model.Category updatedCategory =
categoryService.update(id, category.getText(), user);
+
+ return Response.ok(new
Category(updatedCategory)).location(uri.getRequestUri()).build();
+ }
+
+ @Override
+ public Response createCategory(Category category) {
+ User user = userService.getAuthenticatedUser();
+
+ org.apache.rave.model.Category newCategory =
categoryService.create(category.getText(), user);
+
+ UriBuilder builder =
UriBuilder.fromResource(CategoriesResource.class).path("/{id}");
+ return Response.created(builder.build(newCategory.getId())).entity(new
Category(newCategory)).build();
+ }
+
+ public void setCategoryService(CategoryService categoryService) {
+ this.categoryService = categoryService;
+ }
+
+ public void setUserService(UserService userService) {
+ this.userService = userService;
+ }
+}
Modified:
rave/trunk/rave-portal-resources/src/main/webapp/WEB-INF/cxf-applicationContext.xml
URL:
http://svn.apache.org/viewvc/rave/trunk/rave-portal-resources/src/main/webapp/WEB-INF/cxf-applicationContext.xml?rev=1462425&r1=1462424&r2=1462425&view=diff
==============================================================================
---
rave/trunk/rave-portal-resources/src/main/webapp/WEB-INF/cxf-applicationContext.xml
(original)
+++
rave/trunk/rave-portal-resources/src/main/webapp/WEB-INF/cxf-applicationContext.xml
Fri Mar 29 07:59:06 2013
@@ -21,6 +21,7 @@
<jaxrs:serviceBeans>
<ref bean="peopleBean"/>
<ref bean="usersBean"/>
+ <ref bean="categoriesBean"/>
</jaxrs:serviceBeans>
</jaxrs:server>
@@ -31,4 +32,9 @@
<bean id="usersBean"
class="org.apache.rave.rest.impl.DefaultUsersResource">
<property name="userService" ref="userService"/>
</bean>
+
+ <bean id="categoriesBean"
class="org.apache.rave.rest.impl.DefaultCategoriesResource">
+ <property name="categoryService" ref="defaultCategoryService"/>
+ <property name="userService" ref="userService"/>
+ </bean>
</beans>
\ No newline at end of file