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; }
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; }
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; }
/** * 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); }