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 *     bstefanescu
018 */
019package org.nuxeo.ecm.automation.client.model;
020
021import org.codehaus.jackson.annotate.JsonIgnore;
022import org.codehaus.jackson.annotate.JsonProperty;
023
024import java.util.Date;
025
026/**
027 * A document. Documents are as they are returned by the server. To modify documents use operations. Use
028 * {@link #getProperties()} method to fetch the document properties and {@link #getDirties()} to fetch dirty properties
029 * updated.
030 * <p>
031 * You need to create your own wrapper if you need to access the document properties in a multi-level way. This is a
032 * flat representation of the document.
033 * <p>
034 * Possible property value types:
035 * <ul>
036 * <li>String
037 * <li>Number
038 * <li>Date
039 * <ul>
040 *
041 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
042 */
043public class Document extends DocRef {
044
045    private static final long serialVersionUID = 1L;
046
047    protected final String repository;
048
049    protected final String path;
050
051    protected final String type;
052
053    // TODO can be stored in map
054    protected final String state;
055
056    protected final String lockOwner;
057
058    protected final String lockCreated;
059
060    protected final String versionLabel;
061
062    protected final String isCheckedOut;
063
064    protected final PropertyMap properties;
065
066    protected final transient PropertyMapSetter propertiesSetter;
067
068    protected final PropertyMap contextParameters;
069
070    protected final String changeToken;
071
072    protected final PropertyList facets;
073
074    @Deprecated
075    /**
076     * Deprecated now use with the constructor with versionLabel and isCheckedOut
077     *
078     */
079    public Document(String id, String type, PropertyList facets, String changeToken, String path, String state,
080            String lockOwner, String lockCreated, String repository, PropertyMap properties,
081            PropertyMap contextParameters) {
082        this(id, type, facets, changeToken, path, state, lockOwner, lockCreated, repository, null, null, properties,
083                contextParameters);
084    }
085
086    @Deprecated
087    /**
088     * Deprecated now use with the constructor with isCheckedOut
089     *
090     */
091    public Document(String id, String type, PropertyList facets, String changeToken, String path, String state,
092            String lockOwner, String lockCreated, String repository, String versionLabel, PropertyMap properties,
093            PropertyMap contextParameters) {
094        this(id, type, facets, changeToken, path, state, lockOwner, lockCreated, repository, versionLabel, null,
095                properties, contextParameters);
096    }
097
098    /**
099     * Reserved to framework. Should be only called by client framework when unmarshalling documents.
100     *
101     * @since 5.7.3
102     */
103    public Document(String id, String type, PropertyList facets, String changeToken, String path, String state,
104            String lockOwner, String lockCreated, String repository, String versionLabel, String isCheckedOut,
105            PropertyMap properties, PropertyMap contextParameters) {
106        super(id);
107        this.changeToken = changeToken;
108        this.facets = facets;
109        this.path = path;
110        this.type = type;
111        this.state = state;
112        this.lockOwner = lockOwner;
113        this.lockCreated = lockCreated;
114        this.repository = repository;
115        this.versionLabel = versionLabel;
116        this.isCheckedOut = isCheckedOut;
117        this.properties = properties == null ? new PropertyMap() : properties;
118        this.contextParameters = contextParameters == null ? new PropertyMap() : contextParameters;
119        propertiesSetter = new PropertyMapSetter(properties == null ? new PropertyMap() : properties);
120    }
121
122    /**
123     * Minimal constructor for automation client Document. Could be instantiated when creating a document and passing to
124     * the related automation operation.
125     *
126     * @since 5.7
127     */
128    public Document(String id, String type) {
129        super(id);
130        this.type = type;
131        propertiesSetter = new PropertyMapSetter(new PropertyMap());
132        changeToken = null;
133        facets = null;
134        path = null;
135        state = null;
136        lockOwner = null;
137        lockCreated = null;
138        repository = null;
139        versionLabel = null;
140        isCheckedOut = null;
141        properties = new PropertyMap();
142        contextParameters = new PropertyMap();
143    }
144
145    public String getRepository() {
146        return repository;
147    }
148
149    @JsonProperty("uid")
150    public String getId() {
151        return ref;
152    }
153
154    @JsonProperty("entity-type")
155    @Override
156    public String getInputType() {
157        return "document";
158    }
159
160    public String getPath() {
161        return path;
162    }
163
164    public String getType() {
165        return type;
166    }
167
168    public String getLock() {
169        if (lockOwner != null && lockCreated != null) {
170            return lockOwner + ":" + lockCreated;
171        }
172        return null;
173    }
174
175    public String getLockOwner() {
176        return lockOwner;
177    }
178
179    public String getLockCreated() {
180        return lockCreated;
181    }
182
183    public boolean isLocked() {
184        return lockOwner != null;
185    }
186
187    public String getState() {
188        return state;
189    }
190
191    public String getVersionLabel() {
192        return versionLabel;
193    }
194
195    public Boolean isCheckedOut() {
196        return (isCheckedOut == null) ? null : Boolean.parseBoolean(isCheckedOut);
197    }
198
199    public Date getLastModified() {
200        return properties.getDate("dc:modified");
201    }
202
203    public String getTitle() {
204        return properties.getString("dc:title");
205    }
206
207    @JsonIgnore
208    public PropertyMap getProperties() {
209        return properties;
210    }
211
212    public String getString(String key) {
213        return properties.getString(key);
214    }
215
216    public Date getDate(String key) {
217        return properties.getDate(key);
218    }
219
220    public Long getLong(String key) {
221        return properties.getLong(key);
222    }
223
224    public Double getDouble(String key) {
225        return properties.getDouble(key);
226    }
227
228    public String getString(String key, String defValue) {
229        return properties.getString(key, defValue);
230    }
231
232    public Date getDate(String key, Date defValue) {
233        return properties.getDate(key, defValue);
234    }
235
236    public Long getLong(String key, Long defValue) {
237        return properties.getLong(key, defValue);
238    }
239
240    public Double getDouble(String key, Double defValue) {
241        return properties.getDouble(key, defValue);
242    }
243
244    public void set(String key, String defValue) {
245        propertiesSetter.set(key, defValue);
246    }
247
248    public void set(String key, Date defValue) {
249        propertiesSetter.set(key, defValue);
250    }
251
252    public void set(String key, Long defValue) {
253        propertiesSetter.set(key, defValue);
254    }
255
256    public void set(String key, Double defValue) {
257        propertiesSetter.set(key, defValue);
258    }
259
260    /**
261     * @since 5.7
262     */
263    public void set(String key, Boolean defValue) {
264        propertiesSetter.set(key, defValue);
265    }
266
267    /**
268     * @since 5.7
269     */
270    public void set(String key, PropertyMap defValue) {
271        propertiesSetter.set(key, defValue);
272    }
273
274    /**
275     * @since 5.7
276     */
277    public void set(String key, PropertyList defValue) {
278        propertiesSetter.set(key, defValue);
279    }
280
281    public String getChangeToken() {
282        return changeToken;
283    }
284
285    public PropertyList getFacets() {
286        return facets;
287    }
288
289    public PropertyMap getContextParameters() {
290        return contextParameters;
291    }
292
293    /**
294     * This method fetch the dirty properties of the document (which have been updated during the session)
295     *
296     * @since 5.7
297     */
298    public PropertyMap getDirties() {
299        return propertiesSetter.getDirties();
300    }
301
302}