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