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: DocumentLocationImpl.java 25074 2007-09-18 14:23:08Z atchertchian $
013 */
014
015package org.nuxeo.ecm.core.api.impl;
016
017import org.nuxeo.ecm.core.api.DocumentLocation;
018import org.nuxeo.ecm.core.api.DocumentModel;
019import org.nuxeo.ecm.core.api.DocumentRef;
020import org.nuxeo.ecm.core.api.IdRef;
021import org.nuxeo.ecm.core.api.PathRef;
022
023public class DocumentLocationImpl implements DocumentLocation {
024
025    private static final long serialVersionUID = -1109935626596128985L;
026
027    private final String serverName;
028
029    private final DocumentRef docRef;
030
031    private final IdRef docIdRef;
032
033    private final PathRef docPathRef;
034
035    public DocumentLocationImpl(DocumentModel doc) {
036        serverName = doc.getRepositoryName();
037        docRef = doc.getRef();
038        String id = doc.getId();
039        if (id != null) {
040            docIdRef = new IdRef(id);
041        } else {
042            docIdRef = null;
043        }
044        String path = doc.getPathAsString();
045        if (path != null) {
046            docPathRef = new PathRef(path);
047        } else {
048            docPathRef = null;
049        }
050    }
051
052    public DocumentLocationImpl(final String serverName, final IdRef idRef, final PathRef pathRef) {
053        this.serverName = serverName;
054        docRef = idRef;
055        docIdRef = idRef;
056        docPathRef = pathRef;
057    }
058
059    public DocumentLocationImpl(final String serverName, final DocumentRef docRef) {
060        this.serverName = serverName;
061        this.docRef = docRef;
062        if (docRef instanceof IdRef) {
063            docIdRef = (IdRef) docRef;
064            docPathRef = null;
065        } else if (docRef instanceof PathRef) {
066            docIdRef = null;
067            docPathRef = (PathRef) docRef;
068        } else {
069            docIdRef = null;
070            docPathRef = null;
071        }
072    }
073
074    @Override
075    public DocumentRef getDocRef() {
076        return docRef;
077    }
078
079    @Override
080    public String getServerName() {
081        return serverName;
082    }
083
084    @Override
085    public IdRef getIdRef() {
086        return docIdRef;
087    }
088
089    @Override
090    public PathRef getPathRef() {
091        return docPathRef;
092    }
093
094    @Override
095    public String toString() {
096        return String.format("DocumentLocationImpl [docIdRef=%s, docPathRef=%s, docRef=%s, serverName=%s]", docIdRef,
097                docPathRef, docRef, serverName);
098    }
099
100    /*
101     * (non-Javadoc)
102     * @see java.lang.Object#hashCode() Overrides the default to use the docRef and serverName fields for hash value
103     * tests.
104     */
105    @Override
106    public int hashCode() {
107        final int prime = 31;
108        int result = 1;
109        result = prime * result + ((docRef == null) ? 0 : docRef.hashCode());
110        result = prime * result + ((serverName == null) ? 0 : serverName.hashCode());
111        return result;
112    }
113
114    /*
115     * (non-Javadoc)
116     * @see java.lang.Object#equals(java.lang.Object) Overrides the default to use the docRef and serverName fields for
117     * equality tests.
118     */
119    @Override
120    public boolean equals(Object obj) {
121        if (this == obj) {
122            return true;
123        }
124        if (obj == null) {
125            return false;
126        }
127        if (getClass() != obj.getClass()) {
128            return false;
129        }
130        DocumentLocation other = (DocumentLocation) obj;
131        if (docRef == null) {
132            if (other.getDocRef() != null) {
133                return false;
134            }
135        } else if (!docRef.equals(other.getDocRef())) {
136            return false;
137        }
138        if (serverName == null) {
139            if (other.getServerName() != null) {
140                return false;
141            }
142        } else if (!serverName.equals(other.getServerName())) {
143            return false;
144        }
145        return true;
146    }
147
148}