BrightSide Workbench Full Report + Source Code
AgreementComposer.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 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 package org.turro.agreements;
19 
20 import java.util.List;
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import org.apache.commons.mail.EmailException;
24 import org.turro.action.MailSenders;
25 import org.turro.command.Command;
26 import org.turro.elephant.context.Application;
27 import org.turro.elephant.context.ElephantContext;
28 import org.turro.elephant.context.IConstructor;
29 import org.turro.elephant.db.ElephantPU;
30 import org.turro.elephant.entities.db.Agreement;
31 import org.turro.elephant.entities.db.AgreementSignature;
32 import org.turro.elephant.util.Messages;
33 import org.turro.i18n.I_;
34 import org.turro.jpa.Dao;
35 import org.turro.plugin.contacts.IContact;
36 import org.turro.zkoss.dialog.SelectionDialog;
37 import org.turro.zkoss.text.WikiEditor;
38 import org.zkoss.zk.ui.Component;
39 import org.zkoss.zk.ui.select.SelectorComposer;
40 import org.zkoss.zk.ui.select.annotation.Listen;
41 import org.zkoss.zk.ui.select.annotation.Wire;
42 import org.zkoss.zul.Checkbox;
43 import org.zkoss.zul.Label;
44 import org.zkoss.zul.Textbox;
45 
50 public class AgreementComposer extends SelectorComposer<Component> {
51 
52  private Agreement agreement;
53 
54  @Wire("#agreeCombo")
55  private AgreementCombobox agreeCombo;
56 
57  @Wire("#agreed")
58  private Label agreed;
59 
60  @Wire("#declined")
61  private Label declined;
62 
63  @Wire("#notsigned")
64  private Label notsigned;
65 
66  @Wire("#title")
67  private Textbox title;
68 
69  @Wire("#toNotify")
70  private Checkbox toNotify;
71 
72  @Wire("#toAccess")
73  private Checkbox toAccess;
74 
75  @Wire("#toAction")
76  private Checkbox toAction;
77 
78  @Wire("#peremptory")
79  private Checkbox peremptory;
80 
81  @Wire("#action")
82  private Textbox action;
83 
84  @Wire("#agreeWiki")
85  private WikiEditor agreeWiki;
86 
87  @Listen("onClick = #signatures")
88  public final void onSignatures() {
89  if (agreement != null) {
90  SelectionDialog.showComponent(getPage(), I_.get("Signatures"),
91  new SignaturesListbox(agreement), "900px", "500px", (Command) null);
92  }
93  }
94 
95  @Listen("onClick = #reminders")
96  public final void onReminders() {
97  Messages.confirmProcess().show(() -> {
98  try {
99  sendNotSigned();
100  } catch (Exception ex) {
101  Logger.getLogger(AgreementComposer.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
102  }
103  });
104  }
105 
106  @Listen("onClick = #save")
107  public final void onSave() {
108  getDao().saveObject(agreement);
109  }
110 
111  @Listen("onClick = #delete")
112  public final void onDelete() {
113  if (AgreementsUtil.deleteAgreement(agreement)) {
114  agreement = null;
115  updateControls();
116  }
117  }
118 
119  @Listen("onChange = #title")
120  public final void onChange() {
121  checkAgreement().setTitle(title.getValue());
122  updateControls();
123  }
124 
125  @Listen("onChange = #action")
126  public final void onAction() {
127  checkAgreement().setAction(action.getValue());
128  updateControls();
129  }
130 
131  @Listen("onChange = #agreeWiki")
132  public final void onWiki() {
133  checkAgreement().setWiki(agreeWiki.getValue());
134  checkAgreement().setText(agreeWiki.getHtml());
135  updateControls();
136  }
137 
138  @Listen("onSelect = #agreeCombo")
139  public final void onSelect() {
140  agreement = agreeCombo.getObjectValue();
141  if (agreement != null) {
142  List<AgreementSignature> signatures = agreement.getSignatures();
143  agreed.setValue("" + signatures.stream().filter((as) -> (as.isSigned() && as.isAgreed())).count());
144  declined.setValue("" + signatures.stream().filter((as) -> (as.isSigned() && !as.isAgreed())).count());
145  notsigned.setValue("" + signatures.stream().filter((as) -> (!as.isSigned())).count());
146  } else {
147  agreed.setValue("");
148  declined.setValue("");
149  notsigned.setValue("");
150  }
151  updateControls();
152  }
153 
154  @Listen("onCheck = #toNotify; onCheck = #toAccess; onCheck = #toAction; onCheck = #peremptory")
155  public final void onCheck() {
156  checkAgreement().setRequiredToNotify(toNotify.isChecked());
157  checkAgreement().setRequiredToAccess(toAccess.isChecked());
158  checkAgreement().setRequiredToAction(toAction.isChecked());
159  checkAgreement().setPeremptory(peremptory.isChecked());
160  updateControls();
161  }
162 
163  private void updateControls() {
164  if (agreement != null) {
165  title.setReadonly(false);
166  title.setValue(agreement.getTitle());
167  toNotify.setDisabled(false);
168  toNotify.setChecked(agreement.isRequiredToNotify());
169  toAccess.setDisabled(false);
170  toAccess.setChecked(agreement.isRequiredToAccess());
171  toAction.setDisabled(false);
172  toAction.setChecked(agreement.isRequiredToAction());
173  peremptory.setDisabled(false);
174  peremptory.setChecked(agreement.isPeremptory());
175  action.setReadonly(!agreement.isRequiredToAction());
176  action.setValue(agreement.getAction());
177  agreeWiki.setReadonly(false);
178  agreeWiki.setValue(agreement.getWiki());
179  } else {
180  title.setReadonly(true);
181  title.setValue(null);
182  toNotify.setDisabled(true);
183  toNotify.setChecked(false);
184  toAccess.setDisabled(true);
185  toAccess.setChecked(false);
186  toAction.setDisabled(true);
187  toAction.setChecked(false);
188  peremptory.setDisabled(true);
189  peremptory.setChecked(false);
190  action.setReadonly(true);
191  action.setValue(null);
192  agreeWiki.setReadonly(true);
193  agreeWiki.setValue(null);
194  }
195  }
196 
197  private Agreement checkAgreement() {
198  if (agreement == null) {
199  agreement = new Agreement();
200  agreement.setAppliesTo("*");
201  }
202  return agreement;
203  }
204 
205  private void sendNotSigned() throws EmailException, Exception {
206  IConstructor constructor = Application.getApplication().getConstructor();
207  for (AgreementSignature as : agreement.getSignatures()) {
208  if (!as.isSigned()) {
209  IContact contact = as.getContact();
210  if (contact.isWebUser()) {
211  MailSenders.getPool()
212  .setRoot("/agreements")
213  .addContact(contact)
214  .put("signature", as)
215  .put("acceptUrl", AgreementAction.createURL(constructor, as, contact, true))
216  .put("declineUrl", AgreementAction.createURL(constructor, as, contact, false))
217  .sendTemplate("requiredSignature", as.getAgreement().getTitle() + " : " + ElephantContext.getSiteName());
218  }
219  }
220  }
221  }
222 
223  /* Dao */
224  private Dao _dao;
225 
226  private Dao getDao() {
227  if (_dao == null) {
228  _dao = new ElephantPU();
229  }
230  return _dao;
231  }
232 
233 }
static boolean deleteAgreement(Agreement agreement)
void setRequiredToAction(boolean requiredToAction)
Definition: Agreement.java:124
void setAppliesTo(String appliesTo)
Definition: Agreement.java:92
void setRequiredToNotify(boolean requiredToNotify)
Definition: Agreement.java:116
void setRequiredToAccess(boolean requiredToAccess)
Definition: Agreement.java:108
List< AgreementSignature > getSignatures()
Definition: Agreement.java:152
void setPeremptory(boolean peremptory)
Definition: Agreement.java:132
static Messages confirmProcess()
Definition: Messages.java:95
static String get(String msg)
Definition: I_.java:41
static void showComponent(Page page, String title, Component component, String width, String height, final Command command)
void setReadonly(boolean value)
void setValue(String value)
Definition: WikiEditor.java:98