001/*
002 * (C) Copyright 2006-2011 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 *     bstefanescu
018 *
019 * $Id: DocumentModelUpdater.java 29029 2008-01-14 18:38:14Z ldoguin $
020 */
021
022package org.nuxeo.ecm.core.io.impl.plugins;
023
024import java.io.IOException;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentLocation;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.api.DocumentNotFoundException;
032import org.nuxeo.ecm.core.api.IdRef;
033import org.nuxeo.ecm.core.io.DocumentTranslationMap;
034import org.nuxeo.ecm.core.io.ExportedDocument;
035import org.nuxeo.ecm.core.io.impl.AbstractDocumentModelWriter;
036import org.nuxeo.ecm.core.io.impl.DocumentTranslationMapImpl;
037
038/**
039 * A writer that only updates existing documents. The doc ID is used to identity documents. The imported tree structure
040 * is ignored.
041 *
042 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
043 */
044// TODO: improve it ->
045// modify core session to add a batch create method and use it
046public class DocumentModelUpdater extends AbstractDocumentModelWriter {
047
048    private static final Log log = LogFactory.getLog(DocumentModelUpdater.class);
049
050    /**
051     * @param session the session to the repository where to write
052     * @param parentPath where to write the tree. this document will be used as the parent of all top level documents
053     *            passed as input. Note that you may have
054     */
055    public DocumentModelUpdater(CoreSession session, String parentPath) {
056        super(session, parentPath);
057    }
058
059    public DocumentModelUpdater(CoreSession session, String parentPath, int saveInterval) {
060        super(session, parentPath, saveInterval);
061    }
062
063    @Override
064    public DocumentTranslationMap write(ExportedDocument xdoc) throws IOException {
065        if (xdoc.getDocument() == null) {
066            // not a valid doc -> this may be a regular folder for example the
067            // root of the tree
068            return null;
069        }
070
071        DocumentModel doc = null;
072        String id = xdoc.getId();
073        try {
074            doc = session.getDocument(new IdRef(id));
075        } catch (DocumentNotFoundException e) {
076            log.error("Cannot update document. No such document: " + id);
077            return null;
078        }
079
080        doc = updateDocument(xdoc, doc);
081        DocumentLocation source = xdoc.getSourceLocation();
082        DocumentTranslationMap map = new DocumentTranslationMapImpl(source.getServerName(), doc.getRepositoryName());
083        map.put(source.getDocRef(), doc.getRef());
084        return map;
085    }
086
087}