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