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;
022import java.io.Serializable;
023
024import org.apache.commons.lang3.StringUtils;
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
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.api.FileImporterContext;
032import org.nuxeo.ecm.platform.filemanager.service.extension.AbstractFileImporter;
033import org.nuxeo.ecm.platform.filemanager.utils.FileManagerUtils;
034import org.nuxeo.ecm.platform.types.Type;
035import org.nuxeo.ecm.platform.types.TypeManager;
036import org.nuxeo.runtime.api.Framework;
037
038/**
039 * This class will create a Document of type "Audio" from the uploaded file, if the uploaded file matches any of the
040 * mime types listed in the filemanager-plugins.xml file.
041 * <p>
042 * If an existing document with the same title is found, it will overwrite it and increment the version number if the
043 * overwrite flag is set to true; Otherwise, it will generate a new title and create a new Document of type Audio with
044 * that title.
045 */
046public class AudioImporter extends AbstractFileImporter {
047
048    private static final long serialVersionUID = 1L;
049
050    @SuppressWarnings("unused")
051    private static final Log log = LogFactory.getLog(AudioImporter.class);
052
053    public static final String AUDIO_TYPE = "Audio";
054
055    @Override
056    public DocumentModel createOrUpdate(FileImporterContext context) throws IOException {
057        CoreSession session = context.getSession();
058        Blob blob = context.getBlob();
059
060        String filename = FileManagerUtils.fetchFileName(
061                StringUtils.defaultIfBlank(context.getFileName(), blob.getFilename()));
062        blob.setFilename(filename);
063
064        String title = FileManagerUtils.fetchTitle(filename);
065
066        // Check to see if an existing Document with the same title exists.
067        String parentPath = context.getParentPath();
068        DocumentModel docModel = FileManagerUtils.getExistingDocByTitle(session, parentPath, title);
069
070        // if overwrite flag is true and the file already exists, overwrite it
071        if (context.isOverwrite() && (docModel != null)) {
072
073            // update known attributes, format is: schema, attribute, value
074            docModel.setPropertyValue("file:content", (Serializable) blob);
075            docModel.setPropertyValue("dc:title", title);
076
077            // now save the uploaded file as another new version
078            checkIn(docModel);
079            docModel = session.saveDocument(docModel);
080
081        } else {
082            PathSegmentService pss = Framework.getService(PathSegmentService.class);
083
084            String docType = getDocType();
085            if (docType == null) {
086                docType = AUDIO_TYPE;
087            }
088
089            docModel = session.createDocumentModel(docType);
090            // update known attributes, format is: schema, attribute, value
091            docModel.setPropertyValue("file:content", (Serializable) blob);
092            docModel.setPropertyValue("dc:title", title);
093
094            // updating icon
095            Type type = Framework.getService(TypeManager.class).getType(docType);
096            if (type != null) {
097                String iconPath = type.getIcon();
098                docModel.setProperty("common", "icon", iconPath);
099            }
100            docModel.setPathInfo(parentPath, pss.generatePathSegment(docModel));
101            docModel = session.createDocument(docModel);
102        }
103        return docModel;
104    }
105
106}