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;
  }
 private void setExternalThesaurus(Alignment alignment, String extThesaurusId) {
   ExternalThesaurus existingExternalThes =
       externalThesaurusService.getThesaurusByExternalId(extThesaurusId);
   if (existingExternalThes == null) {
     ExternalThesaurus externalThesaurus = new ExternalThesaurus();
     externalThesaurus.setExternalId(extThesaurusId);
     externalThesaurus.setExternalThesaurusType(
         externalThesaurusTypeService.getExternalThesaurusTypeList().get(0));
     alignment.setExternalTargetThesaurus(externalThesaurus);
   } else {
     alignment.setExternalTargetThesaurus(existingExternalThes);
   }
 }
  @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");
    }
  }