1 | package org.greenstone.gsdl3.service;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 |
|
---|
5 | import org.apache.log4j.Logger;
|
---|
6 | import org.greenstone.gsdl3.util.DisplayItemUtil;
|
---|
7 | import org.greenstone.gsdl3.util.GSFile;
|
---|
8 | import org.greenstone.gsdl3.util.GSXML;
|
---|
9 | import org.greenstone.gsdl3.util.Request;
|
---|
10 | import org.greenstone.gsdl3.util.UserContext;
|
---|
11 | import org.greenstone.gsdl3.util.XMLConverter;
|
---|
12 | import org.w3c.dom.Document;
|
---|
13 | import org.w3c.dom.Element;
|
---|
14 | import org.w3c.dom.Node;
|
---|
15 | import org.w3c.dom.NodeList;
|
---|
16 |
|
---|
17 | import java.util.Set;
|
---|
18 |
|
---|
19 | import javax.xml.xpath.XPathConstants;
|
---|
20 | import javax.xml.xpath.XPathExpression;
|
---|
21 | import javax.xml.xpath.XPathExpressionException;
|
---|
22 | import javax.xml.xpath.XPathFactory;
|
---|
23 |
|
---|
24 | import java.util.HashSet;
|
---|
25 | import java.util.HashMap;
|
---|
26 | import java.util.Iterator;
|
---|
27 |
|
---|
28 | public class CollectionGroups extends ServiceRack {
|
---|
29 |
|
---|
30 | private Element hierarchy = null;
|
---|
31 | private Element groupDesc = null;
|
---|
32 | private HashMap<String, Element> group_displayitems_map = null; // new one for display items
|
---|
33 | private HashMap<String, Element> group_extras_map = null; // all additional elements go in here, eg <title>
|
---|
34 | public static final String GROUP_CONTENT_SERVICE = "GroupCurrentContent";
|
---|
35 | // not used??
|
---|
36 | public static final String UNIQUE_COLLECTIONS_SERVICE = "UniqueCollections";
|
---|
37 | // not used??
|
---|
38 | public static final String COLLECTIONS_HIERARCHY_SERVICE = "CollectionsHierarchy";
|
---|
39 | // hack, should be getting this from library servlet
|
---|
40 | private static final String DEFAULT_LANG = "en";
|
---|
41 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.CollectionGroups.class.getName());
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | protected Element getServiceDescription(Document doc, String service, String lang, String subset) {
|
---|
45 | // TODO Auto-generated method stub
|
---|
46 | return null;
|
---|
47 | }
|
---|
48 |
|
---|
49 | String[] _services = { GROUP_CONTENT_SERVICE, UNIQUE_COLLECTIONS_SERVICE, COLLECTIONS_HIERARCHY_SERVICE };
|
---|
50 |
|
---|
51 | public boolean configure(Element info, Element extra_info) {
|
---|
52 | if (!super.configure(info, extra_info)) {
|
---|
53 | return false;
|
---|
54 | }
|
---|
55 |
|
---|
56 | group_displayitems_map = new HashMap<String, Element>();
|
---|
57 | group_extras_map = new HashMap<String, Element>();
|
---|
58 | logger.info("Configuring CollectionGroups...");
|
---|
59 | this.config_info = info;
|
---|
60 |
|
---|
61 | for (int i = 0; i < _services.length; i++) {
|
---|
62 | Element service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
|
---|
63 | service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_GROUPINFO);
|
---|
64 | service.setAttribute(GSXML.NAME_ATT, _services[i]);
|
---|
65 | this.short_service_info.appendChild(service);
|
---|
66 | }
|
---|
67 | // Load group configuration from file
|
---|
68 | return readGroupConfiguration();
|
---|
69 | }
|
---|
70 |
|
---|
71 | protected Element processGroupCurrentContent(Element request) {
|
---|
72 |
|
---|
73 | Document doc = XMLConverter.newDOM();
|
---|
74 | UserContext userContext = new UserContext(request);
|
---|
75 |
|
---|
76 | // Prepare basic response
|
---|
77 | Element result = GSXML.createBasicResponse(doc, GSXML.SERVICE_TYPE_GROUPINFO);
|
---|
78 |
|
---|
79 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
80 | // Get param list from request
|
---|
81 | Element paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
82 | // Set default group path (main page)
|
---|
83 | String groupPath = "";
|
---|
84 | // If param list not null and group param exists extract value and
|
---|
85 | // override groupPath variable
|
---|
86 | if (paramList != null) {
|
---|
87 | Element paramGroup = GSXML.getNamedElement(paramList, GSXML.PARAM_ELEM, GSXML.NAME_ATT, GSXML.GROUP_ELEM);
|
---|
88 | if (paramGroup != null) {
|
---|
89 | groupPath = paramGroup.getAttribute(GSXML.VALUE_ATT);
|
---|
90 | }
|
---|
91 | }
|
---|
92 | //Remove leading, ending / and duplicated /
|
---|
93 | groupPath = groupPath.replaceAll("(/+)", "/");
|
---|
94 | groupPath = groupPath.replaceAll("(^/+)|(/+$)", "");
|
---|
95 |
|
---|
96 | Element mrCollectionList = getAvailableCollectionList(userContext);
|
---|
97 | if (mrCollectionList == null){
|
---|
98 | result.appendChild(doc.createElement(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER));
|
---|
99 | return result;
|
---|
100 | }
|
---|
101 | //Get current groups and collections
|
---|
102 | Element groupContent = getRawCurrentContent(groupPath);
|
---|
103 |
|
---|
104 | //Get ungrouped collection list
|
---|
105 | Element ungroupedCollections = getUngroupedCollections(mrCollectionList);
|
---|
106 |
|
---|
107 | // If groupContent is empty return empty collection list
|
---|
108 | if (groupContent == null) {
|
---|
109 | result.appendChild(doc.createElement(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER));
|
---|
110 | return result;
|
---|
111 | }
|
---|
112 | NodeList currentContent = groupContent.getChildNodes();
|
---|
113 | if (currentContent != null && currentContent.getLength() > 0) {
|
---|
114 | // Create CollectionList element in response
|
---|
115 | Element result_collection_list = doc.createElement(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER);
|
---|
116 | result.appendChild(result_collection_list);
|
---|
117 | Element result_group_list = doc.createElement(GSXML.GROUP_ELEM + GSXML.LIST_MODIFIER);
|
---|
118 | result.appendChild(result_group_list);
|
---|
119 | // Iterate over collections from current view
|
---|
120 | // i represents current position in groupConfig.xml
|
---|
121 | int i;
|
---|
122 | for (i=0; i < currentContent.getLength(); i++) {
|
---|
123 | //logger.warn("NODE CuR Content" + GSXML.xmlNodeToString(currentContent.item(i)));
|
---|
124 |
|
---|
125 | if (currentContent.item(i).getNodeName() == GSXML.COLLECTION_ELEM) {
|
---|
126 |
|
---|
127 | Element collection = (Element) currentContent.item(i);
|
---|
128 | String collection_name = collection.getAttribute(GSXML.NAME_ATT);
|
---|
129 | // Check whether collection from current view exists in message router response
|
---|
130 | Element checkedCollection = GSXML.getNamedElement(mrCollectionList, GSXML.COLLECTION_ELEM,GSXML.NAME_ATT, collection_name);
|
---|
131 | if (checkedCollection != null) {
|
---|
132 | //Set position value
|
---|
133 | // checkedCollection.setAttribute(GSXML.POSITION_ATT, String.valueOf(i));
|
---|
134 | Element coll_copy = (Element)doc.importNode(checkedCollection, true);
|
---|
135 | coll_copy.setAttribute(GSXML.POSITION_ATT, String.valueOf(i));
|
---|
136 | // Add collection to response
|
---|
137 | result_collection_list.appendChild(coll_copy);
|
---|
138 | }
|
---|
139 |
|
---|
140 | //Iterate over groups in current view
|
---|
141 | } else if (currentContent.item(i).getNodeName() == GSXML.GROUP_ELEM) {
|
---|
142 | Element currentGroup = (Element) currentContent.item(i);
|
---|
143 | String currentGroupName = currentGroup.getAttribute(GSXML.NAME_ATT);
|
---|
144 | Element groupDescription = getGroupDescription(doc, currentGroupName, lang);
|
---|
145 |
|
---|
146 | groupDescription.setAttribute(GSXML.POSITION_ATT, String.valueOf(i));
|
---|
147 | result_group_list.appendChild(doc.importNode(groupDescription, true));
|
---|
148 | }
|
---|
149 |
|
---|
150 | }
|
---|
151 | //Add ungrouped collections if groupPath /+ or "" or null
|
---|
152 | if (groupPath.isEmpty()) {
|
---|
153 |
|
---|
154 | //Add each ungrouped collection to the collection list in loop
|
---|
155 | NodeList ungroupedCollectionNodes = GSXML.getChildrenByTagName(ungroupedCollections, GSXML.COLLECTION_ATT);
|
---|
156 | if (ungroupedCollectionNodes != null) {
|
---|
157 | for (int j = 0; j < ungroupedCollectionNodes.getLength(); j++) {
|
---|
158 | //logger.warn("UNGROUPED COLL ELEM" + GSXML.xmlNodeToString(ungroupedCollections).intern());
|
---|
159 | Element ungroupedCollection = (Element) doc.importNode(ungroupedCollectionNodes.item(j), true);
|
---|
160 | ungroupedCollection.setAttribute(GSXML.POSITION_ATT, String.valueOf(i++));
|
---|
161 | result_collection_list.appendChild(ungroupedCollection);
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | } else {
|
---|
166 | Element groupDescription = getPathInfo(groupPath, lang);
|
---|
167 | if (groupDescription != null){
|
---|
168 | result.appendChild(doc.importNode(groupDescription, true));
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | return result;
|
---|
174 | }
|
---|
175 |
|
---|
176 | protected Element processUniqueCollections(Element request) {
|
---|
177 | Document doc = XMLConverter.newDOM();
|
---|
178 | UserContext userContext = new UserContext(request);
|
---|
179 | // Prepare basic response
|
---|
180 | Element result = GSXML.createBasicResponse(doc, GSXML.SERVICE_TYPE_GROUPINFO);
|
---|
181 | Element resultCollections = doc.createElement(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER);
|
---|
182 | result.appendChild(resultCollections);
|
---|
183 | Element mrCollectionList = getAvailableCollectionList(userContext);
|
---|
184 | //Collections from message router check
|
---|
185 | if (mrCollectionList == null){
|
---|
186 | logger.error("mrCollectionList is null!");
|
---|
187 | return result;
|
---|
188 | }
|
---|
189 | //paramList check
|
---|
190 | Element paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
191 | if (paramList == null) {
|
---|
192 | logger.error("UniqueCollections request had no paramList.");
|
---|
193 | return result;
|
---|
194 | }
|
---|
195 | //Add collections from request
|
---|
196 | Set<String> uniq_colls = new HashSet<String>();
|
---|
197 | Element collParam = GSXML.getNamedElement(paramList, GSXML.PARAM_ELEM, GSXML.NAME_ATT, GSXML.COLLECTION_ELEM);
|
---|
198 | if (collParam != null){
|
---|
199 | String colls = GSXML.getValue(collParam);
|
---|
200 | if (!colls.isEmpty())
|
---|
201 | {
|
---|
202 | String[] _colls = colls.split(",");
|
---|
203 | for (int i=0;i < _colls.length;i++){
|
---|
204 | uniq_colls.add(_colls[i]);
|
---|
205 | }
|
---|
206 | }
|
---|
207 | }
|
---|
208 | //groupParam check
|
---|
209 | Element groupParam = GSXML.getNamedElement(paramList, GSXML.PARAM_ELEM, GSXML.NAME_ATT, GSXML.GROUP_ELEM);
|
---|
210 | if (groupParam == null) {
|
---|
211 | logger.error("UniqueCollections request had no groupParam.");
|
---|
212 | return result;
|
---|
213 | }
|
---|
214 | //Add collections from groups
|
---|
215 | String[] groups = null;
|
---|
216 | groups = GSXML.getValue(groupParam).split(",");
|
---|
217 | for (int i = 0; i < groups.length; i++) {
|
---|
218 | if (!groups[i].isEmpty()) {
|
---|
219 | Element groupContent = getRawCurrentContent(groups[i]);
|
---|
220 | // If group exists
|
---|
221 | if (groupContent != null) {
|
---|
222 | NodeList collectionNodes = groupContent.getElementsByTagName(GSXML.COLLECTION_ELEM);
|
---|
223 | for (int j = 0; j < collectionNodes.getLength(); j++) {
|
---|
224 | String collName = ((Element) collectionNodes.item(j)).getAttribute(GSXML.NAME_ATT);
|
---|
225 | uniq_colls.add(collName);
|
---|
226 | }
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | }
|
---|
231 | //Fill result collectionList
|
---|
232 | for (Iterator<String> iterator = uniq_colls.iterator(); iterator.hasNext();) {
|
---|
233 | String collectionName = (String) iterator.next();
|
---|
234 | Element checkedCollection = GSXML.getNamedElement(mrCollectionList, GSXML.COLLECTION_ELEM,GSXML.NAME_ATT, collectionName);
|
---|
235 | if (checkedCollection != null){
|
---|
236 | resultCollections.appendChild(doc.importNode(checkedCollection, true));
|
---|
237 | }
|
---|
238 | }
|
---|
239 | return result;
|
---|
240 |
|
---|
241 | }
|
---|
242 |
|
---|
243 | protected Element processCollectionsHierarchy(Element request){
|
---|
244 |
|
---|
245 | Document doc = XMLConverter.newDOM();
|
---|
246 | UserContext userContext = new UserContext(request);
|
---|
247 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
248 | // Prepare basic response
|
---|
249 | Element result = GSXML.createBasicResponse(doc, GSXML.SERVICE_TYPE_GROUPINFO);
|
---|
250 | String currentPath = "";
|
---|
251 | Element currentContent = getRawCurrentContent(currentPath);
|
---|
252 | if (currentContent == null){
|
---|
253 | return result;
|
---|
254 | }
|
---|
255 | Element searchableCollectionList = getSearchableCollectionList(userContext);
|
---|
256 | if (searchableCollectionList == null){
|
---|
257 | return result;
|
---|
258 | }
|
---|
259 |
|
---|
260 | //Get ungrouped collection list
|
---|
261 | sanitizeCurrentContent(currentContent, searchableCollectionList);
|
---|
262 | addGroupInfo(currentContent, currentPath, lang);
|
---|
263 | addUngroupedCollections(currentContent, searchableCollectionList);
|
---|
264 | addAllOption(currentContent);
|
---|
265 | result.appendChild(doc.importNode(currentContent, true));
|
---|
266 | return result;
|
---|
267 | }
|
---|
268 |
|
---|
269 | private void addAllOption(Element currentContent) {
|
---|
270 | if (currentContent == null){
|
---|
271 | return;
|
---|
272 | }
|
---|
273 | Document doc = currentContent.getOwnerDocument();
|
---|
274 | Element allOption = doc.createElement(GSXML.COLLECTION_ELEM);
|
---|
275 | allOption.setAttribute(GSXML.NAME_ATT, "all");
|
---|
276 | if (currentContent.hasChildNodes()){
|
---|
277 | currentContent.insertBefore(allOption,currentContent.getFirstChild());
|
---|
278 | } else
|
---|
279 | currentContent.appendChild(allOption);
|
---|
280 |
|
---|
281 |
|
---|
282 | }
|
---|
283 |
|
---|
284 | private void addGroupInfo(Element currentContent, String groupPath, String lang) {
|
---|
285 | NodeList groups = GSXML.getChildrenByTagName(currentContent,GSXML.GROUP_ELEM);
|
---|
286 | Document doc = currentContent.getOwnerDocument();
|
---|
287 | for (int i=0;i<groups.getLength();i++){
|
---|
288 | Element group = (Element) groups.item(i);
|
---|
289 | String name = group.getAttribute(GSXML.NAME_ATT);
|
---|
290 | String newPath = groupPath + "/" + name;
|
---|
291 | group.setAttribute(GSXML.PATH_ATT, newPath);
|
---|
292 | Element groupDescription = getGroupDescription(doc, name, lang);
|
---|
293 | Element titleEl = (Element) GSXML.getChildByTagName(groupDescription, GSXML.TITLE_ELEM);
|
---|
294 | String title;
|
---|
295 | if (titleEl != null) {
|
---|
296 | title = titleEl.getTextContent();
|
---|
297 | } else {
|
---|
298 | title = name;
|
---|
299 | }
|
---|
300 | group.setAttribute(GSXML.TITLE_ELEM, title );
|
---|
301 | addGroupInfo(group, newPath, lang);
|
---|
302 | }
|
---|
303 |
|
---|
304 | }
|
---|
305 |
|
---|
306 | private Element getRawCurrentContent(String path) {
|
---|
307 |
|
---|
308 | if (path == null) {
|
---|
309 | path = "";
|
---|
310 | }
|
---|
311 |
|
---|
312 | Document doc = XMLConverter.newDOM();
|
---|
313 | path = path.replaceAll("(/+)", "/");
|
---|
314 | path = path.replaceAll("(^/+)|(/+$)", "");
|
---|
315 |
|
---|
316 | String[] pathSteps = path.split("/");
|
---|
317 |
|
---|
318 |
|
---|
319 | Element currentContent = (Element)doc.importNode( hierarchy, true);//.cloneNode(true);
|
---|
320 | // Get the current view
|
---|
321 | for (int i = 0; i < pathSteps.length; i++) {
|
---|
322 | if (!pathSteps[i].isEmpty()) {
|
---|
323 | currentContent = GSXML.getNamedElement(currentContent, GSXML.GROUP_ELEM, GSXML.NAME_ATT, pathSteps[i]);
|
---|
324 | if (currentContent == null){
|
---|
325 | break;
|
---|
326 | }
|
---|
327 | }
|
---|
328 | }
|
---|
329 | if (currentContent == null || !currentContent.hasChildNodes()) {
|
---|
330 | // Return to the main page
|
---|
331 | return null;
|
---|
332 | }
|
---|
333 | return currentContent;
|
---|
334 | }
|
---|
335 |
|
---|
336 | private void sanitizeCurrentContent(Element currentContent, Element checkedCollectionList){
|
---|
337 | if (currentContent == null){
|
---|
338 | return;
|
---|
339 | }
|
---|
340 | NodeList nodes = currentContent.getElementsByTagName(GSXML.COLLECTION_ELEM);
|
---|
341 |
|
---|
342 | int num_nodes = nodes.getLength();
|
---|
343 | for (int i=num_nodes-1; i>=0; i--) {
|
---|
344 | Element element = (Element) nodes.item(i);
|
---|
345 | String name = element.getAttribute(GSXML.NAME_ATT);
|
---|
346 | Element checkedCollection = GSXML.getNamedElement(checkedCollectionList, GSXML.COLLECTION_ELEM, GSXML.NAME_ATT, name);
|
---|
347 | if (checkedCollection == null) {
|
---|
348 | currentContent.removeChild(element);
|
---|
349 |
|
---|
350 | }
|
---|
351 | }
|
---|
352 | }
|
---|
353 |
|
---|
354 | private void addUngroupedCollections(Element currentContent, Element availableCollections){
|
---|
355 | if (currentContent == null){
|
---|
356 | return;
|
---|
357 | }
|
---|
358 | Document doc = currentContent.getOwnerDocument();
|
---|
359 | NodeList collectionList = availableCollections.getElementsByTagName(GSXML.COLLECTION_ELEM);
|
---|
360 | for (int i = 0; i < collectionList.getLength();i++){
|
---|
361 | Element collection = (Element) collectionList.item(i);
|
---|
362 | String name = collection.getAttribute(GSXML.NAME_ATT);
|
---|
363 | NodeList foundCollection = GSXML.getNamedElements(currentContent, GSXML.COLLECTION_ELEM, GSXML.NAME_ATT, name);
|
---|
364 | if (foundCollection.getLength() == 0){
|
---|
365 | Element ungroupedCollection = doc.createElement(GSXML.COLLECTION_ELEM);
|
---|
366 | ungroupedCollection.setAttribute(GSXML.NAME_ATT, name);
|
---|
367 | currentContent.appendChild(ungroupedCollection);
|
---|
368 | }
|
---|
369 | }
|
---|
370 |
|
---|
371 | }
|
---|
372 |
|
---|
373 | private boolean readGroupConfiguration() {
|
---|
374 |
|
---|
375 | File configFile = new File(GSFile.groupConfigFile(site_home));
|
---|
376 | //
|
---|
377 | if (!configFile.exists()) {
|
---|
378 | logger.info("Groups config file " + configFile.getPath() + " does not exist.");
|
---|
379 | return false;
|
---|
380 | }
|
---|
381 | Document doc;
|
---|
382 | Element content;
|
---|
383 | try {
|
---|
384 | doc = XMLConverter.getDOM(configFile);
|
---|
385 | content = doc.getDocumentElement();
|
---|
386 | } catch (Exception e) {
|
---|
387 | System.out.println(e.getClass());
|
---|
388 | e.printStackTrace();
|
---|
389 | return false;
|
---|
390 | }
|
---|
391 | // XPath to find empty text nodes.
|
---|
392 | try {
|
---|
393 | XPathFactory xpathFactory = XPathFactory.newInstance();
|
---|
394 | XPathExpression xpathExp = xpathFactory.newXPath().compile("//text()[normalize-space(.) = '']");
|
---|
395 | NodeList emptyTextNodes = (NodeList) xpathExp.evaluate(doc, XPathConstants.NODESET);
|
---|
396 | for (int i = 0; i < emptyTextNodes.getLength(); i++) {
|
---|
397 | Node emptyTextNode = emptyTextNodes.item(i);
|
---|
398 | emptyTextNode.getParentNode().removeChild(emptyTextNode);
|
---|
399 | }
|
---|
400 |
|
---|
401 | } catch (XPathExpressionException e) {
|
---|
402 | logger.error("Error occurred while trying to remove emtpy nodes from groupConfig.xml");
|
---|
403 | e.printStackTrace();
|
---|
404 | return false;
|
---|
405 | }
|
---|
406 |
|
---|
407 | hierarchy = (Element) GSXML.getChildByTagName(content, GSXML.HIERARCHY_ELEM);
|
---|
408 | groupDesc = (Element) GSXML.getChildByTagName(content, GSXML.GROUP_DESC_ELEM);
|
---|
409 | if (hierarchy == null){
|
---|
410 | logger.error("<groupConfig> has no <hierarchy> element, ignoring it.");
|
---|
411 | return false;
|
---|
412 | }
|
---|
413 | verifyGroupDescription(doc);
|
---|
414 | return true;
|
---|
415 | }
|
---|
416 |
|
---|
417 | private void verifyGroupDescription(Document doc) {
|
---|
418 | if (groupDesc == null) {
|
---|
419 | // we set up one for ourselves
|
---|
420 | groupDesc = doc.createElement(GSXML.GROUP_DESC_ELEM);
|
---|
421 | }
|
---|
422 |
|
---|
423 | // Document doc = groupDesc.getOwnerDocument();
|
---|
424 | Document new_doc = XMLConverter.newDOM(); // use for the hash map
|
---|
425 | NodeList groups = hierarchy.getElementsByTagName(GSXML.GROUP_ELEM);
|
---|
426 | for (int i = 0; i < groups.getLength(); i++) {
|
---|
427 | Element group = (Element) groups.item(i);
|
---|
428 | String name = group.getAttribute(GSXML.NAME_ATT);
|
---|
429 | Element foundDescription = GSXML.getNamedElement(groupDesc, GSXML.GROUP_ELEM, GSXML.NAME_ATT, name);
|
---|
430 | if (foundDescription == null){
|
---|
431 | Element defaultDescription = doc.createElement(GSXML.GROUP_ELEM);
|
---|
432 | defaultDescription.setAttribute(GSXML.NAME_ATT, name);
|
---|
433 | Element groupTitle = doc.createElement(GSXML.TITLE_ELEM);
|
---|
434 | groupTitle.setTextContent(name);
|
---|
435 | defaultDescription.appendChild(groupTitle);
|
---|
436 |
|
---|
437 | Element titleDisplayItem = DisplayItemUtil.createDisplayItem(doc, GSXML.DISPLAY_TEXT_NAME, DEFAULT_LANG, name);
|
---|
438 | defaultDescription.appendChild(titleDisplayItem);
|
---|
439 | groupDesc.appendChild(defaultDescription);
|
---|
440 | foundDescription = defaultDescription;
|
---|
441 | }
|
---|
442 |
|
---|
443 | Element this_di_list = doc.createElement("dummy"+name);
|
---|
444 | DisplayItemUtil.storeDisplayItems(this_di_list, foundDescription);
|
---|
445 | group_displayitems_map.put(name, this_di_list);
|
---|
446 | // find all elements that are not dis
|
---|
447 | Element this_extras_list = doc.createElement("dummy"+name);
|
---|
448 | boolean found_items = storeNonDisplayItems(this_extras_list, foundDescription);
|
---|
449 | if (found_items) {
|
---|
450 | group_extras_map.put(name, this_extras_list);
|
---|
451 | }
|
---|
452 | }
|
---|
453 |
|
---|
454 | }
|
---|
455 |
|
---|
456 | private boolean storeNonDisplayItems(Element new_list, Element description) {
|
---|
457 | Document doc = new_list.getOwnerDocument();
|
---|
458 | Node child = description.getFirstChild();
|
---|
459 | boolean found_some = false;
|
---|
460 | while (child != null) {
|
---|
461 | if (child.getNodeType() == Node.ELEMENT_NODE && !child.getNodeName().equals(GSXML.DISPLAY_TEXT_ELEM)) {
|
---|
462 | new_list.appendChild(doc.importNode(child, true));
|
---|
463 | found_some = true;
|
---|
464 | }
|
---|
465 | child = child.getNextSibling();
|
---|
466 | }
|
---|
467 | return found_some;
|
---|
468 |
|
---|
469 | }
|
---|
470 | private Element getGroupDescriptionOld(String name) {
|
---|
471 | Element description = (Element) GSXML.getNamedElement(groupDesc, GSXML.GROUP_ELEM, GSXML.NAME_ATT, name);
|
---|
472 | if (description == null) {
|
---|
473 | logger.error("GroupDescription is not defined. Check your groupConfig.xml");
|
---|
474 | }
|
---|
475 | return description;
|
---|
476 | }
|
---|
477 | private Element getGroupDescription(Document doc, String name, String lang) {
|
---|
478 | Element new_di_list = doc.createElement(GSXML.GROUP_ELEM);
|
---|
479 | new_di_list.setAttribute(GSXML.NAME_ATT, name);
|
---|
480 |
|
---|
481 | // get all the display items for group
|
---|
482 | Element all_di = group_displayitems_map.get(name);
|
---|
483 | if (all_di != null) {
|
---|
484 | // instead of synchronising, take a copy and use that
|
---|
485 | Element all_di_copy = (Element)doc.importNode(all_di, true);
|
---|
486 |
|
---|
487 | //synchronized( all_di) {
|
---|
488 |
|
---|
489 | DisplayItemUtil.addLanguageSpecificDisplayItems(new_di_list, all_di_copy, lang, DEFAULT_LANG, this.class_loader);
|
---|
490 |
|
---|
491 | //}
|
---|
492 | }
|
---|
493 |
|
---|
494 | Element extras = group_extras_map.get(name);
|
---|
495 | if (extras != null) {
|
---|
496 | GSXML.copyAllChildren(new_di_list, extras);
|
---|
497 | }
|
---|
498 | /**
|
---|
499 | // now merge in the old style metadata - to be backwards compatible
|
---|
500 | Element old_style_desc = (Element) GSXML.getNamedElement(groupDesc, GSXML.GROUP_ELEM, GSXML.NAME_ATT, name);
|
---|
501 | if (old_style_desc != null) {
|
---|
502 | Element meta = (Element)GSXML.getChildByTagName(old_style_desc, "title");
|
---|
503 | if (meta != null) {
|
---|
504 | new_di_list.appendChild(doc.importNode(meta, true));
|
---|
505 | }
|
---|
506 | meta = (Element)GSXML.getChildByTagName(old_style_desc, "description");
|
---|
507 | if (meta != null) {
|
---|
508 | new_di_list.appendChild(doc.importNode(meta, true));
|
---|
509 | }
|
---|
510 | meta = (Element)GSXML.getChildByTagName(old_style_desc, "shortDescription");
|
---|
511 | if (meta != null) {
|
---|
512 | new_di_list.appendChild(doc.importNode(meta, true));
|
---|
513 | }
|
---|
514 |
|
---|
515 | meta = (Element)GSXML.getChildByTagName(old_style_desc, "backgroundImage");
|
---|
516 | if (meta != null) {
|
---|
517 | new_di_list.appendChild(doc.importNode(meta, true));
|
---|
518 | }
|
---|
519 | }
|
---|
520 | */
|
---|
521 |
|
---|
522 | return new_di_list;
|
---|
523 |
|
---|
524 | }
|
---|
525 |
|
---|
526 | private Element getUngroupedCollections(Element mr_collection_list) {
|
---|
527 |
|
---|
528 | Document doc = XMLConverter.newDOM();
|
---|
529 | // Create Set
|
---|
530 | Set<String> hierarchy_unique_collections = new HashSet<String>();
|
---|
531 | // Element hierarchyCollections = doc.createElement("coll_list");
|
---|
532 | // Get collection nodes
|
---|
533 |
|
---|
534 | NodeList hierarchy_all_collection_list = hierarchy.getElementsByTagName(GSXML.COLLECTION_ELEM);
|
---|
535 | // Save hierarchy collection names to Hashset
|
---|
536 | for (int i = 0; i < hierarchy_all_collection_list.getLength(); i++) {
|
---|
537 | Element collection_element = (Element) hierarchy_all_collection_list.item(i);
|
---|
538 | hierarchy_unique_collections.add(collection_element.getAttribute(GSXML.NAME_ATT));
|
---|
539 | }
|
---|
540 | // Save available by message router collection names to Hashset
|
---|
541 | Set<String> mr_collections = new HashSet<String>();
|
---|
542 | NodeList mr_coll_list = mr_collection_list.getElementsByTagName(GSXML.COLLECTION_ELEM);
|
---|
543 |
|
---|
544 | for (int i = 0; i < mr_coll_list.getLength(); i++) {
|
---|
545 | Element collection_element = (Element) mr_coll_list.item(i);
|
---|
546 | mr_collections.add(collection_element.getAttribute(GSXML.NAME_ATT));
|
---|
547 | }
|
---|
548 | //Save collections available by message router and not existed in hierarchy to ungrouped collections set
|
---|
549 | Set<String> ungrouped_collections = new HashSet<String>();
|
---|
550 | for (String string : mr_collections) {
|
---|
551 | if (!hierarchy_unique_collections.contains(string)){
|
---|
552 | ungrouped_collections.add(string);
|
---|
553 | }
|
---|
554 | }
|
---|
555 | //Output
|
---|
556 | Element result = doc.createElement(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER);
|
---|
557 | for (String name : ungrouped_collections) {
|
---|
558 | Element collection = doc.createElement(GSXML.COLLECTION_ELEM);
|
---|
559 | collection.setAttribute(GSXML.NAME_ATT, name);
|
---|
560 | result.appendChild(collection);
|
---|
561 | }
|
---|
562 | return result;
|
---|
563 |
|
---|
564 | }
|
---|
565 | private Element getPathInfo(String path, String lang) {
|
---|
566 |
|
---|
567 | Document doc = XMLConverter.newDOM();
|
---|
568 |
|
---|
569 | if (path == null) {
|
---|
570 | path = "";
|
---|
571 | }
|
---|
572 | String[] pathSteps = path.split("/");
|
---|
573 |
|
---|
574 | Element pathInfo = doc.createElement(GSXML.PATH_ELEM + GSXML.LIST_MODIFIER);
|
---|
575 |
|
---|
576 | String currentPath = "";
|
---|
577 | boolean first = true;
|
---|
578 | for (int i = 0; i < pathSteps.length; i++) {
|
---|
579 | if (!pathSteps[i].isEmpty()) {
|
---|
580 | if (first) {
|
---|
581 | first = false;
|
---|
582 | } else {
|
---|
583 | currentPath += "/";
|
---|
584 | }
|
---|
585 | currentPath += pathSteps[i];
|
---|
586 | Element pathStepDescription = getGroupDescription(doc, pathSteps[i], lang);
|
---|
587 | if (pathStepDescription != null){
|
---|
588 | pathStepDescription.setAttribute(GSXML.POSITION_ATT, String.valueOf(i));
|
---|
589 | pathStepDescription.setAttribute(GSXML.PATH_ATT, currentPath);
|
---|
590 | }
|
---|
591 | pathInfo.appendChild(doc.importNode(pathStepDescription, true));
|
---|
592 | }
|
---|
593 | }
|
---|
594 |
|
---|
595 | return pathInfo;
|
---|
596 | }
|
---|
597 | private Element getAvailableCollectionList(UserContext userContext){
|
---|
598 | Document doc = XMLConverter.newDOM();
|
---|
599 | // Get the message router info
|
---|
600 | Element inforesponseMessage = new Request(doc, userContext, router, GSXML.REQUEST_TYPE_DESCRIBE).send();
|
---|
601 | if (inforesponseMessage == null) {
|
---|
602 | logger.error(" couldn't query the message router!");
|
---|
603 | return null;
|
---|
604 | }
|
---|
605 | Element mr_info_response = (Element) GSXML.getChildByTagName(inforesponseMessage, GSXML.RESPONSE_ELEM);
|
---|
606 | if (mr_info_response == null) {
|
---|
607 | logger.error("Message router response is null!");
|
---|
608 | return null;
|
---|
609 | }
|
---|
610 |
|
---|
611 | Element mr_collection_list = (Element) GSXML.getChildByTagName(mr_info_response, GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER);
|
---|
612 |
|
---|
613 | return mr_collection_list;
|
---|
614 | }
|
---|
615 | private Element getSearchableCollectionList(UserContext userContext){
|
---|
616 | Document doc = XMLConverter.newDOM();
|
---|
617 | Element collectionList = doc.createElement(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER);
|
---|
618 | // Get the message router info
|
---|
619 | Element mr_info_message = doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
620 | Element mr_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, "TextQuery" , userContext);
|
---|
621 | mr_info_message.appendChild(mr_request);
|
---|
622 | Element mr_info_response_message = (Element) this.router.process(mr_info_message);
|
---|
623 | if (mr_info_response_message == null) {
|
---|
624 | logger.error(" couldn't query the message router!");
|
---|
625 | return null;
|
---|
626 | }
|
---|
627 | NodeList options = mr_info_response_message.getElementsByTagName(GSXML.PARAM_OPTION_ELEM);
|
---|
628 | for (int i = 0; i < options.getLength(); i++) {
|
---|
629 | Element option = (Element) options.item(i);
|
---|
630 | String name = option.getAttribute(GSXML.NAME_ATT);
|
---|
631 | if (name.equals("all")){
|
---|
632 | continue;
|
---|
633 | }
|
---|
634 | Element collection = doc.createElement(GSXML.COLLECTION_ELEM);
|
---|
635 | collection.setAttribute(GSXML.NAME_ATT, name);
|
---|
636 | collectionList.appendChild(collection);
|
---|
637 | }
|
---|
638 | return collectionList;
|
---|
639 | }
|
---|
640 | }
|
---|