BrightSide Workbench Full Report + Source Code
LastActivities.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2019 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.activity;
20 
21 import java.io.File;
22 import java.util.Date;
23 import java.util.Optional;
24 import java.util.Set;
25 import org.amic.util.date.CheckDate;
26 import org.turro.string.ObjectString;
27 import org.turro.string.Strings;
28 import org.turro.elephant.impl.util.Serializer;
29 import org.turro.lock.Initializer;
30 import org.turro.plugin.contacts.IContact;
31 import org.turro.reflection.Instances;
32 
37 public class LastActivities {
38 
39  /* Task util */
40 
41  public static void generateUserContent(Date date) {
42  Date from = new CheckDate().addWeeks(LastActivities.LAST_ACTIVITY_WEEKS).getDate();
43  Date now = new Date();
44  if(!existsActivitySession(now, from, null)) {
45  tryInstance(now, from);
46  }
47  }
48 
49  /* User contacts helpers */
50 
51  public static EntityActivitySet getLastActivity(IContact contact) {
52  if(INIT.isBussy()) return new EntityActivitySet();
53  Date from = new CheckDate().addWeeks(LastActivities.LAST_ACTIVITY_WEEKS).getDate();
54  Date now = new Date();
55  return deserializeLastActivitySession(now, from, contact);
56  }
57 
58  /* Last activity session */
59 
60  public static ContactActivityMap getLastActivitySession(Date from, String entityPath, String type) {
61  cleanOldSessions();
62  if(Strings.isBlank(entityPath) && Strings.isBlank(type)) {
63  Date now = new Date();
64  String fileName = "/activity/" + ObjectString.formatObject(now, ObjectString.URL_DATE_PATTERN, true) +
65  "-" + ObjectString.formatObject(from, ObjectString.URL_DATE_PATTERN, true) + ".ser";
66  File ser = Serializer.getFile(fileName);
67  if(ser.exists()) {
69  } else {
70  ContactActivityMap current = getContactLastActivites(from, entityPath, type);
71  Serializer.serialize(ser, current);
72  return current;
73  }
74  } else {
75  return getContactLastActivites(from, entityPath, type);
76  }
77  }
78 
79  /* Helper */
80 
81  public static Object getMainEntity(Object entity) {
82  for(ILastActivity iLast : Instances.cached().byAnnotation(LastActivity.class, ILastActivity.class)) {
83  Object main = iLast.getMainEntity(entity);
84  if(main != null) {
85  return main;
86  }
87  }
88  return null;
89  }
90 
91  /* Last activities */
92 
93  private static ContactActivityMap getContactLastActivites(Date from, String entityPath, String type) {
94  return getContactLastActivites(getLastActivites(from, entityPath, type));
95  }
96 
97  private static ContactActivityMap getContactLastActivites(Set<IEntityLastActivity> activities) {
98  ContactActivityMap map = new ContactActivityMap();
99  activities.forEach((activity) -> {
100  activity.getParticipants().forEach((assistant) -> {
101  map.putActivity(assistant.getContact(), activity);
102  });
103  });
104  return map;
105  }
106 
107  private static Set<IEntityLastActivity> getLastActivites(Date from, String entityPath, String type) {
108  EntityActivitySet activities = new EntityActivitySet();
109  Instances.cached().byAnnotation(LastActivity.class, ILastActivity.class).forEach((iLast) -> {
110  activities.addAll(iLast.getActivities(from, entityPath, type));
111  });
112  return activities;
113  }
114 
115  private static void cleanOldSessions() {
116  Date now = new CheckDate().addDays(-1).getDate();
117  File[] files = Serializer.getFiles("/activity");
118  if(files != null) {
119  for(File file : files) {
120  Date fileDate = (Date) ObjectString.parseString(file.getName().substring(0, 8),
121  ObjectString.URL_DATE_PATTERN, Date.class, false);
122  if(fileDate == null || fileDate.before(now)) {
123  file.delete();
124  }
125  }
126  }
127  }
128 
129  /* Last activity user content */
130 
131  public static final int LAST_ACTIVITY_WEEKS = -4;
132 
133  private static final Initializer<Boolean> INIT = new Initializer<>();
134 
135  private static Boolean tryInstance(Date now, Date from) {
136  return INIT.tryInstance(
137  () -> existsActivitySession(now, from, null),
138  () -> serializeLastActivitySession(now, from));
139  }
140 
141  private static Boolean serializeLastActivitySession(Date now, Date from) {
142  cleanOldSessions();
143  getContactLastActivites(from, null, null).entrySet().forEach(e -> {
144  String fileName = "/activity/" + filePrefix(now, from) + e.getKey() + ".ser";
145  File ser = Serializer.getFile(fileName);
146  Serializer.serialize(ser, e.getValue());
147  });
148  return Boolean.TRUE;
149  }
150 
151  private static EntityActivitySet deserializeLastActivitySession(Date now, Date from, IContact contact) {
152  String fileName = "/activity/" + filePrefix(now, from) + contact.getId() + ".ser";
153  File ser = Serializer.getFile(fileName);
154  if(ser.exists()) {
155  return Optional.ofNullable((EntityActivitySet) Serializer.deserialize(ser)).orElse(new EntityActivitySet());
156  } else {
157  return new EntityActivitySet();
158  }
159  }
160 
161  private LastActivities() {
162  }
163 
164  /* File utils */
165 
166  private static boolean existsActivitySession(Date now, Date from, String key) {
167  String filePrefix = filePrefix(now, from);
168  if(key == null) {
169  for(File file : Serializer.getFiles("/activity")) {
170  if(file.getName().startsWith(filePrefix)) return true;
171  }
172  return false;
173  } else {
174  return Serializer.getFile("/activity/" + filePrefix + key + ".ser").exists();
175  }
176  }
177 
178  private static String filePrefix(Date now, Date from) {
179  return ObjectString.formatObject(now, ObjectString.URL_DATE_PATTERN, true) +
180  "-" + ObjectString.formatObject(from, ObjectString.URL_DATE_PATTERN, true) +
181  "-";
182  }
183 
184 }
static Object getMainEntity(Object entity)
static EntityActivitySet getLastActivity(IContact contact)
static ContactActivityMap getLastActivitySession(Date from, String entityPath, String type)
static void generateUserContent(Date date)
static void serialize(String fileName, Object instance)
Definition: Serializer.java:49
static Object deserialize(String fileName)
Definition: Serializer.java:60
static File getFile(String fileName)
Definition: Serializer.java:30