BrightSide Workbench Full Report + Source Code
CategoriesByContactVM.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.dossier.command;
20 
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
24 import org.turro.auth.Authentication;
25 import org.turro.dossier.db.DossierPU;
26 import org.turro.dossier.entity.Category;
27 import org.turro.dossier.entity.CategoryParticipant;
28 import org.turro.dossier.entity.ParticipantRole;
29 import org.turro.jpa.Dao;
30 import org.turro.plugin.contacts.IContact;
31 import org.zkoss.bind.annotation.BindingParam;
32 import org.zkoss.bind.annotation.Command;
33 import org.zkoss.bind.annotation.ExecutionArgParam;
34 import org.zkoss.bind.annotation.Init;
35 import org.zkoss.bind.annotation.NotifyChange;
36 
41 public class CategoriesByContactVM {
42 
43  private IContact contact = Authentication.getIContact();
44 
45  @Init
46  public void init(@ExecutionArgParam("contact") IContact contact) {
47  if(contact != null) this.contact = contact;
48  }
49 
50  @Command
51  @NotifyChange("*")
52  public void save(@BindingParam("entity") Object entity) {
53  if(entity instanceof CategoryParticipationWrapper) {
54  ((CategoryParticipationWrapper) entity).save();
55  }
56  }
57 
58  @Command
59  @NotifyChange("*")
60  public void delete(@BindingParam("entity") Object entity) {
61  if(entity instanceof CategoryParticipationWrapper) {
62  ((CategoryParticipationWrapper) entity).delete();
63  }
64  }
65 
66  public List<CategoryParticipationWrapper> getModel() {
67  ArrayList<CategoryParticipationWrapper> wrappers = new ArrayList<>();
68  fillCategories(wrappers);
69  return wrappers;
70  }
71 
72  public List<ParticipantRole> getParticipantRoles() {
73  return Arrays.asList(ParticipantRole.values());
74  }
75 
76  private void fillCategories(ArrayList<CategoryParticipationWrapper> wrappers) {
77  for(Category category : (List<Category>) getDao().getResultList("select c from Category c order by c.fullDescription")) {
78  wrappers.add(new CategoryParticipationWrapper(contact, category, getParticipant(category)));
79  }
80  }
81 
82  private CategoryParticipant getParticipant(Category category) {
83  for(CategoryParticipant cp : category.getParticipants()) {
84  if(contact.getId().equals(cp.getIdContact())) {
85  return cp;
86  }
87  }
88  return new CategoryParticipant();
89  }
90 
91  /* Dao */
92 
93  private Dao _dao;
94 
95  private Dao getDao() {
96  if(_dao == null) {
97  _dao = new DossierPU();
98  }
99  return _dao;
100  }
101 
102 }
void delete(@BindingParam("entity") Object entity)
List< CategoryParticipationWrapper > getModel()
void save(@BindingParam("entity") Object entity)
void init(@ExecutionArgParam("contact") IContact contact)