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