BrightSide Workbench Full Report + Source Code
BloggersGrid.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.publication.zul.ctrl;
20 
21 import java.util.Collection;
22 import java.util.List;
23 import org.turro.string.Strings;
24 import org.turro.contacts.Connector;
25 import org.turro.contacts.Contact;
26 import org.turro.contacts.zul.label.ContactInfo;
27 import org.turro.elephant.security.IUser;
28 import org.turro.i18n.I_;
29 import org.turro.jpa.Dao;
30 import org.turro.jpa.grid.RendererOnDemand;
31 import org.turro.log.SystemLogType;
32 import org.turro.log.SystemLogger;
33 import org.turro.publication.db.PublicationPU;
34 import org.turro.publication.entity.PublicationBlogger;
35 import org.turro.publication.entity.PublicationCategory;
36 import org.turro.zkoss.grid.PagingGrid;
37 import org.turro.zul.frame.Framework;
38 import org.zkoss.zk.ui.event.Event;
39 import org.zkoss.zk.ui.event.EventListener;
40 import org.zkoss.zk.ui.event.Events;
41 import org.zkoss.zul.Checkbox;
42 import org.zkoss.zul.Column;
43 import org.zkoss.zul.Columns;
44 import org.zkoss.zul.ListModelList;
45 import org.zkoss.zul.Row;
46 import org.zkoss.zul.Toolbarbutton;
47 
52 public class BloggersGrid extends PagingGrid {
53 
54  private PublicationCategory publicationCategory;
55 
56  public void setPublicationCategory(PublicationCategory publicationCategory) {
57  this.publicationCategory = publicationCategory;
58  addColumns();
59  addRows();
60  }
61 
62  public void addContact(Contact contact) {
63  if(contact == null) {
64  return;
65  }
66  Dao dao = new PublicationPU();
67  if(dao.getSingleResultOrNull(
68  "select ps from PublicationBlogger ps " +
69  "where ps.idContact = ? " +
70  "and ps.publicationCategory = ?",
71  new Object[] { contact.getId(), publicationCategory }
72  ) == null) {
74  ps.setPublicationCategory(publicationCategory);
75  ps.setIdContact(contact.getId());
76  dao.saveObject(ps);
77  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, publicationCategory,
78  "blogged", contact.getName());
79  addRows();
80  }
81  }
82 
83  public void pasteContacts() {
84  List<Contact> contacts = (List<Contact>) Framework.getClipboard().get("bbf_clip_contact");
85  if(contacts != null && publicationCategory != null) {
86  Dao dao = new PublicationPU();
87  for(Contact contact : contacts) {
88  Connector email = contact.getConnectorMap().get(IUser.CONNECTOR_EMAIL);
89  if(email != null && !Strings.isBlank(email.getValue())) {
90  if(dao.getSingleResultOrNull(
91  "select ps from PublicationBlogger ps " +
92  "where ps.idContact = ? " +
93  "and ps.publicationCategory = ?",
94  new Object[] { contact.getId(), publicationCategory }
95  ) == null) {
97  ps.setPublicationCategory(publicationCategory);
98  ps.setIdContact(contact.getId());
99  dao.saveObject(ps);
100  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, publicationCategory,
101  "blogged", contact.getName());
102  }
103  }
104  }
105  Framework.getClipboard().remove("bbf_clip_contact");
106  addRows();
107  }
108  }
109 
110  private void addRows() {
111  getRows(true).getChildren().clear();
112 
113  if(publicationCategory == null) return;
114 
115  setRowRenderer(new RendererOnDemand<PublicationBlogger, Long>(() -> new PublicationPU()) {
116  @Override
117  protected void renderRow(final Row row, final PublicationBlogger ps) {
118  row.appendChild(new ContactInfo(ps.getIdContact()));
119 
120  final Checkbox administrator = new Checkbox();
121  administrator.setChecked(ps.isAdministrator());
122  administrator.addEventListener(Events.ON_CLICK, new EventListener() {
123  @Override
124  public void onEvent(Event event) throws Exception {
125  ps.setAdministrator(administrator.isChecked());
126  getDao().saveObject(ps);
127  }
128  });
129  row.appendChild(administrator);
130 
131  Toolbarbutton delSubscription = new Toolbarbutton();
132  delSubscription.setImage("/_zul/images/edit-delete.png");
133  delSubscription.addEventListener(Events.ON_CLICK, new EventListener() {
134  @Override
135  public void onEvent(Event event) throws Exception {
136  getDao().deleteObject(ps);
137  row.detach();
138  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, publicationCategory,
139  "unblogged", ps.getContact().getName());
140  }
141  });
142  row.appendChild(delSubscription);
143  }
144  });
145 
146  Collection list = publicationCategory.getBloggersId();
147 
148  setModel(new ListModelList(list));
149 
150  setRowCount(list.size());
151  }
152 
153  private void addColumns() {
154  Columns cols = getColumns(true);
155  cols.getChildren().clear();
156 
157  cols.appendChild(new Column(I_.get("Contact")));
158  cols.appendChild(new Column(I_.get("Administrator"), null, "120px"));
159  cols.appendChild(new Column(null, null, "50px"));
160  }
161 
162 }
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419
static ISystemLogger getInstance()
void setPublicationCategory(PublicationCategory publicationCategory)
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)