BrightSide Workbench Full Report + Source Code
EditChallengeControl.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.string.Strings;
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.elephant.context.IConstructor;
28 import org.turro.entities.Entities;
29 import org.turro.entities.EntityCombobox;
30 import org.turro.entities.EntityRole;
31 import org.turro.entities.IElephantEntity;
32 import org.turro.jpa.Dao;
33 import org.turro.marker.MarkerHelper;
34 import org.turro.plugin.contacts.IContact;
35 import org.turro.students.ChallengeContext;
36 import org.turro.students.db.StudentsPU;
37 import org.turro.students.entities.Challenge;
38 import org.turro.students.mail.ChallengeChangedNotification;
39 import org.turro.students.mail.NewChallengeNotification;
40 import org.turro.students.wrappers.ChallengeWrapper;
41 import org.turro.zkoss.text.WikiEditor;
42 import org.zkoss.zk.ui.select.annotation.Listen;
43 import org.zkoss.zk.ui.select.annotation.Wire;
44 import org.zkoss.zul.Datebox;
45 import org.zkoss.zul.Textbox;
46 
51 public class EditChallengeControl extends TemplateControl {
52 
53  private Challenge challenge;
54  private String contextPath;
55 
56  @Wire("#entity") private EntityCombobox entity;
57  @Wire("#question") private Textbox question;
58  @Wire("#deadline") private Datebox deadline;
59  @Wire("#text") private WikiEditor text;
60 
61  @Listen("onChange=#question")
62  public void onQuestion() {
63  challenge.setQuestion(question.getValue());
64  }
65 
66  @Listen("onChange=#text")
67  public void onText() {
68  challenge.setWikiText(text.getValue());
69  }
70 
71  @Listen("onChange=#deadline")
72  public void onDeadline() {
73  challenge.setDeadline(deadline.getValue());
74  }
75 
76  @Listen("onClick=#save")
77  public void onSave() {
78  ChallengeWrapper dw = new ChallengeWrapper(challenge);
79  challenge.setEntityPath(entity.getEntityPath());
80  if(!challenge.isEmpty()) {
81  boolean isNew = challenge.getId() == null;
82  challenge = dw.save();
83  Application.getApplication().sendRedirect(contextPath + "?" +
84  MarkerHelper.setObfuscatedRightNowPars("item=" + challenge.getId()));
85  if(isNew) {
87  } else {
89  }
90  }
91  }
92 
93  @Listen("onClick=#cancel")
94  public void onCancel() {
95  if(challenge.getId() != null && challenge.getId() > 0) {
96  Application.getApplication().sendRedirect(contextPath + "?" +
97  MarkerHelper.setObfuscatedRightNowPars("item=" + challenge.getId()));
98  } else {
99  Application.getApplication().sendRedirect(contextPath);
100  }
101  }
102 
103  public String getContextPath() {
104  return contextPath;
105  }
106 
107  public void setContextPath(String contextPath) {
108  this.contextPath = contextPath;
109  }
110 
111  public void setChallenge(Challenge challenge) {
112  this.challenge = challenge;
113  }
114 
116  checkChallenge();
117  return challenge;
118  }
119 
120  @Override
121  protected void doFinally() {
122  super.doFinally();
123  checkChallenge();
124  initComponents();
125  }
126 
127  private void checkChallenge() {
128  KeyValueMap kvm = MarkerHelper.getObfuscatedParameters();
129  if(challenge == null) {
130  if(kvm != null && !kvm.isEmpty() && kvm.containsKey("item")) {
131  Long challengeId = kvm.get(Long.class, "item");
132  if(challengeId > 0) {
133  challenge = getDao().find(Challenge.class, challengeId);
134  }
135  }
136  }
137  if(challenge == null) {
138  if(kvm != null && !kvm.isEmpty() && kvm.containsKey("entityPath")) {
139  IContact contact = Authentication.getIContact();
140  challenge = new Challenge();
141  challenge.setContact(contact);
142  challenge.setCreation(new Date());
143  challenge.setEntityPath(kvm.get("entityPath"));
144  } else {
145  //Application.getApplication().sendRedirect(contextPath);
146  IContact contact = Authentication.getIContact();
147  challenge = new Challenge();
148  challenge.setContact(contact);
149  challenge.setCreation(new Date());
150  }
151  }
152  }
153 
154  private void initComponents() {
155  KeyValueMap kvm = new KeyValueMap();
156  IConstructor constructor = Application.getApplication().getConstructor();
157  entity.setRoot(ChallengeContext.getAllowedRoots(constructor));
158  String dossierCategories = ChallengeContext.getDossierCategories(constructor);
159  if(!Strings.isBlank(dossierCategories)) {
160  kvm.put("dossier-categories", dossierCategories);
161  }
162  entity.setParameters(kvm);
163  entity.setEntityPath(challenge.getEntityPath());
164  if(challenge.getId() != null && challenge.getId() > 0) {
165  IContact contact = Authentication.getIContact();
166  IElephantEntity iee = Entities.getController(challenge.getEntityPath());
167  if(contact.isAdmin() || iee.hasRelatedRole(EntityRole.CHALLENGER, contact)) {
168  question.setValue(challenge.getQuestion());
169  deadline.setValue(challenge.getDeadline());
170  text.setValue(challenge.getWikiText());
171  text.setFileFolder("/_internal/files/challenge/" + challenge.getId());
172  text.setImageFolder("/_internal/files/challenge/" + challenge.getId());
173  } else {
174  Application.getApplication().sendRedirect(contextPath);
175  }
176  } else {
177  text.setFileFolder("/_internal/files/challenge/empty");
178  text.setImageFolder("/_internal/files/challenge/empty");
179  text.setReadOnlyRepository(true);
180  }
181  }
182 
183  /* Dao */
184 
185  private Dao _dao;
186 
187  private Dao getDao() {
188  if(_dao == null) {
189  _dao = new StudentsPU();
190  }
191  return _dao;
192  }
193 
194 }
abstract void sendRedirect(String uri)
void setParameters(KeyValueMap kvm)
void setEntityPath(String entityPath)
static KeyValueMap getObfuscatedParameters()
static String setObfuscatedRightNowPars(String parameters)
void setContact(IContact contact)
Definition: Challenge.java:206
void setQuestion(String question)
Definition: Challenge.java:118
void setWikiText(String wikiText)
Definition: Challenge.java:150
void setEntityPath(String entityPath)
Definition: Challenge.java:94
void setImageFolder(String folder)
void setValue(String value)
Definition: WikiEditor.java:98
void setFileFolder(String folder)
void setReadOnlyRepository(boolean readOnlyRepository)