BrightSide Workbench Full Report + Source Code
CategorySubscribersGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2017 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.queue.zul;
20 
21 import java.util.Collection;
22 import java.util.List;
23 import org.turro.string.Strings;
24 import org.turro.action.Contacts;
25 import org.turro.action.queue.ConstraintKeys;
26 import org.turro.action.queue.NotificationCategory;
27 import org.turro.action.queue.QueuePeriod;
28 import org.turro.assistant.Assistant;
29 import org.turro.assistant.AssistantConstants;
30 import org.turro.assistant.AssistantDialog;
31 import org.turro.assistant.AssistantSet;
32 import org.turro.assistant.Assistants;
33 import org.turro.elephant.db.ElephantPU;
34 import org.turro.elephant.entities.db.QueueOptions;
35 import org.turro.elephant.util.Messages;
36 import org.turro.entities.Entities;
37 import org.turro.i18n.I_;
38 import org.turro.jpa.grid.RendererOnDemand;
39 import org.turro.mail.queue.QueueManager;
40 import org.turro.plugin.contacts.IContact;
41 import org.turro.zkoss.grid.PagingGrid;
42 import org.turro.zul.frame.Framework;
43 import org.zkoss.zk.ui.Component;
44 import org.zkoss.zk.ui.event.Event;
45 import org.zkoss.zk.ui.event.EventListener;
46 import org.zkoss.zk.ui.event.Events;
47 import org.zkoss.zul.Column;
48 import org.zkoss.zul.Columns;
49 import org.zkoss.zul.ListModelList;
50 import org.zkoss.zul.Row;
51 import org.zkoss.zul.Toolbarbutton;
52 
57 public class CategorySubscribersGrid extends PagingGrid implements EventListener<Event> {
58 
59  private NotificationCategory notificationCategory;
60 
61  public void setNotificationCategory(NotificationCategory notificationCategory) {
62  this.notificationCategory = notificationCategory;
63  addColumns();
64  addRows();
65  }
66 
67  public void addContact(IContact contact) {
68  if(notificationCategory != null && notificationCategory.isValid(contact) && notificationCategory.strongBond(ConstraintKeys.from(contact))) {
69  notificationCategory.getNotification().subscribeIfNot(
70  notificationCategory.getIdCategory(), contact, notificationCategory.getDefaultPeriod());
71  addRows();
72  }
73  }
74 
75  public void pasteContacts() {
76  List<Object> contacts = (List<Object>) Framework.getClipboard().get("bbf_clip_contact");
77  if(contacts != null && notificationCategory != null) {
78  for(Object c : contacts) {
79  IContact contact = Contacts.getContact(c);
80  if(notificationCategory != null && notificationCategory.isValid(contact) && notificationCategory.strongBond(ConstraintKeys.from(contact))) {
81  notificationCategory.getNotification().subscribeIfNot(
82  notificationCategory.getIdCategory(), contact, notificationCategory.getDefaultPeriod());
83  }
84  }
85  Framework.getClipboard().remove("bbf_clip_contact");
86  addRows();
87  }
88  }
89 
90  public void addAttendeesByEntity(String entityPath, boolean deep) {
91  if(Strings.isBlank(entityPath)) return;
92  AssistantSet as = new AssistantSet();
93  Assistants.addAssistants(entityPath, deep, as, deep ? AssistantConstants.all() : null);
94  for(Assistant a : as) {
95  IContact contact = a.contact;
96  if(notificationCategory != null && notificationCategory.isValid(contact) && notificationCategory.strongBond(ConstraintKeys.from(contact))) {
97  notificationCategory.getNotification().subscribe(
98  notificationCategory.getIdCategory(), contact, notificationCategory.getDefaultPeriod());
99  }
100  }
101  addRows();
102  }
103 
104  public void addAssistantsByEntity() {
105  AssistantDialog.load((as) -> {
106  for(Assistant a : as) {
107  IContact contact = a.contact;
108  if(notificationCategory != null && notificationCategory.isValid(contact) && notificationCategory.strongBond(ConstraintKeys.from(contact))) {
109  notificationCategory.getNotification().subscribe(
110  notificationCategory.getIdCategory(), contact, notificationCategory.getDefaultPeriod());
111  }
112  }
113  addRows();
114  });
115  }
116 
117  public void checkConstraints() {
118  if(notificationCategory != null) {
119  Messages.confirmProcess().add(I_.get("This process will check all subscribers...")).show(() -> {
120  notificationCategory.getNotification().checkConstraints(notificationCategory.getIdCategory());
121  addRows();
122  });
123  }
124  }
125 
126  public void removeAll() {
127  if(notificationCategory != null) {
128  Messages.confirmDeletion().show(() -> {
129  notificationCategory.getNotification().removeAll(notificationCategory.getIdCategory());
130  addRows();
131  });
132  }
133  }
134 
135  public void periodToAll(final QueuePeriod period) {
136  if(notificationCategory != null) {
137  if(notificationCategory.getAllowedPeriods().contains(period)) {
138  Messages.confirmProcess().show(() -> {
139  notificationCategory.getNotification().periodToAll(notificationCategory.getIdCategory(), period);
140  addRows();
141  });
142  }
143  }
144  }
145 
146  private void addRows() {
147  getRows(true).getChildren().clear();
148 
149  if(notificationCategory == null) return;
150 
151  setRowRenderer(new RendererOnDemand<QueueOptions, Long>(() -> new ElephantPU()) {
152  @Override
153  protected void renderRow(final Row row, final QueueOptions qo) {
154  row.appendChild((Component) Entities.getController("/contact/" + qo.getIdContact()).getLabelCtrl());
155  final QueuePeriodListbox qpl = new QueuePeriodListbox(notificationCategory.getAllowedPeriods());
156  qpl.afterCompose();
157  qpl.setMold("select");
158  qpl.setObjectValue(qo.getPeriod());
159  qpl.setAttribute("options", qo);
160  qpl.addEventListener(Events.ON_SELECT, CategorySubscribersGrid.this);
161  row.appendChild(qpl);
162  Toolbarbutton del = new Toolbarbutton();
163  del.setAttribute("options", qo);
164  del.setImage("/_zul/images/edit-delete.png");
165  del.addEventListener(Events.ON_CLICK, CategorySubscribersGrid.this);
166  row.appendChild(del);
167  }
168  });
169 
170  Collection list = new QueueManager().getSubcribers(notificationCategory.getIdCategory());
171 
172  setModel(new ListModelList(list));
173 
174  setRowCount(list.size());
175  }
176 
177  private void addColumns() {
178  Columns cols = getColumns(true);
179  cols.getChildren().clear();
180 
181  cols.appendChild(new Column(I_.get("Contact")));
182  cols.appendChild(new Column(I_.get("Period"), null, "300px"));
183  cols.appendChild(new Column(null, null, "50px"));
184  }
185 
186  @Override
187  public void onEvent(Event event) throws Exception {
188  if(event.getTarget() instanceof QueuePeriodListbox) {
189  QueuePeriodListbox qpl = (QueuePeriodListbox) event.getTarget();
190  QueueOptions qo = (QueueOptions) qpl.getAttribute("options");
191  qo.setPeriod(qpl.getObjectValue());
192  new QueueManager().configQueue(qo);
193  } else if(event.getTarget() instanceof Toolbarbutton) {
194  Toolbarbutton del = (Toolbarbutton) event.getTarget();
195  QueueOptions qo = (QueueOptions) del.getAttribute("options");
196  new QueueManager().removeOptions(qo);
197  addRows();
198  }
199  }
200 
201 }
202 
static IContact getContact(Object object)
Definition: Contacts.java:109
static ConstraintKeys from(IContact contact)
static void load(Consumer< AssistantSet > consumer)
static void addAssistants(String role, AssistantSet list, Object data)
Definition: Assistants.java:35
static Messages confirmDeletion()
Definition: Messages.java:87
static Messages confirmProcess()
Definition: Messages.java:95
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
QueueOptions configQueue(QueueOptions qo)
void removeOptions(String idCategory)
void setNotificationCategory(NotificationCategory notificationCategory)
void addAttendeesByEntity(String entityPath, boolean deep)
Columns getColumns(boolean create)
Rows getRows(boolean create)
static FrameClipboard getClipboard()
Definition: Framework.java:220
void subscribeIfNot(String idCategory, IContact contact, QueuePeriod period)
void subscribe(String idCategory, IContact contact, QueuePeriod period)