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