BrightSide Workbench Full Report + Source Code
StudentUserActivity.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 java.util.EnumSet;
23 import java.util.HashSet;
24 import java.util.Set;
25 import org.turro.elephant.db.WhereClause;
26 import org.turro.jpa.Dao;
27 import org.turro.plugin.contacts.IContact;
28 import org.turro.students.db.StudentsPU;
29 import org.turro.students.entities.Response;
30 
35 @UserActivity
37 
38  @Override
39  protected Dao createDao() {
40  return new StudentsPU();
41  }
42 
43  @Override
44  protected Set<ActivityType> getAllowed() {
45  return EnumSet.of(ActivityType.ACTIVITY_CHANGE);
46  }
47 
48  @Override
49  protected Set<Activity> getActivity(IContact contact, Date from, Set<ActivityType> types) {
50  Set<Activity> set = new HashSet<>();
51  if(types.contains(ActivityType.ACTIVITY_CHANGE)) {
52  set.addAll(getResponses(contact, from, types));
53  }
54  return set;
55  }
56 
57  private Set<Activity> getResponses(IContact contact, Date from, Set<ActivityType> types) {
58  WhereClause wc = new WhereClause();
59  wc.addClause("select distinct p from Response p");
60  wc.addClause("join p.studentIds sid");
61  wc.addClause("where 1=1");
62  if(from != null) {
63  wc.addClause("and p.creation >= :date");
64  wc.addNamedValue("date", from);
65  }
66  if(contact != null) {
67  wc.addClause("and sid = :student");
68  wc.addNamedValue("student", contact.getId());
69  }
70  wc.addClause("order by p.creation desc");
71  Set<Activity> set = new HashSet<>();
72  for(Response p : getDao().getResultList(Response.class, wc, 100)) {
73  Activity activity = convertToActivity(types, p, contact.getId());
74  if(activity != null) set.add(activity);
75  }
76  return set;
77  }
78 
79  private Activity convertToActivity(Set<ActivityType> types, Response p, String idContact) {
80  return new Activity(p.getCreation(), ActivityType.ACTIVITY_CHANGE, idContact,
81  StudentsPU.getObjectPath(p.getChallenge()), p);
82  }
83 
84 }
void addNamedValue(String name, Object value)
Set< Activity > getActivity(IContact contact, Date from, Set< ActivityType > types)