BrightSide Workbench Full Report + Source Code
AcceptanceListbox.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.zul.acceptance;
19 
20 import java.util.Collection;
21 import java.util.List;
22 import org.turro.acceptance.AcceptanceMail;
23 import org.turro.acceptance.AcceptanceUtil;
24 import org.turro.auth.Authentication;
25 import org.turro.command.Command;
26 import org.turro.command.Context;
27 import org.turro.contacts.Acceptance;
28 import org.turro.contacts.Contact;
29 import org.turro.contacts.db.ContactsPU;
30 import org.turro.contacts.util.ContactCombobox;
31 import org.turro.elephant.context.Application;
32 import org.turro.i18n.I_;
33 import org.turro.jpa.Dao;
34 import org.turro.plugin.contacts.IContact;
35 import org.turro.zkoss.dialog.InputDialog;
36 import org.turro.zkoss.dialog.InputField;
37 import org.zkoss.zk.ui.HtmlBasedComponent;
38 import org.zkoss.zul.Listbox;
39 import org.zkoss.zul.Listcell;
40 import org.zkoss.zul.Listhead;
41 import org.zkoss.zul.Listheader;
42 import org.zkoss.zul.Listitem;
43 
48 public class AcceptanceListbox extends Listbox {
49 
50  private String path;
51  private List<Acceptance> list;
52 
53  public String getPath() {
54  return path;
55  }
56 
57  public void setPath(String path) {
58  this.path = path;
59  addHeaders();
60  refreshItems();
61  setCheckmark(true);
62  setMultiple(true);
63  }
64 
65  public void refreshStatus() {
66  refreshItems();
67  }
68 
69  public boolean isAcceptable() {
70  if(Authentication.isAdministrator()) return true;
72  for(Acceptance a : list) {
73  if(a.getContact().getId().equals(logged.getId()) && a.getDateAcceptance() == null) {
74  return true;
75  }
76  }
77  return false;
78  }
79 
80  public boolean isPetitioner() {
81  return Application.getApplication().isInRole("acceptance:petition");
82  }
83 
84  public boolean isEraser() {
85  return Application.getApplication().isInRole("acceptance:delete");
86  }
87 
88  public void petition() {
89  final ContactCombobox contactInput = new ContactCombobox();
91  getPage(),
92  I_.get("Petition"),
93  new InputField[] {
94  new InputField("Contact", null, null, 0) {
95  @Override
96  protected HtmlBasedComponent createEditor() {
97  return contactInput;
98  }
99  },
100  new InputField("Comment", null, null, 3)
101  }, new Command() {
102 
103  @Override
104  public Object execute(Context context) {
105  InputField[] fields = (InputField[]) context.get("fields");
106  if(fields.length > 0) {
107  Contact contact = null;
108  String comment = null;
109  for(InputField f : fields) {
110  if("Contact".equals(f.getLabel())) {
111  contact = contactInput.getContact();
112  } else if("Comment".equals(f.getLabel())) {
113  comment = (String) f.getValue();
114  }
115  }
116  if(contact != null) {
117  new AcceptanceMail(
118  AcceptanceUtil.request((Contact) Authentication.getIContact().getContact(), contact, path, comment)
119  ).sendMail();
120  refreshItems();
121  }
122  }
123  return null;
124  }
125  });
126  }
127 
128  public void accept() {
130  for(Listitem li : (Collection<Listitem>) getSelectedItems()) {
131  if(li.getValue() instanceof Acceptance) {
132  Acceptance acceptance = li.getValue();
133  if(Authentication.isAdministrator() || acceptance.getContact().getId().equals(logged.getId())) {
134  new AcceptanceMail(
135  AcceptanceUtil.accept(acceptance)
136  ).sendMail();
137  }
138  }
139  }
140  }
141 
142  public void delete() {
143  Dao dao = new ContactsPU();
144  for(Listitem li : (Collection<Listitem>) getSelectedItems()) {
145  if(li.getValue() instanceof Acceptance) {
146  dao.deleteObject(li.getValue());
147  }
148  }
149  refreshItems();
150  }
151 
152  private void refreshItems() {
153  getItems().clear();
154  list = AcceptanceUtil.getAcceptances(path);
155  for(Acceptance a : list) {
156  Listitem li = new Listitem();
157  li.appendChild(new Listcell(a.getPetitioner().getName()));
158  li.appendChild(new Listcell(a.getContact().getName()));
159  li.appendChild(new Listcell(null,
160  a.getDateAcceptance() == null ?
161  "/_zul/images/cancel.png" :
162  "/_zul/images/ok.png"));
163  li.appendChild(new Listcell(a.getAcceptanceComment()));
164  li.setValue(a);
165  getItems().add(li);
166  }
167  }
168 
169  private void addHeaders() {
170  Listhead head = new Listhead();
171  head.setSizable(true);
172  appendChild(head);
173  head.appendChild(new Listheader(I_.get("Petitioner")));
174  head.appendChild(new Listheader(I_.get("Contact")));
175  head.appendChild(new Listheader(null, null, "40px"));
176  head.appendChild(new Listheader(I_.get("Comment")));
177  }
178 
179 }
static Acceptance accept(Acceptance a)
static List< Acceptance > getAcceptances(String path)
static String get(String msg)
Definition: I_.java:41
void deleteObject(Object obj)
Definition: Dao.java:162
static void getInput(Page page, String title, String label, Object value, String format, int scale, final Command onOk)