BrightSide Workbench Full Report + Source Code
SystemLogs.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.io.Serializable;
22 import java.util.Date;
23 import org.turro.annotation.ElephantLogger;
24 import org.turro.auth.Authentication;
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 
36 @ElephantLogger
37 public class SystemLogs implements ISystemLogger {
38 
39  @Override
40  public void doLog(SystemLogType type, Object entity, String comment, Serializable data) {
42  }
43 
44  @Override
45  public void doLog(SystemLogType type, String entityPath, String comment, Serializable data) {
47  }
48 
49  @Override
50  public void doLog(SystemLogType type, IElephantEntity entity, String comment, Serializable data) {
51  doLog(type, getGenerator(Authentication.getRealIContact()), entity, comment, data);
52  }
53 
54  @Override
55  public void doLog(SystemLogType type, IContact contact, Object entity, String comment, Serializable data) {
56  doLog(type, getGenerator(contact), Entities.getController(entity), comment, data);
57  }
58 
59  @Override
60  public void doLog(SystemLogType type, IContact contact, String entityPath, String comment, Serializable data) {
61  doLog(type, getGenerator(contact), Entities.getController(entityPath), comment, data);
62  }
63 
64  @Override
65  public void doLog(SystemLogType type, IContact contact, IElephantEntity entity, String comment, Serializable data) {
66  doLog(type, getGenerator(contact), entity, comment, data);
67  }
68 
69  @Override
70  public void doLog(SystemLogType type, String generatorPath, String entityPath, String comment, Serializable data) {
71  doLog(type, getGenerator(generatorPath), Entities.getController(entityPath), comment, data);
72  }
73 
74  @Override
75  public void doLog(SystemLogType type, IElephantEntity generator, IElephantEntity entity, String comment, Serializable data) {
76  SystemLog entry = new SystemLog();
77  entry.setDateLog(new Date());
78  entry.setCounts(1);
79  if((generator == null) || (generator instanceof EmptyController)) {
80  entry.setGeneratorPath("#");
81  entry.setGeneratorName("Unknown");
82  } else {
83  entry.setGeneratorPath(generator.getPath());
84  entry.setGeneratorName(generator.getNameOrLabel());
85  }
86  if((entity == null) || (entity instanceof EmptyController)) {
87  entry.setEntityPath("#");
88  entry.setEntityName("Unknown");
89  } else {
90  entry.setEntityPath(entity.getPath());
91  entry.setEntityName(entity.getNameOrLabel());
92  }
93  entry.setComment(comment);
94  entry.setObject(data);
95  entry.setLogType(type);
96  new ElephantPU().saveObject(entry);
97  }
98 
99  @Override
100  public ILogWrapper info() {
101  return LogWrapper.info();
102  }
103 
104  @Override
105  public ILogWrapper warning() {
106  return LogWrapper.warning();
107  }
108 
109  @Override
110  public ILogWrapper severe() {
111  return LogWrapper.severe();
112  }
113 
114  @Override
116  return LogWrapper.type(type);
117  }
118 
119  @Override
120  public long getCountOf(String comment, String logPath, Date since) {
121  return new SystemLogModel().getCountOf(comment, logPath, since);
122  }
123 
124  @Override
125  public Date getLastDateOf(String entityPath, String comment) {
126  SystemLog log = new SystemLogModel().lastEntry(entityPath, comment);
127  return log == null ? null : log.getDateLog();
128  }
129 
130  @Override
131  public void cleanup(String logPath) {
132  new SystemLogModel().cleanup(logPath);
133  }
134 
135  public IElephantEntity getGenerator(Object from) {
136  IElephantEntity generator = null;
137  if(from instanceof IContact) {
138  generator = Entities.getController(((IContact) from).getContact());
139  } else {
140  generator = Entities.getController(from);
141  }
142  return generator;
143  }
144 
145 }
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)
static LogWrapper info()
static LogWrapper warning()
static LogWrapper severe()
void cleanup(String entityPath)
long getCountOf(String comment, String entityPath, Date since)
SystemLog lastEntry(String entityPath, String comment)
void doLog(SystemLogType type, String entityPath, String comment, Serializable data)
Definition: SystemLogs.java:45
void doLog(SystemLogType type, IElephantEntity entity, String comment, Serializable data)
Definition: SystemLogs.java:50
IElephantEntity getGenerator(Object from)
ILogWrapper type(SystemLogType type)
Date getLastDateOf(String entityPath, String comment)
void cleanup(String logPath)
long getCountOf(String comment, String logPath, Date since)
void doLog(SystemLogType type, IContact contact, String entityPath, String comment, Serializable data)
Definition: SystemLogs.java:60
void doLog(SystemLogType type, IContact contact, Object entity, String comment, Serializable data)
Definition: SystemLogs.java:55
void doLog(SystemLogType type, String generatorPath, String entityPath, String comment, Serializable data)
Definition: SystemLogs.java:70
void doLog(SystemLogType type, IElephantEntity generator, IElephantEntity entity, String comment, Serializable data)
Definition: SystemLogs.java:75
void doLog(SystemLogType type, IContact contact, IElephantEntity entity, String comment, Serializable data)
Definition: SystemLogs.java:65
void doLog(SystemLogType type, Object entity, String comment, Serializable data)
Definition: SystemLogs.java:40