BrightSide Workbench Full Report + Source Code
Challenge.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2019 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.students.entities;
20 
21 import java.util.Date;
22 import java.util.HashSet;
23 import java.util.Set;
24 import javax.persistence.CascadeType;
25 import javax.persistence.Column;
26 import javax.persistence.Entity;
27 import javax.persistence.FetchType;
28 import javax.persistence.GeneratedValue;
29 import javax.persistence.GenerationType;
30 import javax.persistence.Id;
31 import javax.persistence.Lob;
32 import javax.persistence.OneToMany;
33 import javax.persistence.OrderBy;
34 import javax.persistence.Temporal;
35 import org.turro.string.Strings;
36 import org.turro.action.Contacts;
37 import org.turro.entities.Entities;
38 import org.turro.entities.IElephantEntity;
39 import org.turro.jpa.entity.EntityCollections;
40 import org.turro.jpa.entity.IDaoEntity;
41 import org.turro.parser.wiki.WikiCompiler;
42 import org.turro.plugin.contacts.IContact;
43 import org.turro.reflection.MappingSet;
44 
49 @Entity
50 public class Challenge implements java.io.Serializable, IDaoEntity {
51 
52  @Id
53  @GeneratedValue(strategy=GenerationType.IDENTITY)
54  @Column(name="IDENTIFIER")
55  private Long id;
56 
57  private String entityPath;
58 
59  private String idChallenger;
60  private String name;
61 
62  private String question;
63 
64  @Temporal(value = javax.persistence.TemporalType.TIMESTAMP)
65  private java.util.Date creation;
66 
67  @Temporal(value = javax.persistence.TemporalType.DATE)
68  private java.util.Date deadline;
69 
70  @Lob
71  @Column(length=4096)
72  private String text;
73 
74  @Lob
75  @Column(length=4096)
76  private String wikiText;
77 
78  @OneToMany(mappedBy = "challenge", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
79  @OrderBy(value="creation")
80  private Set<Response> responses = new HashSet<>();
81 
82  public Long getId() {
83  return id;
84  }
85 
86  public void setId(Long id) {
87  this.id = id;
88  }
89 
90  public String getEntityPath() {
91  return entityPath;
92  }
93 
94  public void setEntityPath(String entityPath) {
95  this.entityPath = entityPath;
96  }
97 
98  public String getIdChallenger() {
99  return idChallenger;
100  }
101 
102  public void setIdChallenger(String idChallenger) {
103  this.idChallenger = idChallenger;
104  }
105 
106  public String getName() {
107  return name;
108  }
109 
110  public void setName(String name) {
111  this.name = name;
112  }
113 
114  public String getQuestion() {
115  return question;
116  }
117 
118  public void setQuestion(String question) {
119  this.question = question;
120  }
121 
122  public Date getCreation() {
123  return creation;
124  }
125 
126  public void setCreation(Date creation) {
127  this.creation = creation;
128  }
129 
130  public Date getDeadline() {
131  return deadline;
132  }
133 
134  public void setDeadline(Date deadline) {
135  this.deadline = deadline;
136  }
137 
138  public String getText() {
139  return text;
140  }
141 
142  public void setText(String text) {
143  this.text = text;
144  }
145 
146  public String getWikiText() {
147  return wikiText;
148  }
149 
150  public void setWikiText(String wikiText) {
151  this.wikiText = wikiText;
152  this.text = WikiCompiler.source(this.wikiText).html();
153  }
154 
155  public Set<Response> getResponses() {
156  return responses;
157  }
158 
159  public void setResponses(Set<Response> responses) {
160  this.responses = responses;
161  }
162 
163  /* IDaoEntity */
164 
165  @Override
166  public Object entityId() {
167  return id;
168  }
169 
170  @Override
171  public boolean isEmpty() {
172  return Strings.isBlank(entityPath) || Strings.isBlank(idChallenger) ||
173  Strings.isBlank(question);
174  }
175 
178  public boolean removeResponse(Response response) {
179  EntityCollections.entities(responses).remove(response);
180  response.setChallenge(null);
181  return true;
182  }
183 
184  public boolean isParticipant(IContact contact) {
185  return idChallenger.equals(contact.getId()) ||
186  responses.stream().anyMatch(r -> r.getStudentIds().contains(contact.getId()));
187  }
188 
192  return Entities.getController(entityPath);
193  }
194 
197  private transient IContact _contact;
198 
199  public IContact getContact() {
200  if(_contact == null) {
201  _contact = Contacts.getContactById(idChallenger);
202  }
203  return _contact;
204  }
205 
206  public void setContact(IContact contact) {
207  _contact = contact;
208  idChallenger = _contact != null ? _contact.getId() : null;
209  name = _contact != null ? _contact.getName() : null;
210  }
211 
212  /* XML Serializer */
213 
214  public MappingSet getSerializerMappings() {
215  MappingSet set = new MappingSet();
216  set.addMapping(Challenge.class, 1,
217  new String[] { "id", "question", "entityPath", "idChallenger", "creation", "deadline" },
218  new String[] { "wikiText", "responses" });
219  set.addMapping(Response.class, 2,
220  new String[] { "id", "creation" },
221  new String[] { "wikiText", "studentIds" });
222  return set;
223  }
224 
225 }
static IContact getContactById(String id)
Definition: Contacts.java:72
static IElephantEntity getController(String path)
Definition: Entities.java:78
static EntityCollections entities(Collection values)
void setContact(IContact contact)
Definition: Challenge.java:206
void setQuestion(String question)
Definition: Challenge.java:118
boolean removeResponse(Response response)
Definition: Challenge.java:178
boolean isParticipant(IContact contact)
Definition: Challenge.java:184
void setWikiText(String wikiText)
Definition: Challenge.java:150
void setIdChallenger(String idChallenger)
Definition: Challenge.java:102
void setEntityPath(String entityPath)
Definition: Challenge.java:94
void setResponses(Set< Response > responses)
Definition: Challenge.java:159
void setChallenge(Challenge challenge)
Definition: Response.java:83