BrightSide Workbench Full Report + Source Code
AcceptanceUtil.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.acceptance;
19 
20 import java.util.Date;
21 import java.util.List;
22 import javax.persistence.NoResultException;
23 import org.turro.action.AcceptanceStatus;
24 import org.turro.contacts.Acceptance;
25 import org.turro.contacts.Contact;
26 import org.turro.contacts.db.ContactsPU;
27 import org.turro.jpa.Dao;
28 import org.turro.plugin.contacts.IContact;
29 
34 public class AcceptanceUtil {
35 
36  public static boolean isAccepted(String path) {
37  List<Acceptance> list = getAcceptances(path);
38  if(list.isEmpty()) return false;
39  for(Acceptance a : list) {
40  if(a.getDateAcceptance() == null) {
41  return false;
42  }
43  }
44  return true;
45  }
46 
47  public static boolean isBlocked(String path) {
48  List<Acceptance> list = getAcceptances(path);
49  return !list.isEmpty();
50  }
51 
52  public static AcceptanceStatus getStatusFor(String path, IContact contact) {
53  return getStatusFor(path, (Contact) contact.getContact());
54  }
55 
56  public static AcceptanceStatus getStatusFor(String path, Contact contact) {
57  List<Acceptance> list = getAcceptances(path);
58  if(list.isEmpty()) {
60  }
61  boolean accepted = false, pending = false;
62  AcceptanceStatus selfStatus = null;
63  for(Acceptance a : list) {
64  if(a.getDateAcceptance() != null) {
65  accepted = true;
66  if(a.getContact().getId().equals(contact.getId())) {
67  selfStatus = AcceptanceStatus.SELF_ACCEPTED;
68  }
69  } else {
70  pending = true;
71  if(a.getContact().getId().equals(contact.getId())) {
72  selfStatus = AcceptanceStatus.SELF_PENDING;
73  }
74  }
75  }
76  if(accepted) {
77  if(pending) {
78  return selfStatus == null ? AcceptanceStatus.SOME_PENDING : selfStatus;
79  } else {
81  }
82  } else {
83  return selfStatus == null ? AcceptanceStatus.FULLY_PENDING : selfStatus;
84  }
85  }
86 
87  public static List<Acceptance> getAcceptances(String path) {
88  Dao dao = new ContactsPU();
89  return dao.getResultList(
90  "select a from Acceptance a " +
91  "where a.path = ?",
92  new Object[] {
93  path
94  });
95  }
96 
97  public static List<Acceptance> getPendingAcceptances(IContact contact) {
98  return getPendingAcceptances((Contact) contact.getContact());
99  }
100 
101  public static List<Acceptance> getPendingAcceptances(Contact contact) {
102  Dao dao = new ContactsPU();
103  return dao.getResultList(
104  "select a from Acceptance as a " +
105  "where a.contact = ? " +
106  "and a.dateAcceptance is null",
107  new Object[] {
108  contact
109  });
110  }
111 
112  public static List<Acceptance> getPendingRequests(IContact contact) {
113  return getPendingRequests((Contact) contact.getContact());
114  }
115 
116  public static List<Acceptance> getPendingRequests(Contact contact) {
117  Dao dao = new ContactsPU();
118  return dao.getResultList(
119  "select a from Acceptance as a " +
120  "where a.petitioner = ? " +
121  "and a.dateAcceptance is null",
122  new Object[] {
123  contact
124  });
125  }
126 
127  public static Acceptance accept(Acceptance a) {
128  return accept(a.getPetitioner(), a.getContact(), a.getPath(), null);
129  }
130 
131  public static Acceptance accept(IContact petitioner, IContact contact, String path, String comment) {
132  return accept((Contact) petitioner.getContact(), (Contact) contact.getContact(), path, comment);
133  }
134 
135  public static Acceptance accept(Contact petitioner, Contact contact, String path, String comment) {
136  Dao dao = new ContactsPU();
137  Acceptance a = null;
138  try {
139  a = (Acceptance) dao.getSingleResult(
140  "select a from Acceptance as a " +
141  "where a.contact = ? " +
142  "and a.petitioner = ? " +
143  "and a.path = ? " +
144  "and a.dateAcceptance is null",
145  new Object[] {
146  contact, petitioner, path
147  });
148  if(comment != null) {
149  a.setAcceptanceComment(a.getAcceptanceComment() + "\n" + comment);
150  }
151  a.setDateAcceptance(new Date());
152  a = dao.saveObject(a);
153  } catch(NoResultException ex) {
154  }
155  return a;
156  }
157 
158  public static Acceptance request(IContact petitioner, IContact contact, String path, String comment) {
159  return request((Contact) petitioner.getContact(), (Contact) contact.getContact(), path, comment);
160  }
161 
162  public static Acceptance request(Contact petitioner, Contact contact, String path, String comment) {
163  Dao dao = new ContactsPU();
164  Acceptance a = null;
165  try {
166  a = (Acceptance) dao.getSingleResult(
167  "select a from Acceptance as a " +
168  "where a.contact = ? " +
169  "and a.petitioner = ? " +
170  "and a.path = ?",
171  new Object[] {
172  contact, petitioner, path
173  });
174  if(comment != null) {
175  a.setAcceptanceComment(a.getAcceptanceComment() + "\n" + comment);
176  }
177  a = dao.saveObject(a);
178  } catch(NoResultException ex) {
179  a = new Acceptance();
180  a.setAcceptanceComment(comment);
181  a.setPetitioner(petitioner);
182  a.setContact(contact);
183  a.setDateRequest(new Date());
184  a.setPath(path);
185  a = dao.saveObject(a);
186  }
187  return a;
188  }
189 
190  public static void delete(IContact contact, String path) {
191  delete((Contact) contact.getContact(), path);
192  }
193 
194  public static void delete(Contact contact, String path) {
195  Dao dao = new ContactsPU();
196  dao.executeUpdate(
197  "delete from Acceptance " +
198  "where contact = ? " +
199  "and path = ?",
200  new Object[] {
201  contact, path
202  });
203  }
204 
205  private AcceptanceUtil() {
206  }
207 
208 }
static List< Acceptance > getPendingRequests(Contact contact)
static boolean isBlocked(String path)
static boolean isAccepted(String path)
static Acceptance accept(Contact petitioner, Contact contact, String path, String comment)
static Acceptance request(IContact petitioner, IContact contact, String path, String comment)
static List< Acceptance > getPendingRequests(IContact contact)
static List< Acceptance > getPendingAcceptances(IContact contact)
static Acceptance accept(Acceptance a)
static AcceptanceStatus getStatusFor(String path, IContact contact)
static Acceptance accept(IContact petitioner, IContact contact, String path, String comment)
static List< Acceptance > getAcceptances(String path)
static List< Acceptance > getPendingAcceptances(Contact contact)
static AcceptanceStatus getStatusFor(String path, Contact contact)
static Acceptance request(Contact petitioner, Contact contact, String path, String comment)
void setDateAcceptance(Date dateAcceptance)
Definition: Acceptance.java:87
void setAcceptanceComment(String acceptanceComment)
Definition: Acceptance.java:69
int executeUpdate(String query)
Definition: Dao.java:463
Object getSingleResult(WhereClause wc)
Definition: Dao.java:380