001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thomas Roger <troger@nuxeo.com>
016 */
017
018package org.nuxeo.ecm.activity;
019
020import java.io.IOException;
021import java.io.StringWriter;
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.Date;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028
029import javax.persistence.Column;
030import javax.persistence.Entity;
031import javax.persistence.GeneratedValue;
032import javax.persistence.GenerationType;
033import javax.persistence.Id;
034import javax.persistence.Lob;
035import javax.persistence.Table;
036import javax.persistence.Temporal;
037import javax.persistence.TemporalType;
038import javax.persistence.Transient;
039
040import org.apache.commons.lang.builder.EqualsBuilder;
041import org.apache.commons.lang.builder.HashCodeBuilder;
042import org.apache.commons.lang.builder.ToStringBuilder;
043import org.apache.commons.logging.Log;
044import org.apache.commons.logging.LogFactory;
045import org.codehaus.jackson.map.ObjectMapper;
046import org.codehaus.jackson.type.TypeReference;
047
048/**
049 * Default implementation of {@link Activity}.
050 *
051 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
052 * @since 5.5
053 */
054@Entity(name = "Activity")
055@Table(name = "nxp_activities")
056public class ActivityImpl implements Activity {
057
058    private static final Log log = LogFactory.getLog(ActivityImpl.class);
059
060    private Long id;
061
062    private String actor;
063
064    private String displayActor;
065
066    private String verb;
067
068    private String object;
069
070    private String displayObject;
071
072    private String target;
073
074    private String displayTarget;
075
076    private String context;
077
078    private Date publishedDate;
079
080    private Date lastUpdatedDate;
081
082    private String replies;
083
084    @Id
085    @GeneratedValue(strategy = GenerationType.AUTO)
086    @Column(nullable = false, columnDefinition = "integer")
087    @Override
088    public Long getId() {
089        return id;
090    }
091
092    public void setId(Long id) {
093        this.id = id;
094    }
095
096    @Column
097    @Override
098    public String getActor() {
099        return actor;
100    }
101
102    @Override
103    public void setActor(String actor) {
104        this.actor = actor;
105    }
106
107    @Column
108    @Override
109    public String getDisplayActor() {
110        return displayActor;
111    }
112
113    @Override
114    public void setDisplayActor(String displayActor) {
115        this.displayActor = displayActor;
116    }
117
118    @Column
119    @Override
120    public String getVerb() {
121        return verb;
122    }
123
124    @Override
125    public void setVerb(String verb) {
126        this.verb = verb;
127    }
128
129    @Column
130    @Override
131    public String getObject() {
132        return object;
133    }
134
135    @Override
136    public void setObject(String object) {
137        this.object = object;
138    }
139
140    @Column
141    @Override
142    public String getDisplayObject() {
143        return displayObject;
144    }
145
146    @Override
147    public void setDisplayObject(String displayObject) {
148        this.displayObject = displayObject;
149    }
150
151    @Column
152    @Override
153    public String getTarget() {
154        return target;
155    }
156
157    @Override
158    public void setTarget(String target) {
159        this.target = target;
160    }
161
162    @Column
163    @Override
164    public String getDisplayTarget() {
165        return displayTarget;
166    }
167
168    @Override
169    public void setDisplayTarget(String displayTarget) {
170        this.displayTarget = displayTarget;
171    }
172
173    @Column
174    @Override
175    public String getContext() {
176        return context;
177    }
178
179    @Override
180    public void setContext(String context) {
181        this.context = context;
182    }
183
184    @Temporal(TemporalType.TIMESTAMP)
185    @Column(nullable = false)
186    @Override
187    public Date getPublishedDate() {
188        return publishedDate;
189    }
190
191    @Override
192    public void setPublishedDate(Date publishedDate) {
193        this.publishedDate = publishedDate;
194    }
195
196    @Temporal(TemporalType.TIMESTAMP)
197    @Column
198    @Override
199    public Date getLastUpdatedDate() {
200        return lastUpdatedDate;
201    }
202
203    @Override
204    public void setLastUpdatedDate(Date lastUpdated) {
205        this.lastUpdatedDate = lastUpdated;
206    }
207
208    @Column
209    @Lob
210    @Override
211    public String getReplies() {
212        return replies;
213    }
214
215    @Override
216    public void setReplies(String replies) {
217        this.replies = replies;
218    }
219
220    @Transient
221    @Override
222    public List<ActivityReply> getActivityReplies() {
223        if (replies == null) {
224            return new ArrayList<ActivityReply>();
225        }
226
227        try {
228            ObjectMapper mapper = new ObjectMapper();
229            return mapper.readValue(replies, new TypeReference<List<ActivityReply>>() {
230            });
231        } catch (IOException e) {
232            log.warn(String.format("Unable to convert replies to ActivityReply: %s", e.getMessage()));
233            log.debug(e, e);
234            return new ArrayList<ActivityReply>();
235        }
236    }
237
238    @Override
239    public void setActivityReplies(List<ActivityReply> activityReplies) {
240        try {
241            ObjectMapper mapper = new ObjectMapper();
242            StringWriter writer = new StringWriter();
243            mapper.writeValue(writer, activityReplies);
244            replies = writer.toString();
245        } catch (IOException e) {
246            log.warn(String.format("Unable to convert replies to ActivityReply: %s", e.getMessage()));
247            log.debug(e, e);
248        }
249    }
250
251    @Override
252    public Map<String, String> toMap() {
253        Map<String, String> m = new HashMap<String, String>();
254        m.put("id", String.valueOf(id));
255        m.put("actor", actor);
256        m.put("displayActor", displayActor);
257        m.put("object", object);
258        m.put("displayObject", displayObject);
259        m.put("target", target);
260        m.put("displayTarget", displayTarget);
261        m.put("verb", verb);
262        m.put("context", context);
263        m.put("publishedDate", publishedDate.toString());
264        m.put("lastUpdatedDate", lastUpdatedDate != null ? lastUpdatedDate.toString() : null);
265        m.put("replies", replies);
266        return Collections.unmodifiableMap(m);
267    }
268
269    @Override
270    public boolean equals(Object obj) {
271        return EqualsBuilder.reflectionEquals(this, obj);
272    }
273
274    @Override
275    public int hashCode() {
276        return HashCodeBuilder.reflectionHashCode(this);
277    }
278
279    @Override
280    public String toString() {
281        return ToStringBuilder.reflectionToString(this);
282    }
283
284}