BrightSide Workbench Full Report + Source Code
UsualUsages.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.usual;
19 
20 import java.util.List;
21 import javax.persistence.NoResultException;
22 import org.turro.string.Strings;
23 import org.turro.contacts.Usually;
24 import org.turro.contacts.db.ContactsPU;
25 import org.turro.jpa.Dao;
26 
31 public class UsualUsages {
32 
33  public static Usually addUsage(String path) {
34  if(!Strings.isBlank(path)) {
35  if(path.length() > 255) {
36  path = path.substring(0, 255);
37  }
38  if(path.endsWith(":null")) {
39  path = path.substring(0, path.length() - 4);
40  }
41  Dao dao = new ContactsPU();
42  Usually u = null;
43  try {
44  u = (Usually) dao.getSingleResult(
45  "select u from Usually as u " +
46  "where u.path = ?",
47  new Object[] {
48  path
49  });
50  u.setUsages(u.getUsages() + 1L);
51  u = dao.saveObject(u);
52  } catch(NoResultException ex) {
53  u = new Usually();
54  u.setPath(path);
55  u.setUsages(1L);
56  u = dao.saveObject(u);
57  }
58  return u;
59  }
60  return null;
61  }
62 
63  public static Usually getUsage(String path) {
64  if(!Strings.isBlank(path)) {
65  if(path.length() > 255) {
66  path = path.substring(0, 255);
67  }
68  Dao dao = new ContactsPU();
69  try {
70  return (Usually) dao.getSingleResult(
71  "select u from Usually as u " +
72  "where u.path = ?",
73  new Object[] {
74  path
75  });
76  } catch(NoResultException ex) {}
77  }
78  return null;
79  }
80 
81  public static long getUsageCount(String path) {
82  Usually u = getUsage(path);
83  return u == null ? 0 : u.getUsages();
84  }
85 
86  public static Usually getMostUsed(String path) {
87  if(!Strings.isBlank(path)) {
88  if(path.length() > 255) {
89  path = path.substring(0, 255);
90  }
91  Dao dao = new ContactsPU();
92  try {
93  List<Usually> l = dao.getResultList(
94  "select u from Usually as u " +
95  "where u.path like ? " +
96  "and u.usages = (" +
97  " select max(u2.usages) from Usually as u2 " +
98  " where u2.path like ? " +
99  ")",
100  new Object[] {
101  path + "%", path + "%"
102  });
103  if(!l.isEmpty()) {
104  return l.get(0);
105  }
106  } catch(NoResultException ex) {}
107  }
108  return null;
109  }
110 
111  public static String getMostUsedvalue(String path) {
112  Usually u = getMostUsed(path);
113  return u == null ? null : u.getPath().substring(path.length());
114  }
115 
116  public static void clearUsuals(String path) {
117  if(!Strings.isBlank(path)) {
118  if(path.length() > 255) {
119  path = path.substring(0, 255);
120  }
121  Dao dao = new ContactsPU();
122  dao.executeUpdate(
123  "delete from Usually " +
124  "where path like ?",
125  new Object[] {
126  path + "%"
127  });
128  }
129  }
130 
131  private UsualUsages() {
132  }
133 
134 }
void setUsages(long usages)
Definition: Usually.java:63
int executeUpdate(String query)
Definition: Dao.java:463
Object getSingleResult(WhereClause wc)
Definition: Dao.java:380
static Usually getUsage(String path)
static Usually addUsage(String path)
static Usually getMostUsed(String path)
static void clearUsuals(String path)
static String getMostUsedvalue(String path)
static long getUsageCount(String path)