001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Nuxeo - initial API and implementation
011 *
012 * $Id: AbstractDocumentWriter.java 29029 2008-01-14 18:38:14Z ldoguin $
013 */
014
015package org.nuxeo.ecm.core.io.impl;
016
017import java.io.IOException;
018import java.util.Collection;
019import java.util.HashMap;
020import java.util.Map;
021
022import org.dom4j.io.OutputFormat;
023import org.nuxeo.ecm.core.api.DocumentRef;
024import org.nuxeo.ecm.core.io.DocumentTranslationMap;
025import org.nuxeo.ecm.core.io.DocumentWriter;
026import org.nuxeo.ecm.core.io.ExportedDocument;
027
028/**
029 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
030 */
031public abstract class AbstractDocumentWriter implements DocumentWriter {
032
033    // this abstract method is needed
034    @Override
035    public abstract DocumentTranslationMap write(ExportedDocument doc) throws IOException;
036
037    @Override
038    public DocumentTranslationMap write(ExportedDocument[] docs) throws IOException {
039        if (docs == null || docs.length == 0) {
040            return null;
041        }
042        String newRepo = null;
043        String oldRepo = null;
044        Map<DocumentRef, DocumentRef> newRefs = new HashMap<DocumentRef, DocumentRef>();
045        for (ExportedDocument doc : docs) {
046            DocumentTranslationMap newMap = write(doc);
047            if (newMap != null) {
048                newRefs.putAll(newMap.getDocRefMap());
049                // assume repo will be the same for all docs
050                if (oldRepo == null) {
051                    oldRepo = newMap.getOldServerName();
052                }
053                if (newRepo == null) {
054                    newRepo = newMap.getNewServerName();
055                }
056            }
057        }
058        return new DocumentTranslationMapImpl(oldRepo, newRepo, newRefs);
059    }
060
061    @Override
062    public DocumentTranslationMap write(Collection<ExportedDocument> docs) throws IOException {
063        if (docs == null || docs.isEmpty()) {
064            return null;
065        }
066        String newRepo = null;
067        String oldRepo = null;
068        Map<DocumentRef, DocumentRef> newRefs = new HashMap<DocumentRef, DocumentRef>();
069        for (ExportedDocument doc : docs) {
070            DocumentTranslationMap newMap = write(doc);
071            if (newMap != null) {
072                newRefs.putAll(newMap.getDocRefMap());
073                // assume repo will be the same for all docs
074                if (oldRepo == null) {
075                    oldRepo = newMap.getOldServerName();
076                }
077                if (newRepo == null) {
078                    newRepo = newMap.getNewServerName();
079                }
080            }
081        }
082        return new DocumentTranslationMapImpl(oldRepo, newRepo, newRefs);
083    }
084
085    public static OutputFormat createPrettyPrint() {
086        OutputFormat format = new OutputFormat();
087        format.setIndentSize(2);
088        format.setNewlines(true);
089        return format;
090    }
091
092    public static OutputFormat createCompactFormat() {
093        OutputFormat format = new OutputFormat();
094        format.setIndent(false);
095        format.setNewlines(false);
096        return format;
097    }
098
099}