BrightSide Workbench Full Report + Source Code
Activities.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2023 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.util.ArrayList;
22 import java.util.Date;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Set;
26 import java.util.stream.Collectors;
27 import org.turro.assistant.AssistantConstants;
28 import org.turro.assistant.AssistantSet;
29 import org.turro.assistant.Assistants;
30 import org.turro.elephant.db.ElephantPU;
31 import org.turro.elephant.entities.db.Activity;
32 import org.turro.elephant.entities.db.ActivityAssistant;
33 import org.turro.jpa.Dao;
34 import org.turro.reflection.Instances;
35 import org.turro.sql.SqlClause;
36 import org.turro.string.Strings;
37 import org.turro.util.Cached;
38 
43 public class Activities {
44 
45  public Activity add(Date activityDate, String activityPath, String entityPath, String sortPath) {
46  if(Strings.anyBlank(entityPath, activityPath, sortPath)) return null;
47  Activity activity = new Activity();
48  activity.setReason(reason);
49  activity.setActivityDate(activityDate);
50  activity.setActivityPath(activityPath);
51  activity.setEntityPath(entityPath);
52  activity.setSortPath(sortPath);
53  entityPaths.add(entityPath);
54  return dao.get().saveObject(activity);
55  }
56 
57  public Date lastDate() {
58  return SqlClause.select("max(a.activityDate)").from("Activity a")
59  .where().equal("reason", reason)
60  .dao(dao.get())
61  .singleResult(Date.class, defaultDate);
62  }
63 
64  /* Process */
65 
66  public static void generate() {
68  for(IElephantActivity activity : Instances.cached().byAnnotation(ElephantActivity.class, IElephantActivity.class)) {
69  activity.generate();
70  }
72  }
73 
74  /* Participants */
75 
76  private static final Set<String> entityPaths = new HashSet<>();
77 
78  public static void resetEntityPaths() {
79  entityPaths.clear();
80  }
81 
82  public static void addParticipants() {
83  Dao dao = new ElephantPU();
84  for(String entityPath : entityPaths) {
85  updateEntityPath(dao, entityPath);
86  }
87  entityPaths.clear();
88  }
89 
90  private static void updateEntityPath(Dao dao, String entityPath) {
91  SqlClause.delete("ActivityAssistant")
92  .where().equal("entityPath", entityPath)
93  .dao(dao)
94  .execute();
95  List<ActivityAssistant> assistants = new ArrayList<>();
96  for(String idContact : getParticipants(entityPath)) {
98  aa.setEntityPath(entityPath);
99  aa.setContactId(idContact);
100  assistants.add(aa);
101  }
102  dao.saveCollection(assistants);
103  }
104 
105  public static Set<String> getParticipants(String entityPath) {
106  AssistantSet participants = new AssistantSet();
107  Assistants.addAssistants(entityPath, true, participants, AssistantConstants.all());
108  return participants.stream().map(a -> a.contact.getId()).collect(Collectors.toSet());
109  }
110 
111  /* Dao */
112 
113  private final Cached<Dao> dao = Cached.instance(() -> new ElephantPU());
114 
115  /* Factory */
116 
117  public static Activities of(String reason) {
118  return new Activities(reason);
119  }
120 
121  private final String reason;
122  private static final Date defaultDate = new Date(0L);
123 
124  public Activities(String reason) {
125  this.reason = reason;
126  }
127 
128 }
static Set< String > getParticipants(String entityPath)
static Activities of(String reason)
Activity add(Date activityDate, String activityPath, String entityPath, String sortPath)
Definition: Activities.java:45
static void addAssistants(String role, AssistantSet list, Object data)
Definition: Assistants.java:35
void saveCollection(Collection objs)
Definition: Dao.java:144