1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.social.core.identity.model;
18
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.apache.commons.lang.StringUtils;
26 import org.exoplatform.social.core.chromattic.entity.ActivityProfileEntity;
27 import org.exoplatform.social.core.model.AvatarAttachment;
28 import org.exoplatform.social.core.profile.ProfileLifeCycle;
29
30
31
32
33 public class Profile {
34
35
36 public static final String GENDER = "gender";
37 public static final String MALE = "male";
38 public static final String FEMALE = "female";
39
40
41 public static final String USERNAME = "username";
42
43
44 public static final String FIRST_NAME = "firstName";
45
46
47 public static final String LAST_NAME = "lastName";
48
49
50 public static final String FULL_NAME = "fullName";
51
52
53 public static final String EMAIL = "email";
54
55
56 public static final String ABOUT_ME = "aboutMe";
57
58
59 public static final String DELETED = "deleted";
60
61
62
63
64 public static final String AVATAR = "avatar";
65
66 public static final String BANNER = "banner";
67
68
69 public static final String EXPERIENCES = "experiences";
70
71
72 public static final String EXPERIENCES_ID = "id";
73
74
75 public static final String EXPERIENCES_COMPANY = "company";
76
77
78 public static final String EXPERIENCES_POSITION = "position";
79
80
81 public static final String EXPERIENCES_SKILLS = "skills";
82
83
84 public static final String EXPERIENCES_START_DATE = "startDate";
85
86
87 public static final String EXPERIENCES_END_DATE = "endDate";
88
89
90 public static final String EXPERIENCES_IS_CURRENT = "isCurrent";
91
92
93 public static final String EXPERIENCES_DESCRIPTION = "description";
94
95
96 public static final String POSITION = "position";
97
98
99
100
101 @Deprecated
102 public static final String URL = "Url";
103
104
105 public static final String CONTACT_PHONES = "phones";
106
107
108 public static final String CONTACT_IMS = "ims";
109
110
111 public static final String CONTACT_URLS = "urls";
112
113
114 public static final String URL_POSTFIX = "Url";
115
116
117 public static final String RESIZED_SUBFIX = "RESIZED_";
118
119
120 private static final String SPACE_STR = " ";
121
122
123 public static enum UpdateType
124 {
125 POSITION,
126 BASIC_INFOR,
127 CONTACT,
128 EXPERIENCES,
129 AVATAR,
130 ABOUT_ME,
131 BANNER;
132
133 public void updateActivity(ProfileLifeCycle profileLifeCycle, Profile profile) {
134 switch (this) {
135 case ABOUT_ME: {
136 profileLifeCycle.aboutMeUpdated(profile.getIdentity().remoteId, profile);
137 break;
138 }
139 case CONTACT: {
140 profileLifeCycle.contactUpdated(profile.getIdentity().getRemoteId(), profile);
141 break;
142 }
143 case EXPERIENCES: {
144 profileLifeCycle.experienceUpdated(profile.getIdentity().getRemoteId(), profile);
145 break;
146 }
147 case AVATAR: {
148 profileLifeCycle.avatarUpdated(profile.getIdentity().getRemoteId(), profile);
149 break;
150 }
151 case BANNER: {
152 profileLifeCycle.bannerUpdated(profile.getIdentity().getRemoteId(), profile);
153 break;
154 }
155 default :
156 break;
157 }
158 }
159 };
160
161 public static enum AttachedActivityType
162 {
163 USER("userProfileActivityId"),
164 SPACE("spaceProfileActivityId"),
165 RELATION("relationActivityId"),
166 RELATIONSHIP("relationShipActivityId");
167
168 private String type;
169 private AttachedActivityType(String type) {
170 this.type = type;
171 }
172 public String value() {
173 return this.type;
174 }
175 public void setActivityId(ActivityProfileEntity entity, String activityId) {
176 switch (this) {
177 case USER: {
178 entity.setUserProfileActivityId(activityId);
179 break;
180 }
181 case SPACE: {
182 entity.setSpaceProfileActivityId(activityId);
183 break;
184 }
185 case RELATION: {
186 entity.setRelationActivityId(activityId);
187 break;
188 }
189 case RELATIONSHIP: {
190 entity.setRelationShipActivityId(activityId);
191 break;
192 }
193 default :
194 break;
195 }
196 }
197 public String getActivityId(ActivityProfileEntity entity) {
198 switch (this) {
199 case USER: {
200 return entity.getUserProfileActivityId();
201 }
202 case SPACE: {
203 return entity.getSpaceProfileActivityId();
204 }
205 case RELATION: {
206 return entity.getRelationActivityId();
207 }
208 case RELATIONSHIP: {
209 return entity.getRelationShipActivityId();
210 }
211 default : {
212 return null;
213 }
214 }
215 }
216 };
217
218
219
220 private final Map<String, Object> properties = new HashMap<String, Object>();
221
222 private static final Map<UpdateType, String[]> updateTypes = new HashMap<UpdateType, String[]>();
223 static {
224 updateTypes.put(UpdateType.POSITION, new String[] {POSITION});
225 updateTypes.put(UpdateType.BASIC_INFOR, new String[] {FIRST_NAME, LAST_NAME, EMAIL});
226 updateTypes.put(UpdateType.CONTACT, new String[] {GENDER, CONTACT_PHONES, CONTACT_IMS, CONTACT_URLS});
227 updateTypes.put(UpdateType.EXPERIENCES, new String[] {EXPERIENCES});
228 updateTypes.put(UpdateType.AVATAR, new String[] {AVATAR});
229 updateTypes.put(UpdateType.BANNER, new String[] {BANNER});
230 }
231
232
233 private final Identity identity;
234
235
236 private String id;
237
238
239 private long lastLoaded;
240
241
242 private boolean hasChanged;
243
244
245 private UpdateType updateType;
246
247
248 private String url;
249
250
251 private String avatarUrl;
252
253
254 private String bannerUrl;
255
256 private AttachedActivityType attachedActivityType;
257
258
259 private long createdTime;
260
261 private List<UpdateType> listUpdateTypes;
262
263
264 private Long avatarLastUpdated;
265
266
267 private Long bannerLastUpdated;
268
269
270
271
272
273
274 public Profile(final Identity identity) {
275 this.identity = identity;
276 }
277
278
279
280
281
282
283 public final Identity getIdentity() {
284 return identity;
285 }
286
287
288
289
290
291
292 public final String getId() {
293 return id;
294 }
295
296
297
298
299
300
301 public final void setId(final String id) {
302 this.id = id;
303 }
304
305
306
307
308
309
310 public final long getLastLoaded() {
311 return lastLoaded;
312 }
313
314
315
316
317
318
319 public final void setLastLoaded(final long lastLoaded) {
320 this.lastLoaded = lastLoaded;
321 }
322
323
324
325
326
327
328 public final boolean hasChanged() {
329 return hasChanged;
330 }
331
332
333
334
335 public final void clearHasChanged() {
336 setHasChanged(false);
337 }
338
339
340
341
342
343
344 public UpdateType getUpdateType() {
345 return updateType;
346 }
347
348 public AttachedActivityType getAttachedActivityType() {
349 return attachedActivityType;
350 }
351
352 public void setAttachedActivityType(AttachedActivityType attachedActivityType) {
353 this.attachedActivityType = attachedActivityType;
354 }
355
356
357
358
359
360
361
362 protected void setUpdateType(String updateType) {
363 for (UpdateType key : updateTypes.keySet()) {
364 String[] updateTypeValues = updateTypes.get(key);
365 for (String value : updateTypeValues) {
366 if(value.equals(updateType)) {
367 this.updateType = key;
368 break;
369 }
370 }
371 }
372 }
373
374
375
376
377
378
379 private void setHasChanged(final boolean hasChanged) {
380 this.hasChanged = hasChanged;
381 }
382
383
384
385
386
387
388
389 public final Object getProperty(final String name) {
390
391
392 if (URL.equals(name)) {
393 return this.url;
394 }
395
396 return properties.get(name);
397 }
398
399
400
401
402
403
404
405 public final void setProperty(final String name, final Object value) {
406
407
408 if (URL.equals(name)) {
409 this.url = value.toString();
410 return;
411 }
412
413 properties.put(name, value);
414 setHasChanged(true);
415 setUpdateType(name);
416
417 }
418
419
420
421
422
423
424
425 public final boolean contains(final String name) {
426 return properties.containsKey(name);
427 }
428
429
430
431
432
433
434 public final Map<String, Object> getProperties() {
435 return properties;
436 }
437
438
439
440
441
442
443 public final void removeProperty(final String name) {
444 properties.remove(name);
445 setHasChanged(true);
446 }
447
448
449
450
451
452
453
454
455
456 public final Object getPropertyValue(final String name) {
457 return getProperty(name);
458 }
459
460
461
462
463
464
465 public final String getFullName() {
466 String fullName = (String) getProperty(FULL_NAME);
467 if (fullName != null && fullName.length() > 0) {
468 return fullName;
469 }
470
471 String firstName = (String) getProperty(FIRST_NAME);
472 String lastName = (String) getProperty(LAST_NAME);
473 fullName = (firstName != null) ? firstName : StringUtils.EMPTY;
474 fullName += (lastName != null) ? SPACE_STR + lastName : StringUtils.EMPTY;
475 return fullName;
476 }
477
478
479
480
481
482
483 public final String getUrl() {
484 return url;
485 }
486
487
488
489
490 public void setUrl(final String url) {
491 this.url = url;
492 }
493
494
495
496
497
498
499
500 public final String getEmail() {
501 return (String) getProperty(EMAIL);
502 }
503
504
505
506
507
508
509 public final void addOrModifyProperties(final Map<String, Object> props) {
510 Iterator<Map.Entry<String, Object>> it = props.entrySet().iterator();
511 while (it.hasNext()) {
512 Map.Entry<String, Object> entry = it.next();
513 String key = entry.getKey();
514
515 if (key.contains(":")) {
516 continue;
517 }
518 setProperty(key, entry.getValue());
519 }
520 setHasChanged(true);
521 }
522
523
524
525
526
527
528
529 @Deprecated
530 public final String getAvatarImageSource() {
531 return getAvatarUrl();
532 }
533
534
535
536
537
538
539
540 public final String getAvatarUrl() {
541 return avatarUrl;
542 }
543
544
545
546
547
548
549 public void setAvatarUrl(final String avatarUrl) {
550 this.avatarUrl = avatarUrl;
551 }
552
553
554
555
556
557
558
559 public final String getBannerUrl() {
560 return bannerUrl;
561 }
562
563
564
565
566
567
568 public void setBannerUrl(final String bannerUrl) {
569 this.bannerUrl = bannerUrl;
570 }
571
572
573
574
575
576
577
578 public final String getPosition() {
579 return (String) getProperty(Profile.POSITION);
580 }
581
582
583
584
585
586
587
588 public final String getGender() {
589 return (String) getProperty(Profile.GENDER);
590 }
591
592
593
594
595
596
597
598 public final List<Map<String, String>> getPhones() {
599 return (List<Map<String, String>>) getProperty(Profile.CONTACT_PHONES);
600 }
601
602 public long getCreatedTime() {
603 return createdTime;
604 }
605
606 public void setCreatedTime(Long createdTime) {
607 if (createdTime != null) {
608 this.createdTime = createdTime;
609 } else {
610 this.createdTime = System.currentTimeMillis();
611 }
612 }
613
614
615
616
617
618 @Override
619 public final String toString() {
620 return "[uuid : " + id + " identity : " + identity.getId() + " properties: " + properties;
621 }
622
623 public List<UpdateType> getListUpdateTypes() {
624 return listUpdateTypes != null ? listUpdateTypes : new ArrayList<UpdateType>();
625 }
626
627 public void setListUpdateTypes(List<UpdateType> listUpdateTypes) {
628 this.listUpdateTypes = listUpdateTypes;
629 }
630
631
632
633
634
635 public Long getAvatarLastUpdated() {
636 return avatarLastUpdated;
637 }
638
639
640
641
642
643 public void setAvatarLastUpdated(Long avatarLastUpdated) {
644 this.avatarLastUpdated = avatarLastUpdated;
645 }
646
647 public Long getBannerLastUpdated() {
648 return bannerLastUpdated;
649 }
650
651 public void setBannerLastUpdated(Long bannerLastUpdated) {
652 this.bannerLastUpdated = bannerLastUpdated;
653 }
654 }