BrightSide Workbench Full Report + Source Code
Acceptances.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 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.acceptance;
20 
21 import java.util.Date;
22 import java.util.List;
23 import javax.persistence.NoResultException;
24 import org.turro.action.AcceptanceSingleStatus;
25 import org.turro.action.AcceptanceStatus;
26 import org.turro.action.IAcceptance;
27 import org.turro.action.IAcceptances;
28 import org.turro.annotation.ElephantPlugin;
29 import org.turro.contacts.Acceptance;
30 import org.turro.contacts.db.ContactsPU;
31 import org.turro.jpa.Dao;
32 import org.turro.plugin.contacts.IContact;
33 
38 @ElephantPlugin
39 public class Acceptances implements IAcceptances {
40 
41  @Override
42  public boolean isAccepted(String path) {
43  List<IAcceptance> list = getAcceptances(path);
44  if(list.isEmpty()) return false;
45  for(IAcceptance a : list) {
46  if(a.getDateAcceptance() == null) {
47  return false;
48  }
49  }
50  return true;
51  }
52 
53  @Override
54  public boolean isBlocked(String path) {
55  List<IAcceptance> list = getAcceptances(path);
56  return !list.isEmpty();
57  }
58 
59  @Override
60  public AcceptanceStatus getStatusFor(String path) {
61  return getStatusFor(path, null);
62  }
63 
64  @Override
65  public AcceptanceStatus getStatusFor(String path, IContact contact) {
66  List<IAcceptance> list = getAcceptances(path);
67  if(list.isEmpty()) {
69  }
70  boolean accepted = false, pending = false;
71  AcceptanceStatus selfStatus = null;
72  for(IAcceptance a : list) {
73  if(a.getDateAcceptance() != null) {
74  accepted = true;
75  if(contact != null && a.getIContact().getId().equals(contact.getId())) {
76  selfStatus = AcceptanceStatus.SELF_ACCEPTED;
77  }
78  } else {
79  pending = true;
80  if(contact != null && a.getIContact().getId().equals(contact.getId())) {
81  selfStatus = AcceptanceStatus.SELF_PENDING;
82  }
83  }
84  }
85  if(accepted) {
86  if(pending) {
87  return selfStatus == null ? AcceptanceStatus.SOME_PENDING : selfStatus;
88  } else {
90  }
91  } else {
92  return selfStatus == null ? AcceptanceStatus.FULLY_PENDING : selfStatus;
93  }
94  }
95 
96  @Override
97  public AcceptanceSingleStatus getStatusFor(String path, IContact petitioner, IContact contact) {
98  IAcceptance acceptance = getAcceptanceFor(path, petitioner, contact);
99  if(acceptance != null) {
100  if(acceptance.getDateAcceptance() != null) {
102  } else {
104  }
105  } else {
107  }
108  }
109 
110  @Override
111  public List<IAcceptance> getAcceptances(String path) {
112  return getDao().getResultList(
113  "select a from Acceptance a " +
114  "where a.path = ?",
115  new Object[] {
116  path
117  });
118  }
119 
120  @Override
121  public List<IAcceptance> getPendingAcceptances(IContact contact) {
122  return getDao().getResultList(
123  "select a from Acceptance as a " +
124  "where a.contact = ? " +
125  "and a.dateAcceptance is null",
126  new Object[] {
127  contact.getContact()
128  });
129  }
130 
131  @Override
132  public List<IAcceptance> getPendingRequests(IContact contact) {
133  return getDao().getResultList(
134  "select a from Acceptance as a " +
135  "where a.petitioner = ? " +
136  "and a.dateAcceptance is null",
137  new Object[] {
138  contact.getContact()
139  });
140  }
141 
142  @Override
144  return accept(a.getIPetitioner(), a.getIContact(), a.getPath(), null);
145  }
146 
147  @Override
148  public IAcceptance accept(IContact petitioner, IContact contact, String path, String comment) {
149  Dao dao = getDao();
150  IAcceptance a = null;
151  try {
152  a = (IAcceptance) dao.getSingleResult(
153  "select a from Acceptance as a " +
154  "where a.contact = ? " +
155  "and a.petitioner = ? " +
156  "and a.path = ? " +
157  "and a.dateAcceptance is null",
158  new Object[] {
159  contact.getContact(), petitioner.getContact(), path
160  });
161  if(comment != null) {
162  a.setAcceptanceComment(a.getAcceptanceComment() + "\n" + comment);
163  }
164  a.setDateAcceptance(new Date());
165  a = dao.saveObject(a);
166  } catch(NoResultException ex) {
167  }
168  return a;
169  }
170 
171  @Override
172  public IAcceptance request(IContact petitioner, IContact contact, String path, String comment) {
173  Dao dao = getDao();
174  IAcceptance a = null;
175  try {
176  a = (IAcceptance) dao.getSingleResult(
177  "select a from Acceptance as a " +
178  "where a.contact = ? " +
179  "and a.petitioner = ? " +
180  "and a.path = ?",
181  new Object[] {
182  contact.getContact(), petitioner.getContact(), path
183  });
184  if(comment != null) {
185  a.setAcceptanceComment(a.getAcceptanceComment() + "\n" + comment);
186  }
187  a = dao.saveObject(a);
188  } catch(NoResultException ex) {
189  a = new Acceptance();
190  a.setAcceptanceComment(comment);
191  a.setIPetitioner(petitioner);
192  a.setIContact(contact);
193  a.setDateRequest(new Date());
194  a.setPath(path);
195  a = dao.saveObject(a);
196  }
197  return a;
198  }
199 
200  @Override
201  public void delete(IContact contact, String path) {
202  getDao().executeUpdate(
203  "delete from Acceptance " +
204  "where contact = ? " +
205  "and path = ?",
206  new Object[] {
207  contact.getContact(), path
208  });
209  }
210 
211  @Override
212  public IAcceptance getAcceptanceFor(String path, IContact petitioner, IContact contact) {
213  return (IAcceptance) getDao().getSingleResultOrNull(
214  "select a from Acceptance as a " +
215  "where a.contact = ? " +
216  "and a.petitioner = ? " +
217  "and a.path = ?",
218  new Object[] {
219  contact.getContact(), petitioner.getContact(), path
220  });
221  }
222 
223  @Override
224  public String createPetition(String path, IContact contact, String link) {
225  return createPetition(path, contact, link, null);
226  }
227 
228  @Override
229  public String createPetition(String path, IContact contact, String link, String template) {
230  return PetitionCtrl.createPetition(path, contact, link, template);
231  }
232 
233  private Dao _dao;
234 
235  private Dao getDao() {
236  if(_dao == null) {
237  _dao = new ContactsPU();
238  }
239  return _dao;
240  }
241 
242 }
String createPetition(String path, IContact contact, String link)
IAcceptance request(IContact petitioner, IContact contact, String path, String comment)
List< IAcceptance > getPendingRequests(IContact contact)
List< IAcceptance > getAcceptances(String path)
IAcceptance accept(IContact petitioner, IContact contact, String path, String comment)
String createPetition(String path, IContact contact, String link, String template)
boolean isBlocked(String path)
IAcceptance getAcceptanceFor(String path, IContact petitioner, IContact contact)
AcceptanceStatus getStatusFor(String path, IContact contact)
IAcceptance accept(IAcceptance a)
List< IAcceptance > getPendingAcceptances(IContact contact)
boolean isAccepted(String path)
AcceptanceSingleStatus getStatusFor(String path, IContact petitioner, IContact contact)
AcceptanceStatus getStatusFor(String path)
static String createPetition(String path, IContact contact, String link)
Object getSingleResult(WhereClause wc)
Definition: Dao.java:380
void setAcceptanceComment(String acceptanceComment)
void setDateAcceptance(Date dateAcceptance)