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