BrightSide Workbench Full Report + Source Code
SendableControl.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 
19 package org.turro.sendable;
20 
21 import java.util.Collection;
22 import org.turro.action.IElephantSendable;
23 import org.turro.auth.Authentication;
24 import org.turro.command.Command;
25 import org.turro.command.Context;
26 import org.turro.elephant.context.Application;
27 import org.turro.elephant.db.ElephantPU;
28 import org.turro.elephant.entities.db.Sendable;
29 import org.turro.entities.Entities;
30 import org.turro.plugin.contacts.ContactList;
31 import org.turro.plugin.contacts.IContact;
32 import org.zkoss.zk.ui.Executions;
33 import org.zkoss.zk.ui.IdSpace;
34 import org.zkoss.zk.ui.ext.AfterCompose;
35 import org.zkoss.zk.ui.select.Selectors;
36 import org.zkoss.zk.ui.select.annotation.Listen;
37 import org.zkoss.zk.ui.select.annotation.Wire;
38 import org.zkoss.zul.Checkbox;
39 import org.zkoss.zul.Datebox;
40 import org.zkoss.zul.East;
41 import org.zkoss.zul.Panel;
42 
47 public class SendableControl extends Panel implements IdSpace, AfterCompose {
48 
49  private boolean withAssistants = true;
50  private Sendable sendable;
51  private String entityPath;
52  private Object entity;
53 
54  @Wire("#schedule")
55  private Datebox schedule;
56 
57  @Wire("#sent")
58  private Checkbox sent;
59 
60  @Wire("#assistPanel")
61  private East assistPanel;
62 
63  @Wire("#assistants")
64  private AssistantsGrid assistants;
65 
66  @Listen("onChange = #schedule")
67  public void onSchedule() {
68  sendable.setSchedule(schedule.getValue());
69  }
70 
71  @Listen("onCheck = #sent")
72  public void onSent() {
73  sendable.setSent(sent.isChecked());
74  }
75 
76  @Listen("onClick = #save")
77  public void onSave() {
78  if(!sendable.isEmpty()) {
79  new ElephantPU().saveObject(sendable);
81  }
82  }
83 
84  @Listen("onClick = #delete")
85  public void onDelete() {
86  Sendables.delete(entityPath);
88  }
89 
90  @Listen("onClick = #sendTest")
91  public void onSendTest() {
92  ContactList contacts = new ContactList();
93  contacts.add(Authentication.getIContact());
94  MailRecipients.selectRecipients(contacts, new Command() {
95  @Override
96  public Object execute(Context context) {
97  MailRecipients mr = (MailRecipients) context.get("component");
98  Collection<IContact> emails = mr.getRecipients();
99  if(emails != null) {
100  IElephantSendable eSend = Entities.getController(entityPath).getSendable();
101  eSend.send(Application.getApplication().getConstructor(), emails);
102  }
103  return null;
104  }
105  });
106  }
107 
108  public SendableControl() {
109  Executions.createComponents("/WEB-INF/_zul/bs/comps/sendable/sendableControl.zul", this, null);
110  Selectors.wireComponents(this, this, false);
111  Selectors.wireEventListeners(this, this);
112  }
113 
114  public void setWithAssistants(boolean withAssistants) {
115  this.withAssistants = withAssistants;
116  }
117 
118  public void setEntityPath(String entityPath) {
119  this.entityPath = entityPath;
120  checkSendable();
121  }
122 
123  public void setEntity(Object entity) {
124  this.entity = entity;
125  this.entityPath = Entities.getController(entity).getPath();
126  checkSendable();
127  }
128 
129  @Override
130  public void afterCompose() {
131  initComponents();
132  }
133 
134  private void initComponents() {
135  assistPanel.setVisible(withAssistants);
136  schedule.setValue(sendable.getSchedule());
137  sent.setChecked(sendable.isSent());
138  updateButtons();
140  }
141 
142  private void updateButtons() {
143  }
144 
145  private void checkSendable() {
146  sendable = Sendables.getSendable(entityPath);
147  if(sendable == null) {
148  sendable = new Sendable();
149  sendable.setEntityPath(entityPath);
150  }
151  assistants.setSendable(sendable);
152  }
153 
154 }
void setEntityPath(String entityPath)
Definition: Sendable.java:56
static IElephantEntity getController(String path)
Definition: Entities.java:78
void setSendable(Sendable sendable)
static void selectRecipients(Collection< IContact > recipients, Command command)
void setWithAssistants(boolean withAssistants)
void setEntityPath(String entityPath)
static void delete(String entityPath)
Definition: Sendables.java:99
void send(IConstructor constructor)
IElephantSendable getSendable()