//get the communitypojo
communityIdStr = allowCommunityRegex(personIdStr, communityIdStr);
DBObject communitydbo = DbManager.getSocial().getCommunity().findOne(new BasicDBObject("_id",communityId));
if ( communitydbo != null )
{
CommunityPojo cp = CommunityPojo.fromDb(communitydbo, CommunityPojo.class);
//get the personpojo
DBObject persondbo = DbManager.getSocial().getPerson().findOne(new BasicDBObject("_id",new ObjectId(personIdStr)));
if ( persondbo != null )
{
//PersonPojo pp = gson.fromJson(persondbo.toString(),PersonPojo.class);
if ( !cp.getIsPersonalCommunity() )
{
if ( cp.isOwner(new ObjectId(personIdStr)) || isSysAdmin )
{
if (cp.getCommunityStatus().equals("disabled")) { // Delete for good, this is going to be ugly...
if ((null != cp.getChildren()) && !cp.getChildren().isEmpty()) {
rp.setResponse(new ResponseObject("Delete community", false, "Undeleted sub-communities exist, please delete them first"));
return rp;
}
//TESTED
// 1] Remove from all shares (delete shares if that leaves them orphaned)
BasicDBObject deleteQuery1 = new BasicDBObject(ShareCommunityPojo.shareQuery_id_, communityId);
BasicDBObject deleteFields1 = new BasicDBObject(SharePojo.communities_, 1);
List<SharePojo> shares = SharePojo.listFromDb(DbManager.getSocial().getShare().find(deleteQuery1, deleteFields1), SharePojo.listType());
for (SharePojo share: shares) {
if (1 == share.getCommunities().size()) { // delete this share
DbManager.getSocial().getShare().remove(new BasicDBObject(SharePojo._id_, share.get_id()));
}
}
BasicDBObject update1 = new BasicDBObject(DbManager.pull_, new BasicDBObject(SharePojo.communities_,
new BasicDBObject(ShareOwnerPojo._id_, communityId)));
DbManager.getSocial().getShare().update(deleteQuery1, update1, false, true);
//TESTED (both types)
// 2] Remove from all sources (also delete the documents)
// (In most cases this will leave the source orphaned, so delete it)
BasicDBObject deleteQuery2 = new BasicDBObject(SourcePojo.communityIds_, communityId);
BasicDBObject deleteFields2 = new BasicDBObject(SourcePojo.communityIds_, 1);
List<SourcePojo> sources = SourcePojo.listFromDb(DbManager.getIngest().getSource().find(deleteQuery2, deleteFields2), SourcePojo.listType());
List<SourcePojo> failedSources = new ArrayList<SourcePojo>();
for (SourcePojo source: sources)
{
ResponsePojo rp1 = null;
SourceHandler tmpHandler = new SourceHandler();
if (1 == source.getCommunityIds().size()) { // delete this source
rp1 = tmpHandler.deleteSource(source.getId().toString(), communityIdStr, personIdStr, false);
// (deletes all docs and removes from the share)
}
else { // Still need to delete docs from this share from this community
rp1 = tmpHandler.deleteSource(source.getId().toString(), communityIdStr, personIdStr, true);
}
if ( rp1 != null && !rp1.getResponse().isSuccess() )
{
failedSources.add(source);
}
}
//if we have more than 1 failed source, bail out w/ error
if (failedSources.size() > 0 )
{
StringBuilder sb = new StringBuilder();
for ( SourcePojo source : failedSources )
sb.append(source.getId().toString() + " ");
rp.setResponse(new ResponseObject("Delete community", false, "Could not stop sources (they might be currently running): " + sb.toString()));
return rp;
}
BasicDBObject update2 = new BasicDBObject(DbManager.pull_, new BasicDBObject(SourcePojo.communityIds_, communityId));
DbManager.getSocial().getShare().update(deleteQuery2, update2, false, true);
//TESTED (both types, check docs deleted)
// 3] Remove from all map reduce jobs (delete any that it is only comm left on)
String customJobsMessage = removeCommunityFromJobs(personIdStr, communityId);
if ( customJobsMessage.length() > 0)
{
rp.setResponse(new ResponseObject("Delete community", false, "Could not stop all map reduce jobs (they might be currently running): " + customJobsMessage));
return rp;
}
// 4] Finally delete the object itself
DbManager.getSocial().getCommunity().remove(new BasicDBObject("_id", communityId));
// Remove from index:
GenericProcessingController.deleteCommunityDocIndex(communityId.toString(), cp.getParentId(), false);
//TESTED
// 5] Finally finally remove from parent communities
if (null != cp.getParentId()) {
BasicDBObject updateQuery = new BasicDBObject("_id", cp.getParentId());
BasicDBObject updateUpdate = new BasicDBObject(DbManager.pull_, new BasicDBObject("children", cp.getId()));
DbManager.getSocial().getCommunity().update(updateQuery, updateUpdate, false, true);
}
//TESTED
rp.setResponse(new ResponseObject("Delete community", true, "Community deleted forever. " + customJobsMessage));
}
else { // First time, just remove all users and disable
//at this point, we have verified, community/user exist, not a personal group, user is member and owner
//set community as inactive (for some reason we don't delete it)
DbManager.getSocial().getCommunity().update(new BasicDBObject("_id", communityId),
new BasicDBObject(DbManager.set_, new BasicDBObject("communityStatus","disabled")));
//remove all members
for ( CommunityMemberPojo cmp : cp.getMembers())
removeCommunityMember(personIdStr, communityIdStr, cmp.get_id().toString());
rp.setResponse(new ResponseObject("Delete community", true, "Community disabled successfully - call delete again to remove for good, including all sources, shares, and documents"));
}
}
else