gvsig-projects-pool / org.gvsig.vcsgis / trunk / org.gvsig.vcsgis / org.gvsig.vcsgis.lib / org.gvsig.vcsgis.lib.impl / src / main / java / org / gvsig / vcsgis / lib / VCSGisUserImpl.java @ 3315
History | View | Annotate | Download (5.65 KB)
1 |
/*
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2020 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
18 |
*
|
19 |
* For any additional information, do not hesitate to contact us
|
20 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
21 |
*/
|
22 |
|
23 |
package org.gvsig.vcsgis.lib; |
24 |
|
25 |
import java.util.ArrayList; |
26 |
import java.util.Collections; |
27 |
import java.util.List; |
28 |
import javax.json.JsonObject; |
29 |
import org.apache.commons.lang3.StringUtils; |
30 |
import org.gvsig.json.Json; |
31 |
import org.gvsig.json.JsonObjectBuilder; |
32 |
|
33 |
/**
|
34 |
*
|
35 |
* @author gvSIG Team
|
36 |
*/
|
37 |
public class VCSGisUserImpl implements VCSGisUserEditable { |
38 |
|
39 |
private String code; |
40 |
private String description; |
41 |
private String identifier; |
42 |
private String roles; |
43 |
private List<String> roles_list; |
44 |
|
45 |
public VCSGisUserImpl() {
|
46 |
|
47 |
} |
48 |
|
49 |
public VCSGisUserImpl(JsonObject jsonData) {
|
50 |
this.code = jsonData.getString("UserCode", null); |
51 |
this.description = jsonData.getString("Description", null); |
52 |
this.identifier = jsonData.getString("Identifier", null); |
53 |
this.roles = jsonData.getString("Roles", null); |
54 |
} |
55 |
|
56 |
@Override
|
57 |
public String getUserCode() { |
58 |
return this.code; |
59 |
} |
60 |
|
61 |
@Override
|
62 |
public String getDescription() { |
63 |
return this.description; |
64 |
} |
65 |
|
66 |
@Override
|
67 |
public String getIdentifier() { |
68 |
return this.identifier; |
69 |
} |
70 |
|
71 |
@Override
|
72 |
public String getRoles() { |
73 |
return this.roles; |
74 |
} |
75 |
|
76 |
@Override
|
77 |
public void setUserCode(String userCode) { |
78 |
this.code = userCode;
|
79 |
} |
80 |
|
81 |
@Override
|
82 |
public void setDescription(String description) { |
83 |
this.description = description;
|
84 |
} |
85 |
|
86 |
@Override
|
87 |
public void setIdentifier(String identifier) { |
88 |
this.identifier = identifier;
|
89 |
} |
90 |
|
91 |
@Override
|
92 |
public void setRoles(String roles) { |
93 |
this.roles = roles;
|
94 |
} |
95 |
|
96 |
@Override
|
97 |
public void copy(VCSGisUser source, VCSGisUser target) { |
98 |
VCSGisUserImpl.copyUser(source, (VCSGisUserEditable) target); |
99 |
} |
100 |
|
101 |
@Override
|
102 |
public void copyFrom(VCSGisUser other) { |
103 |
VCSGisUserImpl.copyUser(other, this);
|
104 |
} |
105 |
|
106 |
@Override
|
107 |
public VCSGisUser clone() {
|
108 |
try {
|
109 |
VCSGisUserEditable other = (VCSGisUserEditable) super.clone();
|
110 |
VCSGisUserImpl.copyUser(this, other);
|
111 |
return other;
|
112 |
} catch (CloneNotSupportedException ex) { |
113 |
// Esto no deberia pasar.
|
114 |
return null; |
115 |
} |
116 |
} |
117 |
|
118 |
@Override
|
119 |
public List<String> getRolesAsList() { |
120 |
if( this.roles_list==null ) { |
121 |
this.roles_list = VCSGisUserImpl.getRolesAsList(this); |
122 |
} |
123 |
return this.roles_list; |
124 |
} |
125 |
|
126 |
@Override
|
127 |
public boolean hasRole(String roleid) { |
128 |
return VCSGisUserImpl.hasRole(this, roleid); |
129 |
} |
130 |
|
131 |
@Override
|
132 |
public void fromJson(JsonObject json) { |
133 |
VCSGisUserImpl.fromJson(this, json);
|
134 |
} |
135 |
|
136 |
@Override
|
137 |
public JsonObject toJson() {
|
138 |
return this.toJsonBuilder().build(); |
139 |
} |
140 |
|
141 |
@Override
|
142 |
public JsonObjectBuilder toJsonBuilder() {
|
143 |
return VCSGisUserImpl.toJsonBuilder(this); |
144 |
} |
145 |
|
146 |
@Override
|
147 |
public String toString() { |
148 |
return VCSGisUserImpl.toString(this); |
149 |
} |
150 |
|
151 |
public static String toString(VCSGisUser user) { |
152 |
return user.getIdentifier();
|
153 |
} |
154 |
|
155 |
public static JsonObjectBuilder toJsonBuilder(VCSGisUser user) { |
156 |
JsonObjectBuilder builder = Json.createObjectBuilder(); |
157 |
builder.add("UserCode", user.getUserCode());
|
158 |
builder.add("Identifier", user.getIdentifier());
|
159 |
builder.add("Roles", user.getRoles());
|
160 |
builder.add("Description", user.getDescription());
|
161 |
return builder;
|
162 |
} |
163 |
|
164 |
public static boolean hasRole(VCSGisUser user, String roleid) { |
165 |
if( StringUtils.isBlank(roleid) ) {
|
166 |
return false; |
167 |
} |
168 |
List<String> rl = user.getRolesAsList(); |
169 |
if( rl == null ) { |
170 |
return false; |
171 |
} |
172 |
return rl.contains(roleid.trim().toLowerCase());
|
173 |
} |
174 |
|
175 |
public static void fromJson(VCSGisUserEditable user, JsonObject json) { |
176 |
user.setUserCode(json.getString("UserCode", null)); |
177 |
user.setIdentifier(json.getString("Identifier", null)); |
178 |
user.setRoles(json.getString("Roles", null)); |
179 |
user.setDescription(json.getString("Description", null)); |
180 |
} |
181 |
|
182 |
public static void copyUser(VCSGisUser source, VCSGisUserEditable target) { |
183 |
target.setUserCode(source.getUserCode()); |
184 |
target.setIdentifier(source.getIdentifier()); |
185 |
target.setRoles(source.getRoles()); |
186 |
target.setDescription(source.getDescription()); |
187 |
} |
188 |
|
189 |
public static List<String> getRolesAsList(VCSGisUser user) { |
190 |
String s = user.getRoles();
|
191 |
if( StringUtils.isBlank(s) ) {
|
192 |
return Collections.EMPTY_LIST; |
193 |
} |
194 |
List<String> l = new ArrayList<>(); |
195 |
String[] ss = StringUtils.split(s, ","); |
196 |
for (String s1 : ss) { |
197 |
if( StringUtils.isBlank(s1) ) {
|
198 |
continue;
|
199 |
} |
200 |
l.add(s1.trim().toLowerCase()); |
201 |
} |
202 |
return l;
|
203 |
} |
204 |
|
205 |
} |