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