private String computeUniqueName(
     AttachmentDetail attachment,
     int increment,
     List<SimpleDocument> existingAttachments,
     String logicalName,
     String updateRule) {
   String uniqueName = logicalName;
   int incrementSuffixe = increment;
   for (SimpleDocument ad_toCreate : existingAttachments) {
     if (ad_toCreate.getFilename().equals(uniqueName)) {
       if ((ad_toCreate.getSize() != attachment.getSize())
           && AttachmentDetail.IMPORT_UPDATE_RULE_ADD.equalsIgnoreCase(updateRule)) {
         uniqueName = attachment.getLogicalName();
         int extPosition = logicalName.lastIndexOf('.');
         if (extPosition != -1) {
           uniqueName =
               uniqueName.substring(0, extPosition)
                   + '_'
                   + (++incrementSuffixe)
                   + uniqueName.substring(extPosition, uniqueName.length());
         } else {
           uniqueName += '_' + (++incrementSuffixe);
         }
         // On reprend la boucle au debut pour verifier que le nom
         // genere n est pas lui meme un autre nom d'attachment de la publication
         return computeUniqueName(
             attachment, incrementSuffixe, existingAttachments, uniqueName, updateRule);
       } else { // on efface l'ancien fichier joint et on stoppe la boucle
         AttachmentServiceFactory.getAttachmentService().deleteAttachment(ad_toCreate);
         return uniqueName;
       }
     }
   }
   return logicalName;
 }
  /**
   * 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);
  }