001/*
002 * (C) Copyright 2006-2014 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 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.storage.sql.coremodel;
020
021import java.io.Serializable;
022
023import org.nuxeo.ecm.core.api.DocumentNotFoundException;
024import org.nuxeo.ecm.core.api.model.VersionNotModifiableException;
025import org.nuxeo.ecm.core.model.Document;
026import org.nuxeo.ecm.core.schema.types.ComplexType;
027import org.nuxeo.ecm.core.storage.sql.Model;
028import org.nuxeo.ecm.core.storage.sql.Node;
029
030public class SQLDocumentVersion extends SQLDocumentLive {
031
032    private final Node versionableNode;
033
034    protected SQLDocumentVersion(Node node, ComplexType type, SQLSession session, boolean readonly) {
035        super(node, type, session, readonly);
036        Serializable versionSeriesId = getPropertyValue(Model.VERSION_VERSIONABLE_PROP);
037        if (versionSeriesId == null) {
038            throw new DocumentNotFoundException("Version was removed: " + node.getId());
039        }
040        versionableNode = session.getNodeById(versionSeriesId);
041    }
042
043    /*
044     * ----- version-specific overrides -----
045     */
046
047    @Override
048    public boolean isVersion() {
049        return true;
050    }
051
052    @Override
053    public boolean isCheckedOut() {
054        return false;
055    }
056
057    @Override
058    public boolean isVersionSeriesCheckedOut() {
059        if (versionableNode == null) {
060            return false;
061        }
062        return !Boolean.TRUE.equals(versionableNode.getSimpleProperty(Model.MAIN_CHECKED_IN_PROP).getValue());
063    }
064
065    @Override
066    public boolean isMajorVersion() {
067        return Long.valueOf(0).equals(getPropertyValue(Model.MAIN_MINOR_VERSION_PROP));
068    }
069
070    @Override
071    public boolean isLatestVersion() {
072        return Boolean.TRUE.equals(getPropertyValue(Model.VERSION_IS_LATEST_PROP));
073    }
074
075    @Override
076    public boolean isLatestMajorVersion() {
077        return Boolean.TRUE.equals(getPropertyValue(Model.VERSION_IS_LATEST_MAJOR_PROP));
078    }
079
080    @Override
081    public Document getWorkingCopy() {
082        if (versionableNode == null) {
083            return null;
084        }
085        return session.getDocumentByUUID(versionableNode.getId().toString());
086    }
087
088    @Override
089    public Document getBaseVersion() {
090        return null;
091    }
092
093    @Override
094    public String getVersionSeriesId() {
095        Serializable versionSeriesId = getPropertyValue(Model.VERSION_VERSIONABLE_PROP);
096        return session.idToString(versionSeriesId);
097    }
098
099    @Override
100    public Document getSourceDocument() {
101        return getWorkingCopy();
102    }
103
104    @Override
105    public String getPath() {
106        if (versionableNode == null) {
107            return null; // TODO return what? error?
108        }
109        return session.getPath(versionableNode);
110    }
111
112    @Override
113    public Document getParent() {
114        if (versionableNode == null) {
115            return null;
116        }
117        return session.getParent(versionableNode);
118    }
119
120    // protected Property getACLProperty() not overriden, no ACL anyway
121
122    /*
123     * ----- folder overrides -----
124     */
125
126    @Override
127    public boolean isFolder() {
128        return false;
129    }
130
131    @Override
132    public void orderBefore(String src, String dest) {
133        throw new VersionNotModifiableException();
134    }
135
136    /*
137     * ----- versioning overrides -----
138     */
139
140    @Override
141    public Document checkIn(String label, String description) {
142        throw new VersionNotModifiableException();
143    }
144
145    @Override
146    public void checkOut() {
147        throw new VersionNotModifiableException();
148    }
149
150    @Override
151    public void restore(Document version) {
152        throw new VersionNotModifiableException();
153    }
154
155    @Override
156    public Document getVersion(String label) {
157        return null;
158    }
159
160    /*
161     * ----- property write overrides -----
162     */
163
164    @Override
165    public void setPropertyValue(String name, Serializable value) {
166        if (isReadOnlyProperty(name)) {
167            throw new VersionNotModifiableException(String.format("Cannot set property on a version: %s = %s", name,
168                    value));
169        }
170        // import
171        super.setPropertyValue(name, value);
172    }
173
174    protected boolean isReadOnlyProperty(String name) {
175        return isReadOnly() && !isVersionWritableProperty(name);
176    }
177
178    /*
179     * ----- equals/hashcode -----
180     */
181
182    @Override
183    public boolean equals(Object other) {
184        if (other == this) {
185            return true;
186        }
187        if (other == null) {
188            return false;
189        }
190        if (other.getClass() == this.getClass()) {
191            return equals((SQLDocumentVersion) other);
192        }
193        return false;
194    }
195
196    private boolean equals(SQLDocumentVersion other) {
197        return getNode().equals(other.getNode());
198    }
199
200    @Override
201    public int hashCode() {
202        return getNode().hashCode();
203    }
204
205}