BrightSide Workbench Full Report + Source Code
LogWrapper.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 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.io.Serializable;
22 import java.util.Date;
23 import org.turro.string.Strings;
24 import org.turro.elephant.context.Application;
25 import org.turro.elephant.db.ElephantPU;
26 import org.turro.elephant.entities.db.SystemLog;
27 import org.turro.entities.EmptyController;
28 import org.turro.entities.Entities;
29 import org.turro.entities.IElephantEntity;
30 import org.turro.plugin.contacts.IContact;
31 import org.turro.reflection.stub.Stubs;
32 import org.turro.sql.SqlClause;
33 
38 public class LogWrapper implements ILogWrapper {
39 
40  @Override
41  public LogWrapper date(Date date) {
42  systemLog.setDateLog(date);
43  return this;
44  }
45 
46  @Override
47  public LogWrapper comment(String comment) {
48  systemLog.setComment(comment);
49  return this;
50  }
51 
52  @Override
53  public LogWrapper object(Serializable object) {
54  systemLog.setObject(object);
55  return this;
56  }
57 
58  @Override
59  public LogWrapper stub(Object entity) {
60  systemLog.setData(Stubs.json(entity).getBytes());
61  return this;
62  }
63 
64  @Override
65  public LogWrapper data(byte[] data) {
66  systemLog.setData(data);
67  return this;
68  }
69 
70  @Override
71  public LogWrapper entityPath(String path) {
72  systemLog.setEntityPath(path);
73  return this;
74  }
75 
76  @Override
77  public LogWrapper entityName(String name) {
78  systemLog.setEntityName(name);
79  return this;
80  }
81 
82  @Override
83  public LogWrapper generatorPath(String path) {
84  systemLog.setGeneratorPath(path);
85  return this;
86  }
87 
88  @Override
89  public LogWrapper generatorName(String name) {
90  systemLog.setGeneratorName(name);
91  return this;
92  }
93 
94  @Override
95  public LogWrapper entity(Object entity) {
97  }
98 
99  @Override
101  if((entity == null) || (entity instanceof EmptyController)) {
102  systemLog.setEntityPath("#");
103  systemLog.setEntityName("Unknown");
104  } else {
105  systemLog.setEntityPath(entity.getPath());
106  systemLog.setEntityName(entity.getNameOrLabel());
107  }
108  return this;
109  }
110 
111  @Override
113  if(from instanceof IContact) {
114  generator(Entities.getController(((IContact) from).getContact()));
115  } else {
117  }
118  return this;
119  }
120 
121  @Override
123  if((generator == null) || (generator instanceof EmptyController)) {
124  systemLog.setGeneratorPath("#");
125  systemLog.setGeneratorName("Unknown");
126  } else {
127  systemLog.setGeneratorPath(generator.getPath());
128  systemLog.setGeneratorName(generator.getNameOrLabel());
129  }
130  return this;
131  }
132 
133  @Override
134  public void log() {
135  if(systemLog.getDateLog() == null) date(new Date());
136  if(systemLog.getCounts() == 0) systemLog.setCounts(1);
137  if(Strings.isBlank(systemLog.getGeneratorPath())) generator(Application.getUser());
138  new ElephantPU().saveObject(systemLog);
139  }
140 
141  @Override
142  public long count() {
143  return SqlClause.select("sum(l.counts)")
144  .from("SystemLog l")
145  .where().equal("l.logType", systemLog.getLogType())
146  .startIf(!Strings.isBlank(systemLog.getComment()))
147  .and().equal("l.comment", systemLog.getComment())
148  .endIf()
149  .startIf(!Strings.isBlank(systemLog.getEntityPath()))
150  .and().equal("l.entityPath", systemLog.getEntityPath())
151  .endIf()
152  .startIf(!Strings.isBlank(systemLog.getGeneratorPath()))
153  .and().equal("l.generatorPath", systemLog.getGeneratorPath())
154  .endIf()
155  .startIf(systemLog.getDateLog() != null)
156  .and().greaterOrEqual("l.dateLog", systemLog.getDateLog())
157  .endIf()
158  .dao(new ElephantPU())
159  .singleResult(Long.class);
160  }
161 
162  /* Factory */
163 
164  private final SystemLog systemLog;
165 
166  public static LogWrapper info() {
167  return type(SystemLogType.LOG_INFO);
168  }
169 
170  public static LogWrapper warning() {
172  }
173 
174  public static LogWrapper severe() {
175  return type(SystemLogType.LOG_SEVERE);
176  }
177 
179  return new LogWrapper(type);
180  }
181 
183  this.systemLog = new SystemLog();
184  this.systemLog.setLogType(type);
185  }
186 
187 }
void setEntityName(String entityName)
Definition: SystemLog.java:108
void setLogType(SystemLogType logType)
Definition: SystemLog.java:76
void setGeneratorPath(String generatorPath)
Definition: SystemLog.java:84
void setObject(Serializable object)
Definition: SystemLog.java:155
void setGeneratorName(String generatorName)
Definition: SystemLog.java:100
void setEntityPath(String entityPath)
Definition: SystemLog.java:92
static IElephantEntity getController(String path)
Definition: Entities.java:78
static LogWrapper type(SystemLogType type)
LogWrapper entityPath(String path)
Definition: LogWrapper.java:71
LogWrapper generatorPath(String path)
Definition: LogWrapper.java:83
LogWrapper entity(Object entity)
Definition: LogWrapper.java:95
static LogWrapper info()
LogWrapper generator(IElephantEntity generator)
LogWrapper comment(String comment)
Definition: LogWrapper.java:47
static LogWrapper warning()
LogWrapper entity(IElephantEntity entity)
LogWrapper object(Serializable object)
Definition: LogWrapper.java:53
LogWrapper entityName(String name)
Definition: LogWrapper.java:77
LogWrapper generator(IContact from)
LogWrapper stub(Object entity)
Definition: LogWrapper.java:59
LogWrapper data(byte[] data)
Definition: LogWrapper.java:65
static LogWrapper severe()
LogWrapper date(Date date)
Definition: LogWrapper.java:41
LogWrapper generatorName(String name)
Definition: LogWrapper.java:89
LogWrapper(SystemLogType type)