BrightSide Workbench Full Report + Source Code
LogLoader.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 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.contacts.log;
20 
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Date;
24 import java.util.List;
25 import org.turro.string.Strings;
26 import org.turro.annotation.ElephantPlugin;
27 import org.turro.contacts.db.ContactsPU;
28 import org.turro.elephant.db.WhereClause;
29 import org.turro.elephant.log.LogType;
30 import org.turro.jpa.Dao;
31 import org.turro.plugin.log.ILogLoader;
32 import org.turro.plugin.log.ILogRegister;
33 import org.turro.plugin.log.LogUtil;
34 import org.turro.util.PhraseBuilder;
35 
40 @ElephantPlugin
41 public class LogLoader implements ILogLoader {
42 
43  @Override
44  public Collection<ILogRegister> load(LogType type, String pathOrigin, int group, String comment) {
45  Dao dao = new ContactsPU();
46  List<Object[]> list = dao.getResultList(createCriteria(type, pathOrigin, group, comment));
47  ArrayList<ILogRegister> result = new ArrayList<ILogRegister>();
48  for(Object o[] : list) {
49  String path = null, name = null, actualComment = null;
50  Date date = null;
51  int count = 0, field = 0;
52  if(group == LogUtil.GROUP_ENTITY) {
53  path = (String) o[field++];
54  } else if(group == LogUtil.GROUP_USER) {
55  name = (String) o[field++];
56  }
57  if(Strings.isBlank(comment)) {
58  actualComment = (String) o[field++];
59  }
60  count = ((Long) o[field++]).intValue();
61  date = (Date) o[field++];
62  result.add(new LogRegister(type, path, null, name, date, actualComment, null, count));
63  }
64  return result;
65  }
66 
67  private WhereClause createCriteria(LogType type, String pathOrigin, int group, String comment) {
68  WhereClause wc = new WhereClause();
69  wc.addClause("select");
70  // Select
71  PhraseBuilder pb = new PhraseBuilder();
72  if(group == LogUtil.GROUP_ENTITY) {
73  pb.addWord("ce.path");
74  pb.addPendingSeparator(",");
75  } else if(group == LogUtil.GROUP_USER) {
76  //pb.addWord("ce.idUser");
77  //pb.addPendingSeparator(",");
78  pb.addWord("trim(ce.name)");
79  pb.addPendingSeparator(",");
80  }
81  if(Strings.isBlank(comment)) {
82  pb.addWord("ce.comment");
83  pb.addPendingSeparator(",");
84  }
85  pb.addWord("count(ce.id)");
86  pb.addPendingSeparator(",");
87  pb.addWord("max(ce.dateLog)");
88  pb.addPendingSeparator(",");
89  wc.addClause(pb.toString());
90  // From
91  wc.addClause("from LogEntry as ce where 1=1");
92  // Where
93  if(type != null) {
94  wc.addClause("and ce.logType = :type");
95  wc.addNamedValue("type", type);
96  }
97  if(!Strings.isBlank(pathOrigin)) {
98  wc.addClause("and ce.path like '" + pathOrigin + "%'");
99  }
100  if(!Strings.isBlank(comment)) {
101  wc.addClause("and ce.comment = :comment");
102  wc.addNamedValue("comment", comment);
103  }
104  // Group by
105  if(group != LogUtil.GROUP_NONE) {
106  pb = new PhraseBuilder();
107  pb.addWord("group by");
108  if(group == LogUtil.GROUP_ENTITY) {
109  pb.addWord("ce.path");
110  pb.addPendingSeparator(",");
111  } else if(group == LogUtil.GROUP_USER) {
112  //pb.addWord("ce.idUser");
113  //pb.addPendingSeparator(",");
114  pb.addWord("trim(ce.name)");
115  pb.addPendingSeparator(",");
116  }
117  if(Strings.isBlank(comment)) {
118  pb.addWord("ce.comment");
119  pb.addPendingSeparator(",");
120  }
121  wc.addClause(pb.toString());
122  }
123  return wc;
124  }
125 
126  @Override
127  public Collection<ILogRegister> loadLogin() {
128  Dao dao = new ContactsPU();
129  List<Object[]> list = dao.getResultList(createLoginCriteria());
130  ArrayList<ILogRegister> result = new ArrayList<ILogRegister>();
131  for(Object o[] : list) {
132  String path, name;
133  Date date;
134  int count = 0, field = 0;
135  path = (String) o[field++];
136  name = (String) o[field++];
137  count = ((Long) o[field++]).intValue();
138  date = (Date) o[field++];
139  result.add(new LogRegister(null, null, null, name, date, path, null, count));
140  }
141  return result;
142  }
143 
144  private WhereClause createLoginCriteria() {
145  WhereClause wc = new WhereClause();
146  wc.addClause("select");
147  // Select
148  PhraseBuilder pb = new PhraseBuilder();
149  pb.addWord("ce.path");
150  pb.addPendingSeparator(",");
151  pb.addWord("trim(ce.name)");
152  pb.addPendingSeparator(",");
153  pb.addWord("count(ce.id)");
154  pb.addPendingSeparator(",");
155  pb.addWord("max(ce.dateLog)");
156  pb.addPendingSeparator(",");
157  wc.addClause(pb.toString());
158  // From
159  wc.addClause("from LogEntry as ce where 1=1");
160  // Where
161  wc.addClause("and ce.path like '/log/%'");
162  // Group by
163  pb = new PhraseBuilder();
164  pb.addWord("group by");
165  pb.addWord("ce.path");
166  pb.addPendingSeparator(",");
167  pb.addWord("trim(ce.name)");
168  pb.addPendingSeparator(",");
169  wc.addClause(pb.toString());
170  return wc;
171  }
172 
173 }
Collection< ILogRegister > loadLogin()
Definition: LogLoader.java:127
Collection< ILogRegister > load(LogType type, String pathOrigin, int group, String comment)
Definition: LogLoader.java:44
void addNamedValue(String name, Object value)