Changeset 31285 for main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/CollectionGroups.java
- Timestamp:
- 2017-01-13T03:46:43+13:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/CollectionGroups.java
r31181 r31285 30 30 private static final String GROUP_CONTENT = "GroupCurrentContent"; 31 31 private static final String UNIQUE_COLLECTIONS = "UniqueCollections"; 32 private static final String COLLECTIONS_HIERARCHY = "CollectionsHierarchy"; 32 33 33 34 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.BerryBasket.class.getName()); … … 39 40 } 40 41 41 String[] _services = { GROUP_CONTENT, UNIQUE_COLLECTIONS };42 String[] _services = { GROUP_CONTENT, UNIQUE_COLLECTIONS, COLLECTIONS_HIERARCHY }; 42 43 43 44 public boolean configure(Element info, Element extra_info) { … … 56 57 } 57 58 // Load group configuration from file 58 readGroupConfiguration(); 59 60 return true; 59 return readGroupConfiguration(); 61 60 } 62 61 … … 86 85 groupPath = groupPath.replaceAll("(^/+)|(/+$)", ""); 87 86 88 Element mrCollectionList = get MRCollectionList(userContext);87 Element mrCollectionList = getAvailableCollectionList(userContext); 89 88 if (mrCollectionList == null){ 90 89 result.appendChild(doc.createElement(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER)); … … 92 91 } 93 92 //Get current groups and collections 94 Element groupContent = get CurrentContent(groupPath);93 Element groupContent = getRawCurrentContent(groupPath); 95 94 96 95 //Get ungrouped collection list … … 99 98 // If groupContent is empty return empty collection list 100 99 if (groupContent == null) { 101 // If config file is broken 102 if (hierarchy == null || groupDesc == null) { 103 //Get ungrouped collection list 104 result.appendChild(doc.importNode(ungroupedCollections, true)); 105 } else { 106 //If config is ok, but in this group no any collection or group 107 result.appendChild(doc.createElement(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER)); 108 } 100 result.appendChild(doc.createElement(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER)); 109 101 return result; 110 102 } … … 139 131 Element currentGroup = (Element) currentContent.item(i); 140 132 String currentGroupName = currentGroup.getAttribute(GSXML.NAME_ATT); 141 //Get group description 142 Element group = getGroupDescription(currentGroupName); 143 //Set position value 144 if (group != null) { 145 group.setAttribute(GSXML.POSITION_ATT, String.valueOf(i)); 146 result_group_list.appendChild(doc.importNode(group, true)); 147 } 148 133 Element groupDescription = getGroupDescription(currentGroupName); 134 135 groupDescription.setAttribute(GSXML.POSITION_ATT, String.valueOf(i)); 136 result_group_list.appendChild(doc.importNode(groupDescription, true)); 149 137 } 150 138 … … 152 140 //Add ungrouped collections if groupPath /+ or "" or null 153 141 if (groupPath.isEmpty()) { 154 155 142 156 143 //Add each ungrouped collection to the collection list in loop … … 183 170 Element resultCollections = doc.createElement(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER); 184 171 result.appendChild(resultCollections); 185 Element mrCollectionList = get MRCollectionList(userContext);172 Element mrCollectionList = getAvailableCollectionList(userContext); 186 173 //Collections from message router check 187 174 if (mrCollectionList == null){ … … 218 205 groups = GSXML.getValue(groupParam).split(","); 219 206 for (int i = 0; i < groups.length; i++) { 220 Element groupContent = get CurrentContent(groups[i]);207 Element groupContent = getRawCurrentContent(groups[i]); 221 208 //If group exists 222 209 if (groupContent != null) { … … 241 228 } 242 229 243 private Element getCurrentContent(String path) { 244 245 if (hierarchy == null || groupDesc == null) { 246 return null; 247 } 248 249 Document doc = XMLConverter.newDOM(); 250 230 protected Element processCollectionsHierarchy(Element request){ 231 232 Document doc = XMLConverter.newDOM(); 233 UserContext userContext = new UserContext(request); 234 // Prepare basic response 235 Element result = GSXML.createBasicResponse(doc, GSXML.SERVICE_TYPE_GROUPINFO); 236 String currentPath = ""; 237 Element currentContent = getRawCurrentContent(currentPath); 238 if (currentContent == null){ 239 return result; 240 } 241 Element searchableCollectionList = getSearchableCollectionList(userContext); 242 if (searchableCollectionList == null){ 243 return result; 244 } 245 246 //Get ungrouped collection list 247 sanitizeCurrentContent(currentContent, searchableCollectionList); 248 addGroupInfo(currentContent, currentPath); 249 addUngroupedCollections(currentContent, searchableCollectionList); 250 result.appendChild(doc.importNode(currentContent, true)); 251 return result; 252 } 253 254 private void addGroupInfo(Element currentContent, String groupPath) { 255 NodeList groups = GSXML.getChildrenByTagName(currentContent,GSXML.GROUP_ELEM); 256 for (int i=0;i<groups.getLength();i++){ 257 Element group = (Element) groups.item(i); 258 String name = group.getAttribute(GSXML.NAME_ATT); 259 String newPath = groupPath + "/" + name; 260 group.setAttribute(GSXML.PATH_ATT, newPath); 261 Element groupDescription = getGroupDescription(name); 262 Element titleEl = (Element) GSXML.getChildByTagName(groupDescription, GSXML.TITLE_ELEM); 263 String title = titleEl.getTextContent(); 264 group.setAttribute(GSXML.TITLE_ELEM, title ); 265 addGroupInfo(group, newPath); 266 } 267 268 } 269 270 private Element getRawCurrentContent(String path) { 271 251 272 if (path == null) { 252 273 path = ""; 253 274 } 254 275 276 Document doc = XMLConverter.newDOM(); 255 277 path = path.replaceAll("(/+)", "/"); 256 278 path = path.replaceAll("(^/+)|(/+$)", ""); … … 259 281 260 282 261 Element current View= (Element) hierarchy.cloneNode(true);283 Element currentContent = (Element) hierarchy.cloneNode(true); 262 284 // Get the current view 263 285 for (int i = 0; i < pathSteps.length; i++) { 264 286 if (!pathSteps[i].isEmpty()) { 265 current View = GSXML.getNamedElement(currentView, GSXML.GROUP_ELEM, GSXML.NAME_ATT, pathSteps[i]);266 if (current View== null){287 currentContent = GSXML.getNamedElement(currentContent, GSXML.GROUP_ELEM, GSXML.NAME_ATT, pathSteps[i]); 288 if (currentContent == null){ 267 289 break; 268 290 } 269 291 } 270 292 } 271 if (current View == null || !currentView.hasChildNodes()) {293 if (currentContent == null || !currentContent.hasChildNodes()) { 272 294 // Return to the main page 273 295 return null; 274 296 } 275 return currentView; 276 } 277 278 private void readGroupConfiguration() { 297 return currentContent; 298 } 299 300 private void sanitizeCurrentContent(Element currentContent, Element checkedCollectionList){ 301 if (currentContent == null){ 302 return; 303 } 304 NodeList nodes = currentContent.getElementsByTagName(GSXML.COLLECTION_ELEM); 305 for (int i = 0; i < nodes.getLength(); i++) { 306 Element element = (Element) nodes.item(i); 307 String name = element.getAttribute(GSXML.NAME_ATT); 308 Element checkedCollection = GSXML.getNamedElement(checkedCollectionList, GSXML.COLLECTION_ELEM, GSXML.NAME_ATT, name); 309 if (checkedCollection == null) { 310 element.getParentNode().removeChild(element); 311 i--; 312 } 313 } 314 } 315 316 private void addUngroupedCollections(Element currentContent, Element availableCollections){ 317 if (currentContent == null){ 318 return; 319 } 320 Document doc = currentContent.getOwnerDocument(); 321 NodeList collectionList = availableCollections.getElementsByTagName(GSXML.COLLECTION_ELEM); 322 for (int i = 0; i < collectionList.getLength();i++){ 323 Element collection = (Element) collectionList.item(i); 324 String name = collection.getAttribute(GSXML.NAME_ATT); 325 NodeList foundCollection = GSXML.getNamedElements(currentContent, GSXML.COLLECTION_ELEM, GSXML.NAME_ATT, name); 326 if (foundCollection.getLength() == 0){ 327 Element ungroupedCollection = doc.createElement(GSXML.COLLECTION_ELEM); 328 ungroupedCollection.setAttribute(GSXML.NAME_ATT, name); 329 currentContent.appendChild(ungroupedCollection); 330 } 331 } 332 333 } 334 335 private boolean readGroupConfiguration() { 279 336 280 337 File configFile = new File(GSFile.groupConfigFile(site_home)); … … 282 339 if (!configFile.exists()) { 283 340 logger.info("Groups config file " + configFile.getPath() + " does not exist."); 284 return ;341 return false; 285 342 } 286 343 // Try to read and catch exception if it fails … … 301 358 logger.error("Error occurred while trying to remove emtpy nodes from groupConfig.xml"); 302 359 e.printStackTrace(); 360 return false; 303 361 } 304 362 305 363 hierarchy = (Element) GSXML.getChildByTagName(content, GSXML.HIERARCHY_ELEM); 306 364 groupDesc = (Element) GSXML.getChildByTagName(content, GSXML.GROUP_DESC_ELEM); 365 if (hierarchy == null || groupDesc == null){ 366 logger.error("Error processing groups configuration. Check groupConfig.xml"); 367 return false; 368 } 369 verifyGroupDescription(); 370 return true; 371 } 372 373 private void verifyGroupDescription() { 374 Document doc = groupDesc.getOwnerDocument(); 375 NodeList groups = hierarchy.getElementsByTagName(GSXML.GROUP_ELEM); 376 for (int i = 0; i < groups.getLength(); i++) { 377 Element group = (Element) groups.item(i); 378 String name = group.getAttribute(GSXML.NAME_ATT); 379 Element foundDescription = GSXML.getNamedElement(groupDesc, GSXML.GROUP_ELEM, GSXML.NAME_ATT, name); 380 if (foundDescription == null){ 381 Element defaultDescription = doc.createElement(GSXML.GROUP_ELEM); 382 defaultDescription.setAttribute(GSXML.NAME_ATT, name); 383 Element groupTitle = doc.createElement(GSXML.TITLE_ELEM); 384 groupTitle.setTextContent(name); 385 defaultDescription.appendChild(groupTitle); 386 groupDesc.appendChild(defaultDescription); 387 } 388 } 389 307 390 } 308 391 309 392 private Element getGroupDescription(String name) { 310 if (groupDesc != null) { 311 Element description = (Element) GSXML.getNamedElement(groupDesc, GSXML.GROUP_ELEM, GSXML.NAME_ATT, name); 312 if (description != null) { 313 return description; 314 } 393 Element description = (Element) GSXML.getNamedElement(groupDesc, GSXML.GROUP_ELEM, GSXML.NAME_ATT, name); 394 if (description == null) { 315 395 logger.error("GroupDescription is not defined. Check your groupConfig.xml"); 316 } 317 318 logger.error("No group descriptions found. Check your groupConfig.xml"); 319 return null; 396 } 397 return description; 320 398 } 321 399 322 400 private Element getUngroupedCollections(Element mr_collection_list) { 323 401 324 if (groupDesc == null || hierarchy == null) {325 logger.error("No group descriptions in groupConfig.xml. Check your groupConfig.xml");326 //return mr_collection_list327 return mr_collection_list;328 }329 402 Document doc = XMLConverter.newDOM(); 330 403 // Create Set … … 366 439 private Element getPathInfo(String path) { 367 440 368 if (hierarchy == null || groupDesc == null) {369 return null;370 }371 372 441 Document doc = XMLConverter.newDOM(); 373 442 … … 387 456 pathStepDescription.setAttribute(GSXML.POSITION_ATT, String.valueOf(i)); 388 457 pathStepDescription.setAttribute(GSXML.PATH_ATT, currentPath); 389 pathInfo.appendChild(doc.importNode(pathStepDescription, true));390 }458 } 459 pathInfo.appendChild(doc.importNode(pathStepDescription, true)); 391 460 } 392 461 } … … 394 463 return pathInfo; 395 464 } 396 private Element get MRCollectionList(UserContext userContext){465 private Element getAvailableCollectionList(UserContext userContext){ 397 466 Document doc = XMLConverter.newDOM(); 398 467 // Get the message router info … … 415 484 return mr_collection_list; 416 485 } 486 private Element getSearchableCollectionList(UserContext userContext){ 487 Document doc = XMLConverter.newDOM(); 488 Element collectionList = doc.createElement(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER); 489 // Get the message router info 490 Element mr_info_message = doc.createElement(GSXML.MESSAGE_ELEM); 491 Element mr_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, "TextQuery" , userContext); 492 mr_info_message.appendChild(mr_request); 493 Element mr_info_response_message = (Element) this.router.process(mr_info_message); 494 if (mr_info_response_message == null) { 495 logger.error(" couldn't query the message router!"); 496 return null; 497 } 498 NodeList options = mr_info_response_message.getElementsByTagName(GSXML.PARAM_OPTION_ELEM); 499 for (int i = 0; i < options.getLength(); i++) { 500 Element option = (Element) options.item(i); 501 String name = option.getAttribute(GSXML.NAME_ATT); 502 if (name.equals("all")){ 503 continue; 504 } 505 Element collection = doc.createElement(GSXML.COLLECTION_ELEM); 506 collection.setAttribute(GSXML.NAME_ATT, name); 507 collectionList.appendChild(collection); 508 } 509 return collectionList; 510 } 417 511 }
Note:
See TracChangeset
for help on using the changeset viewer.