BrightSide Workbench Full Report + Source Code
CategoryRequestsVM.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2019 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.model;
20 
21 import java.util.Date;
22 import java.util.List;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import org.turro.collections.parser.ParserException;
26 import org.turro.action.Contacts;
27 import org.turro.auth.Authentication;
28 import org.turro.collections.KeyValueMap;
29 import org.turro.dossier.db.DossierPU;
30 import org.turro.dossier.dossier.ProjectContext;
31 import org.turro.dossier.entity.CategoryRequest;
32 import org.turro.elephant.context.ElephantContext;
33 import org.turro.elephant.db.WhereClause;
34 import org.turro.i18n.I_;
35 import org.turro.jpa.Dao;
36 import org.turro.plugin.contacts.IContact;
37 import org.zkoss.bind.annotation.BindingParam;
38 import org.zkoss.bind.annotation.Command;
39 import org.zkoss.bind.annotation.ExecutionArgParam;
40 import org.zkoss.bind.annotation.Init;
41 import org.zkoss.bind.annotation.NotifyChange;
42 import org.zkoss.lang.Strings;
43 
48 public class CategoryRequestsVM {
49 
50  private IContact contact = Authentication.getIContact();
51  private String contactName, contactMail;
52  private CategoryModel catModel;
53 
54  @Init
55  public void init(@ExecutionArgParam("contact") IContact contact,
56  @ExecutionArgParam("idCats") String idCats) {
57  if(contact != null) this.contact = contact;
58  }
59 
60  @Command
61  @NotifyChange("*")
62  public void add() {
63  if(!Strings.isBlank(contactName) && !Strings.isBlank(contactMail)) {
64  Date now = new Date();
66  if(cw.isSelected()) {
68  cr.setIContact(contact);
69  cr.setCreation(now);
70  cr.setContactName(contactName);
71  cr.setContactEmail(contactMail);
72  cr.setCategory(cw.getCategory());
73  getDao().saveObject(cr);
74  }
75  }
76  setContactName("");
77  setContactMail("");
78  }
79  }
80 
81  @Command
82  @NotifyChange("*")
83  public void remove(@BindingParam("entity") Object entity) {
84  if(entity instanceof CategoryRequest) {
85  getDao().deleteObject(entity);
86  }
87  }
88 
89  @Command
90  @NotifyChange("*")
91  public void createContact(@BindingParam("entity") Object entity) {
92  if(entity instanceof CategoryRequest) {
93  try {
94  KeyValueMap kvm = new KeyValueMap("");
95  kvm.put("name", ((CategoryRequest) entity).getContactName());
96  kvm.put("email", ((CategoryRequest) entity).getContactEmail());
98  } catch (ParserException ex) {
99  Logger.getLogger(CategoryRequestsVM.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
100  }
101  }
102  }
103 
104  public List<CategoryRequest> getModel() {
105  WhereClause wc = new WhereClause();
106  wc.addClause("select cr from CategoryRequest as cr");
107  wc.addClause("where cr.idContact = :contact");
108  wc.addNamedValue("contact", contact.getId());
109  return getDao().getResultList(wc);
110  }
111 
112  public List<CategoryRequest> getFullModel() {
113  WhereClause wc = new WhereClause();
114  wc.addClause("select cr from CategoryRequest as cr");
115  return getDao().getResultList(wc);
116  }
117 
118  public List<CategorySelectionWrapper> getCategoryModel() {
119  if(catModel == null) {
120  WhereClause wc = new WhereClause();
121  wc.addClause("select c from Category as c");
122  wc.addClause("where c.parent in (" + ProjectContext.getProjectContextAttribute("catsRequest", "0") + ")");
123  catModel = new CategoryModel(getDao().getResultList(wc));
124  }
125  return catModel;
126  }
127 
128  public String getLabel(String label) {
129  return I_.get(label);
130  }
131 
132  public String getContactName() {
133  return contactName;
134  }
135 
136  public void setContactName(String contactName) {
137  this.contactName = contactName;
138  }
139 
140  public String getContactMail() {
141  return contactMail;
142  }
143 
144  public void setContactMail(String contactMail) {
145  this.contactMail = contactMail;
146  }
147 
148  /* Dao */
149 
150  private Dao _dao;
151 
152  private Dao getDao() {
153  if(_dao == null) {
154  _dao = new DossierPU();
155  }
156  return _dao;
157  }
158 
159 }
static void startContactFromValues(KeyValueMap values)
Definition: Contacts.java:163
static String getProjectContextAttribute(String attribute, String defaultValue)
void setContactEmail(String contactEmail)
List< CategorySelectionWrapper > getCategoryModel()
void init(@ExecutionArgParam("contact") IContact contact, @ExecutionArgParam("idCats") String idCats)
void createContact(@BindingParam("entity") Object entity)
void remove(@BindingParam("entity") Object entity)
void addNamedValue(String name, Object value)
static String get(String msg)
Definition: I_.java:41
void deleteObject(Object obj)
Definition: Dao.java:162