コード例 #1
0
  /**
   * 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);
  }
コード例 #2
0
  public List<AttachmentDetail> importAttachments(
      String pubId,
      String componentId,
      List<AttachmentDetail> attachments,
      String userId,
      boolean indexIt)
      throws FileNotFoundException {
    FormTemplateImportExport xmlIE = null;
    for (AttachmentDetail attDetail : attachments) {
      // TODO check user id
      attDetail.setAuthor(userId);
      attDetail.setInstanceId(componentId);
      XMLModelContentType xmlContent = attDetail.getXMLModelContentType();
      if (xmlContent != null) {
        attDetail.setXmlForm(xmlContent.getName());
      }
      InputStream input = null;
      // Store xml content
      try {
        input = getAttachmentContent(attDetail);
        this.addAttachmentToPublication(pubId, componentId, attDetail, input, indexIt);
        if (xmlContent != null) {
          if (xmlIE == null) {
            xmlIE = new FormTemplateImportExport();
          }
          ForeignPK pk =
              new ForeignPK(attDetail.getPK().getId(), attDetail.getPK().getInstanceId());
          xmlIE.importXMLModelContentType(pk, "Attachment", xmlContent, attDetail.getAuthor());
        }
      } catch (Exception e) {
        SilverTrace.error(
            "attachment",
            "AttachmentImportExport.importAttachments()",
            "root.MSG_GEN_PARAM_VALUE",
            e);
        SilverpeasTransverseErrorUtil.throwTransverseErrorIfAny(e, attDetail.getLanguage());
      } finally {
        IOUtils.closeQuietly(input);
      }

      if (attDetail.isRemoveAfterImport()) {
        boolean removed = FileUtils.deleteQuietly(getAttachmentFile(attDetail));
        if (!removed) {
          SilverTrace.error(
              "attachment",
              "AttachmentImportExport.importAttachments()",
              "root.MSG_GEN_PARAM_VALUE",
              "Can't remove file " + getAttachmentFile(attDetail));
        }
      }
    }
    return attachments;
  }