BrightSide Workbench Full Report + Source Code
elephant/src/main/java/org/turro/user/activity/Activity.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.user.activity;
20 
21 import java.util.Date;
22 import org.turro.elephant.context.IConstructor;
23 import org.turro.entities.Entities;
24 import org.turro.entities.IElephantEntity;
25 import org.turro.marker.ElephantMarker;
26 import org.turro.plugin.contacts.IContact;
27 import org.turro.util.CompareUtil;
28 
33 public class Activity implements Comparable<Activity> {
34 
35  private final Date date;
36  private final String contactId, entityPath;
37  private final ActivityType type;
38  private final Object origin;
39  private IElephantEntity entity;
40 
41  public Activity(Date date, ActivityType type, String contactId, String entityPath, Object origin) {
42  this.date = date;
43  this.type = type;
44  this.contactId = contactId;
45  this.entityPath = entityPath;
46  this.origin = origin;
47  }
48 
49  public Date getDate() {
50  return date;
51  }
52 
53  public String getContactId() {
54  return contactId;
55  }
56 
57  public String getEntityPath() {
58  return entityPath;
59  }
60 
62  if(entity == null) {
63  entity = Entities.getController(entityPath);
64  }
65  return entity;
66  }
67 
68  public ActivityType getType() {
69  return type;
70  }
71 
72  public Object getOrigin() {
73  return origin;
74  }
75 
76  public String parse(IConstructor constructor) {
77  return parseFor(constructor, null);
78  }
79 
80  public String parseFor(IConstructor constructor, IContact contact) {
81  String template = type.toString().replaceAll("_", "-").toLowerCase();
82  if(contact != null) {
83  if(ActivityType.ACTIVITY_FOLLOW.equals(type) && entityPath.equals("/contact/" + contact.getId())) {
84  template += "ed"; // followed
85  }
86  }
87  template += "-" + origin.getClass().getSimpleName().toLowerCase();
88  if(ElephantMarker.existsTemplate(constructor, false, "widgets/activity", template)) {
89  ElephantMarker marker = new ElephantMarker(constructor);
90  marker.put("activity", this);
91  return marker.parse("widgets/activity", template);
92  }
93  return "";
94  }
95 
96  @Override
97  public int compareTo(Activity activity) {
98  int result = CompareUtil.compare(activity.date, date);
99  if(result == 0) {
100  result = CompareUtil.compare(type.ordinal(), activity.type.ordinal());
101  }
102  if(result == 0) {
103  result = CompareUtil.compare(contactId, activity.contactId);
104  }
105  if(result == 0) {
106  result = CompareUtil.compare(entityPath, activity.entityPath);
107  }
108  return result;
109  }
110 
111 }
static IElephantEntity getController(String path)
Definition: Entities.java:78
static boolean existsTemplate(IConstructor constructor, boolean mail, String root, String name)
String parse(String rootTmpl, String tmpl)
Object put(Object key, Object value)
Activity(Date date, ActivityType type, String contactId, String entityPath, Object origin)
String parseFor(IConstructor constructor, IContact contact)