001/*
002 * (C) Copyright 2009 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Peter Di Lorenzo
016 */
017
018package org.nuxeo.ecm.platform.audio.extension;
019
020import java.io.IOException;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.common.utils.IdUtils;
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    public DocumentModel create(CoreSession documentManager, Blob content, String path, boolean overwrite,
053            String fullname, TypeManager typeService) throws IOException {
054
055        String filename = FileManagerUtils.fetchFileName(fullname);
056
057        String title = FileManagerUtils.fetchTitle(filename);
058
059        // Check to see if an existing Document with the same title exists.
060        DocumentModel docModel = FileManagerUtils.getExistingDocByTitle(documentManager, path, title);
061
062        // if overwrite flag is true and the file already exists, overwrite it
063        if (overwrite && (docModel != null)) {
064
065            // update known attributes, format is: schema, attribute, value
066            docModel.setProperty("file", "content", content);
067            docModel.setProperty("dublincore", "title", title);
068
069            // now save the uploaded file as another new version
070            docModel = overwriteAndIncrementversion(documentManager, docModel);
071
072        } else {
073            PathSegmentService pss = Framework.getService(PathSegmentService.class);
074
075            String docType = getDocType();
076            if (docType == null) {
077                docType = AUDIO_TYPE;
078            }
079
080            docModel = documentManager.createDocumentModel(docType);
081            // update known attributes, format is: schema, attribute, value
082            docModel.setProperty("dublincore", "title", title);
083            docModel.setProperty("file", "content", content);
084            docModel.setProperty("file", "filename", filename);
085
086            // updating icon
087            Type type = typeService.getType(docType);
088            if (type != null) {
089                String iconPath = type.getIcon();
090                docModel.setProperty("common", "icon", iconPath);
091            }
092            docModel.setPathInfo(path, pss.generatePathSegment(docModel));
093            docModel = documentManager.createDocument(docModel);
094        }
095        return docModel;
096    }
097
098}