BrightSide Workbench Full Report + Source Code
PublishChallengeVM.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.alliance.service.AxConstants;
25 import org.turro.alliance.service.AxServer;
26 import org.turro.elephant.util.Toasts;
27 import org.turro.i18n.I_;
28 import org.turro.jpa.Dao;
29 import org.turro.json.Jsons;
30 import org.turro.member.db.entities.AxChallengeCategory;
31 import org.turro.sql.SqlClause;
32 import org.turro.students.entities.Challenge;
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 PublishChallengeVM {
47 
48  private Challenge challenge;
49 
50  @Init
51  public void init(@ExecutionArgParam("challenge") Challenge challenge) {
52  if(challenge != null) this.challenge = challenge;
53  if(this.challenge == null) {
54  this.challenge = (Challenge) Executions.getCurrent().getAttribute("challenge");
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.setChallenge(challenge);
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") AxChallengeCategory 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<AxChallengeCategory> model;
106  private List<AxChallengeCategory> deletions = new ArrayList<>();
107 
108  public List<AxChallengeCategory> getModel() {
109  if(model == null) {
110  model = SqlClause.select("pc").from("AxChallengeCategory pc")
111  .where().equal("pc.challengeId", String.valueOf(challenge.getId()))
112  .dao(new AlliancePU())
113  .resultList(AxChallengeCategory.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 
135  return Servers.getData(WsServer.from(pc.getServerDomain(), AxServer.SERVER_SERVICE), AxConstants.TALENT_CATEGORIES);
136  }
137 
138 }
void init(@ExecutionArgParam("challenge") Challenge challenge)
void delete(@BindingParam("category") AxChallengeCategory category)
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