@Test
 public void testGetDefaultAssociativeRelationshipRoleStatus() {
   ThesaurusConcept concept1 = new ThesaurusConcept();
   concept1.setIdentifier("http://www.culturecommunication.gouv.fr/co1");
   List<String> associatedConcepts =
       associativeRelationshipDAO.getAssociatedConcepts(concept1, ConceptStatusEnum.DEPRECATED);
   Assert.assertEquals(1, associatedConcepts.size());
   ListAssert.assertContains(associatedConcepts, "http://www.culturecommunication.gouv.fr/co2");
 }
  public Alignment buildAlignment(
      Statement stmt, AlignmentType alignmentType, ThesaurusConcept concept) {
    logger.debug("Building alignment " + stmt.getObject().toString());

    Alignment alignment = new Alignment();
    alignment.setIdentifier(generatorService.generate(Alignment.class));
    alignment.setAlignmentType(alignmentType);
    alignment.setAndRelation(false);
    alignment.setCreated(concept.getCreated());
    alignment.setModified(concept.getModified());
    alignment.setSourceConcept(concept);

    if (alignmentType.isResource()) {

      Set<AlignmentResource> targetResources = new HashSet<AlignmentResource>();
      AlignmentResource targetResource = new AlignmentResource();

      String externalTargetResource = stmt.getObject().toString();

      targetResource.setAlignment(alignment);
      targetResource.setExternalTargetResource(externalTargetResource);

      targetResources.add(targetResource);
      alignment.setTargetResources(targetResources);

    } else {

      Set<AlignmentConcept> targetConcepts = new HashSet<AlignmentConcept>();

      AlignmentConcept targetConcept = new AlignmentConcept();
      targetConcept.setAlignment(alignment);

      String targetConceptId = stmt.getObject().toString();
      ThesaurusConcept internalTargetConcept =
          thesaurusConceptService.getThesaurusConceptById(targetConceptId);
      if (internalTargetConcept != null) {
        targetConcept.setInternalTargetConcept(internalTargetConcept);
        alignment.setInternalTargetThesaurus(internalTargetConcept.getThesaurus());
      } else {
        // Check if the concept has an ark indentifier.

        Matcher arkMt = arkPt.matcher(targetConceptId);
        Matcher urlMt = urlPt.matcher(targetConceptId);

        if (arkMt.find()) {
          setExternalThesaurus(alignment, arkMt.group());
        } else if (urlMt.find()) {
          setExternalThesaurus(alignment, urlMt.group());
        }
        targetConcept.setExternalTargetConcept(targetConceptId);
      }

      targetConcepts.add(targetConcept);
      alignment.setTargetConcepts(targetConcepts);
    }
    return alignment;
  }
 @Override
 public int getStatusByConceptId(String conceptId) {
   ThesaurusConcept thesaurusConcept = thesaurusConceptDAO.getById(conceptId);
   if (thesaurusConcept != null) {
     return thesaurusConcept.getStatus();
   } else {
     throw new BusinessException(
         "Concept with identifier " + conceptId + " does not exist", "concepts-does-not-exist");
   }
 }
  private List<ThesaurusConcept> getRecursiveChildrenByConceptId(
      String conceptId, String originalParentId, List<ThesaurusConcept> allRecursiveChildren) {
    List<ThesaurusConcept> childrenConcepts =
        thesaurusConceptDAO.getChildrenConcepts(conceptId, 0, null);

    for (ThesaurusConcept concept : childrenConcepts) {
      if (concept.getIdentifier() != originalParentId) {
        if (!allRecursiveChildren.contains(concept)) {
          allRecursiveChildren.add(concept);
        }
        getRecursiveChildrenByConceptId(
            concept.getIdentifier(), originalParentId, allRecursiveChildren);
      }
    }
    return allRecursiveChildren;
  }
 @Override
 public List<ThesaurusConcept> getAllRootChildren(ThesaurusConcept concept) {
   Criteria criteria = getCurrentSession().createCriteria(ThesaurusConcept.class, "tc");
   criteria
       .createCriteria("tc.rootConcepts", "rc")
       .add(Restrictions.eq("rc.identifier", concept.getIdentifier()));
   return criteria.list();
 }
  @Transactional(readOnly = false)
  @Override
  public ThesaurusConcept destroyThesaurusConcept(ThesaurusConcept object) {
    if (object.getStatus() == ConceptStatusEnum.CANDIDATE.getStatus()
        || object.getStatus() == ConceptStatusEnum.REJECTED.getStatus()) {
      List<ThesaurusTerm> terms = thesaurusTermDAO.findTermsByConceptId(object.getIdentifier());
      for (ThesaurusTerm term : terms) {
        term.setConcept(null);
        thesaurusTermDAO.update(term);
      }

      List<ThesaurusConcept> childrenConcepts =
          getChildrenByConceptId(object.getIdentifier(), null);
      for (ThesaurusConcept childConcept : childrenConcepts) {
        childConcept.getParentConcepts().remove(object);
        thesaurusConceptDAO.update(childConcept);
      }

      List<ThesaurusConcept> rootChildrenConcepts = thesaurusConceptDAO.getAllRootChildren(object);
      for (ThesaurusConcept rootChild : rootChildrenConcepts) {
        rootChild.getRootConcepts().remove(object);
        thesaurusConceptDAO.update(rootChild);
      }

      List<ThesaurusArray> arrays =
          thesaurusArrayDAO.getConceptSuperOrdinateArrays(object.getIdentifier());
      for (ThesaurusArray array : arrays) {
        thesaurusArrayDAO.delete(array);
      }
      List<Alignment> alignments = alignmentDAO.findByTargetConceptId(object.getIdentifier());
      for (Alignment alignment : alignments) {
        if (alignment.getTargetConcepts().size() <= 1) {
          alignmentDAO.delete(alignment);
        }
      }

      return thesaurusConceptDAO.delete(object);
    } else {
      throw new BusinessException(
          "It's not possible to delete a concept with a status different from candidate or rejected",
          "delete-concept");
    }
  }
  private List<ThesaurusConcept> getRecursiveParentByConceptId(
      ThesaurusConcept beginConcept, List<ThesaurusConcept> arrayOfParent) {

    Set<ThesaurusConcept> parentConcepts = beginConcept.getParentConcepts();
    if (parentConcepts.size() > 0) {
      ThesaurusConcept parent = parentConcepts.iterator().next();
      arrayOfParent.add(parent);
      getRecursiveParentByConceptId(parent, arrayOfParent);
    }
    return arrayOfParent;
  }
 @Override
 public String getConceptTitleLanguage(ThesaurusConcept concept) {
   return getConceptPreferredTerm(concept.getIdentifier()).getLanguage().getPart1();
 }
 @Override
 public String getConceptTitle(ThesaurusConcept concept) {
   return getConceptPreferredTerm(concept.getIdentifier()).getLexicalValue();
 }
  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;
  }
  @Transactional(readOnly = false)
  @Override
  public ThesaurusConcept updateThesaurusConcept(
      ThesaurusConcept object,
      List<ThesaurusTerm> terms,
      List<AssociativeRelationship> associatedConcepts,
      List<ConceptHierarchicalRelationship> hierarchicalRelationships,
      List<ThesaurusConcept> childrenConceptToDetach,
      List<ThesaurusConcept> childrenConceptToAttach,
      List<Alignment> alignments) {

    thesaurusTermUtils.checkTerms(terms);
    alignmentService.saveExternalThesauruses(alignments);

    if (StringUtils.isNotEmpty(object.getIdentifier())) {
      List<ThesaurusTerm> existingTerms =
          thesaurusTermDAO.findTermsByConceptId(object.getIdentifier());
      for (ThesaurusTerm existingTerm : existingTerms) {
        if (!terms.contains(existingTerm)) {
          ThesaurusTerm term = thesaurusTermDAO.getById(existingTerm.getIdentifier());
          term.setConcept(null);
          thesaurusTermDAO.update(term);
          logger.info("Marking Term with ID " + existingTerm.getIdentifier() + " as SandBoxed.");
        }
      }
    } else {
      object.setIdentifier(generatorService.generate(ThesaurusConcept.class));
      for (ConceptHierarchicalRelationship hierarchicalRelationship : hierarchicalRelationships) {
        hierarchicalRelationship.getIdentifier().setChildconceptid(object.getIdentifier());
      }
      for (AssociativeRelationship relation : associatedConcepts) {
        relation.getIdentifier().setConcept1(object.getIdentifier());
      }
    }
    List<ThesaurusConcept> allRecursiveChild =
        getRecursiveChildrenByConceptId(object.getIdentifier());
    List<ThesaurusConcept> allRecursiveParents =
        getRecursiveParentsByConceptId(object.getIdentifier());

    object =
        conceptHierarchicalRelationshipServiceUtil.saveHierarchicalRelationship(
            object,
            hierarchicalRelationships,
            allRecursiveParents,
            allRecursiveChild,
            childrenConceptToDetach,
            childrenConceptToAttach);

    if (object.getStatus() == ConceptStatusEnum.CANDIDATE.getStatus()) {
      // We can set status = candidate only if concept has not relation
      // (both hierarchical or associative)
      if (!associatedConcepts.isEmpty()
          || !object.getParentConcepts().isEmpty()
          || hasChildren(object.getIdentifier())) {
        throw new BusinessException(
            "A concept must not have a status candidate when it still have relations",
            "concept-status-candidate-relation");
      }
    }

    if (object.getStatus() == ConceptStatusEnum.VALIDATED.getStatus()) {
      // Test if parent concepts are validated
      Set<ThesaurusConcept> parents = object.getParentConcepts();
      for (ThesaurusConcept parent : parents) {
        if (parent.getStatus() != ConceptStatusEnum.VALIDATED.getStatus()) {
          throw new BusinessException(
              "A concept cannot have a parent which status is not validated",
              "concept-parent-not-validated");
        }
      }
    }

    ThesaurusConcept concept = thesaurusConceptDAO.update(object);
    updateConceptTerms(concept, terms);

    alignmentService.saveAlignments(concept, alignments);
    if (!alignments.isEmpty()) {
      alignmentService.deleteExternalThesauruses();
    }
    return thesaurusConceptDAO.update(saveAssociativeRelationship(concept, associatedConcepts));
  }