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 *     Nuxeo - initial API and implementation
018 *
019 * $Id: AbstractDocumentWriter.java 29029 2008-01-14 18:38:14Z ldoguin $
020 */
021
022package org.nuxeo.ecm.core.io.impl;
023
024import java.io.IOException;
025import java.util.Collection;
026import java.util.HashMap;
027import java.util.Map;
028
029import org.dom4j.io.OutputFormat;
030import org.nuxeo.ecm.core.api.DocumentRef;
031import org.nuxeo.ecm.core.io.DocumentTranslationMap;
032import org.nuxeo.ecm.core.io.DocumentWriter;
033import org.nuxeo.ecm.core.io.ExportedDocument;
034
035/**
036 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
037 */
038public abstract class AbstractDocumentWriter implements DocumentWriter {
039
040    // this abstract method is needed
041    @Override
042    public abstract DocumentTranslationMap write(ExportedDocument doc) throws IOException;
043
044    @Override
045    public DocumentTranslationMap write(ExportedDocument[] docs) throws IOException {
046        if (docs == null || docs.length == 0) {
047            return null;
048        }
049        String newRepo = null;
050        String oldRepo = null;
051        Map<DocumentRef, DocumentRef> newRefs = new HashMap<>();
052        for (ExportedDocument doc : docs) {
053            DocumentTranslationMap newMap = write(doc);
054            if (newMap != null) {
055                newRefs.putAll(newMap.getDocRefMap());
056                // assume repo will be the same for all docs
057                if (oldRepo == null) {
058                    oldRepo = newMap.getOldServerName();
059                }
060                if (newRepo == null) {
061                    newRepo = newMap.getNewServerName();
062                }
063            }
064        }
065        return new DocumentTranslationMapImpl(oldRepo, newRepo, newRefs);
066    }
067
068    @Override
069    public DocumentTranslationMap write(Collection<ExportedDocument> docs) throws IOException {
070        if (docs == null || docs.isEmpty()) {
071            return null;
072        }
073        String newRepo = null;
074        String oldRepo = null;
075        Map<DocumentRef, DocumentRef> newRefs = new HashMap<>();
076        for (ExportedDocument doc : docs) {
077            DocumentTranslationMap newMap = write(doc);
078            if (newMap != null) {
079                newRefs.putAll(newMap.getDocRefMap());
080                // assume repo will be the same for all docs
081                if (oldRepo == null) {
082                    oldRepo = newMap.getOldServerName();
083                }
084                if (newRepo == null) {
085                    newRepo = newMap.getNewServerName();
086                }
087            }
088        }
089        return new DocumentTranslationMapImpl(oldRepo, newRepo, newRefs);
090    }
091
092    public static OutputFormat createPrettyPrint() {
093        OutputFormat format = new OutputFormat();
094        format.setIndentSize(2);
095        format.setNewlines(true);
096        return format;
097    }
098
099    public static OutputFormat createCompactFormat() {
100        OutputFormat format = new OutputFormat();
101        format.setIndent(false);
102        format.setNewlines(false);
103        return format;
104    }
105
106}