BrightSide Workbench Full Report + Source Code
EditPracticalWorkControl.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.jobboard;
20 
21 import java.util.HashMap;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24 import org.turro.string.Strings;
25 import org.turro.url.UrlCompose;
26 import org.apache.commons.mail.EmailException;
27 import org.turro.action.IAgreements;
28 import org.turro.action.MailSenders;
29 import org.turro.action.Plugins;
30 import org.turro.auth.Authentication;
31 import org.turro.collections.KeyValueMap;
32 import org.turro.contacts.BusinessCombobox;
33 import org.turro.contacts.CoworkerCombobox;
34 import org.turro.elephant.TemplateControl;
35 import org.turro.elephant.context.Application;
36 import org.turro.elephant.context.ElephantContext;
37 import org.turro.elephant.context.IConstructor;
38 import org.turro.elephant.util.Messages;
39 import org.turro.entities.Entities;
40 import org.turro.i18n.I_;
41 import org.turro.jpa.Dao;
42 import org.turro.marker.MarkerHelper;
43 import org.turro.plugin.contacts.IContact;
44 import org.turro.students.db.StudentsPU;
45 import org.turro.students.entities.PracticalWork;
46 import org.turro.students.entities.PracticalWorkRole;
47 import org.turro.zkoss.input.ExpressionInput;
48 import org.turro.zkoss.text.WikiEditor;
49 import org.zkoss.zk.ui.select.annotation.Listen;
50 import org.zkoss.zk.ui.select.annotation.Wire;
51 import org.zkoss.zk.ui.util.Clients;
52 import org.zkoss.zul.Button;
53 import org.zkoss.zul.Checkbox;
54 import org.zkoss.zul.Datebox;
55 import org.zkoss.zul.Textbox;
56 
62 
63  private PracticalWork practicalWork;
64  private String contextPath;
65 
66  @Wire("#title") private Textbox title;
67  @Wire("#business") private BusinessCombobox business;
68  @Wire("#responsible") private CoworkerCombobox responsible;
69  @Wire("#role") private PracticalWorkRoleListbox role;
70  @Wire("#type") private PracticalWorkTypeCombobox type;
71  @Wire("#zipcode") private Textbox zipCode;
72  @Wire("#relurl") private Textbox relatedUrl;
73  @Wire("#start") private Datebox start;
74  @Wire("#end") private Datebox end;
75  @Wire("#hours") private ExpressionInput hours;
76  @Wire("#price") private ExpressionInput price;
77  @Wire("#immediate") private Checkbox immediate;
78  @Wire("#text") private WikiEditor text;
79  @Wire("#publish") private Button publish;
80  @Wire("#delete") private Button delete;
81 
82  @Listen("onChange=#title")
83  public void onTitle() {
84  practicalWork.setTitle(title.getValue());
85  }
86 
87  @Listen("onSelect=#business")
88  public void onBusiness() {
89  practicalWork.setIContact(business.getObjectValue());
90  responsible.setContact(business.getObjectValue());
91  responsible.setObjectValue(null);
92  practicalWork.setRoleByContact();
93  role.setObjectValue(practicalWork.getRole());
94  immediate.setDisabled(PracticalWorkRole.PRACTICALWORK_DEMAND.equals(practicalWork.getRole()));
95  if(immediate.isDisabled()) immediate.setChecked(false);
96  }
97 
98  @Listen("onSelect=#responsible")
99  public void onResponsible() {
100  practicalWork.setIResponsible(responsible.getObjectValue());
101  }
102 
103  @Listen("onChange=#type")
104  public void onType() {
105  practicalWork.setType(type.getObjectValue());
106  }
107 
108  @Listen("onChange=#zipcode")
109  public void onZipCode() {
110  practicalWork.setZipCode(zipCode.getValue());
111  }
112 
113  @Listen("onChange=#relurl")
114  public void onRelatedUrl() {
115  String relUrl = relatedUrl.getValue();
116  if(!Strings.isBlank(relUrl)) {
117  if(relUrl.contains("@")) {
118  practicalWork.setRelatedURL(UrlCompose.fillMailGaps(relUrl));
119  } else {
120  practicalWork.setRelatedURL(UrlCompose.fillWebGaps(relUrl));
121  }
122  } else {
123  practicalWork.setRelatedURL(null);
124  }
125  relatedUrl.setValue(practicalWork.getRelatedURL());
126  }
127 
128  @Listen("onChange=#start")
129  public void onStart() {
130  practicalWork.setStartDate(start.getValue());
131  }
132 
133  @Listen("onChange=#end")
134  public void onEnd() {
135  practicalWork.setEndDate(end.getValue());
136  }
137 
138  @Listen("onChange=#hours")
139  public void onTax() {
140  practicalWork.setHours(hours.getDoubleValue());
141  hours.setValue(practicalWork.getHours());
142  }
143 
144  @Listen("onChange=#price")
145  public void onPrice() {
146  practicalWork.setPrice(price.getDoubleValue());
147  price.setValue(practicalWork.getPrice());
148  }
149 
150  @Listen("onCheck=#immediate")
151  public void onImmediate() {
152  practicalWork.setImmediate(immediate.isChecked());
153  }
154 
155  @Listen("onChange=#text")
156  public void onText() {
157  practicalWork.setWiki(text.getValue());
158  }
159 
160  @Listen("onClick=#sendTest")
161  public void onSendTest() {
162  if(!practicalWork.isEmpty()) {
163  HashMap args = new HashMap();
164  args.put("practicalWork", practicalWork);
165  args.put("wrapper", new PracticalWorkWrapper(practicalWork));
166  args.put("notifier", this);
167  if(practicalWork.getId() != null) {
168  args.put("link", Entities.getController(practicalWork).getEntityUrl());
169  }
170  try {
173  .addByEntity(practicalWork, null)
174  .setRoot("/practicalworks")
175  .putAll(args)
176  .sendTemplate("practicalwork-test", composeSubject(practicalWork));
177  Clients.showNotification(I_.get("Test sent"));
178  } catch (EmailException ex) {
179  Logger.getLogger(EditPracticalWorkControl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
180  }
181  } else {
182  Clients.showNotification(I_.get("Fill required fields"));
183  }
184  }
185 
186  @Listen("onClick=#save")
187  public void onSave() {
188  if(!practicalWork.isEmpty()) {
189  PracticalWorkWrapper wrapper = new PracticalWorkWrapper(practicalWork);
190  practicalWork = wrapper.save();
191  Application.getApplication().sendRedirect(contextPath + "?" +
192  MarkerHelper.setObfuscatedRightNowPars("item=" + practicalWork.getId()));
193  } else {
194  Clients.showNotification(I_.get("Fill required fields"));
195  }
196  }
197 
198  @Listen("onClick=#cancel")
199  public void onCancel() {
200  if(practicalWork.getId() != null) {
201  Application.getApplication().sendRedirect(contextPath + "?" +
202  MarkerHelper.setObfuscatedRightNowPars("item=" + practicalWork.getId()));
203  } else {
204  Application.getApplication().sendRedirect(contextPath);
205  }
206  }
207 
208  @Listen("onClick=#publish")
209  public void onPublish() {
210  if(!practicalWork.isEmpty()) {
211  practicalWork.setPublished(!practicalWork.isPublished());
212  onSave();
213  if(practicalWork.isPublished()) {
214  HashMap args = new HashMap();
215  args.put("practicalWork", practicalWork);
216  args.put("wrapper", new PracticalWorkWrapper(practicalWork));
217  args.put("notifier", this);
218  if(practicalWork.getId() > 0) {
219  args.put("link", Entities.getController(practicalWork).getEntityUrl());
220  }
221  try {
224  .addByEntity(practicalWork, null)
225  .setRoot("/practicalworks")
226  .putAll(args)
227  .sendTemplate("practicalwork-published", composeSubject(practicalWork));
228  } catch (EmailException ex) {
229  Logger.getLogger(EditPracticalWorkControl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
230  }
231  Application.getApplication().sendRedirect(contextPath + "?" +
232  MarkerHelper.setObfuscatedRightNowPars("item=" + practicalWork.getId()));
234  }
235  }
236  }
237 
238  @Listen("onClick=#delete")
239  public void onDelete() {
240  if(practicalWork.getId() != null) {
241  Messages.confirmDeletion().add(practicalWork.getTitle()).show(() -> {
242  PracticalWorkWrapper wrapper = new PracticalWorkWrapper(practicalWork);
243  wrapper.delete();
244  Application.getApplication().sendRedirect(contextPath);
245  });
246  }
247  }
248 
249  public String getContextPath() {
250  return contextPath;
251  }
252 
253  public void setContextPath(String contextPath) {
254  this.contextPath = contextPath;
255  }
256 
257  public void setResponse(PracticalWork practicalWork) {
258  this.practicalWork = practicalWork;
259  }
260 
262  checkPracticalWork();
263  return practicalWork;
264  }
265 
266  @Override
267  protected void doFinally() {
268  super.doFinally();
269  IAgreements agreements = Plugins.loadImplementation(IAgreements.class, "agreements");
270  agreements.setContact(Authentication.getIContact());
271  if(!agreements.canAct("practicalwork-new")) {
272  Application.getApplication().sendRedirect(agreements.getPendingActURL("practicalwork-new"));
273  } else {
274  checkPracticalWork();
275  initComponents();
276  }
277  }
278 
279  /* Send test */
280 
281  public String createRef(IConstructor constructor, PracticalWork practicalWork, IContact contact) throws Exception {
282  return createRef(constructor, practicalWork.isPublished() ?
283  Entities.getController(practicalWork).getEntityUrl() :
284  Entities.getController(practicalWork).getMemberUrl());
285  }
286 
287  public String createRef(IConstructor constructor, String redir) throws Exception {
288  return "{liveref:" + redir + "}";
289  }
290 
291  private void checkPracticalWork() {
292  KeyValueMap kvm = MarkerHelper.getObfuscatedParameters();
293  if(practicalWork == null) {
294  if(kvm != null && !kvm.isEmpty() && kvm.containsKey("item")) {
295  Long pwId = kvm.get(Long.class, "item");
296  if(pwId != null) {
297  practicalWork = getDao().find(PracticalWork.class, pwId);
298  }
299  } else {
300  practicalWork = new PracticalWork();
301  practicalWork.setRole(PracticalWorkRole.PRACTICALWORK_SUPPLY);
302  }
303  }
304  if(practicalWork == null) {
305  Application.getApplication().sendRedirect(contextPath);
306  }
307  }
308 
309  private void initComponents() {
310  business.setAllowSelf(PracticalWorkContext.getAllowSingle());
311  responsible.setAllowSelf(PracticalWorkContext.getAllowSingle());
312  business.setContact(Authentication.getIContact());
313  responsible.setContact(null);
314  if(practicalWork.getId() != null) {
315  title.setValue(practicalWork.getTitle());
316  business.setObjectValue(practicalWork.getIContact());
317  responsible.setObjectValue(practicalWork.getIResponsible());
318  role.setObjectValue(practicalWork.getRole());
319  type.setObjectValue(practicalWork.getType());
320  relatedUrl.setValue(practicalWork.getRelatedURL());
321  start.setValue(practicalWork.getStartDate());
322  end.setValue(practicalWork.getEndDate());
323  hours.setValue(practicalWork.getHours());
324  price.setValue(practicalWork.getPrice());
325  text.setValue(practicalWork.getWiki());
326  text.setFileFolder("/_internal/files/practicalwork/" + practicalWork.getId());
327  text.setImageFolder("/_internal/files/practicalwork/" + practicalWork.getId());
328  } else {
329  text.setFileFolder("/_internal/files/practicalwork/empty");
330  text.setImageFolder("/_internal/files/practicalwork/empty");
331  text.setReadOnlyRepository(true);
332  }
333  boolean all = Application.getApplication().isInRole("contact:all");
334  if(practicalWork.isPublished()) {
335  publish.setLabel(I_.get("Unpublish"));
336  } else {
337  publish.setLabel(I_.get("Publish"));
338  }
339  delete.setDisabled(practicalWork.getId() == null || !(all ||
340  practicalWork.isInBusiness(business.getContact())));
341  }
342 
343  private String composeSubject(PracticalWork practicalWork) {
344  return "[" + I_.byKey(practicalWork.getRole().toString()) + "]" +
345  " " + I_.get(practicalWork.getType()) +
346  ": " + practicalWork.getTitle();
347  }
348 
349  /* Dao */
350 
351  private Dao _dao;
352 
353  private Dao getDao() {
354  if(_dao == null) {
355  _dao = new StudentsPU();
356  }
357  return _dao;
358  }
359 
360 }
static IMailSender getPool()
static< T > T loadImplementation(Class< T > jclass)
Definition: Plugins.java:57
void setAllowSelf(boolean allowSelf)
void setAllowSelf(boolean allowSelf)
abstract void sendRedirect(String uri)
static Messages confirmDeletion()
Definition: Messages.java:87
Messages add(String word)
Definition: Messages.java:50
static IElephantEntity getController(String path)
Definition: Entities.java:78
static String get(String msg)
Definition: I_.java:41
String createRef(IConstructor constructor, String redir)
String createRef(IConstructor constructor, PracticalWork practicalWork, IContact contact)
static KeyValueMap getObfuscatedParameters()
static String setObfuscatedRightNowPars(String parameters)
void setRole(PracticalWorkRole role)
void setIResponsible(IContact responsible)
void setImageFolder(String folder)
void setValue(String value)
Definition: WikiEditor.java:98
void setFileFolder(String folder)
void setReadOnlyRepository(boolean readOnlyRepository)
void setContact(IContact contact)
String getPendingActURL(String action)
boolean canAct(String action)