001/*
002 * (C) Copyright 2006-2008 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 *     Alexandre Russel
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.annotations.gwt.client.model;
023
024import java.util.Date;
025import java.util.HashMap;
026import java.util.Map;
027
028import org.nuxeo.ecm.platform.annotations.gwt.client.util.XPointer;
029
030import com.google.gwt.i18n.client.DateTimeFormat;
031
032/**
033 * @author Alexandre Russel
034 */
035public class Annotation {
036    private String uuid;
037
038    private XPointer xpointer;
039
040    private boolean isBodyUrl;
041
042    private Date date;
043
044    private String formattedDate = "";
045
046    private String author;
047
048    private String body;
049
050    private String type;
051
052    private int id;
053
054    private Map<String, String> fields = new HashMap<String, String>();
055
056    private Container startContainer;
057
058    private Container endContainer;
059
060    public Annotation(String uuid) {
061        this.uuid = uuid;
062    }
063
064    public Annotation() {
065    }
066
067    public String getUUID() {
068        return uuid;
069    }
070
071    public String getAuthor() {
072        return author;
073    }
074
075    public void setAuthor(String author) {
076        this.author = author;
077    }
078
079    public int getId() {
080        return id;
081    }
082
083    public void setId(int id) {
084        this.id = id;
085    }
086
087    public void setStringDate(String stringDate) {
088        date = computeDate(stringDate);
089        String dateFormatPattern = getDateFormatPattern();
090        DateTimeFormat dateTimeFormat = dateFormatPattern != null ? DateTimeFormat.getFormat(dateFormatPattern)
091                : DateTimeFormat.getShortDateFormat();
092        formattedDate = dateTimeFormat.format(date);
093    }
094
095    private native String getDateFormatPattern() /*-{
096                                                 return top['dateFormatPattern'];
097                                                 }-*/;
098
099    public Annotation(XPointer xpointer) {
100        this.xpointer = xpointer;
101    }
102
103    public boolean isBodyUrl() {
104        return isBodyUrl;
105    }
106
107    public void setBodyUrl(boolean isBodyUrl) {
108        this.isBodyUrl = isBodyUrl;
109    }
110
111    public String getBody() {
112        return body;
113    }
114
115    public void setBody(String body) {
116        this.body = body;
117    }
118
119    public String getType() {
120        return type;
121    }
122
123    public String getShortType() {
124        return type.substring(type.lastIndexOf("#") + 1);
125    }
126
127    public void setType(String type) {
128        this.type = type;
129    }
130
131    public XPointer getXpointer() {
132        return xpointer;
133    }
134
135    public String serialize() {
136        return type + ' ' + xpointer + ' ' + body;
137    }
138
139    public void setXpointer(XPointer xpointer) {
140        this.xpointer = xpointer;
141    }
142
143    public String getFormattedDate() {
144        return formattedDate;
145    }
146
147    public Date getDate() {
148        return date;
149    }
150
151    @SuppressWarnings("deprecation")
152    private static Date computeDate(String stringDate) {
153        String d = stringDate.substring(0, stringDate.indexOf("T"));
154        String t = stringDate.substring(stringDate.indexOf("T") + 1, stringDate.indexOf("Z"));
155        String[] ds = d.split("-");
156        String[] ts = t.split(":");
157        Date now = new Date();
158        int second = ts.length == 3 ? Integer.parseInt(ts[2]) : 0;
159        now = new Date(Date.UTC(Integer.parseInt(ds[0]) - 1900, Integer.parseInt(ds[1]) - 1, Integer.parseInt(ds[2]),
160                Integer.parseInt(ts[0]), Integer.parseInt(ts[1]), second) + now.getTimezoneOffset());
161        return now;
162    }
163
164    public void setFields(Map<String, String> fields) {
165        this.fields = fields;
166    }
167
168    public Map<String, String> getFields() {
169        return fields;
170    }
171
172    public Container getStartContainer() {
173        return startContainer;
174    }
175
176    public void setStartContainer(Container container) {
177        startContainer = container;
178    }
179
180    public boolean hasStartContainer() {
181        return startContainer != null;
182    }
183
184    public Container getEndContainer() {
185        return endContainer;
186    }
187
188    public void setEndContainer(Container container) {
189        endContainer = container;
190    }
191
192    public boolean hasEndContainer() {
193        return endContainer != null;
194    }
195
196    @Override
197    public boolean equals(Object obj) {
198        if (!(obj instanceof Annotation)) {
199            return false;
200        }
201
202        Annotation annotation = (Annotation) obj;
203        return xpointer.equals(annotation.xpointer) && author.equals(annotation.author);
204    }
205
206    @Override
207    public int hashCode() {
208        int result = 17;
209        result += 17 * xpointer.hashCode();
210        result += 17 * author.hashCode();
211        return result;
212    }
213
214}