BrightSide Workbench Full Report + Source Code
SubscriptorsGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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.publication.zul.ctrl;
19 
20 import java.util.Collection;
21 import java.util.Date;
22 import java.util.List;
23 import org.turro.string.Strings;
24 import org.turro.assistant.Assistant;
25 import org.turro.assistant.AssistantConstants;
26 import org.turro.assistant.AssistantSet;
27 import org.turro.assistant.Assistants;
28 import org.turro.contacts.Connector;
29 import org.turro.contacts.Contact;
30 import org.turro.contacts.zul.label.ContactInfo;
31 import org.turro.elephant.security.IUser;
32 import org.turro.elephant.util.DateFormats;
33 import org.turro.i18n.I_;
34 import org.turro.jpa.Dao;
35 import org.turro.jpa.grid.RendererOnDemand;
36 import org.turro.log.SystemLogType;
37 import org.turro.log.SystemLogger;
38 import org.turro.publication.db.PublicationPU;
39 import org.turro.publication.entity.PublicationCategory;
40 import org.turro.publication.entity.PublicationSubscription;
41 import org.turro.zkoss.grid.PagingGrid;
42 import org.turro.zul.frame.Framework;
43 import org.zkoss.zk.ui.event.Event;
44 import org.zkoss.zk.ui.event.EventListener;
45 import org.zkoss.zk.ui.event.Events;
46 import org.zkoss.zul.*;
47 
52 @Deprecated
53 public class SubscriptorsGrid extends PagingGrid {
54 
55  private PublicationCategory publicationCategory;
56 
57  public void setPublicationCategory(PublicationCategory publicationCategory) {
58  this.publicationCategory = publicationCategory;
59  addColumns();
60  addRows();
61  }
62 
63  public void addContact(Contact contact) {
64  if(contact == null) {
65  return;
66  }
67  Dao dao = new PublicationPU();
68  Connector email = contact.getConnectorMap().get(IUser.CONNECTOR_EMAIL);
69  if(email != null && !Strings.isBlank(email.getValue())) {
70  if(dao.getSingleResultOrNull(
71  "select ps from PublicationSubscription ps " +
72  "where ps.idContact = ? " +
73  "and ps.publicationCategory = ?",
74  new Object[] { contact.getId(), publicationCategory }
75  ) == null) {
77  ps.setCreation(new Date());
78  ps.setPublicationCategory(publicationCategory);
79  ps.setIdContact(contact.getId());
80  dao.saveObject(ps);
81  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, publicationCategory,
82  "subscribed", contact.getName());
83  addRows();
84  }
85  }
86  }
87 
88  public void pasteContacts() {
89  List<Contact> contacts = (List<Contact>) Framework.getClipboard().get("bbf_clip_contact");
90  if(contacts != null && publicationCategory != null) {
91  Dao dao = new PublicationPU();
92  for(Contact contact : contacts) {
93  Connector email = contact.getConnectorMap().get(IUser.CONNECTOR_EMAIL);
94  if(email != null && !Strings.isBlank(email.getValue())) {
95  if(dao.getSingleResultOrNull(
96  "select ps from PublicationSubscription ps " +
97  "where ps.idContact = ? " +
98  "and ps.publicationCategory = ?",
99  new Object[] { contact.getId(), publicationCategory }
100  ) == null) {
102  ps.setCreation(new Date());
103  ps.setPublicationCategory(publicationCategory);
104  ps.setIdContact(contact.getId());
105  dao.saveObject(ps);
106  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, publicationCategory,
107  "subscribed", contact.getName());
108  }
109  }
110  }
111  Framework.getClipboard().remove("bbf_clip_contact");
112  addRows();
113  }
114  }
115 
116  public void addAttendeesByEntity(String entityPath, boolean deep) {
117  if(Strings.isBlank(entityPath)) return;
118  AssistantSet as = new AssistantSet();
119  Assistants.addAssistants(entityPath, deep, as, deep ? AssistantConstants.all() : null);
120  Dao dao = new PublicationPU();
121  for(Assistant a : as) {
122  Contact contact = (Contact) a.contact.getContact();
123  Connector email = contact.getConnectorMap().get(IUser.CONNECTOR_EMAIL);
124  if(email != null && !Strings.isBlank(email.getValue())) {
125  if(dao.getSingleResultOrNull(
126  "select ps from PublicationSubscription ps " +
127  "where ps.idContact = ? " +
128  "and ps.publicationCategory = ?",
129  new Object[] { contact.getId(), publicationCategory }
130  ) == null) {
132  ps.setCreation(new Date());
133  ps.setPublicationCategory(publicationCategory);
134  ps.setIdContact(contact.getId());
135  dao.saveObject(ps);
136  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, publicationCategory,
137  "subscribed", contact.getName());
138  }
139  }
140  addRows();
141  }
142  }
143 
144  private void addRows() {
145  getRows(true).getChildren().clear();
146 
147  if(publicationCategory == null) return;
148 
149  setRowRenderer(new RendererOnDemand<PublicationSubscription, Long>(() -> new PublicationPU()) {
150  @Override
151  protected void renderRow(final Row row, final PublicationSubscription ps) {
152  row.appendChild(new ContactInfo(ps.getIdContact()));
153  row.appendChild(new Label(DateFormats.format(ps.getCreation(), true)));
154 
155  Toolbarbutton delSubscription = new Toolbarbutton();
156  delSubscription.setImage("/_zul/images/edit-delete.png");
157  delSubscription.addEventListener(Events.ON_CLICK, new EventListener() {
158  @Override
159  public void onEvent(Event event) throws Exception {
160  getDao().deleteObject(ps);
161  row.detach();
162  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, publicationCategory,
163  "unsubscribed", ps.getContact().getName());
164  }
165  });
166  row.appendChild(delSubscription);
167  }
168  });
169 
170  Collection list = publicationCategory.getSubscriptorsId();
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("Creation"), null, "200px"));
183  cols.appendChild(new Column(null, null, "50px"));
184  }
185 
186 }
static void addAssistants(String role, AssistantSet list, Object data)
Definition: Assistants.java:35
Map< String, Connector > getConnectorMap()
Definition: Contact.java:499
static final String format(Date d, boolean dateOnly)
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419
static ISystemLogger getInstance()
void setPublicationCategory(PublicationCategory publicationCategory)
void addAttendeesByEntity(String entityPath, boolean deep)
void setPublicationCategory(PublicationCategory publicationCategory)
Columns getColumns(boolean create)
Rows getRows(boolean create)
static FrameClipboard getClipboard()
Definition: Framework.java:220
static final String CONNECTOR_EMAIL
Definition: IUser.java:27
void doLog(SystemLogType type, Object entity, String comment, Serializable data)