BrightSide Workbench Full Report + Source Code
AxResponse.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.HashSet;
24 import java.util.Map;
25 import java.util.Objects;
26 import java.util.Set;
27 import javax.json.JsonValue;
28 import javax.persistence.Column;
29 import javax.persistence.ElementCollection;
30 import javax.persistence.EmbeddedId;
31 import javax.persistence.Entity;
32 import javax.persistence.FetchType;
33 import javax.persistence.Lob;
34 import javax.persistence.Temporal;
35 import org.turro.jpa.entity.IDaoEntity;
36 import org.turro.json.IJSONizable;
37 import org.turro.students.entities.Challenge;
38 
43 @Entity
44 public class AxResponse implements Serializable, IDaoEntity, IJSONizable {
45 
46  @EmbeddedId private RelationId relation;
47 
48  @Temporal(value = javax.persistence.TemporalType.TIMESTAMP)
49  private java.util.Date creation;
50 
51  @ElementCollection(fetch = FetchType.EAGER)
52  private Set<String> studentIds = new HashSet<>();
53 
54  @Lob
55  @Column(length=4096)
56  private String text;
57 
59  return relation;
60  }
61 
62  public void setRelation(RelationId relation) {
63  this.relation = relation;
64  }
65 
66  public Date getCreation() {
67  return creation;
68  }
69 
70  public void setCreation(Date creation) {
71  this.creation = creation;
72  }
73 
74  public Set<String> getStudentIds() {
75  return studentIds;
76  }
77 
78  public void setStudentIds(Set<String> studentIds) {
79  this.studentIds = studentIds;
80  }
81 
82  public String getText() {
83  return text;
84  }
85 
86  public void setText(String text) {
87  this.text = text;
88  }
89 
90  /* Factory */
91 
92  public static Set<AxResponse> from(long memberId, Challenge challenge) {
93  Set<AxResponse> responses = new HashSet<>();
94  challenge.getResponses().forEach(response -> {
95  RelationId relation = new RelationId();
96  relation.setMainEntityId(Long.toString(challenge.getId()));
97  relation.setMainMemberId(memberId);
98  relation.setRelatedEntityId(response.getId());
99  relation.setRelatedMemberId(memberId);
100  AxResponse responder = new AxResponse();
101  responder.setRelation(relation);
102  responder.setCreation(response.getCreation());
103  responder.setStudentIds(response.getStudentIds());
104  responder.setText(response.getText());
105  responses.add(responder);
106  });
107  return responses;
108  }
109 
110  /* IDaoEntity */
111 
112  @Override
113  public Object entityId() {
114  return relation;
115  }
116 
117  @Override
118  public boolean isEmpty() {
119  return relation.isEmpty();
120  }
121 
122  /* JSONizable */
123 
124  @Override
125  public String toJson() {
126  return toJson(this);
127  }
128 
129  @Override
130  public String toJson(Map<String, Object> properties) {
131  return toJson(this, properties);
132  }
133 
134  public static AxResponse fromJson(JsonValue value) {
135  return IJSONizable.fromJson(value.toString(), AxResponse.class);
136  }
137 
138  @Override
139  public int hashCode() {
140  int hash = 7;
141  hash = 11 * hash + Objects.hashCode(this.relation);
142  return hash;
143  }
144 
145  @Override
146  public boolean equals(Object obj) {
147  if (this == obj) {
148  return true;
149  }
150  if (obj == null) {
151  return false;
152  }
153  if (getClass() != obj.getClass()) {
154  return false;
155  }
156  final AxResponse other = (AxResponse) obj;
157  return Objects.equals(this.relation, other.relation);
158  }
159 
160 }
void setStudentIds(Set< String > studentIds)
Definition: AxResponse.java:78
void setRelation(RelationId relation)
Definition: AxResponse.java:62
static AxResponse fromJson(JsonValue value)
String toJson(Map< String, Object > properties)
static Set< AxResponse > from(long memberId, Challenge challenge)
Definition: AxResponse.java:92
void setRelatedMemberId(Long relatedMemberId)
Definition: RelationId.java:70
void setMainMemberId(Long mainMemberId)
Definition: RelationId.java:54
void setRelatedEntityId(String relatedEntityId)
Definition: RelationId.java:62
void setMainEntityId(String mainEntityId)
Definition: RelationId.java:46