19 package org.turro.activity;
22 import java.util.Date;
23 import java.util.Optional;
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;
43 Date now =
new Date();
44 if(!existsActivitySession(now, from,
null)) {
45 tryInstance(now, from);
54 Date now =
new Date();
55 return deserializeLastActivitySession(now, from, contact);
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";
75 return getContactLastActivites(from, entityPath, type);
83 Object main = iLast.getMainEntity(entity);
93 private static ContactActivityMap getContactLastActivites(Date from, String entityPath, String type) {
94 return getContactLastActivites(getLastActivites(from, entityPath, type));
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);
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));
115 private static void cleanOldSessions() {
116 Date now =
new CheckDate().addDays(-1).getDate();
117 File[] files = Serializer.getFiles(
"/activity");
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)) {
133 private static final Initializer<Boolean> INIT =
new Initializer<>();
135 private static Boolean tryInstance(Date now, Date from) {
136 return INIT.tryInstance(
137 () -> existsActivitySession(now, from,
null),
138 () -> serializeLastActivitySession(now, from));
141 private static Boolean serializeLastActivitySession(Date now, Date from) {
143 getContactLastActivites(from,
null,
null).entrySet().forEach(e -> {
144 String fileName =
"/activity/" + filePrefix(now, from) + e.getKey() +
".ser";
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);
155 return Optional.ofNullable((EntityActivitySet) Serializer.deserialize(ser)).orElse(
new EntityActivitySet());
157 return new EntityActivitySet();
161 private LastActivities() {
166 private static boolean existsActivitySession(Date now, Date from, String key) {
167 String filePrefix = filePrefix(now, from);
169 for(File file : Serializer.getFiles(
"/activity")) {
170 if(file.getName().startsWith(filePrefix))
return true;
174 return Serializer.getFile(
"/activity/" + filePrefix + key +
".ser").exists();
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) +
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 final int LAST_ACTIVITY_WEEKS
static void serialize(String fileName, Object instance)
static Object deserialize(String fileName)
static File getFile(String fileName)