001/*
002 * (C) Copyright 2009 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 */
019
020package org.nuxeo.ecm.platform.audio.extension;
021
022import java.io.IOException;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.common.utils.IdUtils;
027import org.nuxeo.ecm.core.api.Blob;
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.pathsegment.PathSegmentService;
031import org.nuxeo.ecm.platform.filemanager.service.extension.AbstractFileImporter;
032import org.nuxeo.ecm.platform.filemanager.utils.FileManagerUtils;
033import org.nuxeo.ecm.platform.types.Type;
034import org.nuxeo.ecm.platform.types.TypeManager;
035import org.nuxeo.runtime.api.Framework;
036
037/**
038 * This class will create a Document of type "Audio" from the uploaded file, if the uploaded file matches any of the
039 * mime types listed in the filemanager-plugins.xml file.
040 * <p>
041 * If an existing document with the same title is found, it will overwrite it and increment the version number if the
042 * overwrite flag is set to true; Otherwise, it will generate a new title and create a new Document of type Audio with
043 * that title.
044 */
045public class AudioImporter extends AbstractFileImporter {
046
047    private static final long serialVersionUID = 1L;
048
049    @SuppressWarnings("unused")
050    private static final Log log = LogFactory.getLog(AudioImporter.class);
051
052    public static final String AUDIO_TYPE = "Audio";
053
054    public DocumentModel create(CoreSession documentManager, Blob content, String path, boolean overwrite,
055            String fullname, TypeManager typeService) throws IOException {
056
057        String filename = FileManagerUtils.fetchFileName(fullname);
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            docModel = overwriteAndIncrementversion(documentManager, docModel);
073
074        } else {
075            PathSegmentService pss = Framework.getService(PathSegmentService.class);
076
077            String docType = getDocType();
078            if (docType == null) {
079                docType = AUDIO_TYPE;
080            }
081
082            docModel = documentManager.createDocumentModel(docType);
083            // update known attributes, format is: schema, attribute, value
084            docModel.setProperty("dublincore", "title", title);
085            docModel.setProperty("file", "content", content);
086            docModel.setProperty("file", "filename", filename);
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}