BrightSide Workbench Full Report + Source Code
SystemLogModel.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.Date;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Optional;
25 import java.util.Set;
26 import java.util.stream.Stream;
27 import org.turro.string.Strings;
28 import org.turro.elephant.db.ElephantPU;
29 import org.turro.elephant.entities.db.SystemLog;
30 import org.turro.jpa.Dao;
31 import org.turro.path.Path;
32 import org.turro.sql.SqlClause;
33 
38 public class SystemLogModel {
39 
40  public long getCountOf(String comment, String entityPath, Date since) {
41  return Optional.ofNullable(
42  SqlClause.select("sum(counts)").from("SystemLog")
43  .where().any()
44  .startIf(!Strings.isBlank(comment))
45  .and().equal("comment", comment)
46  .endIf()
47  .startIf(!Strings.isBlank(entityPath))
48  .and().equal("entityPath", entityPath)
49  .endIf()
50  .startIf(since != null)
51  .and().greaterOrEqual("dateLog", since)
52  .endIf()
53  .dao(getDao())
54  .singleResult(Long.class))
55  .orElse(0L);
56  }
57 
58  public void cleanup(String entityPath) {
59  SqlClause.delete("SystemLog")
60  .where().group()
61  .equal("entityPath", entityPath)
62  .or().startsWith("entityPath", entityPath + "/")
63  .endGroup()
64  .and().notIn("comment", List.of("cleanup", "deleted")) // keep cleanups
65  .dao(getDao())
66  .execute();
67  }
68 
69  /* Utils */
70 
71  public static Set<String> getAllPaths(String root) {
72  try(Stream<String> stream = SqlClause.select("distinct entityPath").from("SystemLog")
73  .where().startsWith("entityPath", "/" + root + "/")
74  .and().notIn("comment", List.of("cleanup", "deleted")) // keep cleanups
75  .dao(new ElephantPU())
76  .stream(String.class)) {
77  return new HashSet<>(stream.map(path -> Path.pathFrom(path).getTill(2)).toList());
78  }
79  }
80 
81  /* Search */
82 
83  public SystemLog firstEntry(String entityPath, String comment) {
84  return SqlClause.select("l").from("SystemLog l")
85  .where().equal("l.comment", comment)
86  .and().equal("l.entityPath", entityPath)
87  .and("l.dateLog =").sub(
88  SqlClause.select("min(l2.dateLog)").from("SystemLog l2")
89  .where("l2.comment = l.comment")
90  .and("l2.entityPath = l.entityPath")
91  .groupBy("l2.comment, l2.entityPath"))
92  .dao(getDao())
93  .singleResult(SystemLog.class);
94  }
95 
96  public SystemLog lastEntry(String entityPath, String comment) {
97  return SqlClause.select("l").from("SystemLog l")
98  .where().equal("l.comment", comment)
99  .and().equal("l.entityPath", entityPath)
100  .and("l.dateLog =").sub(
101  SqlClause.select("max(l2.dateLog)").from("SystemLog l2")
102  .where("l2.comment = l.comment")
103  .and("l2.entityPath = l.entityPath")
104  .groupBy("l2.comment, l2.entityPath"))
105  .dao(getDao())
106  .singleResult(SystemLog.class);
107  }
108 
109  /* Dao */
110 
111  private Dao _dao;
112 
113  private Dao getDao() {
114  if(_dao == null) {
115  _dao = new ElephantPU();
116  }
117  return _dao;
118  }
119 
120 }
void cleanup(String entityPath)
long getCountOf(String comment, String entityPath, Date since)
SystemLog lastEntry(String entityPath, String comment)
static Set< String > getAllPaths(String root)
SystemLog firstEntry(String entityPath, String comment)