Revision 32557
Added by Andrea Mannocci over 9 years ago
ContextsController.java | ||
---|---|---|
10 | 10 |
|
11 | 11 |
import javax.annotation.Resource; |
12 | 12 |
|
13 |
import lombok.extern.apachecommons.CommonsLog;
|
|
14 |
|
|
13 |
import org.apache.commons.logging.Log;
|
|
14 |
import org.apache.commons.logging.LogFactory; |
|
15 | 15 |
import org.dom4j.Document; |
16 | 16 |
import org.dom4j.DocumentException; |
17 | 17 |
import org.dom4j.Element; |
... | ... | |
35 | 35 |
|
36 | 36 |
/** |
37 | 37 |
* Web controller for the UI |
38 |
*
|
|
38 |
* |
|
39 | 39 |
* @author Andrea Mannocci |
40 | 40 |
*/ |
41 |
@CommonsLog |
|
42 | 41 |
@Controller |
43 | 42 |
public class ContextsController { |
44 | 43 |
|
44 |
private static final Log log = LogFactory.getLog(ContextsController.class); |
|
45 |
|
|
45 | 46 |
@Resource(name = "lookupLocator") |
46 | 47 |
private ServiceLocator<ISLookUpService> lookupLocator; |
47 | 48 |
|
... | ... | |
50 | 51 |
|
51 | 52 |
/** |
52 | 53 |
* Returns all contexts serialized in json. Invoked by angularJS |
53 |
*
|
|
54 |
* |
|
54 | 55 |
* @param response |
55 |
* @return
|
|
56 |
* @return |
|
56 | 57 |
* @throws ISLookUpException |
57 | 58 |
* @throws IOException |
58 | 59 |
*/ |
59 | 60 |
@RequestMapping("/ui/contexts.json") |
60 | 61 |
public @ResponseBody List<Context> getContexts() throws ISLookUpException, IOException { |
61 | 62 |
log.info("contexts.json"); |
62 |
|
|
63 |
|
|
63 | 64 |
final String query = "for $x in collection('/db/DRIVER/ContextDSResources/ContextDSResourceType') order by $x//label " |
64 | 65 |
+ "return concat ($x//RESOURCE_IDENTIFIER/@value,' §§§ ',$x//context/@id,' §§§ ',$x//context/@label,' §§§ ',$x//context/@type)"; |
65 | 66 |
|
66 | 67 |
final List<Context> contexts = Lists.transform(lookupLocator.getService().quickSearchProfile(query), new Function<String, Context>() { |
68 |
|
|
67 | 69 |
@Override |
68 | 70 |
public Context apply(final String s) { |
69 | 71 |
String[] tokens = s.split("§§§"); |
70 | 72 |
return new Context(tokens[0].trim(), tokens[1].trim(), tokens[2].trim(), tokens[3].trim()); |
71 | 73 |
} |
72 | 74 |
}); |
73 |
|
|
75 |
|
|
74 | 76 |
return contexts; |
75 | 77 |
} |
76 | 78 |
|
77 | 79 |
/** |
78 | 80 |
* Returns all categories and their concepts belonging to a context. Invoked by AngularJS |
79 |
*
|
|
81 |
* |
|
80 | 82 |
* @param response |
81 | 83 |
* @param contextId |
82 | 84 |
* @throws ISLookUpException |
83 | 85 |
* @throws IOException |
84 |
* @throws DocumentException
|
|
86 |
* @throws DocumentException |
|
85 | 87 |
*/ |
86 | 88 |
@RequestMapping("/ui/categories.json") |
87 |
public @ResponseBody List<Category> getCategories(@RequestParam(value = "contextId", required = true) final String contextId)
|
|
89 |
public @ResponseBody List<Category> getCategories(@RequestParam(value = "contextId", required = true) final String contextId) |
|
88 | 90 |
throws ISLookUpException, IOException, DocumentException { |
89 | 91 |
log.info("categories.json?contextId=" + contextId); |
90 | 92 |
|
91 | 93 |
final String profile = lookupLocator.getService().getResourceProfile(contextId); |
92 |
final Document doc = (new SAXReader()).read(new StringReader(profile));
|
|
93 |
|
|
94 |
final Document doc = new SAXReader().read(new StringReader(profile));
|
|
95 |
|
|
94 | 96 |
final Map<String, Category> categories = Maps.newHashMap(); |
95 |
|
|
97 |
|
|
96 | 98 |
for (Object cat : doc.selectNodes("//category")) { |
97 |
|
|
99 |
|
|
98 | 100 |
final Element categoryNode = (Element) cat; |
99 | 101 |
|
100 | 102 |
final String id = categoryNode.valueOf("@id"); |
101 |
|
|
103 |
|
|
102 | 104 |
if (!categories.containsKey(id)) { |
103 | 105 |
final Category category = new Category(); |
104 | 106 |
category.setId(categoryNode.valueOf("@id")); |
... | ... | |
108 | 110 |
categories.put(id, category); |
109 | 111 |
} |
110 | 112 |
final Category category = categories.get(id); |
111 |
|
|
113 |
|
|
112 | 114 |
category.setConcepts(new ArrayList<Concept>()); |
113 | 115 |
for (Object con : categoryNode.selectNodes("./concept")) { |
114 | 116 |
category.getConcepts().add(createConcept((Element) con)); |
115 | 117 |
} |
116 |
|
|
118 |
|
|
117 | 119 |
Collections.sort(category.getConcepts()); |
118 | 120 |
} |
119 |
|
|
121 |
|
|
120 | 122 |
final List<Category> list = Lists.newArrayList(categories.values()); |
121 | 123 |
Collections.sort(list); |
122 | 124 |
|
123 | 125 |
return list; |
124 | 126 |
} |
125 |
|
|
126 |
private Concept createConcept(Element conceptNode) { |
|
127 |
|
|
128 |
private Concept createConcept(final Element conceptNode) {
|
|
127 | 129 |
final Concept outerConcept = new Concept(); |
128 | 130 |
outerConcept.setId(conceptNode.valueOf("@id")); |
129 | 131 |
outerConcept.setLabel(conceptNode.valueOf("@label")); |
130 | 132 |
outerConcept.setClaim(conceptNode.valueOf("@claim")); |
131 |
|
|
133 |
|
|
132 | 134 |
Map<String, String> paramsMap = new HashMap<String, String>(); |
133 |
for (Object param: conceptNode.selectNodes("./param")) { |
|
135 |
for (Object param : conceptNode.selectNodes("./param")) {
|
|
134 | 136 |
final Element paramNode = (Element) param; |
135 | 137 |
paramsMap.put(paramNode.valueOf("@name"), paramNode.valueOf(".")); |
136 | 138 |
} |
137 | 139 |
outerConcept.setParams(paramsMap); |
138 |
|
|
140 |
|
|
139 | 141 |
outerConcept.setConcepts(new ArrayList<Concept>()); |
140 | 142 |
for (Object nestedConcept : conceptNode.selectNodes("./concept")) { |
141 | 143 |
outerConcept.getConcepts().add(createConcept((Element) nestedConcept)); |
142 | 144 |
} |
143 |
|
|
145 |
|
|
144 | 146 |
return outerConcept; |
145 | 147 |
} |
146 |
|
|
147 |
// @RequestMapping(value = "/ui/commitContext", method = RequestMethod.POST) |
|
148 |
// public @ResponseBody String commitTerms(@RequestBody(required=true) final List<Term> terms, |
|
149 |
// @RequestParam(value = "contextId", required = true) final String contextId) throws IOException, ISRegistryException, JAXBException { |
|
150 |
// log.info("committing context id = " + contextId); |
|
151 |
// |
|
152 |
// // prepare terms for XML |
|
153 |
// for (Term t : terms) { |
|
154 |
// t.setCode(StringEscapeUtils.escapeXml(t.getCode())); |
|
155 |
// t.setEncoding(StringEscapeUtils.escapeXml(t.getEncoding())); |
|
156 |
// t.setEnglishName(StringEscapeUtils.escapeXml(t.getEnglishName())); |
|
157 |
// t.setNativeName(StringEscapeUtils.escapeXml(t.getNativeName())); |
|
158 |
// for (Synonym s : t.getSynonyms()) { |
|
159 |
// s.setEncoding(StringEscapeUtils.escapeXml(s.getEncoding())); |
|
160 |
// s.setTerm(StringEscapeUtils.escapeXml(s.getTerm())); |
|
161 |
// } |
|
162 |
// for (Relation r : t.getRelations()) { |
|
163 |
// r.setType(StringEscapeUtils.escapeXml(r.getType())); |
|
164 |
// r.setValue(StringEscapeUtils.escapeXml(r.getValue())); |
|
165 |
// } |
|
166 |
// } |
|
167 |
// |
|
168 |
// StringTemplate st = new StringTemplate(IOUtils.toString(getClass().getResourceAsStream("/eu/dnetlib/functionality/modular/templates/terms.xml.st"))); |
|
169 |
// st.setAttribute("terms", terms); |
|
170 |
// registryLocator.getService().updateProfileNode(contextId, "//TERMS", st.toString()); |
|
171 |
// |
|
172 |
// return "Context saved correctly!"; |
|
173 |
// } |
|
174 |
// |
|
175 |
// @RequestMapping(value = "/ui/commitContextInfo", method = RequestMethod.POST) |
|
176 |
// public @ResponseBody String commitContextInfo(@RequestBody(required=true) final Context voc, |
|
177 |
// @RequestParam(value = "contextId", required = true) final String contextId) throws IOException, ISRegistryException, JAXBException { |
|
178 |
// log.info("committing context info id = " + contextId); |
|
179 |
// |
|
180 |
// // String xml = "<VOCABULARY_NAME code='{code}'>{name}</VOCABULARY_NAME>"; |
|
181 |
// // xml = xml.replace("{code}", StringEscapeUtils.escapeXml(voc.getCode())).replace("{name}", |
|
182 |
// // StringEscapeUtils.escapeXml(voc.getName())); |
|
183 |
// // registryLocator.getService().updateProfileNode(contextId, "//VOCABULARY_NAME", xml); |
|
184 |
// |
|
185 |
// String xml = "<VOCABULARY_DESCRIPTION>{desc}</VOCABULARY_DESCRIPTION>"; |
|
186 |
// xml = xml.replace("{desc}", StringEscapeUtils.escapeXml(voc.getDescription())); |
|
187 |
// registryLocator.getService().updateProfileNode(contextId, "//VOCABULARY_DESCRIPTION", xml); |
|
188 |
// |
|
189 |
// return "Context info saved correctly!"; |
|
190 |
// } |
|
191 |
// |
|
192 |
// @RequestMapping(value = "/ui/createContext", method = RequestMethod.POST) |
|
193 |
// public @ResponseBody String createContext(@RequestBody(required=true) final Context voc) throws IOException, ISRegistryException, JAXBException { |
|
194 |
// log.info("create context"); |
|
195 |
// |
|
196 |
// StringTemplate st = new StringTemplate( |
|
197 |
// IOUtils.toString(getClass().getResourceAsStream("/eu/dnetlib/functionality/modular/templates/context.xml.st"))); |
|
198 |
// st.setAttribute("name", voc.getName()); |
|
199 |
// st.setAttribute("description", voc.getDescription()); |
|
200 |
// st.setAttribute("code", voc.getCode()); |
|
201 |
// st.setAttribute("date", DateUtils.now_ISO8601()); |
|
202 |
// String newContextId = registryLocator.getService().registerProfile(st.toString()); |
|
203 |
// |
|
204 |
// return newContextId; |
|
205 |
// } |
|
206 |
// |
|
207 |
// @RequestMapping(value = "/ui/dropContext", method = RequestMethod.GET) |
|
208 |
// public @ResponseBody boolean dropContext(@RequestParam(value = "contextId", required = true) final String contextId) throws IOException, ISRegistryException, JAXBException { |
|
209 |
// log.info("delete context id=" + contextId); |
|
210 |
// |
|
211 |
// return registryLocator.getService().deleteProfile(contextId); |
|
212 |
// } |
|
148 |
|
|
149 |
// @RequestMapping(value = "/ui/commitContext", method = RequestMethod.POST) |
|
150 |
// public @ResponseBody String commitTerms(@RequestBody(required=true) final List<Term> terms, |
|
151 |
// @RequestParam(value = "contextId", required = true) final String contextId) throws IOException, ISRegistryException, JAXBException { |
|
152 |
// log.info("committing context id = " + contextId); |
|
153 |
// |
|
154 |
// // prepare terms for XML |
|
155 |
// for (Term t : terms) { |
|
156 |
// t.setCode(StringEscapeUtils.escapeXml(t.getCode())); |
|
157 |
// t.setEncoding(StringEscapeUtils.escapeXml(t.getEncoding())); |
|
158 |
// t.setEnglishName(StringEscapeUtils.escapeXml(t.getEnglishName())); |
|
159 |
// t.setNativeName(StringEscapeUtils.escapeXml(t.getNativeName())); |
|
160 |
// for (Synonym s : t.getSynonyms()) { |
|
161 |
// s.setEncoding(StringEscapeUtils.escapeXml(s.getEncoding())); |
|
162 |
// s.setTerm(StringEscapeUtils.escapeXml(s.getTerm())); |
|
163 |
// } |
|
164 |
// for (Relation r : t.getRelations()) { |
|
165 |
// r.setType(StringEscapeUtils.escapeXml(r.getType())); |
|
166 |
// r.setValue(StringEscapeUtils.escapeXml(r.getValue())); |
|
167 |
// } |
|
168 |
// } |
|
169 |
// |
|
170 |
// StringTemplate st = new |
|
171 |
// StringTemplate(IOUtils.toString(getClass().getResourceAsStream("/eu/dnetlib/functionality/modular/templates/terms.xml.st"))); |
|
172 |
// st.setAttribute("terms", terms); |
|
173 |
// registryLocator.getService().updateProfileNode(contextId, "//TERMS", st.toString()); |
|
174 |
// |
|
175 |
// return "Context saved correctly!"; |
|
176 |
// } |
|
177 |
// |
|
178 |
// @RequestMapping(value = "/ui/commitContextInfo", method = RequestMethod.POST) |
|
179 |
// public @ResponseBody String commitContextInfo(@RequestBody(required=true) final Context voc, |
|
180 |
// @RequestParam(value = "contextId", required = true) final String contextId) throws IOException, ISRegistryException, JAXBException { |
|
181 |
// log.info("committing context info id = " + contextId); |
|
182 |
// |
|
183 |
// // String xml = "<VOCABULARY_NAME code='{code}'>{name}</VOCABULARY_NAME>"; |
|
184 |
// // xml = xml.replace("{code}", StringEscapeUtils.escapeXml(voc.getCode())).replace("{name}", |
|
185 |
// // StringEscapeUtils.escapeXml(voc.getName())); |
|
186 |
// // registryLocator.getService().updateProfileNode(contextId, "//VOCABULARY_NAME", xml); |
|
187 |
// |
|
188 |
// String xml = "<VOCABULARY_DESCRIPTION>{desc}</VOCABULARY_DESCRIPTION>"; |
|
189 |
// xml = xml.replace("{desc}", StringEscapeUtils.escapeXml(voc.getDescription())); |
|
190 |
// registryLocator.getService().updateProfileNode(contextId, "//VOCABULARY_DESCRIPTION", xml); |
|
191 |
// |
|
192 |
// return "Context info saved correctly!"; |
|
193 |
// } |
|
194 |
// |
|
195 |
// @RequestMapping(value = "/ui/createContext", method = RequestMethod.POST) |
|
196 |
// public @ResponseBody String createContext(@RequestBody(required=true) final Context voc) throws IOException, ISRegistryException, |
|
197 |
// JAXBException { |
|
198 |
// log.info("create context"); |
|
199 |
// |
|
200 |
// StringTemplate st = new StringTemplate( |
|
201 |
// IOUtils.toString(getClass().getResourceAsStream("/eu/dnetlib/functionality/modular/templates/context.xml.st"))); |
|
202 |
// st.setAttribute("name", voc.getName()); |
|
203 |
// st.setAttribute("description", voc.getDescription()); |
|
204 |
// st.setAttribute("code", voc.getCode()); |
|
205 |
// st.setAttribute("date", DateUtils.now_ISO8601()); |
|
206 |
// String newContextId = registryLocator.getService().registerProfile(st.toString()); |
|
207 |
// |
|
208 |
// return newContextId; |
|
209 |
// } |
|
210 |
// |
|
211 |
// @RequestMapping(value = "/ui/dropContext", method = RequestMethod.GET) |
|
212 |
// public @ResponseBody boolean dropContext(@RequestParam(value = "contextId", required = true) final String contextId) throws |
|
213 |
// IOException, ISRegistryException, JAXBException { |
|
214 |
// log.info("delete context id=" + contextId); |
|
215 |
// |
|
216 |
// return registryLocator.getService().deleteProfile(contextId); |
|
217 |
// } |
|
213 | 218 |
} |
Also available in: Unified diff
removed lombok