BrightSide Workbench Full Report + Source Code
elephant-www/src/main/java/org/turro/log/EntriesGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 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.log;
20 
21 import java.util.List;
22 import java.util.Optional;
23 import org.turro.string.Strings;
24 import org.turro.elephant.db.ElephantPU;
25 import org.turro.elephant.db.WhereClause;
26 import org.turro.elephant.entities.db.SystemLog;
27 import org.turro.elephant.util.DateFormats;
28 import org.turro.entities.Entities;
29 import org.turro.entities.IElephantEntity;
30 import org.turro.i18n.I_;
31 import org.turro.jpa.serializer.SerializerMessage;
32 import org.turro.zkoss.grid.PagingGrid;
33 import org.zkoss.zk.ui.Component;
34 import org.zkoss.zk.ui.event.Event;
35 import org.zkoss.zk.ui.event.EventListener;
36 import org.zkoss.zk.ui.event.Events;
37 import org.zkoss.zk.ui.ext.AfterCompose;
38 import org.zkoss.zul.Column;
39 import org.zkoss.zul.Columns;
40 import org.zkoss.zul.Image;
41 import org.zkoss.zul.Label;
42 import org.zkoss.zul.Row;
43 import org.zkoss.zul.Rows;
44 import org.zkoss.zul.Space;
45 
50 public class EntriesGrid extends PagingGrid {
51 
52  private String comment, generatorPath, entityPath;
53  private int max;
54 
55  public void setComment(String comment) {
56  this.comment = comment;
57  }
58 
59  public void setGeneratorPath(String generatorPath) {
60  this.generatorPath = generatorPath;
61  }
62 
63  public void setEntityPath(String entityPath) {
64  this.entityPath = entityPath;
65  }
66 
67  public void setMax(int max) {
68  this.max = max;
69  }
70 
71  public void search() {
72  populateRows();
73  }
74 
75  private void populateRows() {
76  clearColumns();
77  addColumns();
78  clearRows();
79  Rows rows = getRows(true);
80  List<SystemLog> entries = getEntries();
81  for(SystemLog entry : entries) {
82  Row row = new Row();
83  row.appendChild(new Label(I_.byKey(entry.getLogType().toString()) +
84  (entry.getCounts() > 1 ? "(" + entry.getCounts()+ ")" : "")));
85  row.appendChild(new Label(DateFormats.format(entry.getDateLog(), false)));
86  if(Strings.isBlank(generatorPath)) {
87  IElephantEntity iee = Entities.getController(entry.getGeneratorPath());
88  Object obj = iee.getLabelCtrl();
89  if(obj instanceof Component) {
90  row.appendChild((Component) obj);
91  } else {
92  row.appendChild(new Label(entry.getGeneratorName()));
93  }
94  }
95  if(Strings.isBlank(entityPath)) {
96  IElephantEntity iee = Entities.getController(entry.getEntityPath());
97  Object obj = iee.getLabelCtrl();
98  if(obj instanceof Component) {
99  row.appendChild((Component) obj);
100  if(obj instanceof AfterCompose) {
101  ((AfterCompose) obj).afterCompose();
102  }
103  } else {
104  row.appendChild(new Label(Strings.isBlank(entry.getEntityName(), iee.getLabel())));
105  }
106  }
107  row.appendChild(new Label(entry.getComment()));
108  final Object data = Optional.ofNullable(entry.getObject()).orElse(entry.getString());
109  if(!(data instanceof String)) {
110  row.appendChild(new Space());
111  } else if(isReadableAndShort((String) data)) {
112  row.appendChild(new Label((String) data));
113  } else {
114  Image info = new Image("/_zul/images/info.png");
115  info.addEventListener(Events.ON_CLICK, new EventListener<Event>() {
116  @Override
117  public void onEvent(Event event) throws Exception {
118  new SerializerMessage((String) data).show(getPage());
119  }
120  });
121  row.appendChild(info);
122  }
123  rows.appendChild(row);
124  }
125  setRowCount(entries.size());
126  }
127 
128  private List<SystemLog> getEntries() {
129  WhereClause wc = new WhereClause();
130  wc.addClause("select l from SystemLog as l");
131  if(!"*".equals(comment)) {
132  wc.setPrefix("where");
133  wc.addLikeFields(new String[] {"l.comment"}, comment);
134  } else {
135  wc.addClause("where 1=1");
136  }
137  wc.setPrefix(null);
138  if(!Strings.isBlank(generatorPath)) {
139  wc.addClause("and l.generatorPath = :generatorPath");
140  wc.addNamedValue("generatorPath", generatorPath);
141  }
142  if(!Strings.isBlank(entityPath)) {
143  wc.addClause("and l.entityPath = :entityPath");
144  wc.addNamedValue("entityPath", entityPath);
145  }
146  wc.addClause("order by l.dateLog desc");
147  return new ElephantPU().getResultList(wc, max);
148  }
149 
150  private void addColumns() {
151  Columns cols = getColumns(true);
152  cols.appendChild(new Column(I_.get("Type"), null, "80px"));
153  cols.appendChild(new Column(I_.get("Date"), null, "150px"));
154  if(Strings.isBlank(generatorPath)) {
155  cols.appendChild(new Column(I_.get("Generator"), null, "20%"));
156  }
157  if(Strings.isBlank(entityPath)) {
158  cols.appendChild(new Column(I_.get("Entity"), null, "30%"));
159  }
160  cols.appendChild(new Column(I_.get("Comment"), null, "20%"));
161  cols.appendChild(new Column(I_.get("Data"), null, "20%"));
162  }
163 
164  private boolean isReadableAndShort(String data) {
165  return (!(data.startsWith("<") || data.startsWith("{")) && data.length() <= 50);
166  }
167 
168 }
static final String format(Date d, boolean dateOnly)
static IElephantEntity getController(String path)
Definition: Entities.java:78
static String byKey(String key)
Definition: I_.java:83
Rows getRows(boolean create)
IElephantEntity getController(String entityPath)