PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/core/infinit.e.data_model/src/com/ikanow/infinit/e/data_model/store/social/community/CommunityPojo.java

https://github.com/IKANOW/Infinit.e
Java | 588 lines | 325 code | 71 blank | 192 comment | 25 complexity | 46f46c02a15d4492235c6de727067b2b MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*******************************************************************************
  2. * Copyright 2012 The Infinit.e Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. ******************************************************************************/
  16. package com.ikanow.infinit.e.data_model.store.social.community;
  17. import java.io.IOException;
  18. import java.net.UnknownHostException;
  19. import java.util.Date;
  20. import java.util.HashSet;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Set;
  25. import org.bson.types.ObjectId;
  26. import com.mongodb.MongoException;
  27. import com.google.gson.reflect.TypeToken;
  28. import com.ikanow.infinit.e.data_model.store.BaseDbPojo;
  29. import com.ikanow.infinit.e.data_model.store.document.DocCountPojo;
  30. import com.ikanow.infinit.e.data_model.store.social.person.*;;
  31. /**
  32. * Class used to establish the community information in the environment
  33. * @author cvitter
  34. */
  35. public class CommunityPojo extends BaseDbPojo
  36. {
  37. // Standard static function for readability
  38. @SuppressWarnings("unchecked")
  39. static public TypeToken<List<CommunityPojo>> listType() { return new TypeToken<List<CommunityPojo>>(){}; }
  40. // (NOTE CAN'T CURRENTLY USE THIS AS QUERY POJO BECAUSE IT HAS DEFAULT PARAMS)
  41. /**
  42. * Private Class Variables
  43. */
  44. private ObjectId _id = null;
  45. private Date created = null;
  46. private Date modified = null;
  47. private String name = null;
  48. private String description = null;
  49. private Boolean isSystemCommunity = false;
  50. private ObjectId parentId = null;
  51. private List<ObjectId> parentTree = null;
  52. private String parentName = null;
  53. private Boolean isPersonalCommunity = false;
  54. private List<String> tags = null;
  55. private Map<String, CommunityAttributePojo> communityAttributes = null;
  56. private Map<String, CommunityUserAttributePojo> userAttributes = null;
  57. private ObjectId ownerId = null;
  58. private String communityStatus = "active"; //communityStatusEnum defaults to active
  59. private String ownerDisplayName = null;
  60. private int numberOfMembers = 0;
  61. private Set<CommunityMemberPojo> members = null;
  62. private Set<ObjectId> children = null;
  63. private DocCountPojo documentInfo = null; // (added by certain API calls, stored separately in the DB, under doc_metadata.doc_counts)
  64. public enum communityStatusEnum
  65. {
  66. ACTIVE {
  67. public String toString() {
  68. return "active";
  69. }
  70. },
  71. DISABLED {
  72. public String toString() {
  73. return "disabled";
  74. }
  75. },
  76. PENDING {
  77. public String toString() {
  78. return "pending";
  79. }
  80. }
  81. }
  82. /**
  83. * Get the id
  84. */
  85. public ObjectId getId() {
  86. return _id;
  87. }
  88. /**
  89. * Set the id
  90. */
  91. public void setId(ObjectId _id) {
  92. this._id = _id;
  93. }
  94. public void setCommunityStatus(String communityStatus) {
  95. this.communityStatus = communityStatus;
  96. }
  97. public String getCommunityStatus() {
  98. return this.communityStatus;
  99. }
  100. /**
  101. * Get the created date
  102. */
  103. public Date getCreated() {
  104. return created;
  105. }
  106. /**
  107. * Set the created date
  108. */
  109. public void setCreated(Date created) {
  110. this.created = created;
  111. }
  112. /**
  113. * Get the modified date
  114. */
  115. public Date getModified() {
  116. return modified;
  117. }
  118. /**
  119. * Set modified date
  120. */
  121. public void setModified(Date modified) {
  122. this.modified = modified;
  123. }
  124. /**
  125. * Get the community name
  126. */
  127. public String getName() {
  128. return name;
  129. }
  130. /**
  131. * Set the community name
  132. */
  133. public void setName(String name) {
  134. this.name = name;
  135. }
  136. /**
  137. * Get the community description
  138. */
  139. public String getDescription() {
  140. return description;
  141. }
  142. /**
  143. * Set the community description
  144. */
  145. public void setDescription(String description) {
  146. this.description = description;
  147. }
  148. /**
  149. * @param isSystemCommunity the isSystemCommunity to set
  150. */
  151. public void setIsSystemCommunity(Boolean isSystemCommunity) {
  152. this.isSystemCommunity = isSystemCommunity;
  153. }
  154. /**
  155. * @return the isSystemCommunity
  156. */
  157. public Boolean getIsSystemCommunity() {
  158. return isSystemCommunity;
  159. }
  160. /**
  161. * Get the community parent id
  162. */
  163. public ObjectId getParentId() {
  164. return parentId;
  165. }
  166. /**
  167. * Set the community parent id
  168. */
  169. public void setParentId(ObjectId parentId) {
  170. this.parentId = parentId;
  171. }
  172. /**
  173. * Get the community parent Tree
  174. */
  175. public List<ObjectId> getParentTree() {
  176. return parentTree;
  177. }
  178. /**
  179. * Set the community parent Tree
  180. */
  181. public void setParentTree(List<ObjectId> parentTree) {
  182. this.parentTree = parentTree;
  183. }
  184. /**
  185. * Get the community parent named
  186. */
  187. public String getParentName() {
  188. return parentName;
  189. }
  190. /**
  191. * Set the community parent name
  192. */
  193. public void setParentName(String parentName) {
  194. this.parentName = parentName;
  195. }
  196. /**
  197. * Set the is personal community true/false
  198. */
  199. public void setIsPersonalCommunity(Boolean isPersonalCommunity) {
  200. this.isPersonalCommunity = isPersonalCommunity;
  201. }
  202. /**
  203. * Get the is personal community value
  204. */
  205. public Boolean getIsPersonalCommunity() {
  206. return isPersonalCommunity;
  207. }
  208. /**
  209. * Set the list of community tags
  210. */
  211. public void setTags(List<String> tags) {
  212. this.tags = tags;
  213. }
  214. /**
  215. * Get the list of community tags
  216. */
  217. public List<String> getTags() {
  218. return tags;
  219. }
  220. /**
  221. * @param communityAttributes the communityAttributes to set
  222. */
  223. public void setCommunityAttributes(Map<String, CommunityAttributePojo> communityAttributes) {
  224. this.communityAttributes = communityAttributes;
  225. }
  226. /**
  227. * @return the communityAttributes
  228. */
  229. public Map<String, CommunityAttributePojo> getCommunityAttributes() {
  230. return communityAttributes;
  231. }
  232. /**
  233. * @param communityUserAttribute the communityUserAttribute to set
  234. */
  235. public void setCommunityUserAttribute(Map<String, CommunityUserAttributePojo> communityUserAttribute) {
  236. this.userAttributes = communityUserAttribute;
  237. }
  238. /**
  239. * @return the communityUserAttribute
  240. */
  241. public Map<String, CommunityUserAttributePojo> getCommunityUserAttribute() {
  242. return userAttributes;
  243. }
  244. /**
  245. * Get the community owner id
  246. */
  247. public ObjectId getOwnerId() {
  248. return ownerId;
  249. }
  250. /**
  251. * Set the community owner id
  252. */
  253. public void setOwnerId(ObjectId ownerId) {
  254. this.ownerId = ownerId;
  255. }
  256. /**
  257. * Get the community owner display name
  258. */
  259. public String getOwnerDisplayName() {
  260. return ownerDisplayName;
  261. }
  262. /**
  263. * Set the community owner display name
  264. */
  265. public void setOwnerDisplayName(String ownerDisplayName) {
  266. this.ownerDisplayName = ownerDisplayName;
  267. }
  268. /**
  269. * Set the number of community members
  270. */
  271. public void setNumberOfMembers(int numberOfMembers) {
  272. this.numberOfMembers = numberOfMembers;
  273. }
  274. /**
  275. * Get the number of community members
  276. */
  277. public int getNumberOfMembers() {
  278. return numberOfMembers;
  279. }
  280. /*
  281. * Set the community members
  282. */
  283. public void setMembers(Set<CommunityMemberPojo> members) {
  284. this.members = members;
  285. }
  286. /*
  287. * Get the community members
  288. */
  289. public Set<CommunityMemberPojo> getMembers() {
  290. return members;
  291. }
  292. /**
  293. * Check a user is a member of this community
  294. */
  295. public boolean isMember(ObjectId userID)
  296. {
  297. for ( CommunityMemberPojo cmp : members)
  298. if ( cmp.get_id().equals(userID) )
  299. return true;
  300. return false;
  301. }
  302. /**
  303. * Check a user is the owner of this community
  304. *
  305. * @param objectId
  306. * @return
  307. */
  308. public boolean isOwner(ObjectId userID)
  309. {
  310. if ( isPersonalCommunity ) {
  311. return userID.equals(_id);
  312. }
  313. else {
  314. return userID.equals(ownerId);
  315. }
  316. }
  317. /**
  318. * addMember
  319. * Attempts to add a user to community
  320. * @param pp
  321. * @throws IOException
  322. * @throws MongoException
  323. * @throws UnknownHostException
  324. */
  325. public void addMember(PersonPojo pp) throws UnknownHostException, MongoException, IOException
  326. {
  327. addMember(pp, false);
  328. }
  329. /**
  330. * addMember
  331. * Attempts to add a user to community
  332. *
  333. * If the person is already a member and isInvite is false, will set
  334. * the person to active
  335. *
  336. * @param pp
  337. * @param isInvite
  338. * @throws IOException
  339. * @throws MongoException
  340. * @throws UnknownHostException
  341. */
  342. public void addMember(PersonPojo pp, boolean isInvite) throws UnknownHostException, MongoException, IOException
  343. {
  344. CommunityMemberPojo old_cmp = null;
  345. for ( CommunityMemberPojo cmp : this.getMembers() )
  346. {
  347. if ( cmp.get_id().equals(pp.get_id()) )
  348. {
  349. //found the user
  350. old_cmp = cmp;
  351. break;
  352. }
  353. }
  354. // If the person is not already a member of the community or pending
  355. if ( old_cmp == null || old_cmp.getUserStatus().equals("pending") )
  356. {
  357. // Create the new member object
  358. CommunityMemberPojo cmp = new CommunityMemberPojo();
  359. cmp.set_id(pp.get_id());
  360. cmp.setEmail(pp.getEmail());
  361. cmp.setDisplayName(pp.getDisplayName());
  362. cmp.setUserType("member");
  363. if (isInvite)
  364. {
  365. cmp.setUserStatus("pending");
  366. }
  367. else
  368. {
  369. cmp.setUserStatus("active");
  370. this.setNumberOfMembers(this.getNumberOfMembers()+1);
  371. }
  372. // Set the userAttributes based on default
  373. Set<CommunityMemberUserAttributePojo> cmua = new HashSet<CommunityMemberUserAttributePojo>();
  374. Map<String, CommunityUserAttributePojo> cua = this.getCommunityUserAttribute();
  375. Iterator<Map.Entry<String, CommunityUserAttributePojo>> it = cua.entrySet().iterator();
  376. while (it.hasNext())
  377. {
  378. CommunityMemberUserAttributePojo c = new CommunityMemberUserAttributePojo();
  379. Map.Entry<String, CommunityUserAttributePojo> pair = it.next();
  380. c.setType(pair.getKey().toString());
  381. CommunityUserAttributePojo v = (CommunityUserAttributePojo)pair.getValue();
  382. c.setValue(v.getDefaultValue());
  383. cmua.add(c);
  384. }
  385. cmp.setUserAttributes(cmua);
  386. // Get Person data to added to member record
  387. if (pp.getContacts() != null)
  388. {
  389. // Set contacts from person record
  390. Set<CommunityMemberContactPojo> contacts = new HashSet<CommunityMemberContactPojo>();
  391. Map<String, PersonContactPojo> pcp = pp.getContacts();
  392. Iterator<Map.Entry<String, PersonContactPojo>> it2 = pcp.entrySet().iterator();
  393. while (it.hasNext())
  394. {
  395. CommunityMemberContactPojo c = new CommunityMemberContactPojo();
  396. Map.Entry<String, PersonContactPojo> pair = it2.next();
  397. c.setType(pair.getKey().toString());
  398. PersonContactPojo v = (PersonContactPojo)pair.getValue();
  399. c.setValue(v.getValue());
  400. contacts.add(c);
  401. }
  402. cmp.setContacts(contacts);
  403. }
  404. // Set links from person record
  405. if (pp.getLinks() != null)
  406. {
  407. // Set contacts from person record
  408. Set<CommunityMemberLinkPojo> links = new HashSet<CommunityMemberLinkPojo>();
  409. Map<String, PersonLinkPojo> plp = pp.getLinks();
  410. Iterator<Map.Entry<String, PersonLinkPojo>> it3 = plp.entrySet().iterator();
  411. while (it.hasNext())
  412. {
  413. CommunityMemberLinkPojo c = new CommunityMemberLinkPojo();
  414. Map.Entry<String, PersonLinkPojo> pair = it3.next();
  415. c.setTitle(pair.getKey().toString());
  416. PersonLinkPojo v = (PersonLinkPojo)pair.getValue();
  417. c.setUrl(v.getUrl());
  418. links.add(c);
  419. }
  420. cmp.setLinks(links);
  421. }
  422. //remove the old entry if it exists
  423. if ( old_cmp != null )
  424. {
  425. members.remove(old_cmp);
  426. }
  427. //TESTED removes the actually entry
  428. members.add(cmp);
  429. }
  430. }
  431. /**
  432. * removeMember
  433. * Attempts to remove a user from this community
  434. * @param personId
  435. */
  436. public void removeMember(ObjectId personId)
  437. {
  438. for ( CommunityMemberPojo cmp : members)
  439. {
  440. if ( cmp.get_id().equals(personId) )
  441. {
  442. //found member, remove
  443. members.remove(cmp);
  444. numberOfMembers--;
  445. break;
  446. }
  447. }
  448. }
  449. /**
  450. * getOwner
  451. * @return
  452. */
  453. public CommunityMemberPojo getOwner()
  454. {
  455. for ( CommunityMemberPojo cmp : members)
  456. {
  457. if ( cmp.get_id().equals(ownerId) )
  458. {
  459. //found owner, return
  460. return cmp;
  461. }
  462. }
  463. return null; //hopefully we find an owner
  464. }
  465. /**
  466. * updateMemberStatus
  467. * @param personId
  468. * @param userStatus
  469. * @return
  470. */
  471. public boolean updateMemberStatus(String personId, String userStatus)
  472. {
  473. ObjectId userID = new ObjectId(personId);
  474. //verified user, update status
  475. for ( CommunityMemberPojo cmp : members)
  476. {
  477. if ( cmp.get_id().equals(userID) )
  478. {
  479. cmp.setUserStatus(userStatus);
  480. return true;
  481. }
  482. }
  483. return false;
  484. }
  485. /**
  486. * createCommunityApprove
  487. * @param personId
  488. * @param communityId
  489. * @param requestType
  490. * @param requesterId
  491. * @return
  492. */
  493. public CommunityApprovePojo createCommunityApprove(String personId, String communityId,
  494. String requestType, String requesterId)
  495. {
  496. CommunityApprovePojo cap = new CommunityApprovePojo();
  497. cap.set_id(new ObjectId());
  498. cap.setCommunityId(communityId);
  499. cap.setIssueDate(new Date());
  500. cap.setPersonId(personId);
  501. cap.setRequesterId(requesterId);
  502. cap.setType(requestType);
  503. return cap;
  504. }
  505. public void setChildren(Set<ObjectId> children) {
  506. this.children = children;
  507. }
  508. public Set<ObjectId> getChildren() {
  509. return children;
  510. }
  511. public DocCountPojo getDocumentInfo() {
  512. return documentInfo;
  513. }
  514. public void setDocumentInfo(DocCountPojo documentInfo) {
  515. this.documentInfo = documentInfo;
  516. }
  517. }