<commit_before>/**
 * Copyright (C) 2000 - 2009 Silverpeas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * As a special exception to the terms and conditions of version 3.0 of
 * the GPL, you may redistribute this Program in connection with Free/Libre
 * Open Source Software ("FLOSS") applications as described in Silverpeas's
 * FLOSS exception.  You should have received a copy of the text describing
 * the FLOSS exception, and it is also available here:
 * "http://repository.silverpeas.com/legal/licensing"
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * Created on 25 janv. 2005
 */
package com.silverpeas.kmelia.importexport;

import java.rmi.RemoteException;
import java.util.Date;

import com.silverpeas.importExport.control.GEDImportExport;
import com.silverpeas.importExport.model.ImportExportException;
import com.silverpeas.importExport.report.MassiveReport;
import com.silverpeas.importExport.report.UnitReport;
import com.silverpeas.util.StringUtil;
import com.stratelia.silverpeas.silvertrace.SilverTrace;
import com.stratelia.webactiv.beans.admin.ObjectType;
import com.stratelia.webactiv.beans.admin.OrganizationController;
import com.stratelia.webactiv.beans.admin.UserDetail;
import com.stratelia.webactiv.kmelia.KmeliaException;
import com.stratelia.webactiv.kmelia.control.ejb.KmeliaBm;
import com.stratelia.webactiv.kmelia.control.ejb.KmeliaBmHome;
import com.stratelia.webactiv.kmelia.control.ejb.KmeliaHelper;
import com.stratelia.webactiv.util.EJBUtilitaire;
import com.stratelia.webactiv.util.JNDINames;
import com.stratelia.webactiv.util.node.model.NodeDetail;
import com.stratelia.webactiv.util.node.model.NodePK;
import com.stratelia.webactiv.util.publication.model.CompletePublication;
import com.stratelia.webactiv.util.publication.model.PublicationDetail;
import com.stratelia.webactiv.util.publication.model.PublicationPK;

/**
 * Classe métier de création d'entités silverpeas utilisée par le moteur d'importExport.
 * @author sDevolder.
 */
public class KmeliaImportExport extends GEDImportExport {

  // Variables
  private static OrganizationController org_Ctrl = new OrganizationController();

  /**
   * Constructeur public de la classe
   * @param userDetail - informations sur l'utilisateur faisant appel au moteur d'importExport
   * @param targetComponentId - composant silverpeas cible
   * @param topicId - topic cible du composant targetComponentId
   */
  public KmeliaImportExport(UserDetail curentUserDetail, String currentComponentId) {
    super(curentUserDetail, currentComponentId);
  }

  /**
   * @return l'EJB KmeliaBm
   * @throws ImportExportException
   */
  protected KmeliaBm getKmeliaBm() throws ImportExportException {

    KmeliaBm kmeliaBm = null;

    try {
      KmeliaBmHome ejbHome = (KmeliaBmHome) EJBUtilitaire.getEJBObjectRef(
          JNDINames.KMELIABM_EJBHOME, KmeliaBmHome.class);
      kmeliaBm = ejbHome.create();

    } catch (Exception e) {
      throw new ImportExportException("KmeliaImportExport.getKmeliaBm()",
          "root.EX_CANT_GET_REMOTE_OBJECT", e);
    }

    return kmeliaBm;
  }

  @Override
  protected void updatePublication(PublicationDetail pubDet_temp,
      PublicationDetail pubDetailToCreate, UserDetail userDetail) throws Exception {
    // Ces tests sont utiles dans le cas d'une publication à mettre à jour par id
    if (StringUtil.isDefined(pubDetailToCreate.getName())) {
      pubDet_temp.setName(pubDetailToCreate.getName());
    }
    if (StringUtil.isDefined(pubDetailToCreate.getDescription())) {
      pubDet_temp.setDescription(pubDetailToCreate.getDescription());
    }
    if (StringUtil.isDefined(pubDetailToCreate.getVersion())) {
      pubDet_temp.setVersion(pubDetailToCreate.getVersion());
    }
    if (StringUtil.isDefined(pubDetailToCreate.getKeywords())) {
      pubDet_temp.setKeywords(pubDetailToCreate.getKeywords());
    }
    if (StringUtil.isDefined(pubDetailToCreate.getStatus())) {
      pubDet_temp.setStatusMustBeChecked(false);
      pubDet_temp.setStatus(pubDetailToCreate.getStatus());
    }
    
    pubDet_temp.setUpdateDate(new Date());
    pubDet_temp.setUpdaterId(userDetail.getId());
    getKmeliaBm().updatePublication(pubDet_temp);
  }

