Source:
protected Collection<Map<String, ?>> replaceOtherWithTopSections(Collection<Map<String, ?>> originalTemplate) { List<Map<String, ?>> template = new LinkedList<Map<String, ?>>(); template.addAll(originalTemplate); Map<String, String> m = new HashMap<String, String>(); m.put("is_a", "HP:0000118"); Set<OntologyTerm> topSections = ontologyService.search(m); Set<String> topSectionsId = new HashSet<String>(); for (OntologyTerm section : topSections) { topSectionsId.add(section.getId()); } for (Map<String, ?> sectionTemplate : template) { try { Object templateCategoriesUC = sectionTemplate.get("categories"); if (templateCategoriesUC instanceof ArrayList) { ArrayList<String> templateCategories = (ArrayList<String>) templateCategoriesUC; for (String category : templateCategories) { topSectionsId.remove(category); } templateCategories.remove("HP:0000118"); if (templateCategories.isEmpty()) { template.remove(sectionTemplate); } } else { String templateCategory = (String) templateCategoriesUC; if (StringUtils.equals(templateCategory, "HP:0000118")) { template.remove(sectionTemplate); } else { topSectionsId.remove(templateCategory); } } } catch (Exception ex) { continue; } if (topSectionsId.isEmpty()) { break; } } for (String sectionId : topSectionsId) { OntologyTerm term = ontologyService.getTerm(sectionId); Map<String, Object> templateSection = new HashMap<String, Object>(); String title = term.getName(); templateSection.put(TYPE_KEY, ITEM_TYPE_SECTION); templateSection.put(TITLE_KEY, title); templateSection.put(CATEGORIES_KEY, Arrays.asList(sectionId)); templateSection.put(DATA_KEY, new ArrayList<Map<String, String>>()); template.add(templateSection); } return template; }

Target:
protected Collection<Map<String, ?>> replaceOtherWithTopSections(Collection<Map<String, ?>> originalTemplate) { List<Map<String, ?>> template = new LinkedList<Map<String, ?>>(); template.addAll(originalTemplate); Map<String, String> m = new HashMap<String, String>(); m.put("is_a", "HP:0000118"); Set<OntologyTerm> topSections = ontologyService.search(m); Set<String> topSectionsId = new HashSet<String>(); for (OntologyTerm section : topSections) { topSectionsId.add(section.getId()); } for (Map<String, ?> sectionTemplate : template) { try { Object templateCategoriesUC = sectionTemplate.get("categories"); if (templateCategoriesUC instanceof ArrayList) { ArrayList<String> templateCategories = (ArrayList<String>) templateCategoriesUC; for (String category : templateCategories) { topSectionsId.remove(category); } templateCategories.remove("HP:0000118"); if (templateCategories.isEmpty()) { template.remove(sectionTemplate); } } else { String templateCategory = (String) templateCategoriesUC; if (StringUtils.equals(templateCategory, "HP:0000118")) { template.remove(sectionTemplate); } else { topSectionsId.remove(templateCategory); } } } catch (Exception ex) { continue; } if (topSectionsId.isEmpty()) { break; } } for (String sectionId : topSectionsId) { OntologyTerm term = ontologyService.getTerm(sectionId); Map<String, Object> templateSection = new HashMap<String, Object>(); String title = term.getName(); title = title.replace("Abnormality of the ", "").replace("Abnormality of ", ""); title = WordUtils.capitalizeFully(title); templateSection.put(TYPE_KEY, ITEM_TYPE_SECTION); templateSection.put(TITLE_KEY, title); templateSection.put(CATEGORIES_KEY, Arrays.asList(sectionId)); templateSection.put(DATA_KEY, new ArrayList<Map<String, String>>()); template.add(templateSection); } return template; }