Skip to content

Commit

Permalink
Merge pull request #104 from diging/feature/CCP-154
Browse files Browse the repository at this point in the history
Catch ClassCastException when parsing JSON to return proper error codes.
  • Loading branch information
jdamerow committed Apr 29, 2016
2 parents b310e26 + d2773b2 commit 7668b40
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public ResponseEntity<String> addConcepts(@RequestBody String body,
JSONArray jsonArray = null;
try {
jsonArray = (JSONArray) jsonParser.parse(reader);
} catch (IOException | ParseException e1) {
} catch (IOException | ParseException | ClassCastException e1) {
logger.error("Error parsing request.", e1);
return new ResponseEntity<String>("Error parsing request: " + e1,
HttpStatus.BAD_REQUEST);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,129 +1,139 @@
package edu.asu.conceptpower.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jettison.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.asu.conceptpower.core.ConceptEntry;
import edu.asu.conceptpower.core.ConceptList;
import edu.asu.conceptpower.core.ConceptType;
import edu.asu.conceptpower.core.IConceptListManager;
import edu.asu.conceptpower.core.IConceptManager;
import edu.asu.conceptpower.db4o.TypeDatabaseClient;
import edu.asu.conceptpower.util.URIHelper;
import edu.asu.conceptpower.wrapper.ConceptEntryWrapper;
import edu.asu.conceptpower.wrapper.IConceptWrapperCreator;

@Controller
public class ConceptListController {

@Autowired
private IConceptManager conceptManager;

@Autowired
private IConceptListManager conceptListManager;

@Autowired
private TypeDatabaseClient typeDatabaseClient;

@Autowired
private URIHelper URICreator;

@Autowired
private IConceptWrapperCreator wrapperCreator;

/**
* This method provides information of all the existing concept lists for
* showing concept list page
*
* @param model
* A generic model holder for Servlet
* @return String value to redirect user to all concpet list page
*/
@RequestMapping(value = "auth/conceptlist")
public String prepareShowConceptList(ModelMap model) {

List<ConceptList> conceptLists = conceptListManager.getAllConceptLists();
model.addAttribute("result", conceptLists);

return "/auth/conceptlist";
}

/**
* This method provides infomaiton of concepts for a given concept list
*
* @param list
* @param model
* A generic model holder for Servlet
* @return Return sting value to redirect user to a particular concept list
* page
*/
@RequestMapping(value = "auth/{listid}/concepts", method = RequestMethod.GET)
public String getConceptsOfConceptList(@PathVariable("listid") String list,
ModelMap model) {

List<ConceptEntry> founds = conceptManager.getConceptListEntries(list);

List<ConceptEntryWrapper> foundConcepts = wrapperCreator
.createWrappers(founds != null ? founds
.toArray(new ConceptEntry[founds.size()])
: new ConceptEntry[0]);

model.addAttribute("result", foundConcepts);
return "/auth/conceptlist/concepts";
}

/**
* This method provides details of a concept for given concept ID
*
* @param conceptid
* ID of a concept
* @return Map containing concept details
*/
@RequestMapping(method = RequestMethod.GET, value = "conceptDetail", produces = "application/json")
public @ResponseBody ResponseEntity<String> getConceptDetails(
@RequestParam("conceptid") String conceptid) {
ConceptEntryWrapper conceptEntry = new ConceptEntryWrapper(
conceptManager.getConceptEntry(conceptid));
Map<String, String> details = new HashMap<String, String>();

details.put("name", conceptEntry.getEntry().getWord());
details.put("id", conceptEntry.getEntry().getId());
details.put("uri", URICreator.getURI(conceptEntry.getEntry()));
//This condition has been included to make sure null values are not displayed in the details dialog box
details.put("wordnetid", conceptEntry.getEntry().getWordnetId()==null?"":conceptEntry.getEntry().getWordnetId());
details.put("pos", conceptEntry.getEntry().getPos());
details.put("conceptlist", conceptEntry.getEntry().getConceptList());

ConceptType type = conceptEntry.getEntry().getTypeId() == null ? null
: typeDatabaseClient.getType(
conceptEntry.getEntry().getTypeId());

details.put(
"type",
type == null ? "" : type.getTypeName());
details.put("equalto",
conceptEntry.getEntry().getEqualTo() == null ? ""
: conceptEntry.getEntry().getEqualTo());
details.put("similarto",
conceptEntry.getEntry().getSimilarTo() == null ? ""
: conceptEntry.getEntry().getSimilarTo());
details.put("creator",
conceptEntry.getEntry().getCreatorId() == null ? ""
: conceptEntry.getEntry().getCreatorId());

return new ResponseEntity<String>(new JSONObject(details).toString(), HttpStatus.OK);
}
}
package edu.asu.conceptpower.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jettison.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.asu.conceptpower.core.ConceptEntry;
import edu.asu.conceptpower.core.ConceptList;
import edu.asu.conceptpower.core.ConceptType;
import edu.asu.conceptpower.core.IConceptListManager;
import edu.asu.conceptpower.core.IConceptManager;
import edu.asu.conceptpower.db4o.TypeDatabaseClient;
import edu.asu.conceptpower.util.URIHelper;
import edu.asu.conceptpower.wrapper.ConceptEntryWrapper;
import edu.asu.conceptpower.wrapper.IConceptWrapperCreator;

@Controller
public class ConceptListController {

@Autowired
private IConceptManager conceptManager;

@Autowired
private IConceptListManager conceptListManager;

@Autowired
private TypeDatabaseClient typeDatabaseClient;

@Autowired
private URIHelper URICreator;

@Autowired
private IConceptWrapperCreator wrapperCreator;

/**
* This method provides information of all the existing concept lists for
* showing concept list page
*
* @param model
* A generic model holder for Servlet
* @return String value to redirect user to all concpet list page
*/
@RequestMapping(value = "auth/conceptlist")
public String prepareShowConceptList(ModelMap model) {

List<ConceptList> conceptLists = conceptListManager.getAllConceptLists();
model.addAttribute("result", conceptLists);

return "/auth/conceptlist";
}

/**
* This method provides infomaiton of concepts for a given concept list
*
* @param list
* @param model
* A generic model holder for Servlet
* @return Return sting value to redirect user to a particular concept list
* page
*/
@RequestMapping(value = "auth/{listid}/concepts", method = RequestMethod.GET)
public String getConceptsOfConceptList(@PathVariable("listid") String list,
ModelMap model) {

List<ConceptEntry> founds = conceptManager.getConceptListEntries(list);

List<ConceptEntryWrapper> foundConcepts = wrapperCreator
.createWrappers(founds != null ? founds
.toArray(new ConceptEntry[founds.size()])
: new ConceptEntry[0]);

model.addAttribute("result", foundConcepts);
return "/auth/conceptlist/concepts";
}

/**
* This method provides details of a concept for given concept ID
*
* @param conceptid
* ID of a concept
* @return Map containing concept details
*/
@RequestMapping(method = RequestMethod.GET, value = "conceptDetail", produces = "application/json")
public @ResponseBody ResponseEntity<String> getConceptDetails(
@RequestParam("conceptid") String conceptid) {
ConceptEntry entry = conceptManager.getConceptEntry(conceptid);
List<ConceptEntryWrapper> wrappers = wrapperCreator.createWrappers(new ConceptEntry[] { entry } );
ConceptEntryWrapper wrapper = null;
if (wrappers.size() > 0) {
wrapper = wrappers.get(0);
}
else {
return new ResponseEntity<String>("No entry for the provided id.", HttpStatus.BAD_REQUEST);
}

Map<String, String> details = new HashMap<String, String>();

details.put("name", wrapper.getEntry().getWord());
details.put("id", wrapper.getEntry().getId());
details.put("uri", URICreator.getURI(wrapper.getEntry()));
//This condition has been included to make sure null values are not displayed in the details dialog box
details.put("wordnetid", wrapper.getEntry().getWordnetId()==null?"":wrapper.getEntry().getWordnetId());
details.put("pos", wrapper.getEntry().getPos());
details.put("conceptlist", wrapper.getEntry().getConceptList());

ConceptType type = wrapper.getEntry().getTypeId() == null ? null
: typeDatabaseClient.getType(
wrapper.getEntry().getTypeId());

details.put("description", wrapper.getEntry().getDescription());

details.put(
"type",
type == null ? "" : type.getTypeName());
details.put("equalto",
wrapper.getEntry().getEqualTo() == null ? ""
: wrapper.getEntry().getEqualTo());
details.put("similarto",
wrapper.getEntry().getSimilarTo() == null ? ""
: wrapper.getEntry().getSimilarTo());
details.put("creator",
wrapper.getEntry().getCreatorId() == null ? ""
: wrapper.getEntry().getCreatorId());

return new ResponseEntity<String>(new JSONObject(details).toString(), HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,12 @@ public List<ConceptEntryWrapper> createWrappers(ConceptEntry[] entries) {

// build description considering all the wordnet entries wrappe
StringBuffer sb = new StringBuffer();
sb.append(entry.getDescription());
if (wordnetEntries.size() > 0) {
for (ConceptEntry wordnetConcept : wordnetEntries) {
if (wordnetConcept.getDescription() != null) {
if (wordnetConcept.getDescription() != null &&
(entry.getDescription() == null ||
!wordnetConcept.getDescription().trim().equals(entry.getDescription().trim()))) {
sb.append("<br/><i>" + wordnetConcept.getWord()
+ "</i>");
sb.append("<br/>" + wordnetConcept.getDescription());
Expand Down
5 changes: 5 additions & 0 deletions Conceptpower+Spring/src/main/webapp/WEB-INF/views/home.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
$("#detailsequalto").text(details.equalto);
$("#detailssimilarto").text(details.similarto);
$("#detailscreator").text(details.creator);
$("#detailsdescription").text(details.description);
$("#detailsdiv").dialog({
title : details.name,
Expand Down Expand Up @@ -214,6 +215,10 @@
<td>Wordnet Id:</td>
<td id="detailswordnetid"></td>
</tr>
<tr>
<td>Description:</td>
<td id="detailsdescription"></td>
</tr>
<tr>
<td>POS:</td>
<td id="detailspos"></td>
Expand Down

0 comments on commit 7668b40

Please sign in to comment.