BrightSide Workbench Full Report + Source Code
OffersVM.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 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.dossier.offer;
20 
21 import java.util.Collections;
22 import java.util.Date;
23 import java.util.List;
24 import org.turro.action.Contacts;
25 import org.turro.action.MailSenders;
26 import org.turro.action.PushSenders;
27 import org.turro.auth.Authentication;
28 import org.turro.dossier.db.DossierPU;
29 import org.turro.dossier.entity.Dossier;
30 import org.turro.dossier.entity.DossierOffer;
31 import org.turro.elephant.util.Messages;
32 import org.turro.entities.Entities;
33 import org.turro.entities.IElephantEntity;
34 import org.turro.file.Document;
35 import org.turro.file.FileWrapper;
36 import org.turro.file.Folder;
37 import org.turro.i18n.I_;
38 import org.turro.jpa.Dao;
39 import org.turro.plugin.contacts.IContact;
40 import org.turro.sql.SqlClause;
41 import org.turro.string.Strings;
42 import org.turro.upload.Medias;
43 import org.turro.visual.DossierVisualElement;
44 import org.turro.visual.DossierVisualElements;
45 import org.turro.zkoss.dialog.DialogField;
46 import org.turro.zkoss.dialog.Dialogs;
47 import org.zkoss.bind.BindUtils;
48 import org.zkoss.bind.annotation.BindingParam;
49 import org.zkoss.bind.annotation.Command;
50 import org.zkoss.bind.annotation.ExecutionArgParam;
51 import org.zkoss.bind.annotation.Init;
52 import org.zkoss.bind.annotation.NotifyChange;
53 import org.zkoss.util.media.Media;
54 import org.zkoss.zk.ui.Executions;
55 
60 public class OffersVM {
61 
62  private Dossier dossier;
63  private DossierVisualElement visual;
64  private IContact contact;
65  private List<DossierOffer> model;
66 
67  @Init
68  public void init(@ExecutionArgParam("dossierId") String dossierId) {
69  if(Strings.isBlank(dossierId)) {
70  dossierId = Executions.getCurrent().getParameter("dossierId");
71  }
72  this.dossier = getDao().find(Dossier.class, Long.valueOf(dossierId));
73  this.visual = DossierVisualElements.load().getElement("Offers");
74  }
75 
76  @NotifyChange("model")
77  @Command("upload")
78  public void upload(@BindingParam("offer") DossierOffer offer, @BindingParam("medias") Media medias[]) {
79  if(canUpload(offer)) {
80  Folder folder = offer.getFolder();
81  for(Media media : medias) {
82  Medias.toFolder(media, folder);
83  }
84  IElephantEntity iee = Entities.getController(dossier);
86  .addContact(offer.getDossier().getDefaultResponsible())
87  .addContact(offer.getBidder())
88  .send(I_.get("Offer"), I_.get("New documents"), iee.getEntityUrl());
89  MailSenders.getPool().setRoot("/offers")
90  .addContact(offer.getDossier().getDefaultResponsible())
91  .addContact(offer.getBidder())
92  .silentSendTemplate(iee, "newdocs", I_.get("New documents"));
93  }
94  }
95 
96  @Command("download")
97  public void download(@BindingParam("document") Document document) {
98  new FileWrapper(document.document()).download();
99  }
100 
101  @Command("deleteDoc")
102  public void deleteDoc(@BindingParam("document") Document document) {
103  Messages.confirmDeletion().add(document.name()).show(() -> {
104  document.delete();
105  BindUtils.postNotifyChange(null, null, OffersVM.this, "model");
106  });
107  }
108 
109  @Command("delete")
110  public void delete(@BindingParam("offer") DossierOffer offer) {
111  Messages.confirmDeletion().add(offer.getDescription()).show(() -> {
112  offer.getFolder().remove();
113  getDao().deleteObject(offer);
114  BindUtils.postNotifyChange(null, null, OffersVM.this, "model");
115  });
116  }
117 
118  @Command("create")
119  public void create() {
120  if(!canCreate) return;
121  Dialogs.title(I_.get("New offer"))
122  .width("800px")
123  .height("220px")
124  .addField(DialogField.field("Description").lines(5).maxLength(1000))
125  .onOk((dialogs) -> {
126  String description = dialogs.getValue("Description", String.class);
127  if(!Strings.isBlank(description)) {
128  DossierOffer offer = new DossierOffer();
129  offer.setDossier(dossier);
130  offer.setCreation(new Date());
131  offer.setDescription(description);
132  offer.setBidderId(contact.getId());
133  offer = getDao().saveEntity(offer);
134  BindUtils.postNotifyChange(null, null, OffersVM.this, "model");
135  IElephantEntity iee = Entities.getController(dossier);
136  PushSenders.getPool()
137  .addContact(offer.getDossier().getDefaultResponsible())
138  .addContact(offer.getBidder())
139  .send(I_.get("Offer"), I_.get("New offer"), iee.getEntityUrl());
140  MailSenders.getPool().setRoot("/offers")
141  .addContact(offer.getDossier().getDefaultResponsible())
142  .addContact(offer.getBidder())
143  .silentSendTemplate(iee, "new", I_.get("New offer"));
144  }
145  })
146  .emptyCancel()
147  .show();
148  }
149 
150  @NotifyChange("model")
151  @Command("accept")
152  public void accept(@BindingParam("offer") DossierOffer offer) {
153  Messages.confirmAcceptation().add(offer.getDescription()).paragraph()
154  .add(I_.get("This action will accept the offer and send a notification.")).show(() -> {
155  offer.setAccepted(true);
156  offer.setDeclined(false);
157  getDao().saveObject(offer);
158  IElephantEntity iee = Entities.getController(dossier);
160  .addContact(offer.getDossier().getDefaultResponsible())
161  .addContact(offer.getBidder())
162  .send(I_.get("Offer"), I_.get("Accepted"), iee.getEntityUrl());
163  MailSenders.getPool().setRoot("/offers")
164  .addContact(offer.getDossier().getDefaultResponsible())
165  .addContact(offer.getBidder())
166  .silentSendTemplate(iee, "accepted", I_.get("Accepted"));
167  });
168  }
169 
170  @NotifyChange("model")
171  @Command("decline")
172  public void decline(@BindingParam("offer") DossierOffer offer) {
173  Messages.confirmProcess().add(offer.getDescription()).paragraph()
174  .add(I_.get("This action will decline the offer and send a notification.")).show(() -> {
175  offer.setAccepted(false);
176  offer.setDeclined(true);
177  getDao().saveObject(offer);
178  IElephantEntity iee = Entities.getController(dossier);
180  .addContact(offer.getDossier().getDefaultResponsible())
181  .addContact(offer.getBidder())
182  .send(I_.get("Offer"), I_.get("Declined"), iee.getEntityUrl());
183  MailSenders.getPool().setRoot("/offers")
184  .addContact(offer.getDossier().getDefaultResponsible())
185  .addContact(offer.getBidder())
186  .silentSendTemplate(iee, "declined", I_.get("Declined"));
187  });
188  }
189 
190  public List<DossierOffer> getModel() {
191  if(prepareValues()) {
192  model = SqlClause.select("p")
193  .from("DossierOffer p")
194  .where().equal("p.dossier", dossier)
195  .startIf(!canAll)
196  .and().equal("p.bidderId", contact.getId())
197  .endIf()
198  .dao(getDao())
199  .resultList(DossierOffer.class);
200  if(!model.isEmpty()) canCreate = false;
201  return model;
202  }
203  return Collections.EMPTY_LIST;
204  }
205 
206  /* Roles */
207 
208  public boolean canUpload(DossierOffer offer) {
209  return canAll ||
210  (canAddWorking && !(offer.isAccepted() || offer.isDeclined())) ||
211  (canAddFinal && offer.isAccepted());
212  }
213 
214  public boolean canDelete(DossierOffer offer) {
215  return canAll || !(offer.isAccepted() || offer.isDeclined());
216  }
217 
218  public boolean canCreate() {
219  return canCreate;
220  }
221 
222  public boolean canAccept(DossierOffer offer) {
223  return canAll && !(offer.isAccepted() || offer.isDeclined());
224  }
225 
226  private boolean canAll, canCreate, canShow, canAddWorking, canAddFinal;
227 
228  private boolean prepareValues() {
229  contact = Authentication.getIContact();
230  if(dossier != null && visual != null && Contacts.isValid(contact)) {
231  canAll = contact.isAdmin();
232  canCreate = visual.canCreateOffer(dossier, contact);
233  canShow = visual.canShowOffer(dossier, contact);
234  canAddWorking = visual.canAddWorkingOffer(dossier, contact);
235  canAddFinal = visual.canAddFinalOffer(dossier, contact);
236  return canAll || canShow || canCreate;
237  }
238  return false;
239  }
240 
241  /* Dao */
242 
243  private Dao _dao;
244 
245  private Dao getDao() {
246  if(_dao == null) {
247  _dao = new DossierPU();
248  }
249  return _dao;
250  }
251 
252 }
static boolean isValid(IContact contact)
Definition: Contacts.java:52
static IMailSender getPool()
static IPushSender getPool()
void deleteDoc(@BindingParam("document") Document document)
Definition: OffersVM.java:102
boolean canDelete(DossierOffer offer)
Definition: OffersVM.java:214
void upload(@BindingParam("offer") DossierOffer offer, @BindingParam("medias") Media medias[])
Definition: OffersVM.java:78
void accept(@BindingParam("offer") DossierOffer offer)
Definition: OffersVM.java:152
boolean canAccept(DossierOffer offer)
Definition: OffersVM.java:222
List< DossierOffer > getModel()
Definition: OffersVM.java:190
boolean canUpload(DossierOffer offer)
Definition: OffersVM.java:208
void decline(@BindingParam("offer") DossierOffer offer)
Definition: OffersVM.java:172
void init(@ExecutionArgParam("dossierId") String dossierId)
Definition: OffersVM.java:68
void delete(@BindingParam("offer") DossierOffer offer)
Definition: OffersVM.java:110
void download(@BindingParam("document") Document document)
Definition: OffersVM.java:97
static Messages confirmDeletion()
Definition: Messages.java:87
static Messages confirmProcess()
Definition: Messages.java:95
static Messages confirmAcceptation()
Definition: Messages.java:103
Messages add(String word)
Definition: Messages.java:50
static IElephantEntity getController(String path)
Definition: Entities.java:78
static String get(String msg)
Definition: I_.java:41
void deleteObject(Object obj)
Definition: Dao.java:162
static void toFolder(Media media, Folder folder)
Definition: Medias.java:50
boolean canCreateOffer(Dossier dossier, IContact contact)
boolean canShowOffer(Issue issue, IContact contact)
boolean canAddFinalOffer(Dossier dossier, IContact contact)
boolean canAddWorkingOffer(Dossier dossier, IContact contact)
DialogField lines(int lines)
static DialogField field(String label)
Dialogs width(String width)
Definition: Dialogs.java:70
Dialogs height(String height)
Definition: Dialogs.java:75
Dialogs onOk(Consumer< Dialogs > onOk)
Definition: Dialogs.java:85
static Dialogs title(String title)
Definition: Dialogs.java:161
Dialogs addField(DialogField field)
Definition: Dialogs.java:110
T addContact(IContact contact)
IMailSender setRoot(String root)