@Override
 @Transactional(
     readOnly = false,
     propagation = Propagation.REQUIRED,
     rollbackFor = Exception.class)
 public ActionResponseDTO<CollectionItem> createResource(
     String collectionId, CollectionItem collectionItem, User user) {
   Resource resource = collectionItem.getResource();
   final Errors errors = validateResource(resource);
   if (!errors.hasErrors()) {
     Collection collection =
         getCollectionDao().getCollectionByType(collectionId, COLLECTION_TYPES);
     rejectIfNull(collection, GL0056, 404, COLLECTION);
     resource.setSharing(collection.getSharing());
     resource = getResourceBoService().createResource(resource, user);
     collectionItem.setItemType(ADDED);
     collectionItem = createCollectionItem(collectionItem, collection, resource, user);
     getCollectionEventLog()
         .collectionItemEventLog(
             collectionId, collectionItem, user.getPartyUid(), RESOURCE, collectionItem, ADD);
     updateCollectionMetaDataSummary(collection.getContentId(), RESOURCE, ADD);
     Map<String, Object> data =
         generateResourceMetaData(resource, collectionItem.getResource(), user);
     createContentMeta(resource, data);
   }
   return new ActionResponseDTO<CollectionItem>(collectionItem, errors);
 }
 public Segment setSegmentImageAbsolutePath(Resource resource, Segment segment) {
   if (resource != null && segment != null && segment.getSegmentImage() != null) {
     segment.setSegmentImage(
         resource.getAssetURI()
             + resource.getFolder()
             + Constants.SEGMENT_FOLDER
             + "/"
             + segment.getSegmentImage());
   }
   return segment;
 }
 public static void resetSegmentsSequence(Resource resource) {
   if (resource == null || resource.getResourceSegments() == null) {
     return;
   }
   List<Segment> segmentsList = new ArrayList<Segment>();
   segmentsList.addAll(resource.getResourceSegments());
   Collections.sort(segmentsList, new SegmentComparator());
   int sequence = 0;
   for (Segment segment : segmentsList) {
     segment.setSequence(++sequence);
   }
   resource.getResourceSegments().clear();
   resource.getResourceSegments().addAll(segmentsList);
 }
 private Errors validateResource(final Resource resource) {
   final Errors errors = new BindException(resource, RESOURCE);
   if (resource != null) {
     rejectIfNullOrEmpty(
         errors, resource.getTitle(), TITLE, GL0006, generateErrorMessage(GL0006, TITLE));
   }
   return errors;
 }
 @Override
 @Transactional(
     readOnly = false,
     propagation = Propagation.REQUIRED,
     rollbackFor = Exception.class)
 public CollectionItem addResource(String collectionId, String resourceId, User user) {
   Collection collection = getCollectionDao().getCollectionByType(collectionId, COLLECTION_TYPES);
   rejectIfNull(collection, GL0056, 404, COLLECTION);
   Resource resource = getResourceBoService().getResource(resourceId);
   rejectIfNull(resource, GL0056, 404, RESOURCE);
   reject(!resource.getContentType().getName().equalsIgnoreCase(QUESTION), GL0056, 404, RESOURCE);
   updateCollectionMetaDataSummary(collection.getContentId(), RESOURCE, ADD);
   CollectionItem collectionItem = new CollectionItem();
   getCollectionEventLog()
       .collectionItemEventLog(
           collectionId, collectionItem, user.getPartyUid(), RESOURCE, null, ADD);
   collectionItem.setItemType(ADDED);
   return createCollectionItem(collectionItem, collection, resource, user);
 }
 @Override
 @Transactional(
     readOnly = false,
     propagation = Propagation.REQUIRED,
     rollbackFor = Exception.class)
 public void deleteCollectionItem(String collectionId, String collectionItemId, String userUid) {
   CollectionItem collectionItem = this.getCollectionDao().getCollectionItem(collectionItemId);
   rejectIfNull(collectionItem, GL0056, 404, _COLLECTION_ITEM);
   Resource resource =
       this.getResourceBoService().getResource(collectionItem.getContent().getGooruOid());
   rejectIfNull(resource, GL0056, 404, RESOURCE);
   String contentType = resource.getContentType().getName();
   Long collectionContentId = collectionItem.getCollection().getContentId();
   this.resetSequence(
       collectionId, collectionItem.getCollectionItemId(), userUid, COLLECTION_ITEM);
   getCollectionEventLog()
       .collectionItemEventLog(collectionId, collectionItem, userUid, contentType, null, DELETE);
   if (contentType.equalsIgnoreCase(QUESTION)) {
     getCollectionDao().remove(resource);
   } else {
     getCollectionDao().remove(collectionItem);
   }
   updateCollectionMetaDataSummary(collectionContentId, RESOURCE, contentType);
 }
  private void sendMailToCollabrators(List<User> users, Resource resource, User apiCaller) {
    try {
      String flag = "";
      String collectionOrQuizTitle = "";
      Learnguide learnguide = learnguideRepository.findByContent(resource.getGooruOid());
      Assessment assessment = assessmentRepository.findQuizContent(resource.getGooruOid());
      if (resource
              .getResourceType()
              .getName()
              .equalsIgnoreCase(ResourceType.Type.CLASSPLAN.getType())
          || resource
              .getResourceType()
              .getName()
              .equalsIgnoreCase(ResourceType.Type.CLASSBOOK.getType())) {
        collectionOrQuizTitle = learnguide.getLesson();
        if (collectionOrQuizTitle == null) {
          collectionOrQuizTitle = learnguide.getTitle();
        }
        flag = COLLECTION;
      } else if (resource
              .getResourceType()
              .getName()
              .equalsIgnoreCase(ResourceType.Type.ASSESSMENT_QUIZ.getType())
          || resource
              .getResourceType()
              .getName()
              .equalsIgnoreCase(ResourceType.Type.ASSESSMENT_EXAM.getType())) {
        collectionOrQuizTitle = assessment.getName();
        if (collectionOrQuizTitle == null) {
          collectionOrQuizTitle = assessment.getTitle();
        }
        flag = "quiz";
      }
      for (User user : users) {

        mailHandler.sendMailForCollaborator(
            user.getPartyUid(),
            apiCaller.getUsername(),
            resource.getGooruOid(),
            collectionOrQuizTitle,
            flag);
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public void updateCollaborators(
      Resource resource,
      List<User> userList,
      User apiCaller,
      String predicate,
      boolean addToShelf) {

    Set<ContentPermission> contentPermissions = resource.getContentPermissions();
    List<User> newUsers = new ArrayList<User>();
    if (contentPermissions == null) {
      contentPermissions = new HashSet<ContentPermission>();
    }
    if (userList != null && userList.size() > 0) {
      Date date = new Date();
      for (User user : userList) {
        if (!user.getGooruUId().equals(resource.getUser().getGooruUId())) {
          boolean newFlag = true;
          for (ContentPermission contentPermission : contentPermissions) {
            if (contentPermission.getParty().getPartyUid().equals(user.getPartyUid())) {
              newFlag = false;
              break;
            }
          }
          if (newFlag) {
            ContentPermission contentPerm = new ContentPermission();
            contentPerm.setParty(user);
            contentPerm.setContent(resource);
            contentPerm.setPermission(EDIT);
            contentPerm.setValidFrom(date);
            contentPermissions.add(contentPerm);
            if (!newUsers.contains(user)) {
              newUsers.add(user);
            }
            if (addToShelf) {
              this.getShelfService().addCollaboratorShelf(contentPerm);
            }
          }
        }
      }
      if (addToShelf) {
        if (newUsers.size() > 0) {
          sendMailToCollabrators(newUsers, resource, apiCaller);
        }
      }
    }
    Set<ContentPermission> removePermissions = new HashSet<ContentPermission>();
    for (ContentPermission contentPermission : contentPermissions) {
      boolean remove = true;
      if (userList != null) {
        for (User user : userList) {
          if (user.getPartyUid().equals(contentPermission.getParty().getPartyUid())) {
            remove = false;
            break;
          }
        }
      }
      if (remove) {
        removePermissions.add(contentPermission);
      }
    }
    if (removePermissions.size() > 0) {
      this.getShelfService().removeCollaboratorShelf(removePermissions);
      contentPermissions.removeAll(removePermissions);
      this.getBaseRepository().removeAll(removePermissions);
    }

    this.getBaseRepository().save(resource);
  }