001/*
002 * (C) Copyright 2006-2009 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
018 */
019
020package org.nuxeo.ecm.platform.publisher.remoting.marshaling;
021
022import org.nuxeo.ecm.core.api.*;
023import org.nuxeo.ecm.core.api.impl.DocumentLocationImpl;
024
025/**
026 * Extension to {@link DocumentLocation} to provide information about source server.
027 *
028 * @author tiry
029 */
030public class ExtendedDocumentLocation extends DocumentLocationImpl {
031
032    private static final long serialVersionUID = 1L;
033
034    protected String originalServer;
035
036    public ExtendedDocumentLocation(String serverName, DocumentRef docRef) {
037        super(serverName, docRef);
038    }
039
040    public ExtendedDocumentLocation(String originalServer, String serverName, DocumentRef docRef) {
041        super(serverName, docRef);
042        this.originalServer = originalServer;
043    }
044
045    public ExtendedDocumentLocation(String originalServer, DocumentModel doc) {
046        super(doc);
047        this.originalServer = originalServer;
048    }
049
050    public String getOriginalServer() {
051        return this.originalServer;
052    }
053
054    @Override
055    public String toString() {
056        return getServerName() + "@" + getOriginalServer() + ":" + getDocRef().toString();
057    }
058
059    public static ExtendedDocumentLocation parseString(String source) {
060        String[] refParts = source.split("@");
061        String sourceServer = refParts[1].split(":")[0];
062        String repositoryName = refParts[0];
063        DocumentRef ref = new IdRef(refParts[1].split(":")[1]);
064        return new ExtendedDocumentLocation(sourceServer, repositoryName, ref);
065    }
066
067    public static ExtendedDocumentLocation extractFromDoc(DocumentModel doc) {
068        if (doc.hasSchema("dublincore")) {
069            String source = (String) doc.getProperty("dublincore", "source");
070
071            if (source != null) {
072                return parseString(source);
073            }
074        }
075        return null;
076    }
077
078}