public void updateXmlForm(AttachmentPK pk, String language, String xmlFormName) throws AttachmentException { Connection con = getConnection(); try { if (!I18NHelper.isI18N || !StringUtil.isDefined(language)) { dao.updateXmlForm(con, pk, xmlFormName); } else { AttachmentDetail attachment = dao.findByPrimaryKey(con, pk); if (!StringUtil.isDefined(attachment.getLanguage()) || attachment.getLanguage().equals(language)) { dao.updateXmlForm(con, pk, xmlFormName); } else { AttachmentI18NDAO.updateXmlForm(con, pk, language, xmlFormName); } } } catch (SQLException se) { throw new AttachmentException( "AttachmentBmImpl.updateXmlForm()", SilverpeasException.ERROR, "EX_RECORD_NOT_UPDATE_ATTACHMENT", se); } finally { closeConnection(con); } }
/** * Creates details about the uploaded attached video file. * * @param objectId * @param componentId the identifier of the component for which the video is uploaded. * @param physicalName the name of the physical file in which the video will be stored. * @param logicalName the logical name of the video file (name in the upload form). * @param mimeType the MIME type of the video (video/flv, ...) * @param size the size of the video. * @param contextVideo the upload context. * @param userId the identifier of the user that is uploading the video. * @return an AttachmentDetail object. */ private AttachmentDetail createAttachmentDetail( String objectId, String componentId, String physicalName, String logicalName, String mimeType, long size, String contextVideo, String userId) { // create AttachmentPK with spaceId and componentId AttachmentPK atPK = new AttachmentPK(null, "useless", componentId); // create foreignKey with spaceId, componentId and id // use AttachmentPK to build the foreign key of customer object. AttachmentPK foreignKey = new AttachmentPK("-1", "useless", componentId); if (objectId != null) { foreignKey.setId(objectId); } // create AttachmentDetail Object AttachmentDetail attachmentDetail = new AttachmentDetail( atPK, physicalName, logicalName, null, mimeType, size, contextVideo, new Date(), foreignKey); attachmentDetail.setAuthor(userId); return attachmentDetail; }
/** * Uploads the file containing the video and that is identified by the specified field name. * * @param items the items of the form. One of them is containg the video file. * @param itemKey the key of the item containing the video. * @param pageContext the context of the page displaying the form. * @throws Exception if an error occurs while uploading the video file. * @return the identifier of the uploaded attached video file. */ private String uploadVideoFile( final List<FileItem> items, final String itemKey, final PagesContext pageContext) throws Exception { String attachmentId = ""; FileItem item = FileUploadUtil.getFile(items, itemKey); if (!item.isFormField()) { String componentId = pageContext.getComponentId(); String userId = pageContext.getUserId(); String objectId = pageContext.getObjectId(); String logicalName = item.getName(); long size = item.getSize(); if (StringUtil.isDefined(logicalName)) { if (!FileUtil.isWindows()) { logicalName = logicalName.replace('\\', File.separatorChar); SilverTrace.info( "form", "VideoFieldDisplayer.uploadVideoFile", "root.MSG_GEN_PARAM_VALUE", "fullFileName on Unix = " + logicalName); } logicalName = logicalName.substring( logicalName.lastIndexOf(File.separator) + 1, logicalName.length()); String type = FileRepositoryManager.getFileExtension(logicalName); String mimeType = item.getContentType(); String physicalName = Long.toString(System.currentTimeMillis()) + "." + type; File dir = getVideoPath(componentId, physicalName); item.write(dir); AttachmentDetail attachmentDetail = createAttachmentDetail( objectId, componentId, physicalName, logicalName, mimeType, size, VideoFieldDisplayer.CONTEXT_FORM_VIDEO, userId); attachmentDetail = AttachmentController.createAttachment(attachmentDetail, true); attachmentId = attachmentDetail.getPK().getId(); } } return attachmentId; }
/** * Computes the URL of the video to display from the identifier of the attached video file and by * taking into account the displaying context. * * @param attachmentId the identifier of the attached video file. * @param pageContext the displaying page context. * @return the URL of the video file as String. */ private String computeVideoURL(final String attachmentId, final PagesContext pageContext) { String videoURL = ""; if (StringUtil.isDefined(attachmentId)) { if (attachmentId.startsWith("/")) { videoURL = attachmentId; } else { AttachmentDetail attachment = AttachmentController.searchAttachmentByPK( new AttachmentPK(attachmentId, pageContext.getComponentId())); if (attachment != null) { String webContext = FileServerUtils.getApplicationContext(); videoURL = webContext + attachment.getAttachmentURL(); } } } return videoURL; }
@Override public void updateAttachment(AttachmentDetail attachDetail) throws AttachmentException { Connection con = getConnection(); try { boolean updateDefault = false; AttachmentDetail oldAttachment = dao.findByPrimaryKey(con, attachDetail.getPK()); String oldLang = oldAttachment.getLanguage(); if (attachDetail.isRemoveTranslation()) { if ("-1".equals(attachDetail.getTranslationId())) { List<AttachmentDetailI18N> translations = AttachmentI18NDAO.getTranslations(con, attachDetail.getPK()); if (translations != null && translations.size() > 0) { AttachmentDetailI18N translation = translations.get(0); attachDetail.setPhysicalName(translation.getPhysicalName()); attachDetail.setLogicalName(translation.getLogicalName()); attachDetail.setType(translation.getType()); attachDetail.setCreationDate(translation.getCreationDate()); attachDetail.setSize(translation.getSize()); attachDetail.setAuthor(translation.getAuthor()); attachDetail.setTitle(translation.getTitle()); attachDetail.setInfo(translation.getInfo()); attachDetail.setLanguage(translation.getLanguage()); AttachmentI18NDAO.removeTranslation(con, translation.getId()); updateDefault = true; } } else { AttachmentI18NDAO.removeTranslation(con, attachDetail.getTranslationId()); } } else { // Add or update a translation if (attachDetail.getLanguage() != null) { if (oldLang == null) { // translation for the first time oldLang = I18NHelper.defaultLanguage; } if (!oldLang.equalsIgnoreCase(attachDetail.getLanguage())) { AttachmentDetailI18N translation = new AttachmentDetailI18N(attachDetail); String translationId = attachDetail.getTranslationId(); if (translationId != null && !"-1".equals(translationId)) { AttachmentI18NDAO.updateTranslation(con, translation); } else { AttachmentI18NDAO.addTranslation(con, translation); } attachDetail.setPhysicalName(oldAttachment.getPhysicalName()); attachDetail.setLogicalName(oldAttachment.getLogicalName()); attachDetail.setType(oldAttachment.getType()); attachDetail.setCreationDate(oldAttachment.getCreationDate()); attachDetail.setSize(oldAttachment.getSize()); attachDetail.setAuthor(oldAttachment.getAuthor()); attachDetail.setTitle(oldAttachment.getTitle()); attachDetail.setInfo(oldAttachment.getInfo()); attachDetail.setLanguage(oldLang); } else { updateDefault = true; } } else { updateDefault = true; } } if (updateDefault) { dao.updateRow(con, attachDetail); } } catch (SQLException se) { throw new AttachmentException( "AttachmentBmImpl.updateAttachment()", SilverpeasException.ERROR, "attachment.EX_RECORD_NOT_UPDATE_ATTACHMENT", se); } finally { closeConnection(con); } }