| author | ymh <ymh.work@gmail.com> |
| Mon, 21 Oct 2013 17:55:12 +0200 | |
| branch | user_management |
| changeset 224 | 0167b777ad15 |
| parent 222 | 6ac00231ee34 |
| child 232 | b7000ff4989b |
| permissions | -rw-r--r-- |
| 45 | 1 |
package org.iri_research.renkan.models; |
2 |
||
| 51 | 3 |
import java.io.UnsupportedEncodingException; |
4 |
import java.security.MessageDigest; |
|
5 |
import java.security.NoSuchAlgorithmException; |
|
|
46
7e132e2a48ca
add most of the model and repositories
ymh <ymh.work@gmail.com>
parents:
45
diff
changeset
|
6 |
import java.util.ArrayList; |
| 51 | 7 |
import java.util.Date; |
| 84 | 8 |
import java.util.HashMap; |
|
46
7e132e2a48ca
add most of the model and repositories
ymh <ymh.work@gmail.com>
parents:
45
diff
changeset
|
9 |
import java.util.List; |
| 84 | 10 |
import java.util.Map; |
|
46
7e132e2a48ca
add most of the model and repositories
ymh <ymh.work@gmail.com>
parents:
45
diff
changeset
|
11 |
|
|
106
d34e253f5c32
Simplify spring integration + correct key for collaborative usage
ymh <ymh.work@gmail.com>
parents:
84
diff
changeset
|
12 |
import javax.crypto.spec.SecretKeySpec; |
|
d34e253f5c32
Simplify spring integration + correct key for collaborative usage
ymh <ymh.work@gmail.com>
parents:
84
diff
changeset
|
13 |
|
| 51 | 14 |
import org.apache.commons.codec.binary.Hex; |
15 |
import org.iri_research.renkan.Constants; |
|
| 80 | 16 |
import org.iri_research.renkan.Constants.EditMode; |
|
224
0167b777ad15
remove deprecated warnings + fix date picker default langauge
ymh <ymh.work@gmail.com>
parents:
222
diff
changeset
|
17 |
import org.iri_research.renkan.RenkanException; |
| 215 | 18 |
import org.iri_research.renkan.utils.ColorGenerator; |
| 45 | 19 |
import org.slf4j.Logger; |
20 |
import org.slf4j.LoggerFactory; |
|
| 84 | 21 |
import org.springframework.beans.factory.annotation.Autowired; |
|
46
7e132e2a48ca
add most of the model and repositories
ymh <ymh.work@gmail.com>
parents:
45
diff
changeset
|
22 |
import org.springframework.data.mongodb.core.mapping.DBRef; |
| 45 | 23 |
import org.springframework.data.mongodb.core.mapping.Document; |
| 71 | 24 |
import org.springframework.data.mongodb.core.mapping.Field; |
25 |
||
|
220
ecbb104cf3b6
Upgrade to jetty 8, Jersey 2.3, cometd 2.7, coweb 1.0.1 and more...
ymh <ymh.work@gmail.com>
parents:
215
diff
changeset
|
26 |
import com.fasterxml.jackson.annotation.JsonFormat; |
| 71 | 27 |
import com.fasterxml.jackson.annotation.JsonProperty; |
| 45 | 28 |
|
| 222 | 29 |
@Document(collection = "projects") |
| 45 | 30 |
public class Project extends AbstractRenkanModel<String> { |
| 222 | 31 |
|
32 |
@SuppressWarnings("unused") |
|
33 |
private static Logger logger = LoggerFactory.getLogger(Project.class); |
|
34 |
||
35 |
@Field("rev_counter") |
|
36 |
private int revCounter = 1; |
|
37 |
||
38 |
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ", timezone = "GMT") |
|
39 |
private Date created; |
|
40 |
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ", timezone = "GMT") |
|
41 |
private Date updated; |
|
42 |
||
43 |
// Space |
|
44 |
@Field("space_id") |
|
45 |
@JsonProperty("space_id") |
|
46 |
private String spaceId = null; |
|
47 |
||
48 |
// Nodes |
|
49 |
@DBRef |
|
50 |
private List<Node> nodes = new ArrayList<Node>(); |
|
|
46
7e132e2a48ca
add most of the model and repositories
ymh <ymh.work@gmail.com>
parents:
45
diff
changeset
|
51 |
|
| 222 | 52 |
// edges |
53 |
@DBRef |
|
54 |
private List<Edge> edges = new ArrayList<Edge>(); |
|
55 |
||
56 |
// Users |
|
57 |
private List<RenkanUser> users = new ArrayList<RenkanUser>(); |
|
58 |
||
59 |
public Project(Project project) { |
|
60 |
this(project.spaceId, Constants.UUID_GENERATOR.generate().toString(), |
|
61 |
project.title, project.description, project.uri, new Date()); |
|
62 |
||
63 |
Map<String, Node> nodeCloneMap = new HashMap<String, Node>( |
|
64 |
project.nodes.size()); |
|
65 |
for (Node node : project.nodes) { |
|
66 |
Node newNode = new Node(node, this.id); |
|
67 |
this.nodes.add(newNode); |
|
68 |
nodeCloneMap.put(node.id, newNode); |
|
69 |
} |
|
70 |
||
71 |
for (Edge edge : project.edges) { |
|
72 |
this.edges.add(new Edge(edge, nodeCloneMap.get(edge.getFrom()), |
|
73 |
nodeCloneMap.get(edge.getTo()), this.id)); |
|
74 |
} |
|
75 |
for (RenkanUser user : project.users) { |
|
76 |
this.users.add(new RenkanUser(user)); |
|
77 |
} |
|
78 |
} |
|
| 84 | 79 |
|
| 222 | 80 |
public Project(String spaceId, String id, String title, String description, |
81 |
String uri, Date created, int revCounter) { |
|
82 |
super(id, title, description, uri, null); |
|
83 |
this.revCounter = revCounter; |
|
84 |
this.spaceId = spaceId; |
|
85 |
this.created = created; |
|
86 |
if (this.created == null) { |
|
87 |
this.created = new Date(); |
|
88 |
} |
|
89 |
this.setUpdated(new Date()); |
|
90 |
} |
|
91 |
||
92 |
@Autowired(required = true) |
|
93 |
public Project(String spaceId, String id, String title, String description, |
|
94 |
String uri, Date created) { |
|
95 |
this(spaceId, id, title, description, uri, created, 1); |
|
96 |
} |
|
97 |
||
98 |
@SuppressWarnings("unused") |
|
99 |
private Project() { |
|
100 |
} |
|
101 |
||
102 |
public int getRevCounter() { |
|
103 |
return this.revCounter; |
|
104 |
} |
|
105 |
||
106 |
public List<Node> getNodes() { |
|
107 |
return this.nodes; |
|
108 |
} |
|
| 45 | 109 |
|
| 222 | 110 |
public List<Edge> getEdges() { |
111 |
return this.edges; |
|
112 |
} |
|
113 |
||
114 |
public List<RenkanUser> getUsers() { |
|
115 |
return this.users; |
|
116 |
} |
|
117 |
||
118 |
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ", timezone = "GMT") |
|
119 |
public Date getCreated() { |
|
120 |
return created; |
|
121 |
} |
|
122 |
||
123 |
public void setCreated(Date date) { |
|
124 |
this.created = date; |
|
|
46
7e132e2a48ca
add most of the model and repositories
ymh <ymh.work@gmail.com>
parents:
45
diff
changeset
|
125 |
|
| 222 | 126 |
} |
127 |
||
128 |
@JsonProperty("space_id") |
|
129 |
public String getSpaceId() { |
|
130 |
return spaceId; |
|
131 |
} |
|
| 45 | 132 |
|
| 222 | 133 |
private String getRawKey(String prefix, Constants.EditMode editMode) { |
134 |
StringBuffer key = new StringBuffer(prefix != null ? prefix + "|" : ""); |
|
135 |
key.append(this.getId()); |
|
136 |
key.append('|'); |
|
137 |
key.append(this.getSpaceId()); |
|
138 |
key.append('|'); |
|
139 |
key.append(this.getCreated().getTime()); |
|
140 |
key.append('|'); |
|
141 |
key.append(editMode.toString()); |
|
142 |
return key.toString(); |
|
143 |
} |
|
| 77 | 144 |
|
| 222 | 145 |
public String getKey(int editMode) throws RenkanException { |
146 |
return this.getKey(EditMode.fromInt(editMode)); |
|
147 |
} |
|
148 |
||
149 |
public String getKey(Constants.EditMode editMode) throws RenkanException { |
|
150 |
||
151 |
String rawKey = this.getRawKey("", editMode); |
|
152 |
||
153 |
MessageDigest md; |
|
154 |
try { |
|
155 |
md = MessageDigest.getInstance("SHA-256"); |
|
156 |
} catch (NoSuchAlgorithmException e) { |
|
157 |
throw new RenkanException("NoSuchAlgorithmException digest: " |
|
158 |
+ e.getMessage(), e); |
|
159 |
} |
|
160 |
String key; |
|
161 |
final SecretKeySpec secret_key = new SecretKeySpec( |
|
162 |
Constants.KEYHEX.getBytes(), "HmacSHA256"); |
|
163 |
md.update(secret_key.getEncoded()); |
|
164 |
try { |
|
165 |
key = Hex.encodeHexString(md.digest(rawKey.getBytes("UTF-8"))); |
|
166 |
} catch (UnsupportedEncodingException e) { |
|
167 |
throw new RenkanException("UnsupportedEncodingException digest: " |
|
168 |
+ e.getMessage(), e); |
|
169 |
} |
|
| 51 | 170 |
|
| 222 | 171 |
return key; |
172 |
} |
|
173 |
||
174 |
public boolean checkKey(String key, Constants.EditMode editMode) |
|
175 |
throws RenkanException { |
|
176 |
||
177 |
if (key == null || key.isEmpty()) { |
|
178 |
return false; |
|
179 |
} |
|
180 |
||
181 |
String signature = key; |
|
182 |
||
183 |
String new_key = this.getKey(editMode); |
|
| 51 | 184 |
|
| 222 | 185 |
return new_key.equals(signature); |
186 |
} |
|
187 |
||
188 |
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ", timezone = "GMT") |
|
189 |
public Date getUpdated() { |
|
190 |
return updated; |
|
191 |
} |
|
192 |
||
193 |
public void setUpdated(Date updated) { |
|
194 |
this.updated = updated; |
|
195 |
} |
|
196 |
||
197 |
public void addUser(User user) { |
|
| 127 | 198 |
|
| 222 | 199 |
if (user == null) { |
200 |
// we add an anonymous user |
|
201 |
// find an unique user name |
|
202 |
this.addUser(null, null); |
|
203 |
} else { |
|
204 |
// if user is already in list do nothing |
|
205 |
for (RenkanUser renkanUser : this.users) { |
|
206 |
if (renkanUser.getUserId() != null |
|
207 |
&& renkanUser.getUserId().equals(user.getId())) { |
|
208 |
return; |
|
209 |
} |
|
210 |
} |
|
211 |
// user not found |
|
212 |
this.users.add(new RenkanUser(this.getId(), user.getId(), user |
|
213 |
.getColor(), user.getUsername())); |
|
214 |
||
215 |
} |
|
216 |
||
217 |
} |
|
218 |
||
219 |
public void addUser(String username, String color) { |
|
220 |
||
221 |
if (username == null) { |
|
222 |
// find a new username |
|
223 |
int i = 0; |
|
224 |
boolean usernameFound = true; |
|
225 |
while (i++ < 1000000 && usernameFound) { |
|
226 |
username = String.format("%s-%s", |
|
227 |
Constants.ANONYMOUS_USER_BASE_NAME, i); |
|
228 |
usernameFound = false; |
|
229 |
for (RenkanUser renkanUser : this.users) { |
|
230 |
if (username.equals(renkanUser.getUsername())) { |
|
231 |
usernameFound = true; |
|
232 |
break; |
|
233 |
} |
|
234 |
} |
|
235 |
} |
|
236 |
} |
|
237 |
||
238 |
if (color == null) { |
|
239 |
int i = 0; |
|
240 |
boolean colorFound = true; |
|
241 |
while (i++ < 10000000 && colorFound) { |
|
242 |
color = "#" + ColorGenerator.randomColorHex(); |
|
243 |
colorFound = false; |
|
244 |
for (RenkanUser renkanUser : this.users) { |
|
245 |
if (username.equals(renkanUser.getUsername())) { |
|
246 |
colorFound = true; |
|
247 |
break; |
|
248 |
} |
|
249 |
} |
|
250 |
} |
|
251 |
} |
|
252 |
||
253 |
RenkanUser ruser = new RenkanUser(this.getId(), null, color, username); |
|
254 |
this.users.add(ruser); |
|
255 |
} |
|
256 |
||
257 |
@Override |
|
258 |
protected String getRawKeyPart() { |
|
259 |
return Long.toString(this.getCreated().getTime()); |
|
260 |
} |
|
261 |
||
| 45 | 262 |
} |