BrightSide Workbench Full Report + Source Code
StudentsCtrl.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.students.www;
20 
21 import java.util.List;
22 import javax.servlet.ServletContext;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25 import org.turro.action.Actions;
26 import org.turro.action.Contacts;
27 import org.turro.action.IEntityCtrl;
28 import org.turro.annotation.ElephantPlugin;
29 import org.turro.assistant.ParticipationInfo;
30 import org.turro.auth.Authentication;
31 import org.turro.collections.KeyValueMap;
32 import org.turro.elephant.context.ElephantContext;
33 import org.turro.elephant.context.IConstructor;
34 import org.turro.elephant.db.WhereClause;
35 import org.turro.elephant.direct.DirectContent;
36 import org.turro.elephant.direct.DirectContents;
37 import org.turro.elephant.direct.IDirectContent;
38 import org.turro.entities.Entities;
39 import org.turro.entities.EntityRole;
40 import org.turro.entities.IElephantEntity;
41 import org.turro.jpa.Dao;
42 import org.turro.marker.ElephantMarker;
43 import org.turro.participation.IEntityParticipation;
44 import org.turro.participation.ParticipationReason;
45 import org.turro.plugin.contacts.ContactList;
46 import org.turro.plugin.contacts.IContact;
47 import org.turro.students.db.StudentsPU;
48 import org.turro.students.entities.Challenge;
49 
54 @ElephantPlugin(label="students-ctrl")
55 @DirectContent(identifier="students-action")
56 public class StudentsCtrl implements IDirectContent, IEntityCtrl {
57 
58  private String entityPath, template = "related";
59  private IConstructor constructor;
60  private IContact challenger;
61  private List<IContact> students;
62  private List<Challenge> challenges;
63 
64  @Override
65  public void setConstructor(IConstructor constructor) {
66  this.constructor = constructor;
67  }
68 
69  @Override
70  public void setEntityPath(String entityPath) {
71  this.entityPath = entityPath;
72  }
73 
74  @Override
75  public void setTemplate(String template) {
76  this.template = template;
77  }
78 
79  @Override
80  public boolean hasContent() {
81  getStudents();
82  getChallenger();
83  getRelatedChallenges();
85  (challenger != null && challenger.isWebUser()) ||
86  !students.isEmpty() || !challenges.isEmpty();
87  }
88 
89  @Override
90  public void render(IConstructor constructor) {
91  getMarker().process("students", template);
92  }
93 
94  @Override
95  public String parse(IConstructor constructor) {
96  return getMarker().parse("students", template);
97  }
98 
99  public boolean isMine(Challenge challenge) {
100  IContact contact = Authentication.getIContact();
101  return challenge.getIdChallenger().equals(contact.getId());
102  }
103 
104  public Object getFollowCtrl() {
106  }
107 
108  public boolean allowsQuestion() {
109  IContact contact = Authentication.getIContact();
110  return contact.isWebUser() && contact.isInNetworking();
111  }
112 
113  private ElephantMarker getMarker() {
114  ElephantMarker marker = new ElephantMarker(constructor);
115  marker.put("actions", this);
116  marker.put("entityPath", entityPath);
117  marker.put("challenger", getChallenger());
118  marker.put("students", getStudents());
119  marker.put("challenges", getRelatedChallenges());
120  return marker;
121  }
122 
123  private List<IContact> getStudents() {
124  if(students == null) {
125  ContactList list = new ContactList();
126  for(IEntityParticipation participation : new ParticipationInfo(entityPath, ParticipationReason.REASON_FOLLOW).getParticipations()) {
127  list.add(participation.getContact());
128  }
129  students = list.getStudents();
130  }
131  return students;
132  }
133 
134  private List<Challenge> getRelatedChallenges() {
135  if(challenges == null) {
136  WhereClause wc = new WhereClause();
137  wc.addClause("select c from Challenge c");
138  wc.addClause("where c.entityPath = :entityPath");
139  wc.addNamedValue("entityPath", entityPath);
140  challenges = getDao().getResultList(wc);
141  }
142  return challenges;
143  }
144 
145  private IContact getChallenger() {
146  if(challenger == null) {
147  IElephantEntity iee = Entities.getController(entityPath);
148  IContact contact = Authentication.getIContact();
149  if(contact.isHHRR() || iee.hasRelatedRole(EntityRole.CHALLENGER, contact)) {
150  challenger = contact;
151  }
152  }
153  return challenger;
154  }
155 
156  /* IDirectContent */
157 
158  public static String createPOST(String values) {
159  return "$.post(\"" + ElephantContext.getRootWebPath() + DirectContents.DIRECT_CONTENT_PATH + getIdentifier() + "\"," +
160  "{ exrino : \"" + Actions.createRightNowParameter(values) + "\" })";
161  }
162 
163  public static String createURL(String values) {
164  String exrino = Actions.createRightNowAction(values);
166  DirectContents.DIRECT_CONTENT_PATH + getIdentifier() +
167  "?" + exrino;
168  }
169 
170  public static String getIdentifier() {
171  return StudentsCtrl.class.getAnnotation(DirectContent.class).identifier();
172  }
173 
174  @Override
175  public boolean itsMe(String id) {
176  return getIdentifier().equals(id);
177  }
178 
179  @Override
180  public boolean myTurn(HttpServletRequest request) {
181  return DirectContents.isYourTurn(request, getIdentifier());
182  }
183 
184  @Override
185  public void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response) {
186  IConstructor current = ElephantContext.getConstructor(request, response);
187  KeyValueMap map = Actions.getRightNowAction(current);
188  if(map != null) {
189  String type = map.get("type");
190  }
191  }
192 
193  /* Iterators helpers */
194 
195  public long countParticipations(String root, IContact contact, String reason) {
196  return ParticipationInfo.getParticipationCount(root, contact, ParticipationReason.valueOf(reason));
197  }
198 
199  public long countResponses(IContact contact) {
200  WhereClause wc = new WhereClause();
201  wc.addClause("select count(sp) from Response sp");
202  wc.addClause("where :student member of sp.studentIds");
203  wc.addNamedValue("student", contact.getId());
204  return (long) getDao().getSingleResultOrNull(wc);
205  }
206 
207  /* Dao */
208 
209  private Dao _dao;
210 
211  private Dao getDao() {
212  if(_dao == null) {
213  _dao = new StudentsPU();
214  }
215  return _dao;
216  }
217 
218 }
static String createRightNowParameter(String values)
Definition: Actions.java:333
static KeyValueMap getRightNowAction(IConstructor constructor)
Definition: Actions.java:341
static String createRightNowAction(String values)
Definition: Actions.java:312
static boolean isStudent(IContact contact)
Definition: Contacts.java:186
static long getParticipationCount(String root, IContact contact, ParticipationReason reason)
static IConstructor getConstructor(HttpServletRequest request, HttpServletResponse response)
void addNamedValue(String name, Object value)
static boolean isYourTurn(HttpServletRequest request, String path)
static IElephantEntity getController(String path)
Definition: Entities.java:78
Object put(Object key, Object value)
long countParticipations(String root, IContact contact, String reason)
boolean isMine(Challenge challenge)
String parse(IConstructor constructor)
static String createPOST(String values)
void setConstructor(IConstructor constructor)
void execute(ServletContext context, HttpServletRequest request, HttpServletResponse response)
void setEntityPath(String entityPath)
void render(IConstructor constructor)
void setTemplate(String template)
static String createURL(String values)
boolean myTurn(HttpServletRequest request)
long countResponses(IContact contact)
Object getParticipationControl(IConstructor constructor, ParticipationReason reason)