BrightSide Workbench Full Report + Source Code
PublishGrantVM.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2024 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.alliance.client.model;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 import org.turro.alliance.db.AlliancePU;
24 import org.turro.dossier.entity.ProjectGrant;
25 import org.turro.elephant.util.Toasts;
26 import org.turro.i18n.I_;
27 import org.turro.jpa.Dao;
28 import org.turro.member.db.entities.AxProjectGrantPublic;
29 import org.turro.sql.SqlClause;
30 import org.turro.ws.WsServer;
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 import org.zkoss.zk.ui.Executions;
37 
42 public class PublishGrantVM {
43 
44  private ProjectGrant projectGrant;
45 
46  @Init
47  public void init(@ExecutionArgParam("projectGrant") ProjectGrant projectGrant) {
48  if(projectGrant != null) this.projectGrant = projectGrant;
49  if(this.projectGrant == null) {
50  this.projectGrant = (ProjectGrant) Executions.getCurrent().getAttribute("projectGrant");
51  }
52  }
53 
54  public boolean isNeedsSave() {
55  return needsSave;
56  }
57 
58  public void setNeedsSave(boolean needsSave) {
59  this.needsSave = needsSave;
60  }
61 
62  @NotifyChange("model")
63  @Command("add")
64  public void add() {
65  if(server != null && !inModel(server)) {
67  pc.setServerDomain(server.getServerDomain());
68  pc.setProjectGrant(projectGrant);
69  model.add(pc);
70  needsSave = true;
71  } else {
72  Toasts.message(I_.get("Already in model"));
73  }
74  }
75 
76  @NotifyChange("model")
77  @Command("save")
78  public void save() {
79  Dao dao = new AlliancePU();
80  model.stream().filter(pc -> pc.isEmpty())
81  .forEach(pc -> dao.deleteEntity(pc));
82  model.removeIf(pc -> pc.isEmpty());
83  dao.saveEntities(model);
84  dao.deleteCollection(deletions);
85  deletions.clear();
86  needsSave = false;
87  }
88 
89  @NotifyChange("model")
90  @Command("delete")
91  public void delete(@BindingParam("pgpublic") AxProjectGrantPublic pgpublic) {
92  deletions.add(pgpublic);
93  model.removeIf(pc -> pc.getServerDomain().equals(pgpublic.getServerDomain()));
94  needsSave = true;
95  }
96 
97  /* Model */
98 
99  private boolean needsSave;
100 
101  private List<AxProjectGrantPublic> model;
102  private List<AxProjectGrantPublic> deletions = new ArrayList<>();
103 
104  public List<AxProjectGrantPublic> getModel() {
105  if(model == null) {
106  model = SqlClause.select("pc").from("AxProjectGrantPublic pc")
107  .where().equal("pc.projectGrantId", String.valueOf(projectGrant.getId()))
108  .dao(new AlliancePU())
109  .resultList(AxProjectGrantPublic.class);
110  }
111  return model;
112  }
113 
114  private boolean inModel(WsServer server) {
115  return model.stream().anyMatch(apc -> apc.getServerDomain().equals(server.getServerDomain()));
116  }
117 
118  /* Server */
119 
120  private WsServer server;
121 
122  public WsServer getServer() {
123  return server;
124  }
125 
126  public void setServer(WsServer server) {
127  this.server = server;
128  }
129 
130 }
void delete(@BindingParam("pgpublic") AxProjectGrantPublic pgpublic)
void init(@ExecutionArgParam("projectGrant") ProjectGrant projectGrant)
static Toasts message(String message)
Definition: Toasts.java:114
static String get(String msg)
Definition: I_.java:41
void deleteEntity(IDaoEntity entity)
Definition: Dao.java:168
void deleteCollection(Collection objs)
Definition: Dao.java:175
void saveEntities(Collection<? extends IDaoEntity > entities)
Definition: Dao.java:133
String getServerDomain()
Definition: WsServer.java:52