private ThesaurusConcept saveAssociativeRelationship( ThesaurusConcept concept, List<AssociativeRelationship> associatedConcepts) { Set<AssociativeRelationship> relations = new HashSet<AssociativeRelationship>(); if (concept.getAssociativeRelationshipLeft() == null) { concept.setAssociativeRelationshipLeft(new HashSet<AssociativeRelationship>()); } else { for (AssociativeRelationship associativeRelationship : concept.getAssociativeRelationshipLeft()) { associativeRelationshipDAO.delete(associativeRelationship); } } concept.getAssociativeRelationshipLeft().clear(); if (concept.getAssociativeRelationshipRight() == null) { concept.setAssociativeRelationshipRight(new HashSet<AssociativeRelationship>()); } else { for (AssociativeRelationship associativeRelationship : concept.getAssociativeRelationshipRight()) { associativeRelationshipDAO.delete(associativeRelationship); } } concept.getAssociativeRelationshipRight().clear(); for (AssociativeRelationship association : associatedConcepts) { logger.debug("Settings associated concept " + association); if (association.getIdentifier().getConcept1() == association.getIdentifier().getConcept2()) { throw new BusinessException( "It's not possible to make association to itself!", "association-to-itself-error"); } ThesaurusConcept linkedThesaurusConcept = association.getConceptRight(); if (linkedThesaurusConcept.getStatus() != ConceptStatusEnum.VALIDATED.getStatus()) { throw new BusinessException( "A concept must associate a validated concept", "concept-associate-validated-concept"); } List<String> alreadyAssociatedConcepts = associativeRelationshipDAO.getAssociatedConcepts(linkedThesaurusConcept); if (!alreadyAssociatedConcepts.contains(concept.getIdentifier())) { relations.add(association); if (association.getRelationshipRole() == null) { association.setRelationshipRole(new AssociativeRelationshipRole()); } if (StringUtils.isEmpty(association.getRelationshipRole().getCode())) { association .getRelationshipRole() .setCode( associativeRelationshipRoleDAO.getDefaultAssociativeRelationshipRole().getCode()); } } associativeRelationshipDAO.update(association); } concept.getAssociativeRelationshipLeft().addAll(relations); return concept; }
@Test public void testGetRelationshipRole() { AssociativeRelationship associativeRelationship = associativeRelationshipDAO.getAssociativeRelationship( "http://www.culturecommunication.gouv.fr/co1", "http://www.culturecommunication.gouv.fr/co2"); Assert.assertEquals("TA", associativeRelationship.getRelationshipRole().getCode()); AssociativeRelationship associativeRelationship2 = associativeRelationshipDAO.getAssociativeRelationship( "http://www.culturecommunication.gouv.fr/co2", "http://www.culturecommunication.gouv.fr/co1"); Assert.assertEquals("TA", associativeRelationship2.getRelationshipRole().getCode()); }