BrightSide Workbench Full Report + Source Code
EditResponseControl.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.Date;
22 import org.turro.action.Contacts;
23 import org.turro.auth.Authentication;
24 import org.turro.collections.KeyValueMap;
25 import org.turro.elephant.TemplateControl;
26 import org.turro.elephant.context.Application;
27 import org.turro.entities.EntityCombobox;
28 import org.turro.jpa.Dao;
29 import org.turro.marker.MarkerHelper;
30 import org.turro.plugin.contacts.IContact;
31 import org.turro.students.db.StudentsPU;
32 import org.turro.students.entities.Challenge;
33 import org.turro.students.entities.Response;
34 import org.turro.students.mail.ChallengeChangedNotification;
35 import org.turro.students.mail.NewResponseNotification;
36 import org.turro.zkoss.text.WikiEditor;
37 import org.zkoss.zk.ui.select.annotation.Listen;
38 import org.zkoss.zk.ui.select.annotation.Wire;
39 import org.zkoss.zul.Div;
40 import org.zkoss.zul.Hlayout;
41 import org.zkoss.zul.Html;
42 
47 public class EditResponseControl extends TemplateControl {
48 
49  private Response response;
50  private String contextPath;
51 
52  @Wire("#studentbox") private Div studentbox;
53  @Wire("#text") private WikiEditor text;
54  @Wire("#students") private Hlayout students;
55  @Wire("#newStudent") private EntityCombobox studentCombobox;
56 
57  @Listen("onChange=#text")
58  public void onText() {
59  response.setWikiText(text.getValue());
60  }
61 
62  @Listen("onClick=#addStudent")
63  public void onAddStudent() {
64  response.addStudent(Contacts.getContact(studentCombobox.getObjectValue()));
65  updateStudents();
66  }
67 
68  @Listen("onClick=#save")
69  public void onSave() {
70  if(!response.isEmpty()) {
71  boolean isNew = response.getId() == null;
72  response = getDao().saveObject(response);
74  if(response.getId() != null && response.getId() > 0) {
75  Application.getApplication().sendRedirect(contextPath + "?" +
77  } else {
78  Application.getApplication().sendRedirect(contextPath);
79  }
80  if(isNew) {
82  } else {
84  }
85  }
86  }
87 
88  @Listen("onClick=#cancel")
89  public void onCancel() {
90  if(response.getId() != null && response.getId() > 0) {
91  Application.getApplication().sendRedirect(contextPath + "?" +
93  } else {
94  Application.getApplication().sendRedirect(contextPath);
95  }
96  }
97 
98  public String getContextPath() {
99  return contextPath;
100  }
101 
102  public void setContextPath(String contextPath) {
103  this.contextPath = contextPath;
104  }
105 
106  public void setResponse(Response response) {
107  this.response = response;
108  }
109 
111  checkResponse();
112  return response;
113  }
114 
115  @Override
116  protected void doFinally() {
117  super.doFinally();
118  checkResponse();
119  initComponents();
120  }
121 
122  private void checkResponse() {
123  KeyValueMap kvm = MarkerHelper.getObfuscatedParameters();
124  if(response == null) {
125  if(kvm != null && !kvm.isEmpty() && kvm.containsKey("item")) {
126  Long responseId = kvm.get(Long.class, "item");
127  if(responseId > 0) {
128  response = getDao().find(Response.class, responseId);
129  }
130  }
131  }
132  if(response == null) {
133  if(kvm != null && !kvm.isEmpty() && kvm.containsKey("challenge")) {
134  Challenge challenge = null;
135  Long challengeId = kvm.get(Long.class, "challenge");
136  if(challengeId > 0) {
137  challenge = getDao().find(Challenge.class, challengeId);
138  }
139  IContact contact = Authentication.getIContact();
140  if(challenge != null && (contact.isStudent() || contact.isAdmin())) {
141  response = new Response();
142  response.setCreation(new Date());
143  response.setChallenge(challenge);
144  response.addStudent(contact);
145  } else {
146  Application.getApplication().sendRedirect(contextPath);
147  }
148  } else {
149  Application.getApplication().sendRedirect(contextPath);
150  }
151  }
152  }
153 
154  private void initComponents() {
155  if(response.getId() != null && response.getId() > 0) {
156  IContact contact = Authentication.getIContact();
157  if(contact.isAdmin() || contact.isStudent()) {
158  text.setValue(response.getWikiText());
159  text.setFileFolder("/_internal/files/response/" + response.getId());
160  text.setImageFolder("/_internal/files/response/" + response.getId());
161  updateStudents();
162  } else {
163  Application.getApplication().sendRedirect(contextPath);
164  }
165  } else {
166  text.setFileFolder("/_internal/files/response/empty");
167  text.setImageFolder("/_internal/files/response/empty");
168  text.setReadOnlyRepository(true);
169  studentbox.setVisible(false);
170  }
171  }
172 
173  private void updateStudents() {
174  students.getChildren().clear();
175  for(IContact student : response.getStudentList()) {
176  students.appendChild(new Html(
177  "<div class='ui label'>" +
178  student.getName() +
179  "</div>"
180  ));
181  }
182  }
183 
184  /* Dao */
185 
186  private Dao _dao;
187 
188  private Dao getDao() {
189  if(_dao == null) {
190  _dao = new StudentsPU();
191  }
192  return _dao;
193  }
194 
195 }
static IContact getContact(Object object)
Definition: Contacts.java:109
abstract void sendRedirect(String uri)
static KeyValueMap getObfuscatedParameters()
static String setObfuscatedRightNowPars(String parameters)
void setCreation(Date creation)
Definition: Response.java:99
void setWikiText(String wikiText)
Definition: Response.java:115
void setChallenge(Challenge challenge)
Definition: Response.java:83
void addStudent(IContact contact)
Definition: Response.java:142
void setImageFolder(String folder)
void setValue(String value)
Definition: WikiEditor.java:98
void setFileFolder(String folder)
void setReadOnlyRepository(boolean readOnlyRepository)