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