001/*
002 * (C) Copyright 2006-2016 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 java.util.Date;
022
023import org.codehaus.jackson.annotate.JsonIgnore;
024import org.codehaus.jackson.annotate.JsonProperty;
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    /**
075     * Reserved to framework. Should be only called by client framework when unmarshalling documents.
076     *
077     * @since 5.7.3
078     */
079    public Document(String id, String type, PropertyList facets, String changeToken, String path, String state,
080            String lockOwner, String lockCreated, String repository, String versionLabel, String isCheckedOut,
081            PropertyMap properties, PropertyMap contextParameters) {
082        super(id);
083        this.changeToken = changeToken;
084        this.facets = facets;
085        this.path = path;
086        this.type = type;
087        this.state = state;
088        this.lockOwner = lockOwner;
089        this.lockCreated = lockCreated;
090        this.repository = repository;
091        this.versionLabel = versionLabel;
092        this.isCheckedOut = isCheckedOut;
093        this.properties = properties == null ? new PropertyMap() : properties;
094        this.contextParameters = contextParameters == null ? new PropertyMap() : contextParameters;
095        propertiesSetter = new PropertyMapSetter(properties == null ? new PropertyMap() : properties);
096    }
097
098    /**
099     * Minimal constructor for automation client Document. Could be instantiated when creating a document and passing to
100     * the related automation operation.
101     *
102     * @since 5.7
103     */
104    public Document(String id, String type) {
105        super(id);
106        this.type = type;
107        propertiesSetter = new PropertyMapSetter(new PropertyMap());
108        changeToken = null;
109        facets = null;
110        path = null;
111        state = null;
112        lockOwner = null;
113        lockCreated = null;
114        repository = null;
115        versionLabel = null;
116        isCheckedOut = null;
117        properties = new PropertyMap();
118        contextParameters = new PropertyMap();
119    }
120
121    public String getRepository() {
122        return repository;
123    }
124
125    @JsonProperty("uid")
126    public String getId() {
127        return ref;
128    }
129
130    @JsonProperty("entity-type")
131    @Override
132    public String getInputType() {
133        return "document";
134    }
135
136    public String getPath() {
137        return path;
138    }
139
140    public String getType() {
141        return type;
142    }
143
144    public String getLock() {
145        if (lockOwner != null && lockCreated != null) {
146            return lockOwner + ":" + lockCreated;
147        }
148        return null;
149    }
150
151    public String getLockOwner() {
152        return lockOwner;
153    }
154
155    public String getLockCreated() {
156        return lockCreated;
157    }
158
159    public boolean isLocked() {
160        return lockOwner != null;
161    }
162
163    public String getState() {
164        return state;
165    }
166
167    public String getVersionLabel() {
168        return versionLabel;
169    }
170
171    public Boolean isCheckedOut() {
172        return (isCheckedOut == null) ? null : Boolean.valueOf(isCheckedOut);
173    }
174
175    public Date getLastModified() {
176        return properties.getDate("dc:modified");
177    }
178
179    public String getTitle() {
180        return properties.getString("dc:title");
181    }
182
183    @JsonIgnore
184    public PropertyMap getProperties() {
185        return properties;
186    }
187
188    public String getString(String key) {
189        return properties.getString(key);
190    }
191
192    public Date getDate(String key) {
193        return properties.getDate(key);
194    }
195
196    public Long getLong(String key) {
197        return properties.getLong(key);
198    }
199
200    public Double getDouble(String key) {
201        return properties.getDouble(key);
202    }
203
204    public String getString(String key, String defValue) {
205        return properties.getString(key, defValue);
206    }
207
208    public Date getDate(String key, Date defValue) {
209        return properties.getDate(key, defValue);
210    }
211
212    public Long getLong(String key, Long defValue) {
213        return properties.getLong(key, defValue);
214    }
215
216    public Double getDouble(String key, Double defValue) {
217        return properties.getDouble(key, defValue);
218    }
219
220    public void set(String key, String defValue) {
221        propertiesSetter.set(key, defValue);
222    }
223
224    public void set(String key, Date defValue) {
225        propertiesSetter.set(key, defValue);
226    }
227
228    public void set(String key, Long defValue) {
229        propertiesSetter.set(key, defValue);
230    }
231
232    public void set(String key, Double defValue) {
233        propertiesSetter.set(key, defValue);
234    }
235
236    /**
237     * @since 5.7
238     */
239    public void set(String key, Boolean defValue) {
240        propertiesSetter.set(key, defValue);
241    }
242
243    /**
244     * @since 5.7
245     */
246    public void set(String key, PropertyMap defValue) {
247        propertiesSetter.set(key, defValue);
248    }
249
250    /**
251     * @since 5.7
252     */
253    public void set(String key, PropertyList defValue) {
254        propertiesSetter.set(key, defValue);
255    }
256
257    public String getChangeToken() {
258        return changeToken;
259    }
260
261    public PropertyList getFacets() {
262        return facets;
263    }
264
265    public PropertyMap getContextParameters() {
266        return contextParameters;
267    }
268
269    /**
270     * This method fetch the dirty properties of the document (which have been updated during the session)
271     *
272     * @since 5.7
273     */
274    public PropertyMap getDirties() {
275        return propertiesSetter.getDirties();
276    }
277
278}