001/*
002 * (C) Copyright 2006-2012 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 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021package org.nuxeo.ecm.platform.scanimporter.processor;
022
023import java.io.IOException;
024import java.util.HashMap;
025import java.util.Map;
026
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.DocumentRef;
030import org.nuxeo.ecm.core.api.PathRef;
031import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
032import org.nuxeo.ecm.platform.importer.factories.DefaultDocumentModelFactory;
033import org.nuxeo.ecm.platform.importer.factories.ImporterDocumentModelFactory;
034import org.nuxeo.ecm.platform.importer.source.SourceNode;
035import org.nuxeo.ecm.platform.scanimporter.service.ImporterConfig;
036import org.nuxeo.ecm.platform.scanimporter.service.ScanFileBlobHolder;
037import org.nuxeo.ecm.platform.scanimporter.service.ScannedFileMapperService;
038import org.nuxeo.runtime.api.Framework;
039
040/**
041 * Custom implementation of the {@link ImporterDocumentModelFactory}. Provides : - container doc type configuration from
042 * service - leaf doc type configuration from service
043 *
044 * @author Thierry Delprat
045 */
046public class ScanedFileFactory extends DefaultDocumentModelFactory implements ImporterDocumentModelFactory {
047
048    protected static String targetContainerType = null;
049
050    protected ImporterConfig config;
051
052    public ScanedFileFactory(ImporterConfig config) {
053        super();
054        this.config = config;
055    }
056
057    protected String getTargetContainerType() {
058        if (targetContainerType == null) {
059            ScannedFileMapperService service = Framework.getLocalService(ScannedFileMapperService.class);
060            targetContainerType = service.getTargetContainerType();
061        }
062        return targetContainerType;
063    }
064
065    @Override
066    public DocumentModel createFolderishNode(CoreSession session, DocumentModel parent, SourceNode node) {
067
068        String docType = getTargetContainerType();
069        String name = getValidNameFromFileName(node.getName());
070        boolean isUpdateDocument = false;
071
072        PathRef ref = new PathRef(parent.getPathAsString(), name);
073        if (parent.getPathAsString().equals(config.getTargetPath())
074                && node.getSourcePath().equals(config.getSourcePath())) {
075            // initial root creation
076            if (!config.isCreateInitialFolder()) {
077                return parent;
078            }
079            if (config.isMergeInitialFolder()) {
080                if (session.exists(ref)) {
081                    return session.getDocument(ref);
082                }
083            } else {
084                if (session.exists(ref)) {
085                    if (config.isUpdate()) {
086                        isUpdateDocument = true;
087                    } else {
088                        name = name + "-" + System.currentTimeMillis();
089                    }
090                }
091            }
092        } else {
093            if (session.exists(ref) && config.isUpdate()) {
094                isUpdateDocument = true;
095            }
096        }
097
098        Map<String, Object> options = new HashMap<String, Object>();
099        DocumentModel doc;
100        if (isUpdateDocument) {
101            doc = session.getDocument(ref);
102        } else {
103            doc = session.createDocumentModel(docType, options);
104            doc.setPathInfo(parent.getPathAsString(), name);
105            doc.setProperty("dublincore", "title", node.getName());
106            doc = session.createDocument(doc);
107        }
108        return doc;
109    }
110
111    @Override
112    public DocumentModel createLeafNode(CoreSession session, DocumentModel parent, SourceNode node) throws IOException {
113
114        String docType = "File";
115        BlobHolder bh = node.getBlobHolder();
116
117        if (bh instanceof ScanFileBlobHolder) {
118            ScanFileBlobHolder scanBH = (ScanFileBlobHolder) bh;
119            docType = scanBH.getTargetType();
120            setLeafType(docType);
121        }
122
123        DocumentModel doc = defaultCreateLeafNode(session, parent, node);
124
125        // XXX should be a callback on commit !!!
126        if (node instanceof ScanedFileSourceNode) {
127            ScanedFileSourceNode scanNode = (ScanedFileSourceNode) node;
128            ScannedFileImporter.addProcessedDescriptor(scanNode.getDescriptorFileName());
129        }
130
131        return doc;
132    }
133
134    @Override
135    protected DocumentModel defaultCreateLeafNode(CoreSession session, DocumentModel parent, SourceNode node)
136            throws IOException {
137
138        BlobHolder bh = node.getBlobHolder();
139
140        String name = getValidNameFromFileName(node.getName());
141        String fileName = node.getName();
142
143        DocumentRef docRef = new PathRef(parent.getPathAsString(), name);
144
145        DocumentModel doc;
146        boolean docExists = session.exists(docRef);
147        if (docExists && config.isUpdate()) {
148            doc = session.getDocument(docRef);
149        } else {
150            Map<String, Object> options = new HashMap<String, Object>();
151            doc = session.createDocumentModel(leafType, options);
152            doc.setPathInfo(parent.getPathAsString(), name);
153            doc.setProperty("dublincore", "title", node.getName());
154        }
155        doc.setProperty("file", "filename", fileName);
156        doc.setProperty("file", "content", bh.getBlob());
157
158        if (docExists && config.isUpdate()) {
159            doc = session.saveDocument(doc);
160        } else {
161            doc = session.createDocument(doc);
162        }
163
164        doc = setDocumentProperties(session, bh.getProperties(), doc);
165
166        return doc;
167    }
168}