Skip to content

Commit

Permalink
Added exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymitrevski committed Apr 5, 2013
1 parent bb1f3cf commit f525e68
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Classifieds web app using Spring MVC and MongoDB

Setup IDE

1) Download Springsource Tool Suite<br>
1) Download Springsource Tool Suite (3.2.0 release)<br>
2) Install EGit plugin - Eclipse Marketplace<br>
3) Install CloudFoundry plugin - Eclipse Marketplace<br>
4) Import projects from Git, https://github.com/johnnymitrevski/classifiedsMVC<br>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package com.blogspot.agilisto.classifieds.controllers;

import java.util.Date;
import java.util.List;
import java.util.zip.DataFormatException;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
Expand All @@ -22,8 +21,6 @@
import org.springframework.web.bind.annotation.ResponseStatus;

import com.blogspot.agilisto.classifieds.model.Category;
import com.blogspot.agilisto.classifieds.model.Listing;
import com.blogspot.agilisto.classifieds.model.SellerIdentity;
import com.blogspot.agilisto.classifieds.services.CategoryService;
import com.blogspot.agilisto.classifieds.services.ListingService;

Expand Down Expand Up @@ -62,30 +59,33 @@ public void createNewCategory(@RequestParam("categoryId")String categoryId, @Req

@ResponseBody
@RequestMapping(value = "/category/{categoryId}", method = RequestMethod.GET)
public Category getCategory(@PathVariable("categoryId")String categoryId)
public Category getCategory(@PathVariable("categoryId")String categoryId) throws Exception
{
return categoryService.getCategory(categoryId);
Category category = categoryService.getCategory(categoryId);

if(category == null)
{
throw new Exception("Category can not be deleted when it has children associated with it");
}

return category;
}

@ResponseBody
@RequestMapping(value = "/category/{categoryId}", method = RequestMethod.DELETE)
@ResponseStatus(value = HttpStatus.OK)
public void deleteCategory(@PathVariable("categoryId")String categoryId)
public void deleteCategory(@PathVariable("categoryId")String categoryId) throws Exception
{
Category category = categoryService.getCategory(categoryId);

if(!listingService.getListings(new Query(Criteria.where("category").is(category))).isEmpty())
{
Logger log = Logger.getLogger(CategoryController.class.getName());
log.warn("Category can not be deleted when it has listings associated with it");
return;
throw new Exception("Category can not be deleted when it has listings associated with it");
}

if(!category.getChildren().isEmpty())
{
Logger log = Logger.getLogger(CategoryController.class.getName());
log.warn("Category can not be deleted when it has children associated with it");
return;
throw new Exception("Category can not be deleted when it has children associated with it");
}

categoryService.deleteCategory(categoryId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.blogspot.agilisto.classifieds.model;

import java.io.Serializable;
import java.util.List;
import java.util.Vector;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.blogspot.agilisto.classifieds.model;

import java.io.Serializable;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

/**
* Enquiry domain class<br>
* <li>Used to represent enquiries made on listings<br>
*/
@Document(collection = "Enquiry")
public class Enquiry implements Serializable {
public class Enquiry {

private String id;
private String listingId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.blogspot.agilisto.classifieds.model;

import java.io.Serializable;
import java.util.Date;

import org.springframework.data.mongodb.core.mapping.Document;
Expand All @@ -10,7 +9,7 @@
* <li>Used to represent a listing on the site.<br>
*/
@Document(collection = "Listing")
public class Listing implements Serializable{
public class Listing {

private String id;
private String title;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.blogspot.agilisto.classifieds.model;

import java.io.Serializable;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

Expand All @@ -10,7 +8,7 @@
* <li>Used to represent the username and password information set of a seller.<br>
*/
@Document(collection = "SellerCredential")
public class SellerCredential implements Serializable{
public class SellerCredential {

@Id
String username;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.blogspot.agilisto.classifieds.model;

import java.io.Serializable;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

Expand All @@ -10,7 +8,7 @@
* <li>Used to represent the information set of a seller.<br>
*/
@Document(collection = "SellerIdentity")
public class SellerIdentity implements Serializable{
public class SellerIdentity {

@Id
String username;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.blogspot.agilisto.classifieds.mongo.services;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
Expand Down
12 changes: 11 additions & 1 deletion src/main/webapp/WEB-INF/spring/root-context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,15 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- Root Context: defines shared resources visible to all other web components -->

<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<map>
<entry key="java.lang.Exception" value="data-error" />
<entry key="com.blogspot.agilisto.classifieds.ClassifiedsRuntimeException" value="app-unchecked-error" />
<entry key="com.blogspot.agilisto.classifieds.ClassifiedsCheckedException" value="app-checked-error" />
</map>
</property>
<property name="defaultErrorView" value="/error/default" />
<property name="warnLogCategory" value="com.blogspot.agilisto.classifieds" />
</bean>
</beans>

0 comments on commit f525e68

Please sign in to comment.