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