001/*
002 * Copyright 2013 Box, Inc. All rights reserved.
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 */
016package org.nuxeo.box.api.marshalling.dao;
017
018import com.fasterxml.jackson.annotation.JsonProperty;
019import com.fasterxml.jackson.annotation.JsonTypeInfo;
020import org.nuxeo.box.api.utils.ISO8601DateParser;
021
022import java.util.Date;
023import java.util.Map;
024
025/**
026 * This is base class for all box objects.
027 */
028@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type", defaultImpl = BoxTypedObject.class)
029public class BoxTypedObject extends BoxObject {
030
031    public static final String FIELD_TYPE = "type";
032
033    public static final String FIELD_ID = "id";
034
035    public static final String FIELD_CREATED_AT = "created_at";
036
037    public static final String FIELD_MODIFIED_AT = "modified_at";
038
039    public BoxTypedObject() {
040    }
041
042    /**
043     * Copy constructor, this does deep copy for all the fields.
044     *
045     * @param obj
046     */
047    public BoxTypedObject(BoxTypedObject obj) {
048        super(obj);
049    }
050
051    /**
052     * Instantiate the object from a map. Each entry in the map reflects to a field.
053     *
054     * @param map
055     */
056    public BoxTypedObject(Map<String, Object> map) {
057        super(map);
058    }
059
060    /**
061     * Get BoxResourceType of this object. using getBoxResourceType() instead . use getTypeFromLowercaseString method in
062     * IBoxResourceHub to parse the result String into type object.
063     *
064     * @return
065     */
066    @Deprecated
067    public BoxResourceType resourceType() {
068        return BoxResourceType.getTypeFromLowercaseString(getType());
069    }
070
071    /**
072     * Get the type.
073     *
074     * @return type
075     */
076    @JsonProperty(FIELD_TYPE)
077    public String getType() {
078        return (String) getValue(FIELD_TYPE);
079    }
080
081    /**
082     * Setter. This is only used by {@see <a href="http://jackson.codehaus .org">Jackson JSON processer</a>}
083     *
084     * @param type type
085     */
086    @JsonProperty(FIELD_TYPE)
087    public void setType(String type) {
088        put(FIELD_TYPE, type);
089    }
090
091    /**
092     * Get id.
093     *
094     * @return id
095     */
096    @JsonProperty(FIELD_ID)
097    public String getId() {
098        return (String) getValue(FIELD_ID);
099    }
100
101    /**
102     * Setter. This is only used by {@see <a href="http://jackson.codehaus .org">Jackson JSON processer</a>}
103     *
104     * @param id id
105     */
106    @JsonProperty(FIELD_ID)
107    private void setId(String id) {
108        put(FIELD_ID, id);
109    }
110
111    /**
112     * Get the time this user was created at. (This returns a String and can be parsed into {@link java.util.Date} by
113     *
114     * @return the created_at
115     */
116    @JsonProperty(FIELD_CREATED_AT)
117    public String getCreatedAt() {
118        return (String) getValue(FIELD_CREATED_AT);
119    }
120
121    /**
122     * Setter. This is only used by {@see <a href="http://jackson.codehaus .org">Jackson JSON processer</a>}
123     *
124     * @param createdAt the created_at to set
125     */
126    @JsonProperty(FIELD_CREATED_AT)
127    private void setCreatedAt(String createdAt) {
128        put(FIELD_CREATED_AT, createdAt);
129    }
130
131    /**
132     * Get the time created at.
133     *
134     * @return Date representation of the created_at value. Null if there was no created_at or if it could not be parsed
135     *         as an ISO8601 date.
136     * @throws java.text.ParseException
137     */
138    public Date dateCreatedAt() {
139        return ISO8601DateParser.parseSilently(getCreatedAt());
140    }
141
142    /**
143     * Get the time this user was modified the last time. (This returns a String and can be parsed into
144     * {@link java.util.Date} by
145     *
146     * @return the modified_at
147     */
148    @JsonProperty(FIELD_MODIFIED_AT)
149    public String getModifiedAt() {
150        return (String) getValue(FIELD_MODIFIED_AT);
151    }
152
153    /**
154     * Setter. This is only used by {@see <a href="http://jackson.codehaus .org">Jackson JSON processer</a>}
155     *
156     * @param modifiedAt the modified_at to set
157     */
158    @JsonProperty(FIELD_MODIFIED_AT)
159    private void setModifiedAt(String modifiedAt) {
160        put(FIELD_MODIFIED_AT, modifiedAt);
161    }
162
163    /**
164     * Get the date this object is modified at.
165     *
166     * @return Date representation of the modified_at value. Null if there was no date_modified or if it could not be
167     *         parsed as an ISO8601 date.
168     * @throws java.text.ParseException
169     */
170    public Date dateModifiedAt() {
171        return ISO8601DateParser.parseSilently(getModifiedAt());
172    }
173}