001/* 002 * (C) Copyright 2009-2016 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Peter Di Lorenzo 018 */ 019package org.nuxeo.ecm.platform.audio.extension; 020 021import java.io.IOException; 022 023import org.apache.commons.logging.Log; 024import org.apache.commons.logging.LogFactory; 025import org.nuxeo.ecm.core.api.Blob; 026import org.nuxeo.ecm.core.api.CoreSession; 027import org.nuxeo.ecm.core.api.DocumentModel; 028import org.nuxeo.ecm.core.api.pathsegment.PathSegmentService; 029import org.nuxeo.ecm.platform.filemanager.service.extension.AbstractFileImporter; 030import org.nuxeo.ecm.platform.filemanager.utils.FileManagerUtils; 031import org.nuxeo.ecm.platform.types.Type; 032import org.nuxeo.ecm.platform.types.TypeManager; 033import org.nuxeo.runtime.api.Framework; 034 035/** 036 * This class will create a Document of type "Audio" from the uploaded file, if the uploaded file matches any of the 037 * mime types listed in the filemanager-plugins.xml file. 038 * <p> 039 * If an existing document with the same title is found, it will overwrite it and increment the version number if the 040 * overwrite flag is set to true; Otherwise, it will generate a new title and create a new Document of type Audio with 041 * that title. 042 */ 043public class AudioImporter extends AbstractFileImporter { 044 045 private static final long serialVersionUID = 1L; 046 047 @SuppressWarnings("unused") 048 private static final Log log = LogFactory.getLog(AudioImporter.class); 049 050 public static final String AUDIO_TYPE = "Audio"; 051 052 @Override 053 public DocumentModel create(CoreSession documentManager, Blob content, String path, boolean overwrite, 054 String fullname, TypeManager typeService) throws IOException { 055 056 String filename = FileManagerUtils.fetchFileName(fullname); 057 content.setFilename(filename); 058 059 String title = FileManagerUtils.fetchTitle(filename); 060 061 // Check to see if an existing Document with the same title exists. 062 DocumentModel docModel = FileManagerUtils.getExistingDocByTitle(documentManager, path, title); 063 064 // if overwrite flag is true and the file already exists, overwrite it 065 if (overwrite && (docModel != null)) { 066 067 // update known attributes, format is: schema, attribute, value 068 docModel.setProperty("file", "content", content); 069 docModel.setProperty("dublincore", "title", title); 070 071 // now save the uploaded file as another new version 072 checkIn(docModel); 073 docModel = documentManager.saveDocument(docModel); 074 075 } else { 076 PathSegmentService pss = Framework.getService(PathSegmentService.class); 077 078 String docType = getDocType(); 079 if (docType == null) { 080 docType = AUDIO_TYPE; 081 } 082 083 docModel = documentManager.createDocumentModel(docType); 084 // update known attributes, format is: schema, attribute, value 085 docModel.setProperty("dublincore", "title", title); 086 docModel.setProperty("file", "content", content); 087 088 // updating icon 089 Type type = typeService.getType(docType); 090 if (type != null) { 091 String iconPath = type.getIcon(); 092 docModel.setProperty("common", "icon", iconPath); 093 } 094 docModel.setPathInfo(path, pss.generatePathSegment(docModel)); 095 docModel = documentManager.createDocument(docModel); 096 } 097 return docModel; 098 } 099 100}