  @Override
  protected String createPublicationIntoTopic(PublicationDetail pubDet_temp, NodePK topicPK,
      UserDetail userDetail) throws Exception {

    if (pubDet_temp.isStatusMustBeChecked()) {
      String profile = "writer";
      if ("yes".equalsIgnoreCase(org_Ctrl.getComponentParameterValue(topicPK.getInstanceId(),
          "rightsOnTopics"))) {
        NodeDetail topic = getNodeBm().getHeader(topicPK);
        if (topic.haveRights()) {
          profile = KmeliaHelper.getProfile(org_Ctrl.getUserProfiles(userDetail.getId(), topicPK
              .getInstanceId(), topic.getRightsDependsOn(), ObjectType.NODE));
        } else {
          profile = KmeliaHelper.getProfile(org_Ctrl.getUserProfiles(userDetail.getId(), topicPK
              .getInstanceId()));
        }
      } else {
        profile = KmeliaHelper.getProfile(org_Ctrl.getUserProfiles(userDetail.getId(), topicPK
            .getInstanceId()));
      }
      if ("publisher".equals(profile) || "admin".equals(profile)) {
        pubDet_temp.setStatus(PublicationDetail.VALID);
      } else {
        pubDet_temp.setStatus(PublicationDetail.TO_VALIDATE);
      }
    }
    return getKmeliaBm().createPublicationIntoTopic(pubDet_temp, topicPK);
  }

  @Override
  protected void addPublicationToTopic(PublicationPK pubPK, NodePK topicPK) throws Exception {
    getKmeliaBm().addPublicationToTopic(pubPK, topicPK, false);
  }

