BrightSide Workbench Full Report + Source Code
NewsletterGrid.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.newsletter;
20 
21 import java.util.List;
22 import org.turro.string.Strings;
23 import org.turro.action.queue.NotificationCategory;
24 import org.turro.elephant.context.Application;
25 import org.turro.elephant.db.WhereClause;
26 import org.turro.i18n.I_;
27 import org.turro.jpa.Dao;
28 import org.turro.jpa.grid.RendererOnDemand;
29 import org.turro.publication.db.PublicationPU;
30 import org.turro.publication.entity.Newsletter;
31 import org.turro.publication.zul.menu.PublicationMenu;
32 import org.turro.tags.Tags;
33 import org.turro.zkoss.grid.PagingGrid;
34 import org.turro.zkoss.label.LabelExtended;
35 import org.zkoss.zk.ui.Component;
36 import org.zkoss.zk.ui.event.Event;
37 import org.zkoss.zk.ui.event.EventListener;
38 import org.zkoss.zk.ui.event.Events;
39 import org.zkoss.zul.Column;
40 import org.zkoss.zul.Columns;
41 import org.zkoss.zul.Label;
42 import org.zkoss.zul.ListModelList;
43 import org.zkoss.zul.Row;
44 import org.zkoss.zul.Vlayout;
45 
50 public class NewsletterGrid extends PagingGrid {
51 
52  private final Application app = Application.getApplication();
53 
54  public NewsletterGrid() {
55  addColumns();
56  }
57 
58  public void setValues(boolean accepted, NotificationCategory category, String text) {
59  addRows(accepted, category, text);
60  }
61 
62  private void addColumns() {
63  Columns cols = new Columns();
64  cols.setSizable(true);
65  cols.setMenupopup("auto");
66  appendChild(cols);
67 
68  Column col = new Column(I_.get("Date"), null, "200px");
69  cols.appendChild(col);
70 
71  col = new Column(I_.get("Title"));
72  cols.appendChild(col);
73  }
74 
75  private void addRows(boolean accepted, NotificationCategory category, String text) {
76 
77  setRowRenderer(new RendererOnDemand<Newsletter, Long>(() -> new PublicationPU()) {
78  @Override
79  protected void renderRow(Row row, final Newsletter newsletter) {
80  fillRow(row, newsletter);
81  }
82  });
83 
84  List<Long> list = getNewsletterIds(accepted, category, text);
85  setModel(new ListModelList<Long>(list));
86 
87  setRowCount(list.size());
88 
89  }
90 
91  private void fillRow(Row row, final Newsletter newsletter) {
92  row.setValign("top");
93 
94  LabelExtended lext = new LabelExtended();
95  lext.setDate(newsletter.getDate());
96  lext.setSclass("ldate");
97  row.appendChild(lext);
98 
99  Vlayout vbox = new Vlayout();
100  Label title = new Label(
101  Strings.isBlank(newsletter.getUnescapedTitle()) ?
102  I_.get("No description!") :
103  newsletter.getUnescapedTitle());
104  if(app.isInRole("publication:edit")) {
105  title.addEventListener(Events.ON_CLICK, new EventListener() {
106  @Override
107  public void onEvent(Event event) throws Exception {
108  PublicationMenu.showNewsletter(newsletter.getId());
109  }
110  });
111  title.setStyle("cursor:pointer");
112  }
113  title.setSclass("ltitle");
114  vbox.appendChild(title);
115 
116  lext = new LabelExtended();
117  lext.setStyle("color:#444;font-size:11px;");
118  lext.setValue(I_.get(newsletter.getCategory().getCategory()));
119 
120  vbox.appendChild(lext);
121 
122 // TagItCtrl tagIt = Plugins.loadImplementation(TagItCtrl.class, "label");
123 // tagIt.setEntity(newsletter);
124 // if(tagIt instanceof AfterCompose) {
125 // ((AfterCompose) tagIt).afterCompose();
126 // }
127  vbox.appendChild((Component) Tags.getControl(newsletter));
128 
129  row.appendChild(vbox);
130  }
131 
132  private List<Long> getNewsletterIds(boolean accepted, NotificationCategory category, String text) {
133  Dao dao = new PublicationPU();
134  return dao.getResultList(createNewsletterCriteria(accepted, category, text));
135  }
136 
137  public static WhereClause createNewsletterCriteria(boolean accepted, NotificationCategory category, String text) {
138  WhereClause wc = new WhereClause();
139  wc.addClause("select newsletter from Newsletter newsletter");
140  wc.addClause("where newsletter.accepted = :accepted");
141  wc.addNamedValue("accepted", accepted);
142  if(category != null) {
143  wc.addClause("and newsletter.idCategory = :category");
144  wc.addNamedValue("category", category.getIdCategory());
145  }
146  if(!Strings.isBlank(text)) {
147  text = "*" + text + "*";
148  wc.addClause("and newsletter.title like :text1");
149  wc.addNamedValue("text1", text.replaceAll("\\*", "%"));
150  // missing sections body
151  }
152  wc.addClause("order by newsletter.date DESC");
153  return wc;
154  }
155 
156 }
void addNamedValue(String name, Object value)
static String get(String msg)
Definition: I_.java:41
static WhereClause createNewsletterCriteria(boolean accepted, NotificationCategory category, String text)
void setValues(boolean accepted, NotificationCategory category, String text)