Package com.ikanow.infinit.e.data_model.store.social.sharing

Examples of com.ikanow.infinit.e.data_model.store.social.sharing.SharePojo


      DBObject dboshare = DbManager.getSocial().getShare().findOne(query);
      if ( dboshare != null )
      {
        //write everything but binary
        dboshare.removeField("binaryData");
        SharePojo share = SharePojo.fromDb(dboshare, 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: 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) {           
              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 json?
        if (null == share.getBinaryId()) {
          rp.setResponse(new ResponseObject("Update Share",false,"Unable to update share: this is not a binary 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.setMediaType(mediatype);
        share.setBinaryData(null);
        share.setBinaryId(updateGridFile(share.getBinaryId(), bytes));
       
        DbManager.getSocial().getShare().update(query, share.toDb());
       
        rp.setResponse(new ResponseObject("Update Share", true, "Binary share updated successfully"));
      }
      else
      {
View Full Code Here


  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)
View Full Code Here

    try
    {
      ObjectId shareId = new ObjectId();

      // Create a new SharePojo object
      SharePojo share = new SharePojo();
      share.set_id(shareId);
      share.setType(type);
      share.setTitle(title);
      share.setDescription(description);
      share.setCreated(new Date());
      share.setModified(new Date());
     
      // Create DocumentLocationPojo and add to the share
      DocumentLocationPojo documentLocation = new DocumentLocationPojo();
      setRefLocation(location, documentLocation);
      share.setDocumentLocation(documentLocation);
      if (null == documentLocation.getDatabase()) { // (local file)
        // Check, need to be admin:
        if (!RESTTools.adminLookup(ownerIdStr, false)) {
          rp.setResponse(new ResponseObject("Share", false, "Permission denied: you need to be admin to create a local file ref"));
          return rp;
        }       
        if ((null != type) && (type.equalsIgnoreCase("binary") || type.startsWith("binary:"))) {
          String[] binaryType = type.split(":", 2);
          if (binaryType.length > 1) {
            share.setMediaType(binaryType[1]);
            share.setType("binary");
          }
          else {
            share.setMediaType(MimeUtils.getMimeType(FilenameUtils.getExtension(idStr)));
          }
        }
        documentLocation.setCollection(idStr); // collection==file, database==id==null
      }//TESTED
      else {
        documentLocation.set_id(new ObjectId(idStr));       
      }
     
      // Get ShareOwnerPojo object
      PersonPojo owner = getPerson(new ObjectId(ownerIdStr));
      share.setOwner(getOwner(owner));
     
      // Endorsements:
      HashSet<ObjectId> endorsedSet = new HashSet<ObjectId>();
      share.setEndorsed(endorsedSet); // (you're always endorsed within your own community)
      endorsedSet.add(new ObjectId(ownerIdStr));       

      // Set Personal Community
      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.setResponse(new ResponseObject("Share", true, "New share added successfully. ID in data field."));
      rp.setData(share.get_id().toString(), null);
    }
    catch (Exception e)
    {
      //logger.error("Exception Message: " + e.getMessage(), e);
      rp.setResponse(new ResponseObject("Share", false, "Unable to add new share: " + e.getMessage()));
View Full Code Here

      query.put("_id", new ObjectId(shareIdStr));

      BasicDBObject dbo = (BasicDBObject)DbManager.getSocial().getShare().findOne(query);
      if (dbo != null)
      {
        SharePojo 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: 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) {           
              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 json?
        if (null == share.getDocumentLocation()) {
          rp.setResponse(new ResponseObject("Update Share",false,"Unable to update share: this is not a reference 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.setType(type);
        share.setTitle(title);
        share.setDescription(description);
        share.setModified(new Date());

        // Create DocumentLocationPojo and add to the share
        DocumentLocationPojo documentLocation = new DocumentLocationPojo();
        setRefLocation(location, documentLocation);
        share.setDocumentLocation(documentLocation);
        if (null == documentLocation.getDatabase()) { // (local file)
          // Check, need to be admin:
          if (!RESTTools.adminLookup(ownerIdStr, false)) {
            rp.setResponse(new ResponseObject("Share", false, "Permission denied: you need to be admin to update a local file ref"));
            return rp;
          }       
          if ((null != type) && (type.equalsIgnoreCase("binary") || type.startsWith("binary:"))) {
            String[] binaryType = type.split(":", 2);
            if (binaryType.length > 1) {
              share.setMediaType(binaryType[1]);
              share.setType("binary");
            }
            else {
              share.setMediaType(MimeUtils.getMimeType(FilenameUtils.getExtension(idStr)));
            }
          }
          documentLocation.setCollection(idStr); // collection==file, database==id==null
        }//TESTED
        else {
          documentLocation.set_id(new ObjectId(idStr));       
        }

        // Get ShareOwnerPojo object
        PersonPojo owner = getPerson(new ObjectId(ownerIdStr));
        share.setOwner(getOwner(owner));

        // Set Personal Community
        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().update(query, share.toDb());
        rp.setResponse(new ResponseObject("Share", true, "Share updated successfully."));
      }
      else
      {
        rp.setResponse(new ResponseObject("Share", false, "Unable to update share: only the owner of the share or admin can update it."));
View Full Code Here

      BasicDBObject query = new BasicDBObject(SharePojo._id_, new ObjectId(shareIdStr));
      query.put(ShareCommunityPojo.shareQuery_id_, communityId);
      BasicDBObject fields = new BasicDBObject(ShareOwnerPojo.shareQuery_id_, 1);
      fields.put(SharePojo.endorsed_, 1);
      BasicDBObject shareObj = (BasicDBObject) DbManager.getSocial().getShare().findOne(query, fields);
      SharePojo shareToEndorse = SharePojo.fromDb(shareObj, SharePojo.class);
      if (null == shareToEndorse) {
        rp.setResponse(new ResponseObject("Share", false, "Failed to locate share in community: " + shareIdStr + " vs " + communityIdStr));       
        return rp;
      }//TESTED
      // If we've got this far we're good to go
      BasicDBObject update = null;
      if ((null == shareToEndorse.getEndorsed()) && (null != shareToEndorse.getOwner())) {
        //Legacy case: create the owner's personal community in there
        update = new BasicDBObject(DbManager.addToSet_, new BasicDBObject(SharePojo.endorsed_, shareToEndorse.getOwner().get_id()));
        DbManager.getSocial().getShare().update(query, update, false, true);
      }//TESTED
      if (isEndorsed) {
        update = new BasicDBObject(DbManager.addToSet_, new BasicDBObject(SharePojo.endorsed_, communityId));
      }//TESTED
View Full Code Here

    mustBeOwnerOrAdmin(ownerIdStr, query);
   
    try
    {
      //first we must get the share to see if we need to remove the binary portion
      SharePojo sp = SharePojo.fromDb(DbManager.getSocial().getShare().findOne(query),SharePojo.class);     
      WriteResult wr = DbManager.getSocial().getShare().remove(query);
      if (wr.getN() ==  1)
      {
        //if remvoe was successful, attempt to remove the gridfs entry
        if ( sp.getBinaryId() != null )
        {
          //remove gridfs
          DbManager.getSocial().getShareBinary().remove(sp.getBinaryId());
        }
        rp.setResponse(new ResponseObject("Share", true, "Share removed from database successfully"));
      }
      else
      {
View Full Code Here

      communityIdStr = allowCommunityRegex(ownerIdStr, communityIdStr);
     
      BasicDBObject dbo = (BasicDBObject)DbManager.getSocial().getShare().findOne(query);
      if (dbo != null)
      {
        SharePojo share = SharePojo.fromDb(dbo, SharePojo.class)
       
        // Read write:
        if (null == share.getReadWrite()) {
          share.setReadWrite(new HashSet<ObjectId>());
        }
        ObjectId communityId = new ObjectId(communityIdStr);
        boolean changedReadWriteAccess = false;
        if (readWrite) { // set read-write up
          changedReadWriteAccess = share.getReadWrite().add(communityId);
        }       
        else {
          changedReadWriteAccess = share.getReadWrite().remove(communityId);
        }       

        // Check to see if the community is already in share.communities
        List<ShareCommunityPojo> communities = share.getCommunities();
        if (null == communities) {
          communities = new ArrayList<ShareCommunityPojo>();
        }
        Boolean addCommunity = true;
        for (ShareCommunityPojo scp : communities)
        {
          if (scp.get_id().toString().equalsIgnoreCase(communityIdStr)) addCommunity = false;
        }
       
        // Add new community to communities (or change its read/write permissions)
        if (addCommunity || changedReadWriteAccess)
        {         
          if (addCommunity) {
            ShareCommunityPojo cp = new ShareCommunityPojo();
            cp.set_id(new ObjectId(communityIdStr));
            cp.setName(getCommunity(new ObjectId(communityIdStr)).getName());
            cp.setComment(comment);
            communities.add(cp);
 
            // Endorse if applicable...
            if (null == share.getEndorsed()) { // legacy case
              share.setEndorsed(new HashSet<ObjectId>());
              share.getEndorsed().add(share.getOwner().get_id()); // user's personal community always endorsed
            }//TESTED
            boolean bAdmin = RESTTools.adminLookup(ownerIdStr, false); // (can be admin-on-request and not enabled, the bar for endorsing is pretty low)
            if (bAdmin || SocialUtils.isOwnerOrModeratorOrContentPublisher(communityIdStr, ownerIdStr))  {
              share.getEndorsed().add(cp.get_id());
            }
            //TESTED - adding as admin/community owner, not adding if not
          }         
          share.setModified(new Date());

          DbManager.getSocial().getShare().update(query, share.toDb());
          rp.setResponse(new ResponseObject("Share", true, "Community successfully added to the share"));
        }
        // Community already in share.communities
        else
        {
View Full Code Here

    BasicDBObject query = new BasicDBObject(SharePojo._id_, shareId);
    query.put("communities._id",new BasicDBObject( MongoDbManager.in_,communityIds.toArray()));
    DBObject dbo = DbManager.getSocial().getShare().findOne(query);
    if ( dbo != null )
    {
      SharePojo share = SharePojo.fromDb(dbo,SharePojo.class);
      return share.getShare();
   
    return null;
  }
View Full Code Here

    _reverseAliasTable.clear();
   
    _lastModified = new Date();
   
    _nNumAliasShares = 0;
    SharePojo personalShare = null;
    for (SharePojo share: aliasTables) {
      // Look for personal shares, apply them last
      if ((null != share.getCommunities()) && !share.getCommunities().isEmpty()) {
        SharePojo.ShareCommunityPojo primaryShareComm = share.getCommunities().iterator().next();
        if (null != primaryShareComm.get_id()) {
View Full Code Here

    return returnRepresentation(rp, startTime);
  }
 
  private SharePojo parseEntity(Representation entity) throws Exception
  {
    SharePojo share = null;
   
    //try to figure out what entity is (because for binary it can be a stream
    if ( entity == null )
    {
      //is empty, hopefully they passed args as url query params
      share = new SharePojo();     
    }
    else if ( entity.getMediaType().equals(MediaType.TEXT_PLAIN))
    {
      //is just a regular json share, parse it
      share = ApiManager.mapFromApi(entity.getText(), SharePojo.class, new SharePojoApiMap(null));
    }
    else
    {
      //is binary, pull in the bytes
      share = new SharePojo();
      InputStream instream = entity.getStream();
      ByteArrayOutputStream buffer = new ByteArrayOutputStream();
      int nRead;
      byte[] data = new byte[16384];
      while ((nRead = instream.read(data,0,data.length)) != -1)
      {
        buffer.write(data,0,nRead);
      }
      buffer.flush();
      share.setBinaryData( buffer.toByteArray() );
    }
   
    if ( share.getShare() != null )
      checkForBadCharacters(share.getShare());
   
    //url params always overwrite passed in json
    if ( type != null )
      share.setType(type);
    if ( title != null )
      share.setTitle(title);
    if ( description != null )
      share.setDescription(description);
    if ( id != null )
      share.set_id(new ObjectId(id));
    if ( communityIds != null )
    {
      String[] splits = communityIds.split("\\s*,\\s*");
      List<ShareCommunityPojo> comms = new ArrayList<SharePojo.ShareCommunityPojo>();
      for ( String s : splits )
      {
        ShareCommunityPojo scp = new ShareCommunityPojo();
        scp.set_id(new ObjectId(s));
        comms.add(scp);
      }
      share.setCommunities(comms);
    }
   
    return share;
  }
View Full Code Here

TOP

Related Classes of com.ikanow.infinit.e.data_model.store.social.sharing.SharePojo

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.