BrightSide Workbench Full Report + Source Code
InTouchUtil.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2012 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.intouch;
20 
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Date;
24 import org.turro.string.Strings;
25 import org.turro.contacts.Address;
26 import org.turro.contacts.Contact;
27 import org.turro.contacts.InTouch;
28 import org.turro.contacts.db.ContactsPU;
29 import org.turro.entities.Entities;
30 import org.turro.jpa.Dao;
31 
36 public class InTouchUtil {
37 
38  public static void addInTouch(InTouch inTouch) {
39  if(!inTouch.isEmpty()) {
40  Dao dao = new ContactsPU();
41  dao.saveObject(inTouch);
42  }
43  }
44 
45  public static void addInTouch(Object entity, String name, Date control,
46  Contact contact, Address address, String description) {
47  String path = Entities.getController(entity).getPath();
48  if(path != null) {
49  addInTouch(path, name, control, contact, address, description);
50  }
51  }
52 
53  public static void addInTouch(String path, String name, Date control,
54  Contact contact, Address address, String description) {
55  if(!Strings.isBlank(path)) {
56  removeInTouch(path, name);
57  Dao dao = new ContactsPU();
58  InTouch inTouch = new InTouch();
59  inTouch.setPath(path);
60  inTouch.setName(name);
61  inTouch.setControl(control);
62  inTouch.setContact(contact);
63  inTouch.setAddress(address);
64  inTouch.setDescription(description);
65  dao.saveObject(inTouch);
66  }
67  }
68 
69  public static void removeInTouch(Object entity, String name) {
70  String path = Entities.getController(entity).getPath();
71  if(path != null) {
72  removeInTouch(path, name);
73  }
74  }
75 
76  public static void removeInTouch(String path, String name) {
77  Dao dao = new ContactsPU();
78  dao.executeUpdate(
79  " delete from InTouch " +
80  " where path = ? " +
81  " and name = ?",
82  new Object[] { path, name }
83  );
84  }
85 
86  public static void removeInTouch(InTouch inTouch) {
87  Dao dao = new ContactsPU();
88  dao.deleteObject(inTouch);
89  }
90 
91  public static void removeInTouchs(Object entity) {
92  String path = Entities.getController(entity).getPath();
93  if(path != null) {
94  removeInTouchs(path);
95  }
96  }
97 
98  public static void removeInTouchs(String path) {
99  Dao dao = new ContactsPU();
100  dao.executeUpdate(
101  " delete from InTouch " +
102  " where path = ?",
103  new Object[] { path }
104  );
105  }
106 
107  public static Collection<InTouch> inTouch(Object entity) {
108  String path = Entities.getController(entity).getPath();
109  if(path != null) {
110  return inTouch(path);
111  }
112  return new ArrayList<InTouch>();
113  }
114 
115  public static Collection<InTouch> inTouch(String path) {
116  Dao dao = new ContactsPU();
117  return dao.getResultList(
118  " select it from InTouch as it " +
119  " where it.path = ? " +
120  " order by it.name ",
121  new Object[] { path }
122  );
123  }
124 
125  public static Collection<String> inTouchChoices(Object entity) {
126  String path = Entities.getController(entity).getPath();
127  if(path != null) {
128  return inTouchChoices(path);
129  }
130  return new ArrayList<String>();
131  }
132 
133  public static Collection<String> inTouchChoices(String path) {
134  Dao dao = new ContactsPU();
135  return dao.getResultList(
136  " select distinct name from InTouch " +
137  " where path <> ? " +
138  " order by it.name ",
139  new Object[] { path }
140  );
141  }
142 
143  private InTouchUtil() {
144  }
145 
146 }
static IElephantEntity getController(String path)
Definition: Entities.java:78
static void addInTouch(Object entity, String name, Date control, Contact contact, Address address, String description)
static Collection< InTouch > inTouch(String path)
static void removeInTouchs(String path)
static void removeInTouch(Object entity, String name)
static void removeInTouch(String path, String name)
static void removeInTouchs(Object entity)
static void addInTouch(InTouch inTouch)
static void addInTouch(String path, String name, Date control, Contact contact, Address address, String description)
static Collection< String > inTouchChoices(String path)
static void removeInTouch(InTouch inTouch)
static Collection< String > inTouchChoices(Object entity)
static Collection< InTouch > inTouch(Object entity)
void deleteObject(Object obj)
Definition: Dao.java:162
int executeUpdate(String query)
Definition: Dao.java:463