BrightSide Workbench Full Report + Source Code
PublishVM.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.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.alliance.service.AxConstants;
25 import org.turro.alliance.service.AxServer;
26 import org.turro.dossier.entity.Dossier;
27 import org.turro.elephant.util.Toasts;
28 import org.turro.i18n.I_;
29 import org.turro.jpa.Dao;
30 import org.turro.json.Jsons;
31 import org.turro.member.db.entities.AxProjectCategory;
32 import org.turro.sql.SqlClause;
33 import org.turro.ws.WsServer;
34 import org.turro.ws.service.member.Servers;
35 import org.zkoss.bind.annotation.BindingParam;
36 import org.zkoss.bind.annotation.Command;
37 import org.zkoss.bind.annotation.ExecutionArgParam;
38 import org.zkoss.bind.annotation.Init;
39 import org.zkoss.bind.annotation.NotifyChange;
40 import org.zkoss.zk.ui.Executions;
41 
46 public class PublishVM {
47 
48  private Dossier dossier;
49 
50  @Init
51  public void init(@ExecutionArgParam("dossier") Dossier dossier) {
52  if(dossier != null) this.dossier = dossier;
53  if(this.dossier == null) {
54  this.dossier = (Dossier) Executions.getCurrent().getAttribute("dossier");
55  }
56  }
57 
58  public boolean isNeedsSave() {
59  return needsSave;
60  }
61 
62  public void setNeedsSave(boolean needsSave) {
63  this.needsSave = needsSave;
64  }
65 
66  @NotifyChange("model")
67  @Command("add")
68  public void add() {
69  if(server != null && !inModel(server)) {
71  pc.setServerDomain(server.getServerDomain());
72  pc.setDossier(dossier);
73  model.add(pc);
74  needsSave = true;
75  } else {
76  Toasts.message(I_.get("Already in model"));
77  }
78  }
79 
80  @NotifyChange("model")
81  @Command("save")
82  public void save() {
83  Dao dao = new AlliancePU();
84  model.stream().filter(pc -> pc.isEmpty())
85  .forEach(pc -> dao.deleteEntity(pc));
86  model.removeIf(pc -> pc.isEmpty());
87  dao.saveEntities(model);
88  dao.deleteCollection(deletions);
89  deletions.clear();
90  needsSave = false;
91  }
92 
93  @NotifyChange("model")
94  @Command("delete")
95  public void delete(@BindingParam("category") AxProjectCategory category) {
96  deletions.add(category);
97  model.removeIf(pc -> pc.getServerDomain().equals(category.getServerDomain()));
98  needsSave = true;
99  }
100 
101  /* Model */
102 
103  private boolean needsSave;
104 
105  private List<AxProjectCategory> model;
106  private List<AxProjectCategory> deletions = new ArrayList<>();
107 
108  public List<AxProjectCategory> getModel() {
109  if(model == null) {
110  model = SqlClause.select("pc").from("AxProjectCategory pc")
111  .where().equal("pc.projectId", String.valueOf(dossier.getId()))
112  .dao(new AlliancePU())
113  .resultList(AxProjectCategory.class);
114  }
115  return model;
116  }
117 
118  private boolean inModel(WsServer server) {
119  return model.stream().anyMatch(apc -> apc.getServerDomain().equals(server.getServerDomain()));
120  }
121 
122  /* Server */
123 
124  private WsServer server;
125 
126  public WsServer getServer() {
127  return server;
128  }
129 
130  public void setServer(WsServer server) {
131  this.server = server;
132  }
133 
134  public Jsons getCategories(AxProjectCategory pc) {
135  return Servers.getData(WsServer.from(pc.getServerDomain(), AxServer.SERVER_SERVICE), AxConstants.CATEGORIES);
136  }
137 
138 }
List< AxProjectCategory > getModel()
Definition: PublishVM.java:108
void init(@ExecutionArgParam("dossier") Dossier dossier)
Definition: PublishVM.java:51
void setNeedsSave(boolean needsSave)
Definition: PublishVM.java:62
Jsons getCategories(AxProjectCategory pc)
Definition: PublishVM.java:134
void delete(@BindingParam("category") AxProjectCategory category)
Definition: PublishVM.java:95
static final String SERVER_SERVICE
Definition: AxServer.java:51
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
static WsServer from(String serverDomain, String service)
Definition: WsServer.java:149
String getServerDomain()
Definition: WsServer.java:52