private void updateConceptTerms(ThesaurusConcept concept, List<ThesaurusTerm> terms) {
    List<ThesaurusTerm> returnTerms = new ArrayList<ThesaurusTerm>();
    for (ThesaurusTerm thesaurusTerm : terms) {

      // We always set validated status to the terms that are joined to a
      // concept
      thesaurusTerm.setStatus(TermStatusEnum.VALIDATED.getStatus());
      thesaurusTerm.setConcept(concept);
      returnTerms.add(thesaurusTermDAO.update(thesaurusTerm));
    }
  }
  /**
   * Exports complex concept
   *
   * @param thesaurus
   * @param model
   * @return
   */
  public Model exportComplexConcept(Thesaurus thesaurus, Model model) {
    List<SplitNonPreferredTerm> complexConcepts =
        splitNonPreferredTermService.getSplitNonPreferredTermList(
            0, -1, thesaurus.getThesaurusId());
    if (!complexConcepts.isEmpty()) {

      for (SplitNonPreferredTerm complexConcept : complexConcepts) {

        Resource inScheme = model.createResource(complexConcept.getThesaurus().getIdentifier());

        // Add splitNonPreferredTerm resource
        Resource complexConceptRes =
            model.createResource(complexConcept.getIdentifier(), ISOTHES.SPLIT_NON_PREFERRED_TERM);

        model.add(complexConceptRes, SKOS.IN_SCHEME, inScheme);

        model.add(
            complexConceptRes,
            SKOSXL.LITERAL_FORM,
            StringEscapeUtils.unescapeXml(complexConcept.getLexicalValue()),
            complexConcept.getLanguage().getId());
        model.add(
            complexConceptRes,
            DCTerms.created,
            DateUtil.toISO8601String(complexConcept.getCreated()));
        model.add(
            complexConceptRes,
            DCTerms.modified,
            DateUtil.toISO8601String(complexConcept.getModified()));
        model.add(complexConceptRes, ISOTHES.STATUS, complexConcept.getStatus().toString());

        if (StringUtils.isNotEmpty(complexConcept.getSource())) {
          model.add(complexConceptRes, DC.source, complexConcept.getSource());
        }

        // Add compoundEquivalence resource

        Resource compoundEquivalenceRes = model.createResource(ISOTHES.COMPOUND_EQUIVALENCE);
        model.add(compoundEquivalenceRes, SKOS.IN_SCHEME, inScheme);
        model.add(compoundEquivalenceRes, ISOTHES.PLUS_UF, complexConcept.getIdentifier());
        for (ThesaurusTerm term : complexConcept.getPreferredTerms()) {
          model.add(compoundEquivalenceRes, ISOTHES.PLUS_USE, term.getIdentifier());
        }
      }
    }
    return model;
  }
  @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");
    }
  }
  @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));
  }
 @Override
 public String getConceptLabel(String conceptId) {
   ThesaurusTerm term = getConceptPreferredTerm(conceptId);
   return LabelUtil.getLocalizedLabel(term.getLexicalValue(), term.getLanguage(), defaultLang);
 }