001/*
002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo
016 */
017
018package org.nuxeo.ecm.platform.publisher.remoting.marshaling;
019
020import org.nuxeo.ecm.core.api.*;
021import org.nuxeo.ecm.core.api.impl.DocumentLocationImpl;
022
023/**
024 * Extension to {@link DocumentLocation} to provide information about source server.
025 *
026 * @author tiry
027 */
028public class ExtendedDocumentLocation extends DocumentLocationImpl {
029
030    private static final long serialVersionUID = 1L;
031
032    protected String originalServer;
033
034    public ExtendedDocumentLocation(String serverName, DocumentRef docRef) {
035        super(serverName, docRef);
036    }
037
038    public ExtendedDocumentLocation(String originalServer, String serverName, DocumentRef docRef) {
039        super(serverName, docRef);
040        this.originalServer = originalServer;
041    }
042
043    public ExtendedDocumentLocation(String originalServer, DocumentModel doc) {
044        super(doc);
045        this.originalServer = originalServer;
046    }
047
048    public String getOriginalServer() {
049        return this.originalServer;
050    }
051
052    @Override
053    public String toString() {
054        return getServerName() + "@" + getOriginalServer() + ":" + getDocRef().toString();
055    }
056
057    public static ExtendedDocumentLocation parseString(String source) {
058        String[] refParts = source.split("@");
059        String sourceServer = refParts[1].split(":")[0];
060        String repositoryName = refParts[0];
061        DocumentRef ref = new IdRef(refParts[1].split(":")[1]);
062        return new ExtendedDocumentLocation(sourceServer, repositoryName, ref);
063    }
064
065    public static ExtendedDocumentLocation extractFromDoc(DocumentModel doc) {
066        if (doc.hasSchema("dublincore")) {
067            String source = (String) doc.getProperty("dublincore", "source");
068
069            if (source != null) {
070                return parseString(source);
071            }
072        }
073        return null;
074    }
075
076}