BrightSide Workbench Full Report + Source Code
AxCenter.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.Map;
23 import java.util.Objects;
24 import javax.json.JsonValue;
25 import javax.persistence.EmbeddedId;
26 import javax.persistence.Entity;
27 import org.hibernate.annotations.Formula;
28 import org.turro.action.Contacts;
29 import org.turro.alliance.db.AlliancePU;
30 import org.turro.elephant.context.ElephantContext;
31 import org.turro.jpa.entity.IDaoEntity;
32 import org.turro.json.IJSONizable;
33 import org.turro.plugin.contacts.IContact;
34 import org.turro.sql.SqlClause;
35 import org.turro.string.Strings;
36 
41 @Entity
42 public class AxCenter implements Serializable, IDaoEntity, IJSONizable {
43 
44  @EmbeddedId private ProcedenceId centerId;
45 
46  @Formula("concat(entityId,'##',memberId)")
47  private String id; /* Widgets compatibility */
48 
49  private String name, email;
50  private String face;
51 
52  private boolean active;
53 
55  return centerId;
56  }
57 
58  public void setCenterId(ProcedenceId centerId) {
59  this.centerId = centerId;
60  }
61 
62  public String getId() {
63  return id;
64  }
65 
66  public String getName() {
67  return name;
68  }
69 
70  public void setName(String name) {
71  this.name = name;
72  }
73 
74  public String getEmail() {
75  return email;
76  }
77 
78  public void setEmail(String email) {
79  this.email = email;
80  }
81 
82  public String getFace() {
83  return face;
84  }
85 
86  public void setFace(String face) {
87  this.face = face;
88  }
89 
90  public boolean isActive() {
91  return active;
92  }
93 
94  public void setActive(boolean active) {
95  this.active = active;
96  }
97 
98  /* Relations */
99 
100  public Iterable<AxStudent> getStudents() {
101  return SqlClause.select("c").from("AxStudent c")
102  .where().equal("c.center", name)
103  .and().equal("c.studentId.memberId", centerId.getMemberId())
104  .dao(new AlliancePU())
105  .resultList(AxStudent.class);
106  }
107 
108  public Iterable<String> getCourses() {
109  return SqlClause.select("distinct c.course").from("AxStudent c")
110  .where().equal("c.center", name)
111  .and().equal("c.studentId.memberId", centerId.getMemberId())
112  .dao(new AlliancePU())
113  .stream(String.class)
114  .filter(s -> !Strings.isBlank(s))
115  .toList();
116  }
117 
118  /* Factory */
119 
120  public static AxCenter from(long memberId, String contactId) {
121  if(!Strings.isBlank(contactId)) {
122  return from(memberId, Contacts.getContactById(contactId));
123  }
124  return null;
125  }
126 
127  public static AxCenter from(long memberId, IContact contact) {
128  if(contact.isValid()) {
129  AxCenter axc = new AxCenter();
130  ProcedenceId id = new ProcedenceId();
131  id.setMemberId(memberId);
132  id.setEntityId(contact.getId());
133  axc.setCenterId(id);
134  axc.setName(contact.getName());
135  axc.setEmail(contact.getEmail());
136  axc.setActive(!contact.isDeactivated());
137  String serverURL = ElephantContext.getServerUrl("http");
138  axc.setFace(Strings.isBlank(contact.getFace()) ? null : serverURL + contact.getFace());
139  return axc;
140  }
141  return null;
142  }
143 
144  /* IDaoEntity */
145 
146  @Override
147  public Object entityId() {
148  return centerId;
149  }
150 
151  @Override
152  public boolean isEmpty() {
153  return centerId.isEmpty() ||
154  Strings.isBlank(name);
155  }
156 
157  /* JSONizable */
158 
159  @Override
160  public String toJson() {
161  return toJson(this);
162  }
163 
164  @Override
165  public String toJson(Map<String, Object> properties) {
166  return toJson(this, properties);
167  }
168 
169  public static AxCenter fromJson(JsonValue value) {
170  return IJSONizable.fromJson(value.toString(), AxCenter.class);
171  }
172 
173  /* Utils */
174 
175  @Override
176  public int hashCode() {
177  int hash = 7;
178  hash = 83 * hash + Objects.hashCode(this.centerId);
179  return hash;
180  }
181 
182  @Override
183  public boolean equals(Object obj) {
184  if (this == obj) {
185  return true;
186  }
187  if (obj == null) {
188  return false;
189  }
190  if (getClass() != obj.getClass()) {
191  return false;
192  }
193  final AxCenter other = (AxCenter) obj;
194  return Objects.equals(this.centerId, other.centerId);
195  }
196 
197 }
static IContact getContactById(String id)
Definition: Contacts.java:72
static AxCenter fromJson(JsonValue value)
Definition: AxCenter.java:169
static AxCenter from(long memberId, String contactId)
Definition: AxCenter.java:120
static AxCenter from(long memberId, IContact contact)
Definition: AxCenter.java:127
void setCenterId(ProcedenceId centerId)
Definition: AxCenter.java:58
String toJson(Map< String, Object > properties)
Definition: AxCenter.java:165
Iterable< AxStudent > getStudents()
Definition: AxCenter.java:100
static String getServerUrl(String scheme)