Skip to content

Commit 7668b40

Browse files
committed
Merge pull request #104 from diging/feature/CCP-154
Catch ClassCastException when parsing JSON to return proper error codes.
2 parents b310e26 + d2773b2 commit 7668b40

File tree

4 files changed

+149
-131
lines changed

4 files changed

+149
-131
lines changed

Conceptpower+Spring/src/main/java/edu/asu/conceptpower/rest/Concepts.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public ResponseEntity<String> addConcepts(@RequestBody String body,
108108
JSONArray jsonArray = null;
109109
try {
110110
jsonArray = (JSONArray) jsonParser.parse(reader);
111-
} catch (IOException | ParseException e1) {
111+
} catch (IOException | ParseException | ClassCastException e1) {
112112
logger.error("Error parsing request.", e1);
113113
return new ResponseEntity<String>("Error parsing request: " + e1,
114114
HttpStatus.BAD_REQUEST);
Lines changed: 139 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,139 @@
1-
package edu.asu.conceptpower.web;
2-
3-
import java.util.HashMap;
4-
import java.util.List;
5-
import java.util.Map;
6-
7-
import org.codehaus.jettison.json.JSONObject;
8-
import org.springframework.beans.factory.annotation.Autowired;
9-
import org.springframework.http.HttpStatus;
10-
import org.springframework.http.ResponseEntity;
11-
import org.springframework.stereotype.Controller;
12-
import org.springframework.ui.ModelMap;
13-
import org.springframework.web.bind.annotation.PathVariable;
14-
import org.springframework.web.bind.annotation.RequestMapping;
15-
import org.springframework.web.bind.annotation.RequestMethod;
16-
import org.springframework.web.bind.annotation.RequestParam;
17-
import org.springframework.web.bind.annotation.ResponseBody;
18-
19-
import edu.asu.conceptpower.core.ConceptEntry;
20-
import edu.asu.conceptpower.core.ConceptList;
21-
import edu.asu.conceptpower.core.ConceptType;
22-
import edu.asu.conceptpower.core.IConceptListManager;
23-
import edu.asu.conceptpower.core.IConceptManager;
24-
import edu.asu.conceptpower.db4o.TypeDatabaseClient;
25-
import edu.asu.conceptpower.util.URIHelper;
26-
import edu.asu.conceptpower.wrapper.ConceptEntryWrapper;
27-
import edu.asu.conceptpower.wrapper.IConceptWrapperCreator;
28-
29-
@Controller
30-
public class ConceptListController {
31-
32-
@Autowired
33-
private IConceptManager conceptManager;
34-
35-
@Autowired
36-
private IConceptListManager conceptListManager;
37-
38-
@Autowired
39-
private TypeDatabaseClient typeDatabaseClient;
40-
41-
@Autowired
42-
private URIHelper URICreator;
43-
44-
@Autowired
45-
private IConceptWrapperCreator wrapperCreator;
46-
47-
/**
48-
* This method provides information of all the existing concept lists for
49-
* showing concept list page
50-
*
51-
* @param model
52-
* A generic model holder for Servlet
53-
* @return String value to redirect user to all concpet list page
54-
*/
55-
@RequestMapping(value = "auth/conceptlist")
56-
public String prepareShowConceptList(ModelMap model) {
57-
58-
List<ConceptList> conceptLists = conceptListManager.getAllConceptLists();
59-
model.addAttribute("result", conceptLists);
60-
61-
return "/auth/conceptlist";
62-
}
63-
64-
/**
65-
* This method provides infomaiton of concepts for a given concept list
66-
*
67-
* @param list
68-
* @param model
69-
* A generic model holder for Servlet
70-
* @return Return sting value to redirect user to a particular concept list
71-
* page
72-
*/
73-
@RequestMapping(value = "auth/{listid}/concepts", method = RequestMethod.GET)
74-
public String getConceptsOfConceptList(@PathVariable("listid") String list,
75-
ModelMap model) {
76-
77-
List<ConceptEntry> founds = conceptManager.getConceptListEntries(list);
78-
79-
List<ConceptEntryWrapper> foundConcepts = wrapperCreator
80-
.createWrappers(founds != null ? founds
81-
.toArray(new ConceptEntry[founds.size()])
82-
: new ConceptEntry[0]);
83-
84-
model.addAttribute("result", foundConcepts);
85-
return "/auth/conceptlist/concepts";
86-
}
87-
88-
/**
89-
* This method provides details of a concept for given concept ID
90-
*
91-
* @param conceptid
92-
* ID of a concept
93-
* @return Map containing concept details
94-
*/
95-
@RequestMapping(method = RequestMethod.GET, value = "conceptDetail", produces = "application/json")
96-
public @ResponseBody ResponseEntity<String> getConceptDetails(
97-
@RequestParam("conceptid") String conceptid) {
98-
ConceptEntryWrapper conceptEntry = new ConceptEntryWrapper(
99-
conceptManager.getConceptEntry(conceptid));
100-
Map<String, String> details = new HashMap<String, String>();
101-
102-
details.put("name", conceptEntry.getEntry().getWord());
103-
details.put("id", conceptEntry.getEntry().getId());
104-
details.put("uri", URICreator.getURI(conceptEntry.getEntry()));
105-
//This condition has been included to make sure null values are not displayed in the details dialog box
106-
details.put("wordnetid", conceptEntry.getEntry().getWordnetId()==null?"":conceptEntry.getEntry().getWordnetId());
107-
details.put("pos", conceptEntry.getEntry().getPos());
108-
details.put("conceptlist", conceptEntry.getEntry().getConceptList());
109-
110-
ConceptType type = conceptEntry.getEntry().getTypeId() == null ? null
111-
: typeDatabaseClient.getType(
112-
conceptEntry.getEntry().getTypeId());
113-
114-
details.put(
115-
"type",
116-
type == null ? "" : type.getTypeName());
117-
details.put("equalto",
118-
conceptEntry.getEntry().getEqualTo() == null ? ""
119-
: conceptEntry.getEntry().getEqualTo());
120-
details.put("similarto",
121-
conceptEntry.getEntry().getSimilarTo() == null ? ""
122-
: conceptEntry.getEntry().getSimilarTo());
123-
details.put("creator",
124-
conceptEntry.getEntry().getCreatorId() == null ? ""
125-
: conceptEntry.getEntry().getCreatorId());
126-
127-
return new ResponseEntity<String>(new JSONObject(details).toString(), HttpStatus.OK);
128-
}
129-
}
1+
package edu.asu.conceptpower.web;
2+
3+
import java.util.HashMap;
4+
import java.util.List;
5+
import java.util.Map;
6+
7+
import org.codehaus.jettison.json.JSONObject;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.http.HttpStatus;
10+
import org.springframework.http.ResponseEntity;
11+
import org.springframework.stereotype.Controller;
12+
import org.springframework.ui.ModelMap;
13+
import org.springframework.web.bind.annotation.PathVariable;
14+
import org.springframework.web.bind.annotation.RequestMapping;
15+
import org.springframework.web.bind.annotation.RequestMethod;
16+
import org.springframework.web.bind.annotation.RequestParam;
17+
import org.springframework.web.bind.annotation.ResponseBody;
18+
19+
import edu.asu.conceptpower.core.ConceptEntry;
20+
import edu.asu.conceptpower.core.ConceptList;
21+
import edu.asu.conceptpower.core.ConceptType;
22+
import edu.asu.conceptpower.core.IConceptListManager;
23+
import edu.asu.conceptpower.core.IConceptManager;
24+
import edu.asu.conceptpower.db4o.TypeDatabaseClient;
25+
import edu.asu.conceptpower.util.URIHelper;
26+
import edu.asu.conceptpower.wrapper.ConceptEntryWrapper;
27+
import edu.asu.conceptpower.wrapper.IConceptWrapperCreator;
28+
29+
@Controller
30+
public class ConceptListController {
31+
32+
@Autowired
33+
private IConceptManager conceptManager;
34+
35+
@Autowired
36+
private IConceptListManager conceptListManager;
37+
38+
@Autowired
39+
private TypeDatabaseClient typeDatabaseClient;
40+
41+
@Autowired
42+
private URIHelper URICreator;
43+
44+
@Autowired
45+
private IConceptWrapperCreator wrapperCreator;
46+
47+
/**
48+
* This method provides information of all the existing concept lists for
49+
* showing concept list page
50+
*
51+
* @param model
52+
* A generic model holder for Servlet
53+
* @return String value to redirect user to all concpet list page
54+
*/
55+
@RequestMapping(value = "auth/conceptlist")
56+
public String prepareShowConceptList(ModelMap model) {
57+
58+
List<ConceptList> conceptLists = conceptListManager.getAllConceptLists();
59+
model.addAttribute("result", conceptLists);
60+
61+
return "/auth/conceptlist";
62+
}
63+
64+
/**
65+
* This method provides infomaiton of concepts for a given concept list
66+
*
67+
* @param list
68+
* @param model
69+
* A generic model holder for Servlet
70+
* @return Return sting value to redirect user to a particular concept list
71+
* page
72+
*/
73+
@RequestMapping(value = "auth/{listid}/concepts", method = RequestMethod.GET)
74+
public String getConceptsOfConceptList(@PathVariable("listid") String list,
75+
ModelMap model) {
76+
77+
List<ConceptEntry> founds = conceptManager.getConceptListEntries(list);
78+
79+
List<ConceptEntryWrapper> foundConcepts = wrapperCreator
80+
.createWrappers(founds != null ? founds
81+
.toArray(new ConceptEntry[founds.size()])
82+
: new ConceptEntry[0]);
83+
84+
model.addAttribute("result", foundConcepts);
85+
return "/auth/conceptlist/concepts";
86+
}
87+
88+
/**
89+
* This method provides details of a concept for given concept ID
90+
*
91+
* @param conceptid
92+
* ID of a concept
93+
* @return Map containing concept details
94+
*/
95+
@RequestMapping(method = RequestMethod.GET, value = "conceptDetail", produces = "application/json")
96+
public @ResponseBody ResponseEntity<String> getConceptDetails(
97+
@RequestParam("conceptid") String conceptid) {
98+
ConceptEntry entry = conceptManager.getConceptEntry(conceptid);
99+
List<ConceptEntryWrapper> wrappers = wrapperCreator.createWrappers(new ConceptEntry[] { entry } );
100+
ConceptEntryWrapper wrapper = null;
101+
if (wrappers.size() > 0) {
102+
wrapper = wrappers.get(0);
103+
}
104+
else {
105+
return new ResponseEntity<String>("No entry for the provided id.", HttpStatus.BAD_REQUEST);
106+
}
107+
108+
Map<String, String> details = new HashMap<String, String>();
109+
110+
details.put("name", wrapper.getEntry().getWord());
111+
details.put("id", wrapper.getEntry().getId());
112+
details.put("uri", URICreator.getURI(wrapper.getEntry()));
113+
//This condition has been included to make sure null values are not displayed in the details dialog box
114+
details.put("wordnetid", wrapper.getEntry().getWordnetId()==null?"":wrapper.getEntry().getWordnetId());
115+
details.put("pos", wrapper.getEntry().getPos());
116+
details.put("conceptlist", wrapper.getEntry().getConceptList());
117+
118+
ConceptType type = wrapper.getEntry().getTypeId() == null ? null
119+
: typeDatabaseClient.getType(
120+
wrapper.getEntry().getTypeId());
121+
122+
details.put("description", wrapper.getEntry().getDescription());
123+
124+
details.put(
125+
"type",
126+
type == null ? "" : type.getTypeName());
127+
details.put("equalto",
128+
wrapper.getEntry().getEqualTo() == null ? ""
129+
: wrapper.getEntry().getEqualTo());
130+
details.put("similarto",
131+
wrapper.getEntry().getSimilarTo() == null ? ""
132+
: wrapper.getEntry().getSimilarTo());
133+
details.put("creator",
134+
wrapper.getEntry().getCreatorId() == null ? ""
135+
: wrapper.getEntry().getCreatorId());
136+
137+
return new ResponseEntity<String>(new JSONObject(details).toString(), HttpStatus.OK);
138+
}
139+
}

Conceptpower+Spring/src/main/java/edu/asu/conceptpower/wrapper/impl/ConceptEntryWrapperCreator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,12 @@ public List<ConceptEntryWrapper> createWrappers(ConceptEntry[] entries) {
8484

8585
// build description considering all the wordnet entries wrappe
8686
StringBuffer sb = new StringBuffer();
87+
sb.append(entry.getDescription());
8788
if (wordnetEntries.size() > 0) {
8889
for (ConceptEntry wordnetConcept : wordnetEntries) {
89-
if (wordnetConcept.getDescription() != null) {
90+
if (wordnetConcept.getDescription() != null &&
91+
(entry.getDescription() == null ||
92+
!wordnetConcept.getDescription().trim().equals(entry.getDescription().trim()))) {
9093
sb.append("<br/><i>" + wordnetConcept.getWord()
9194
+ "</i>");
9295
sb.append("<br/>" + wordnetConcept.getDescription());

Conceptpower+Spring/src/main/webapp/WEB-INF/views/home.jsp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
$("#detailsequalto").text(details.equalto);
5555
$("#detailssimilarto").text(details.similarto);
5656
$("#detailscreator").text(details.creator);
57+
$("#detailsdescription").text(details.description);
5758
5859
$("#detailsdiv").dialog({
5960
title : details.name,
@@ -214,6 +215,10 @@
214215
<td>Wordnet Id:</td>
215216
<td id="detailswordnetid"></td>
216217
</tr>
218+
<tr>
219+
<td>Description:</td>
220+
<td id="detailsdescription"></td>
221+
</tr>
217222
<tr>
218223
<td>POS:</td>
219224
<td id="detailspos"></td>

0 commit comments

Comments
 (0)