BrightSide Workbench Full Report + Source Code
MostUsed.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.contacts.util;
19 
20 import org.turro.contacts.db.ContactsPU;
21 import org.turro.elephant.db.SQLHelper;
22 import org.turro.elephant.db.WhereClause;
23 import org.turro.jpa.Dao;
24 
29 public class MostUsed {
30 
31  public static String getConnector(String choices, String defaultChoice) {
32  Dao dao = new ContactsPU();
33  WhereClause wc = new WhereClause();
34  wc.addClause("select connector.description, count(connector.description)");
35  wc.addClause("from Connector connector");
36  wc.addClause("where connector.description in (" + SQLHelper.quoteTokensForIn(choices) + ")");
37  wc.addClause("group by connector.description");
38  wc.addClause("order by count(connector.description) DESC");
39  Object o[] = (Object[]) dao.getSingleResult(wc);
40  return (String) ((o == null || o.length == 0 || o[0] == null) ? defaultChoice : o[0]);
41  }
42 
43  public static String getAddress(String choices, String defaultChoice) {
44  Dao dao = new ContactsPU();
45  WhereClause wc = new WhereClause();
46  wc.addClause("select address.description, count(address.description)");
47  wc.addClause("from Address address");
48  wc.addClause("where address.description in (" + SQLHelper.quoteTokensForIn(choices) + ")");
49  wc.addClause("group by address.description");
50  wc.addClause("order by count(address.description) DESC");
51  Object o[] = (Object[]) dao.getSingleResult(wc);
52  return (String) ((o == null || o.length == 0 || o[0] == null) ? defaultChoice : o[0]);
53  }
54 
55  private MostUsed() {
56  }
57 
58 }
static String getConnector(String choices, String defaultChoice)
Definition: MostUsed.java:31
static String getAddress(String choices, String defaultChoice)
Definition: MostUsed.java:43
static String quoteTokensForIn(String inValue)
Definition: SQLHelper.java:55
Object getSingleResult(WhereClause wc)
Definition: Dao.java:380