BrightSide Workbench Full Report + Source Code
AxChallenge.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.db.entities;
20 
21 import java.io.Serializable;
22 import java.util.Date;
23 import java.util.Map;
24 import java.util.Objects;
25 import java.util.Set;
26 import javax.json.JsonValue;
27 import javax.persistence.CascadeType;
28 import javax.persistence.Column;
29 import javax.persistence.EmbeddedId;
30 import javax.persistence.Entity;
31 import javax.persistence.FetchType;
32 import javax.persistence.JoinColumn;
33 import javax.persistence.JoinColumns;
34 import javax.persistence.Lob;
35 import javax.persistence.OneToMany;
36 import javax.persistence.Temporal;
37 import org.hibernate.annotations.Formula;
38 import org.turro.alliance.db.AlliancePU;
39 import org.turro.contacts.relation.Relations;
40 import org.turro.elephant.context.ElephantContext;
41 import org.turro.entities.Entities;
42 import org.turro.entities.Faces;
43 import org.turro.jpa.entity.IDaoEntity;
44 import org.turro.json.IJSONizable;
45 import org.turro.member.db.entities.AxChallengeCategory;
46 import org.turro.plugin.contacts.IContact;
47 import org.turro.string.Strings;
48 import org.turro.students.entities.Challenge;
49 import org.turro.ws.WsServer;
50 
55 @Entity
56 public class AxChallenge implements Serializable, IDaoEntity, IJSONizable {
57 
58  @EmbeddedId private ProcedenceId challengeId;
59 
60  @Formula("concat(entityId,'##',memberId)")
61  private String id; /* Widgets compatibility */
62 
63  private Long categoryId;
64 
65  private String idChallenger;
66  private String name, face, company;
67 
68  private String question;
69 
70  @Temporal(value = javax.persistence.TemporalType.TIMESTAMP)
71  private java.util.Date creation;
72 
73  @Temporal(value = javax.persistence.TemporalType.DATE)
74  private java.util.Date deadline;
75 
76  @Lob
77  @Column(length=4096)
78  private String text;
79 
80  private String sourceLink;
81 
82  @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
83  @JoinColumns({
84  @JoinColumn(name="mainEntityId", referencedColumnName = "entityId", insertable = false, updatable = false),
85  @JoinColumn(name="mainMemberId", referencedColumnName = "memberId", insertable = false, updatable = false)
86  })
87  private Set<AxResponse> responses;
88 
89  public String getId() {
90  return id;
91  }
92 
94  return challengeId;
95  }
96 
97  public void setChallengeId(ProcedenceId challengeId) {
98  this.challengeId = challengeId;
99  }
100 
101  public Long getCategoryId() {
102  return categoryId;
103  }
104 
105  public void setCategoryId(Long categoryId) {
106  this.categoryId = categoryId;
107  }
108 
109  public String getIdChallenger() {
110  return idChallenger;
111  }
112 
113  public void setIdChallenger(String idChallenger) {
114  this.idChallenger = idChallenger;
115  }
116 
117  public String getName() {
118  return name;
119  }
120 
121  public void setName(String name) {
122  this.name = name;
123  }
124 
125  public String getFace() {
126  return face;
127  }
128 
129  public void setFace(String face) {
130  this.face = face;
131  }
132 
133  public String getCompany() {
134  return company;
135  }
136 
137  public void setCompany(String company) {
138  this.company = company;
139  }
140 
141  public String getQuestion() {
142  return question;
143  }
144 
145  public void setQuestion(String question) {
146  this.question = question;
147  }
148 
149  public Date getCreation() {
150  return creation;
151  }
152 
153  public void setCreation(Date creation) {
154  this.creation = creation;
155  }
156 
157  public Date getDeadline() {
158  return deadline;
159  }
160 
161  public void setDeadline(Date deadline) {
162  this.deadline = deadline;
163  }
164 
165  public String getText() {
166  return text;
167  }
168 
169  public void setText(String text) {
170  this.text = text;
171  }
172 
174  return new AlliancePU().find(AxTalentCategory.class, categoryId);
175  }
176 
177  public void setCategory(AxTalentCategory category) {
178  categoryId = category.getCategoryId();
179  }
180 
181  public String getSourceLink() {
182  return sourceLink;
183  }
184 
185  public void setSourceLink(String sourceLink) {
186  this.sourceLink = sourceLink;
187  }
188 
189  public Set<AxResponse> getResponses() {
190  return responses;
191  }
192 
193  public void setResponses(Set<AxResponse> responses) {
194  this.responses = responses;
195  }
196 
197  /* Factory */
198 
199  public static AxChallenge from(long memberId, WsServer server, Challenge challenge, AxChallengeCategory pc) {
200  AxChallenge axp = new AxChallenge();
201  ProcedenceId id = new ProcedenceId();
202  id.setMemberId(memberId);
203  id.setEntityId(Long.toString(challenge.getId()));
204  axp.setChallengeId(id);
205  axp.setCreation(challenge.getCreation());
206  axp.setDeadline(challenge.getDeadline());
207  axp.setCategoryId(pc.getCategoryId());
208  IContact challenger = challenge.getContact();
209  axp.setIdChallenger(challenger.getId());
210  axp.setName(challenger.getName());
211  axp.setFace(Faces.of(challenger).getUrl());
212  axp.setCompany(Relations.companies(challenger).date(challenge.getCreation()).getOptCompany().map(c -> c.getName()).orElse(null));
213  axp.setQuestion(challenge.getQuestion());
214  axp.setText(challenge.getText());
216  axp.setResponses(AxResponse.from(memberId, challenge));
217  return axp;
218  }
219 
220  /* IDaoEntity */
221 
222  @Override
223  public Object entityId() {
224  return challengeId;
225  }
226 
227  @Override
228  public boolean isEmpty() {
229  return challengeId.isEmpty() ||
230  Strings.isBlank(name);
231  }
232 
233  /* JSONizable */
234 
235  @Override
236  public String toJson() {
237  return toJson(this);
238  }
239 
240  @Override
241  public String toJson(Map<String, Object> properties) {
242  return toJson(this, properties);
243  }
244 
245  public static AxChallenge fromJson(JsonValue value) {
246  return IJSONizable.fromJson(value.toString(), AxChallenge.class);
247  }
248 
249  /* Utils */
250 
251  @Override
252  public int hashCode() {
253  int hash = 3;
254  hash = 79 * hash + Objects.hashCode(this.challengeId);
255  return hash;
256  }
257 
258  @Override
259  public boolean equals(Object obj) {
260  if (this == obj) {
261  return true;
262  }
263  if (obj == null) {
264  return false;
265  }
266  if (getClass() != obj.getClass()) {
267  return false;
268  }
269  final AxChallenge other = (AxChallenge) obj;
270  return Objects.equals(this.challengeId, other.challengeId);
271  }
272 
273 }
String toJson(Map< String, Object > properties)
void setIdChallenger(String idChallenger)
static AxChallenge fromJson(JsonValue value)
void setChallengeId(ProcedenceId challengeId)
void setCategory(AxTalentCategory category)
void setResponses(Set< AxResponse > responses)
static AxChallenge from(long memberId, WsServer server, Challenge challenge, AxChallengeCategory pc)
static Set< AxResponse > from(long memberId, Challenge challenge)
Definition: AxResponse.java:92
static String getServerUrl(String scheme)
static IElephantEntity getController(String path)
Definition: Entities.java:78
static Faces of(IContact contact)
Definition: Faces.java:64