/**
   * Methode utilisee par la methode importAttachement(String,String,AttachmentDetail) pour creer un
   * attachement sur la publication creee dans la methode citee.
   *
   * @param pubId - id de la publication dans laquelle creer l'attachment
   * @param componentId - id du composant contenant la publication
   * @param attachment
   * @return AttachmentDetail cree
   */
  private SimpleDocument addAttachmentToPublication(
      String pubId,
      String componentId,
      AttachmentDetail attachment,
      InputStream input,
      boolean indexIt) {
    SimpleDocumentPK attachmentPk = new SimpleDocumentPK(null, componentId);
    ForeignPK foreignKey = new ForeignPK(pubId, componentId);
    List<SimpleDocument> existingAttachments =
        AttachmentServiceFactory.getAttachmentService()
            .listDocumentsByForeignKeyAndType(
                foreignKey, DocumentType.attachment, attachment.getLanguage());

    String logicalName = attachment.getLogicalName();
    if (!StringUtil.isDefined(logicalName)) {
      logicalName = FileUtil.getFilename(attachment.getPhysicalName());
    }
    String userId = attachment.getAuthor();
    String updateRule = attachment.getImportUpdateRule();
    if (!StringUtil.isDefined(updateRule) || "null".equalsIgnoreCase(updateRule)) {
      updateRule = AttachmentDetail.IMPORT_UPDATE_RULE_ADD;
    }

    SilverTrace.info(
        "attachment",
        "AttachmentImportExport.addAttachmentToPublication()",
        "root.MSG_GEN_PARAM_VALUE",
        "updateRule=" + updateRule);

    // Verification s'il existe un attachment de meme nom, si oui, ajout
    // d'un suffixe au nouveau fichier
    logicalName = computeUniqueName(attachment, 0, existingAttachments, logicalName, updateRule);
    attachment.setLogicalName(logicalName);

    Date creationDate = attachment.getCreationDate();
    if (creationDate == null) {
      creationDate = new Date();
    }
    SimpleDocument ad_toCreate =
        new SimpleDocument(
            attachmentPk,
            pubId,
            -1,
            false,
            new SimpleAttachment(
                attachment.getLogicalName(),
                attachment.getLanguage(),
                attachment.getTitle(),
                attachment.getInfo(),
                attachment.getSize(),
                FileUtil.getMimeType(attachment.getPhysicalName()),
                userId,
                creationDate,
                attachment.getXmlForm()));
    return AttachmentServiceFactory.getAttachmentService()
        .createAttachment(ad_toCreate, input, indexIt);
  }
 public File getAttachmentFile(AttachmentDetail attachment) throws FileNotFoundException {
   File file = new File(FileUtil.convertPathToServerOS(attachment.getAttachmentPath()));
   if (file == null || !file.exists() || !file.isFile()) {
     String baseDir = resources.getString("importRepository");
     file =
         new File(
             FileUtil.convertPathToServerOS(
                 baseDir + File.separatorChar + attachment.getPhysicalName()));
   }
   attachment.setSize(file.length());
   attachment.setType(FileUtil.getMimeType(file.getName()));
   if (!StringUtil.isDefined(attachment.getLogicalName())) {
     attachment.setLogicalName(file.getName());
   }
   return file;
 }