DBObject dbo = DbManager.getSocial().getCommunity().findOne(query);
if (dbo != null)
{
// Get GsonBuilder object with MongoDb de/serializers registered
CommunityPojo community = CommunityPojo.fromDb(dbo, CommunityPojo.class);
// Get the list of existing members, check to see if user is already
// a member of the community, make sure CommunityMember obj isn't null/empty
Boolean alreadyMember = false;
Set<CommunityMemberPojo> cmps = null;
if (community.getMembers() != null)
{
cmps = community.getMembers();
for (CommunityMemberPojo c : cmps)
{
if (c.get_id().toStringMongod().equals(personIdStr)) alreadyMember = true;
}
}
else
{
cmps = new HashSet<CommunityMemberPojo>();
community.setMembers(cmps);
}
if (!alreadyMember)
{
// Note: This changes community owner
if (userType.equals("owner"))
{
community.setOwnerId(new ObjectId(personIdStr));
community.setOwnerDisplayName(displayName);
}
// Create the new member object
CommunityMemberPojo cmp = new CommunityMemberPojo();
cmp.set_id(new ObjectId(personIdStr));
cmp.setEmail(email);
cmp.setDisplayName(displayName);
cmp.setUserStatus(userStatus);
cmp.setUserType(userType);
// Set the userAttributes based on default
Set<CommunityMemberUserAttributePojo> cmua = new HashSet<CommunityMemberUserAttributePojo>();
Map<String,CommunityUserAttributePojo> cua = community.getCommunityUserAttribute();
Iterator<Map.Entry<String,CommunityUserAttributePojo>> it = cua.entrySet().iterator();
while (it.hasNext())
{
CommunityMemberUserAttributePojo c = new CommunityMemberUserAttributePojo();
Map.Entry<String,CommunityUserAttributePojo> pair = it.next();
c.setType(pair.getKey().toString());
CommunityUserAttributePojo v = (CommunityUserAttributePojo)pair.getValue();
c.setValue(v.getDefaultValue());
cmua.add(c);
}
cmp.setUserAttributes(cmua);
// Get Person data to added to member record
BasicDBObject query2 = new BasicDBObject();
query2.put("_id", new ObjectId(personIdStr));
DBObject dbo2 = DbManager.getSocial().getPerson().findOne(query2);
PersonPojo p = PersonPojo.fromDb(dbo2, PersonPojo.class);
if (p.getContacts() != null)
{
// Set contacts from person record
Set<CommunityMemberContactPojo> contacts = new HashSet<CommunityMemberContactPojo>();
Map<String, PersonContactPojo> pcp = p.getContacts();
Iterator<Map.Entry<String, PersonContactPojo>> it2 = pcp.entrySet().iterator();
while (it2.hasNext())
{
CommunityMemberContactPojo c = new CommunityMemberContactPojo();
Map.Entry<String, PersonContactPojo> pair = it2.next();
c.setType(pair.getKey().toString());
PersonContactPojo v = (PersonContactPojo)pair.getValue();
c.setValue(v.getValue());
contacts.add(c);
}
cmp.setContacts(contacts);
}
// Set links from person record
if (p.getLinks() != null)
{
// Set contacts from person record
Set<CommunityMemberLinkPojo> links = new HashSet<CommunityMemberLinkPojo>();
Map<String, PersonLinkPojo> plp = p.getLinks();
Iterator<Map.Entry<String, PersonLinkPojo>> it3 = plp.entrySet().iterator();
while (it.hasNext())
{
CommunityMemberLinkPojo c = new CommunityMemberLinkPojo();
Map.Entry<String, PersonLinkPojo> pair = it3.next();
c.setTitle(pair.getKey().toString());
PersonLinkPojo v = (PersonLinkPojo)pair.getValue();
c.setUrl(v.getUrl());
links.add(c);
}
cmp.setLinks(links);
}
// Add new member object to the set
cmps.add(cmp);
// Increment number of members by 1
community.setNumberOfMembers(community.getNumberOfMembers() + 1);
/////////////////////////////////////////////////////////////////////////////////////////////////
// TODO (INF-1214): Make this code more robust to handle changes to the community that need to
// Caleb: this means change update to $set
/////////////////////////////////////////////////////////////////////////////////////////////////
DbManager.getSocial().getCommunity().update(query, community.toDb());
PersonHandler person = new PersonHandler();
person.addCommunity(personIdStr, communityIdStr, communityName);
rp.setData(community, new CommunityPojoApiMap());