public ResponsePojo saveJson(String ownerIdStr, String shareIdStr, String type, String title, String description, String json)
{
ResponsePojo rp = new ResponsePojo();
try
{
SharePojo share = null;
BasicDBObject dbo = null;
BasicDBObject query = null;
// Retrieve the share to update (if it exists)
if (shareIdStr != null && ObjectId.isValid(shareIdStr))
{
query = new BasicDBObject();
query.put("_id", new ObjectId(shareIdStr));
dbo = (BasicDBObject)DbManager.getSocial().getShare().findOne(query);
}
else
{
shareIdStr = new ObjectId().toString();
}
// Update existing share
if (dbo != null)
{
// 1a) if I'm the owner then GOTO_UPDATE
// 1b) if I'm the admin/mod
// (NEW) if I am a content publisher for all communities for which this share is read/write
share = SharePojo.fromDb(dbo, SharePojo.class);
// Check ... am I the owner?
ObjectId ownerId = new ObjectId(ownerIdStr);
boolean bAdminOrModOfAllCommunities = RESTTools.adminLookup(ownerIdStr);
if (!share.getOwner().get_id().equals(ownerId)) { // Then I have to be admin (except for one special case)
if (!bAdminOrModOfAllCommunities) {
// Special case #1: I am also community admin/moderator of every community to which this share belongs
bAdminOrModOfAllCommunities = true;
for (ShareCommunityPojo comm: share.getCommunities()) {
if (!SocialUtils.isOwnerOrModerator(comm.get_id().toString(), ownerIdStr)) {
bAdminOrModOfAllCommunities = false;
}
}//TESTED
if (!bAdminOrModOfAllCommunities) {
// Special case #2: I am a admin/mod/content publisher of *any* community that is read/write
boolean readWriteCase = false;
if (null != share.getReadWrite()) {
// I need to be content publisher across all shares
for (ObjectId readWriteCommId: share.getReadWrite()) {
if (SocialUtils.isOwnerOrModeratorOrContentPublisher(readWriteCommId.toString(), ownerIdStr)) {
readWriteCase = true;
break;
}
}
}//TESTED
if (!readWriteCase) {
rp.setResponse(new ResponseObject("Update Share",false,"Unable to update share: you are not owner or admin"));
return rp;
}
}
}
}//end if not owner
// Check: am I trying to update a reference or binary?
if ((null != share.getDocumentLocation()) || (null != share.getBinaryId())) {
rp.setResponse(new ResponseObject("Update Share",false,"Unable to update share: this is not a JSON share"));
return rp;
}
if (!bAdminOrModOfAllCommunities) { // quick check whether I'm admin on-request - if so can endorse
bAdminOrModOfAllCommunities = RESTTools.adminLookup(ownerIdStr, false);
}//TESTED
// Remove endorsements unless I'm admin (if I'm not admin I must be owner...)
if (!bAdminOrModOfAllCommunities) { // Now need to check if I'm admin/mod/content publisher for each community..
if (null == share.getEndorsed()) { // fill this with all allowed communities
share.setEndorsed(new HashSet<ObjectId>());
share.getEndorsed().add(share.getOwner().get_id()); // (will be added later)
for (ShareCommunityPojo comm: share.getCommunities()) {
if (SocialUtils.isOwnerOrModeratorOrContentPublisher(comm.get_id().toString(), ownerIdStr)) {
share.getEndorsed().add(comm.get_id());
}
}
}//TESTED
else {
for (ShareCommunityPojo comm: share.getCommunities()) {
// (leave it as is except remove anything that I can't endorse)
if (!SocialUtils.isOwnerOrModeratorOrContentPublisher(comm.get_id().toString(), ownerIdStr)) {
share.getEndorsed().remove(comm.get_id());
}
}
}//TESTED
}//TESTED
else {
if (null == share.getEndorsed()) { // fill this with all communities
share.setEndorsed(new HashSet<ObjectId>());
share.getEndorsed().add(share.getOwner().get_id());
for (ShareCommunityPojo comm: share.getCommunities()) {
share.getEndorsed().add(comm.get_id());
}
}
//(else just leave with the same set of endorsements as before)
}//TESTED
share.setModified(new Date());
share.setType(type);
share.setTitle(title);
share.setDescription(description);
share.setShare(json);
// Save the document to the share collection
DbManager.getSocial().getShare().update(query, share.toDb());
rp.setData(share, new SharePojoApiMap(null));
rp.setResponse(new ResponseObject("Share", true, "Share updated successfully."));
}
// Create new share
else
{
// Create a new SharePojo object
share = new SharePojo();
share.set_id(new ObjectId(shareIdStr));
share.setCreated(new Date());
share.setModified(new Date());
share.setType(type);
share.setTitle(title);
share.setDescription(description);
share.setShare(json);
HashSet<ObjectId> endorsedSet = new HashSet<ObjectId>();
share.setEndorsed(endorsedSet); // (you're always endorsed within your own community)
endorsedSet.add(new ObjectId(ownerIdStr));
// Get ShareOwnerPojo object and personal community
PersonPojo owner = getPerson(new ObjectId(ownerIdStr));
share.setOwner(getOwner(owner));
share.setCommunities(getPersonalCommunity(owner));
// Serialize the ID and Dates in the object to MongoDB format
// Save the document to the share collection
DbManager.getSocial().getShare().save(share.toDb());
rp.setData(share, new SharePojoApiMap(null));
rp.setResponse(new ResponseObject("Share", true, "New share added successfully."));
}
}
catch (Exception e)