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: DocumentTranslationMapImpl.java 29029 2008-01-14 18:38:14Z ldoguin $
013 */
014
015package org.nuxeo.ecm.core.io.impl;
016
017import java.util.Collections;
018import java.util.HashMap;
019import java.util.List;
020import java.util.Map;
021
022import org.nuxeo.ecm.core.api.DocumentRef;
023import org.nuxeo.ecm.core.io.DocumentTranslationMap;
024
025public class DocumentTranslationMapImpl implements DocumentTranslationMap {
026
027    private final String oldServerName;
028
029    private final String newServerName;
030
031    private Map<DocumentRef, DocumentRef> map;
032
033    public DocumentTranslationMapImpl(String oldServerName, String newServerName) {
034        this.oldServerName = oldServerName;
035        this.newServerName = newServerName;
036    }
037
038    public DocumentTranslationMapImpl(String oldServerName, String newServerName, Map<DocumentRef, DocumentRef> map) {
039        this.oldServerName = oldServerName;
040        this.newServerName = newServerName;
041        this.map = map;
042    }
043
044    @Override
045    public Map<DocumentRef, DocumentRef> getDocRefMap() {
046        if (map == null) {
047            return Collections.emptyMap();
048        }
049        return Collections.unmodifiableMap(map);
050    }
051
052    @Override
053    public String getNewServerName() {
054        return newServerName;
055    }
056
057    @Override
058    public String getOldServerName() {
059        return oldServerName;
060    }
061
062    @Override
063    public void put(DocumentRef oldRef, DocumentRef newRef) {
064        if (map == null) {
065            map = new HashMap<DocumentRef, DocumentRef>();
066        }
067        map.put(oldRef, newRef);
068    }
069
070    @Override
071    public void putAll(Map<DocumentRef, DocumentRef> refs) {
072        if (refs == null) {
073            return;
074        }
075        if (map == null) {
076            map = new HashMap<DocumentRef, DocumentRef>();
077        }
078        map.putAll(refs);
079    }
080
081    public static DocumentTranslationMap merge(List<DocumentTranslationMap> maps) {
082        if (maps == null || maps.isEmpty()) {
083            return null;
084        }
085        // take first one as reference
086        DocumentTranslationMap ref = maps.get(0);
087        String oldRepo = ref.getOldServerName();
088        String newRepo = ref.getNewServerName();
089        DocumentTranslationMap finalMap = new DocumentTranslationMapImpl(oldRepo, newRepo);
090        for (DocumentTranslationMap item : maps) {
091            finalMap.putAll(item.getDocRefMap());
092        }
093        return finalMap;
094    }
095
096}