BrightSide Workbench Full Report + Source Code
CommonsUserActivity.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.ArrayList;
22 import java.util.Date;
23 import java.util.EnumSet;
24 import java.util.HashSet;
25 import java.util.Set;
26 import org.turro.string.Strings;
27 import org.turro.elephant.db.ElephantPU;
28 import org.turro.elephant.db.WhereClause;
29 import org.turro.elephant.entities.db.EntityParticipation;
30 import org.turro.jpa.Dao;
31 import org.turro.participation.ParticipationReason;
32 import org.turro.path.Path;
33 import org.turro.plugin.contacts.IContact;
34 
39 @UserActivity
41 
42  @Override
43  protected Dao createDao() {
44  return new ElephantPU();
45  }
46 
47  @Override
48  protected Set<ActivityType> getAllowed() {
51  }
52 
53  @Override
54  protected Set<Activity> getActivity(IContact contact, Date from, Set<ActivityType> types) {
55  Set<Activity> set = new HashSet<>();
56  set.addAll(getParticipations(contact, from, types));
57  return set;
58  }
59 
60  private Set<Activity> getParticipations(IContact contact, Date from, Set<ActivityType> types) {
61  EnumSet<ParticipationReason> reasons = EnumSet.noneOf(ParticipationReason.class);
62  if(types.contains(ActivityType.ACTIVITY_FOLLOW)) reasons.add(ParticipationReason.REASON_FOLLOW);
63  if(types.contains(ActivityType.ACTIVITY_PARTICIPATE)) {
65  reasons.add(ParticipationReason.REASON_APPLY);
67  }
68  if(types.contains(ActivityType.ACTIVITY_SEEN)) reasons.add(ParticipationReason.REASON_SEEN);
69  if(types.contains(ActivityType.ACTIVITY_SPAM)) reasons.add(ParticipationReason.REASON_SPAM);
70  if(types.contains(ActivityType.ACTIVITY_LIKE)) reasons.add(ParticipationReason.REASON_LIKE);
71  WhereClause wc = new WhereClause();
72  wc.addClause("select ep from EntityParticipation ep");
73  wc.addIn("where", "ep.reason", new ArrayList(reasons));
74  if(from != null) {
75  wc.addClause("and ep.participationDate >= :date");
76  wc.addNamedValue("date", from);
77  }
78  if(contact != null) {
79  wc.addClause("and (");
80  wc.addClause("ep.participatorPath = :participator");
81  //TODO: Followed wc.addClause("or (ep.entityPath = :participator and ep.reason = :follow)");
82  //wc.addNamedValue("follow", ParticipationReason.REASON_FOLLOW);
83  wc.addNamedValue("participator", "/contact/" + contact.getId());
84  wc.addClause(")");
85  }
86  wc.addClause("order by ep.participationDate desc");
87  Set<Activity> set = new HashSet<>();
88  for(EntityParticipation ep : getDao().getResultList(EntityParticipation.class, wc, 100)) {
89  if(!Strings.isBlank(ep.getEntity().getName()) && !Strings.isBlank(ep.getEntity().getImage())) {
90  Activity activity = convertToActivity(types, ep);
91  if(activity != null && !Strings.isBlank(activity.getEntity().getName())) set.add(activity);
92  }
93  }
94  return set;
95  }
96 
97  private Activity convertToActivity(Set<ActivityType> types, EntityParticipation ep) {
98  switch (ep.getReason()) {
99  case REASON_FOLLOW:
100  return new Activity(ep.getParticipationDate(), ActivityType.ACTIVITY_FOLLOW, Path.getIdentifier(ep.getParticipatorPath()), ep.getEntityPath(), ep);
101  case REASON_PARTICIPATE:
102  return new Activity(ep.getParticipationDate(), ActivityType.ACTIVITY_PARTICIPATE, Path.getIdentifier(ep.getParticipatorPath()), ep.getEntityPath(), ep);
103  case REASON_SEEN:
104  return new Activity(ep.getParticipationDate(), ActivityType.ACTIVITY_SEEN, Path.getIdentifier(ep.getParticipatorPath()), ep.getEntityPath(), ep);
105  case REASON_SPAM:
106  return new Activity(ep.getParticipationDate(), ActivityType.ACTIVITY_SPAM, Path.getIdentifier(ep.getParticipatorPath()), ep.getEntityPath(), ep);
107  case REASON_LIKE:
108  return new Activity(ep.getParticipationDate(), ActivityType.ACTIVITY_LIKE, Path.getIdentifier(ep.getParticipatorPath()), ep.getEntityPath(), ep);
109  default:
110  return null;
111  }
112  }
113 
114 }
Set< Activity > getActivity(IContact contact, Date from, Set< ActivityType > types)