001/*
002 * (C) Copyright 2006 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 *     Florent Guillaume
018 */
019package org.nuxeo.project.sample;
020
021import java.io.IOException;
022import java.util.Date;
023import java.util.Random;
024
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.platform.filemanager.service.extension.AbstractFileImporter;
029import org.nuxeo.ecm.platform.filemanager.utils.FileManagerUtils;
030import org.nuxeo.ecm.platform.types.TypeManager;
031import org.nuxeo.runtime.api.Framework;
032
033public class BookFileManagerPlugin extends AbstractFileImporter {
034
035    private static final long serialVersionUID = 1L;
036
037    public DocumentModel create(CoreSession documentManager, Blob content, String path, boolean overwrite,
038            String filename, TypeManager typeService) throws IOException {
039
040        String title = FileManagerUtils.fetchTitle(FileManagerUtils.fetchFileName(filename));
041
042        BookTitleService service = Framework.getService(BookTitleService.class);
043
044        title = service.correctTitle(title);
045
046        Random random = new Random(new Date().getTime());
047        String randomName = String.valueOf(random.nextLong());
048
049        DocumentModel doc = documentManager.createDocumentModel(path, randomName, "Book");
050        doc.setPropertyValue("dublincore:title", title);
051        doc.setPropertyValue("dublincore:description", filename);
052        doc.setProperty("file", "content", content);
053        doc = documentManager.createDocument(doc);
054        documentManager.save();
055
056        return doc;
057    }
058
059}