BrightSide Workbench Full Report + Source Code
WsMember.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 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.ws;
20 
21 import java.io.Serializable;
22 import java.net.UnknownHostException;
23 import java.util.Map;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import javax.persistence.Entity;
27 import javax.persistence.GeneratedValue;
28 import javax.persistence.GenerationType;
29 import javax.persistence.Id;
30 import org.turro.string.Phrases;
31 import org.turro.string.Strings;
32 import org.turro.url.URIs;
33 import org.turro.action.Contacts;
34 import org.turro.elephant.context.ElephantContext;
35 import org.turro.elephant.db.ElephantPU;
36 import org.turro.elephant.db.IdUtils;
37 import org.turro.jpa.entity.IDaoEntity;
38 import org.turro.json.IJSONizable;
39 import org.turro.math.Zero;
40 import org.turro.plugin.contacts.IContact;
41 import org.turro.util.Chars;
42 
47 @Entity
48 public class WsMember implements Serializable, IDaoEntity, IJSONizable {
49 
50  @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
51  private Long memberId;
52 
53  private String contactId;
54 
55  private String memberDomain;
56 
57  private String service;
58 
59  public Long getMemberId() {
60  return memberId;
61  }
62 
63  public void setMemberId(Long memberId) {
64  this.memberId = memberId;
65  }
66 
67  public String getContactId() {
68  return contactId;
69  }
70 
71  public void setContactId(String contactId) {
72  this.contactId = contactId;
73  }
74 
75  public String getMemberDomain() {
76  return memberDomain;
77  }
78 
79  public void setMemberDomain(String memberDomain) {
80  this.memberDomain = memberDomain;
81  }
82 
83  public String getService() {
84  return service;
85  }
86 
87  public void setService(String service) {
88  this.service = service;
89  }
90 
91  /* Utils */
92 
93  public boolean isValidIP(String ip) {
94  try {
95  String[] parts = memberDomain.split("[\\:\\/]");
96  return URIs.validIP(parts[0], ip);
97  } catch (UnknownHostException ex) {
98  Logger.getLogger(WsMember.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
99  return false;
100  }
101  }
102 
103  public String getMemberUrl() {
104  if(memberDomain.contains("localhost")) {
105  return "http://" + memberDomain;
106  } else {
107  return "https://" + memberDomain;
108  }
109  }
110 
111  public String getName() {
112  return Phrases.start(getContact().getName(), Chars.forward().toString(), getService()).toString();
113  }
114 
115  /* Service name */
116 
117  private transient ServiceNamePair serviceName;
118 
120  return serviceName == null ? new ServiceNamePair(service, service) : serviceName;
121  }
122 
123  public void setServiceName(ServiceNamePair serviceName) {
124  this.serviceName = serviceName;
125  this.service = serviceName.getService();
126  }
127 
128  /* Contact */
129 
130  public IContact getContact() {
131  return Contacts.getContactById(contactId);
132  }
133 
134  public void setContact(IContact contact) {
135  contactId = contact == null ? null : contact.getId();
136  }
137 
138  /* IDaoEntity */
139 
140  @Override
141  public Object entityId() {
142  return memberId;
143  }
144 
145  @Override
146  public boolean isEmpty() {
147  return Strings.isBlank(contactId) || Strings.isBlank(memberDomain);
148  }
149 
150  @Override
151  public void prepareSave() {
152  IDaoEntity.super.prepareSave();
153  if(Zero.orNull(memberId)) {
154  memberId = IdUtils.getNewLongIdFromLong(new ElephantPU(), "WsMember", "memberId");
155  }
156  }
157 
158  /* JSONizable */
159 
160  @Override
161  public String toJson() {
162  return toJson(this);
163  }
164 
165  @Override
166  public String toJson(Map<String, Object> properties) {
167  return toJson(this, properties);
168  }
169 
170  /* Factory */
171 
172  public static WsMember from(String memberDomain, String service) {
173  WsMember member = new WsMember();
174  member.setMemberDomain(memberDomain);
175  member.setService(service);
176  return member;
177  }
178 
179 }
static IContact getContactById(String id)
Definition: Contacts.java:72
static long getNewLongIdFromLong(Dao dao, String table, String field)
Definition: IdUtils.java:33
static boolean orNull(Number value)
Definition: Zero.java:26
String getMemberDomain()
Definition: WsMember.java:75
IContact getContact()
Definition: WsMember.java:130
void setServiceName(ServiceNamePair serviceName)
Definition: WsMember.java:123
boolean isValidIP(String ip)
Definition: WsMember.java:93
String toJson(Map< String, Object > properties)
Definition: WsMember.java:166
ServiceNamePair getServiceName()
Definition: WsMember.java:119
void setMemberDomain(String memberDomain)
Definition: WsMember.java:79
void setContactId(String contactId)
Definition: WsMember.java:71
void setService(String service)
Definition: WsMember.java:87
void setContact(IContact contact)
Definition: WsMember.java:134
static WsMember from(String memberDomain, String service)
Definition: WsMember.java:172
void setMemberId(Long memberId)
Definition: WsMember.java:63