BrightSide Workbench Full Report + Source Code
LogListbox.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.zul.log;
19 
20 import java.util.Date;
21 import java.util.List;
22 import org.amic.util.date.DateFormats;
23 import org.amic.util.string.Strings;
24 import org.turro.contacts.LogEntry;
25 import org.turro.contacts.db.ContactsPU;
26 import org.turro.elephant.context.Application;
27 import org.turro.elephant.db.WhereClause;
28 import org.turro.entities.Entities;
29 import org.turro.entities.IElephantEntity;
30 import org.turro.i18n.I_;
31 import org.turro.jpa.Dao;
32 import org.turro.jpa.list.RendererOnDemand;
33 import org.turro.jpa.serializer.SerializerMessage;
34 import org.turro.zkoss.label.TipCell;
35 import org.turro.zkoss.layout.ListheaderAdapter;
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.zk.ui.ext.AfterCompose;
40 import org.zkoss.zul.*;
41 
46 @Deprecated
47 public class LogListbox extends Listbox implements AfterCompose {
48 
49  private String logPath, logName, logComment;
50  private Date fromDate;
51 
52  public Date getFromDate() {
53  return fromDate;
54  }
55 
56  public void setFromDate(Date fromDate) {
57  this.fromDate = fromDate;
58  }
59 
60  public String getLogComment() {
61  return logComment;
62  }
63 
64  public void setLogComment(String logComment) {
65  this.logComment = logComment;
66  }
67 
68  public String getLogName() {
69  return logName;
70  }
71 
72  public void setLogName(String logName) {
73  this.logName = logName;
74  }
75 
76  public String getLogPath() {
77  return logPath;
78  }
79 
80  public void setLogPath(String logPath) {
81  this.logPath = logPath;
82  }
83 
84  public void refresh() {
85  setModel(new ListModelList(getLogEntries()));
86  }
87 
88  @Override
89  public void afterCompose() {
90  addHeaders();
91  addItems();
92  }
93 
94  private void addHeaders() {
95  appendChild(new Listhead());
96  getListhead().appendChild(new ListheaderAdapter(
97  I_.get("Type"), "80px"));
98  getListhead().appendChild(new ListheaderAdapter(
99  I_.get("Date"), "120px"));
100  if(Strings.isBlank(logName)) {
101  getListhead().appendChild(new ListheaderAdapter(
102  I_.get("User"), "/_zul/images/contact.png", "2"));
103  }
104  if(Strings.isBlank(logPath)) {
105  getListhead().appendChild(new ListheaderAdapter(
106  I_.get("Entity"), "/_zul/images/entity.png", "3"));
107  }
108  getListhead().appendChild(new ListheaderAdapter(
109  I_.get("Comment"), "1"));
110  getListhead().appendChild(new ListheaderAdapter(
111  I_.get("Data"), "3"));
112  getListhead().setSizable(true);
113  }
114 
115  private void addItems() {
116  final Dao dao = new ContactsPU();
117 
118  setItemRenderer(new RendererOnDemand<LogEntry, String>() {
119  @Override
120  protected Dao getDao() {
121  return dao;
122  }
123  @Override
124  protected void renderItem(Listitem item, final LogEntry entry) {
125  item.appendChild(new Listcell(I_.byKey(entry.getLogType().toString())));
126  item.appendChild(new Listcell(DateFormats.format(entry.getDateLog(), "SHORT", "SHORT", Application.getUsedLocale())));
127  if(Strings.isBlank(logName)) {
128  item.appendChild(new Listcell(entry.getName()));
129  }
130  if(Strings.isBlank(logPath)) {
131  IElephantEntity ee = Entities.getController(entry.getPath());
132  String name = ee.getName();
133  if(Strings.isBlank(name)) name = ee.getLabel();
134  item.appendChild(new TipCell(name, 100));
135  }
136  item.appendChild(new TipCell(entry.getComment(), 40));
137  final Object data = entry.getObject();
138  if(!(data instanceof String)) {
139  item.appendChild(new Listcell());
140  } else {
141  if(isReadableAndShort((String) data)) {
142  item.appendChild(new Listcell((String) data));
143  } else {
144  Listcell info = new Listcell(null, "/_zul/images/info.png");
145  info.addEventListener(Events.ON_CLICK, new EventListener() {
146  @Override
147  public void onEvent(Event event) throws Exception {
148  new SerializerMessage((String) data).show(getPage());
149  }
150  });
151  item.appendChild(info);
152  }
153  }
154  }
155  });
156 
157  setModel(new ListModelList(getLogEntries()));
158  }
159 
160  private List<LogEntry> getLogEntries() {
161  WhereClause wc = new WhereClause();
162  wc.addClause("select log.id from LogEntry as log");
163  wc.addClause("where 1=1");
164  if(!Strings.isBlank(logPath)) {
165  wc.addClause("and log.path = :path");
166  wc.addNamedValue("path", logPath);
167  }
168  if(!Strings.isBlank(logName)) {
169  wc.addClause("and log.name = :name");
170  wc.addNamedValue("name", logName);
171  }
172  if(fromDate != null) {
173  wc.addClause("and log.dateLog >= :date");
174  wc.addNamedValue("date", fromDate);
175  }
176  if(!Strings.isBlank(logComment)) {
177  wc.addClause("and log.comment = :comment");
178  wc.addNamedValue("comment", logComment);
179  }
180  wc.addClause("order by log.dateLog DESC");
181  return new ContactsPU().getResultList(wc);
182  }
183 
184  private boolean isReadableAndShort(String data) {
185  return (!data.startsWith("<") && data.length() <= 50);
186  }
187 }
static String get(String msg)
Definition: I_.java:41
void setFromDate(Date fromDate)
Definition: LogListbox.java:56
void setLogName(String logName)
Definition: LogListbox.java:72
void setLogComment(String logComment)
Definition: LogListbox.java:64
void setLogPath(String logPath)
Definition: LogListbox.java:80