  /**
   * Méthode ajoutant un thème à un thème déja existant. Si le thème à ajouter existe lui aussi (par
   * exemple avec un même ID), il n'est pas modifié et la méthode ne fait rien et ne lève aucune
   * exception.
   * @param nodeDetail le détail du noeud à ajouter.
   * @param topicId l'identifiant du noeud parent, ou 0 pour désigner le noeud racine.
   * @param unitReport le rapport d'import unitaire.
   * @return un objet clé primaire du nouveau thème créé ou du thème déjà existant (thème de même
   * identifiant non modifié).
   * @throws ImportExportException en cas d'anomalie lors de la création du noeud.
   * @see com.silverpeas.importExport.control.GEDImportExport#addSubTopicToTopic(com.stratelia.webactiv.util.node.model.NodeDetail,
   * int, com.silverpeas.importExport.report.UnitReport)
   */
  @Override
  protected NodePK addSubTopicToTopic(NodeDetail nodeDetail, int topicId, UnitReport unitReport)
      throws ImportExportException {

    try {
      NodePK nodePk = nodeDetail.getNodePK();
      nodePk.setComponentName(getCurrentComponentId());
      nodeDetail.setCreatorId(getCurentUserDetail().getId());

      // Recherche si le noeud existe déjà, on s'arrête si c'est le cas
      try {
        if (getNodeBm().getHeader(nodePk) != null) {
          return null;
        }
      } catch (RemoteException ex) {
        SilverTrace.debug("importExport", "KmeliaImportExport.addSubTopicToTopic()",
            "root.EX_NO_MESSAGE", ex);
      }

      // S'il n'existe pas encore, on le crée et on le configure
      NodePK parentTopicPk = new NodePK(Integer.toString(topicId), getCurrentComponentId());
      NodePK newTopicPk = getKmeliaBm().addSubTopic(parentTopicPk, nodeDetail, "None");

      if (Integer.parseInt(newTopicPk.getId()) < 0) {
        unitReport.setError(UnitReport.ERROR_ERROR);
        SilverTrace.error("importExport", "KmeliaImportExport.addSubTopicToTopic()",
            "root.EX_NO_MESSAGE");
        throw new ImportExportException("KmeliaImportExport.addSubTopicToTopic",
            "importExport.EX_NODE_CREATE");
      }

      unitReport.setStatus(UnitReport.STATUS_TOPIC_CREATED);

      return newTopicPk;

    } catch (RemoteException ex) {<commit_msg>fix bug #1380<commit_after>/**
 * Copyright (C) 2000 - 2009 Silverpeas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * As a special exception to the terms and conditions of version 3.0 of
 * the GPL, you may redistribute this Program in connection with Free/Libre
 * Open Source Software ("FLOSS") applications as described in Silverpeas's
 * FLOSS exception.  You should have received a copy of the text describing
 * the FLOSS exception, and it is also available here:
 * "http://repository.silverpeas.com/legal/licensing"
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * Created on 25 janv. 2005
 */
package com.silverpeas.kmelia.importexport;

import java.rmi.RemoteException;
import java.util.Date;

import com.silverpeas.importExport.control.GEDImportExport;
import com.silverpeas.importExport.model.ImportExportException;
import com.silverpeas.importExport.report.MassiveReport;
import com.silverpeas.importExport.report.UnitReport;
import com.silverpeas.util.StringUtil;
import com.stratelia.silverpeas.silvertrace.SilverTrace;
import com.stratelia.webactiv.beans.admin.ObjectType;
import com.stratelia.webactiv.beans.admin.OrganizationController;
import com.stratelia.webactiv.beans.admin.UserDetail;
import com.stratelia.webactiv.kmelia.KmeliaException;
import com.stratelia.webactiv.kmelia.control.ejb.KmeliaBm;
import com.stratelia.webactiv.kmelia.control.ejb.KmeliaBmHome;
import com.stratelia.webactiv.kmelia.control.ejb.KmeliaHelper;
import com.stratelia.webactiv.util.EJBUtilitaire;
import com.stratelia.webactiv.util.JNDINames;
import com.stratelia.webactiv.util.node.model.NodeDetail;
import com.stratelia.webactiv.util.node.model.NodePK;
import com.stratelia.webactiv.util.publication.model.CompletePublication;
import com.stratelia.webactiv.util.publication.model.PublicationDetail;
import com.stratelia.webactiv.util.publication.model.PublicationPK;

/**
 * Classe métier de création d'entités silverpeas utilisée par le moteur d'importExport.
 * @author sDevolder.
 */
public class KmeliaImportExport extends GEDImportExport {

  // Variables
  private static OrganizationController org_Ctrl = new OrganizationController();

  /**
   * Constructeur public de la classe
   * @param userDetail - informations sur l'utilisateur faisant appel au moteur d'importExport
   * @param targetComponentId - composant silverpeas cible
   * @param topicId - topic cible du composant targetComponentId
   */
  public KmeliaImportExport(UserDetail curentUserDetail, String currentComponentId) {
    super(curentUserDetail, currentComponentId);
  }

  /**
   * @return l'EJB KmeliaBm
   * @throws ImportExportException
   */
  protected KmeliaBm getKmeliaBm() throws ImportExportException {

    KmeliaBm kmeliaBm = null;

    try {
      KmeliaBmHome ejbHome = (KmeliaBmHome) EJBUtilitaire.getEJBObjectRef(
          JNDINames.KMELIABM_EJBHOME, KmeliaBmHome.class);
      kmeliaBm = ejbHome.create();

    } catch (Exception e) {
      throw new ImportExportException("KmeliaImportExport.getKmeliaBm()",
          "root.EX_CANT_GET_REMOTE_OBJECT", e);
    }

    return kmeliaBm;
  }

  @Override
  protected void updatePublication(PublicationDetail pubDet_temp,
      PublicationDetail pubDetailToCreate, UserDetail userDetail) throws Exception {
    // Ces tests sont utiles dans le cas d'une publication à mettre à jour par id
    if (StringUtil.isDefined(pubDetailToCreate.getName())) {
      pubDet_temp.setName(pubDetailToCreate.getName());
    }
    if (StringUtil.isDefined(pubDetailToCreate.getDescription())) {
      pubDet_temp.setDescription(pubDetailToCreate.getDescription());
    }
    if (StringUtil.isDefined(pubDetailToCreate.getVersion())) {
      pubDet_temp.setVersion(pubDetailToCreate.getVersion());
    }
    if (StringUtil.isDefined(pubDetailToCreate.getKeywords())) {
      pubDet_temp.setKeywords(pubDetailToCreate.getKeywords());
    }
    if (StringUtil.isDefined(pubDetailToCreate.getStatus())) {
      pubDet_temp.setStatusMustBeChecked(false);
      pubDet_temp.setStatus(pubDetailToCreate.getStatus());
    }
    
    pubDet_temp.setUpdateDate(new Date());
    pubDet_temp.setUpdaterId(userDetail.getId());
    getKmeliaBm().updatePublication(pubDet_temp);
  }

  @Override
  protected String createPublicationIntoTopic(PublicationDetail pubDet_temp, NodePK topicPK,
      UserDetail userDetail) throws Exception {

    if (pubDet_temp.isStatusMustBeChecked()) {
      String profile = "writer";
      if ("yes".equalsIgnoreCase(org_Ctrl.getComponentParameterValue(topicPK.getInstanceId(),
          "rightsOnTopics"))) {
        NodeDetail topic = getNodeBm().getHeader(topicPK);
        if (topic.haveRights()) {
          profile = KmeliaHelper.getProfile(org_Ctrl.getUserProfiles(userDetail.getId(), topicPK
              .getInstanceId(), topic.getRightsDependsOn(), ObjectType.NODE));
        } else {
          profile = KmeliaHelper.getProfile(org_Ctrl.getUserProfiles(userDetail.getId(), topicPK
              .getInstanceId()));
        }
      } else {
        profile = KmeliaHelper.getProfile(org_Ctrl.getUserProfiles(userDetail.getId(), topicPK
            .getInstanceId()));
      }
      if ("publisher".equals(profile) || "admin".equals(profile)) {
        pubDet_temp.setStatus(PublicationDetail.VALID);
      } else {
        pubDet_temp.setStatus(PublicationDetail.TO_VALIDATE);
      }
    }
    return getKmeliaBm().createPublicationIntoTopic(pubDet_temp, topicPK);
  }

  @Override
  protected void addPublicationToTopic(PublicationPK pubPK, NodePK topicPK) throws Exception {
    getKmeliaBm().addPublicationToTopic(pubPK, topicPK, false);
  }

  /**
   * Méthode ajoutant un thème à un thème déja existant. Si le thème à ajouter existe lui aussi (par
   * exemple avec un même ID), il n'est pas modifié et la méthode ne fait rien et ne lève aucune
   * exception.
   * @param nodeDetail le détail du noeud à ajouter.
   * @param topicId l'identifiant du noeud parent, ou 0 pour désigner le noeud racine.
   * @param unitReport le rapport d'import unitaire.
   * @return un objet clé primaire du nouveau thème créé ou du thème déjà existant (thème de même
   * identifiant non modifié).
   * @throws ImportExportException en cas d'anomalie lors de la création du noeud.
   * @see com.silverpeas.importExport.control.GEDImportExport#addSubTopicToTopic(com.stratelia.webactiv.util.node.model.NodeDetail,
   * int, com.silverpeas.importExport.report.UnitReport)
   */
  @Override
  protected NodePK addSubTopicToTopic(NodeDetail nodeDetail, int topicId, UnitReport unitReport)
      throws ImportExportException {

    try {
      NodePK nodePk = nodeDetail.getNodePK();
      nodePk.setComponentName(getCurrentComponentId());
      nodeDetail.setCreatorId(getCurentUserDetail().getId());

      // Recherche si le noeud existe déjà, on s'arrête si c'est le cas
      try {
        NodeDetail header = getNodeBm().getHeader(nodePk);
        if (header != null) {
          return header.getNodePK();
        }
      } catch (RemoteException ex) {
        SilverTrace.debug("importExport", "KmeliaImportExport.addSubTopicToTopic()",
            "root.EX_NO_MESSAGE", ex);
      }

      // S'il n'existe pas encore, on le crée et on le configure
      NodePK parentTopicPk = new NodePK(Integer.toString(topicId), getCurrentComponentId());
      NodePK newTopicPk = getKmeliaBm().addSubTopic(parentTopicPk, nodeDetail, "None");

      if (Integer.parseInt(newTopicPk.getId()) < 0) {
        unitReport.setError(UnitReport.ERROR_ERROR);
        SilverTrace.error("importExport", "KmeliaImportExport.addSubTopicToTopic()",
            "root.EX_NO_MESSAGE");
        throw new ImportExportException("KmeliaImportExport.addSubTopicToTopic",
            "importExport.EX_NODE_CREATE");
      }

      unitReport.setStatus(UnitReport.STATUS_TOPIC_CREATED);

      return newTopicPk;

    } catch (RemoteException ex) {
{'fraction_non_alphanumeric': 0.0928054995851606, 'fraction_numerical': 0.0024890363873414723, 'mean_word_length': 4.025312686122692, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '538553', 'n_tokens_mistral': 5186, 'n_tokens_neox': 5129, 'n_words': 1189}