@Override
 public void classpageUserRemove(String code, List<String> mailIds, User apiCaller)
     throws Exception {
   UserGroup userGroup = this.getUserGroupService().findUserGroupByGroupCode(code);
   Classpage classpage = this.getCollectionRepository().getClasspageByCode(code);
   if (userGroup != null && classpage != null) {
     for (String mailId : mailIds) {
       Identity identity = this.getUserRepository().findByEmailIdOrUserName(mailId, true, false);
       if (identity != null) {
         UserGroupAssociation userGroupAssociation =
             this.getUserRepository()
                 .getUserGroupMemebrByGroupUid(
                     userGroup.getPartyUid(), identity.getUser().getPartyUid());
         if (userGroupAssociation != null) {
           this.getUserGroupRepository().remove(userGroupAssociation);
         }
         InviteUser inviteUser =
             this.getInviteRepository().findInviteUserById(mailId, classpage.getGooruOid());
         if (inviteUser != null) {
           this.getInviteRepository().remove(inviteUser);
         }
       }
     }
   }
 }
 @Override
 public ActionResponseDTO<Classpage> createClasspage(
     Classpage classpage, boolean addToUserClasspage, String assignmentId) throws Exception {
   Errors errors = validateClasspage(classpage);
   if (!errors.hasErrors()) {
     this.getCollectionRepository().save(classpage);
     if (assignmentId != null && !assignmentId.isEmpty()) {
       CollectionItem collectionItem = new CollectionItem();
       collectionItem.setItemType(ShelfType.AddedType.ADDED.getAddedType());
       collectionItem =
           this.createClasspageItem(
                   assignmentId,
                   classpage.getGooruOid(),
                   collectionItem,
                   classpage.getUser(),
                   CollectionType.CLASSPAGE.getCollectionType())
               .getModel();
       Set<CollectionItem> collectionItems = new TreeSet<CollectionItem>();
       collectionItems.add(collectionItem);
       classpage.setCollectionItems(collectionItems);
     }
     if (addToUserClasspage) {
       CollectionItem collectionItem = new CollectionItem();
       collectionItem.setItemType(ShelfType.AddedType.ADDED.getAddedType());
       this.createClasspageItem(
           classpage.getGooruOid(),
           null,
           collectionItem,
           classpage.getUser(),
           CollectionType.USER_CLASSPAGE.getCollectionType());
     }
     this.getCollectionRepository().save(classpage);
   }
   return new ActionResponseDTO<Classpage>(classpage, errors);
 }
  @Override
  public ActionResponseDTO<Classpage> createClasspage(Classpage classpage, String collectionId)
      throws Exception {

    return this.createClasspage(
        classpage, classpage.getCollectionItem(), collectionId, classpage.getUser());
  }
 @Override
 public List<Map<String, Object>> classpageUserJoin(
     String code, List<String> mailIds, User apiCaller) throws Exception {
   List<Map<String, Object>> classpageMember = new ArrayList<Map<String, Object>>();
   UserGroup userGroup = this.getUserGroupService().findUserGroupByGroupCode(code);
   Classpage classpage = this.getCollectionRepository().getClasspageByCode(code);
   if (userGroup != null && classpage != null) {
     for (String mailId : mailIds) {
       Identity identity = this.getUserRepository().findByEmailIdOrUserName(mailId, true, false);
       if (identity != null) {
         InviteUser inviteUser =
             this.getInviteRepository().findInviteUserById(mailId, classpage.getGooruOid());
         if (inviteUser != null) {
           inviteUser.setStatus(
               this.getCustomTableRepository().getCustomTableValue(INVITE_USER_STATUS, ACTIVE));
           inviteUser.setJoinedDate(new Date(System.currentTimeMillis()));
           this.getInviteRepository().save(inviteUser);
         }
         if (this.getUserRepository()
                 .getUserGroupMemebrByGroupUid(
                     userGroup.getPartyUid(), identity.getUser().getPartyUid())
             == null) {
           UserGroupAssociation groupAssociation = new UserGroupAssociation();
           groupAssociation.setIsGroupOwner(0);
           groupAssociation.setUser(identity.getUser());
           groupAssociation.setUserGroup(userGroup);
           this.getUserRepository().save(groupAssociation);
           classpageMember.add(setMemberResponse(groupAssociation, ACTIVE));
         }
       }
     }
   }
   return classpageMember;
 }
 @Override
 public ActionResponseDTO<Classpage> createClasspage(
     Classpage newClasspage, CollectionItem newCollectionItem, String gooruOid, User user)
     throws Exception {
   Errors errors = validateClasspage(newClasspage);
   if (!errors.hasErrors()) {
     this.getCollectionRepository().save(newClasspage);
     this.getUserGroupService()
         .createGroup(
             newClasspage.getTitle(), newClasspage.getClasspageCode(), "System", user, null);
     if (gooruOid != null && !gooruOid.isEmpty() && newCollectionItem != null) {
       CollectionItem collectionItem = new CollectionItem();
       collectionItem.setItemType(ShelfType.AddedType.ADDED.getAddedType());
       collectionItem.setPlannedEndDate(newCollectionItem.getPlannedEndDate());
       collectionItem.setNarration(newCollectionItem.getNarration());
       collectionItem =
           this.createCollectionItem(
                   gooruOid,
                   newClasspage.getGooruOid(),
                   collectionItem,
                   newClasspage.getUser(),
                   CollectionType.COLLECTION.getCollectionType(),
                   false)
               .getModel();
       Set<CollectionItem> collectionItems = new TreeSet<CollectionItem>();
       collectionItems.add(collectionItem);
       newClasspage.setCollectionItems(collectionItems);
       this.getCollectionRepository().save(newClasspage);
     }
   }
   return new ActionResponseDTO<Classpage>(newClasspage, errors);
 }
 @Override
 public Classpage getClasspage(String classpageCode, User user) throws Exception {
   Classpage classpage = this.getCollectionRepository().getClasspageByCode(classpageCode);
   if (classpage == null) {
     throw new NotFoundException("Class not found");
   }
   return getClasspage(classpage.getGooruOid(), user, PERMISSIONS);
 }
 @Override
 public void deleteClasspage(String classpageId) {
   Classpage classpage = this.getClasspage(classpageId, null, null);
   if (classpage != null) {
     this.getUserRepository()
         .remove(
             this.getUserGroupService().findUserGroupByGroupCode(classpage.getClasspageCode()));
     this.getCollectionRepository().remove(Classpage.class, classpage.getContentId());
   } else {
     throw new NotFoundException(generateErrorMessage(GL0056, CLASSPAGE));
   }
 }
 private List<Map<String, Object>> getPendingMemberList(String code) {
   Classpage classpage = this.getCollectionRepository().getClasspageByCode(code);
   if (classpage == null) {
     throw new NotFoundException("Class not found!!!");
   }
   List<InviteUser> inviteUsers =
       this.getInviteRepository().getInviteUsersById(classpage.getGooruOid());
   List<Map<String, Object>> pendingList = new ArrayList<Map<String, Object>>();
   if (inviteUsers != null) {
     for (InviteUser inviteUser : inviteUsers) {
       pendingList.add(this.setInviteMember(inviteUser, PENDING));
     }
   }
   return pendingList;
 }
 private Errors validateClasspage(Classpage classpage) throws Exception {
   final Errors errors = new BindException(classpage, CLASSPAGE);
   if (classpage != null) {
     rejectIfNullOrEmpty(
         errors, classpage.getTitle(), TITLE, GL0006, generateErrorMessage(GL0006, TITLE));
   }
   return errors;
 }
 @Override
 public Classpage getClasspage(String collectionId, User user, String merge) {
   Classpage classpage = this.getCollectionRepository().getClasspageByGooruOid(collectionId, null);
   if (classpage != null && merge != null) {
     Map<String, Object> permissions = new HashMap<String, Object>();
     Boolean isMember = false;
     String status = NOTINVITED;
     InviteUser inviteUser = null;
     String mailId = null;
     UserGroup userGroup =
         this.getUserGroupService().findUserGroupByGroupCode(classpage.getClasspageCode());
     if (userGroup != null && !user.getGooruUId().equalsIgnoreCase(ANONYMOUS)) {
       isMember =
           this.getUserRepository()
                       .getUserGroupMemebrByGroupUid(userGroup.getPartyUid(), user.getPartyUid())
                   != null
               ? true
               : false;
       if (isMember) {
         status = ACTIVE;
       }
       if (user.getIdentities().size() > 0) {
         mailId = user.getIdentities().iterator().next().getExternalId();
       }
       inviteUser = this.getInviteRepository().findInviteUserById(mailId, collectionId);
       if (!isMember && inviteUser == null && classpage.getSharing().equalsIgnoreCase(PUBLIC)) {
         inviteUser =
             this.getInviteService().createInviteUserObj(mailId, collectionId, CLASS, user);
         this.getInviteRepository().save(inviteUser);
         this.getInviteRepository().flush();
       }
       if (inviteUser != null) {
         status = PENDING;
       }
     }
     if (merge.contains(PERMISSIONS)) {
       permissions.put(
           PERMISSIONS, this.getContentService().getContentPermission(collectionId, user));
     }
     permissions.put(STATUS, status);
     classpage.setMeta(permissions);
   }
   return classpage;
 }
 @Override
 public List<Map<String, String>> inviteUserForClass(
     List<String> emails, String classCode, User apiCaller) {
   Classpage classPage = this.getCollectionRepository().getClasspageByCode(classCode);
   if (classPage == null) {
     throw new NotFoundException(generateErrorMessage(GL0006, CLASS));
   }
   List<Map<String, String>> invites = new ArrayList<Map<String, String>>();
   List<InviteUser> inviteUsers = null;
   for (String email : emails) {
     InviteUser inviteUser =
         this.getInviteRepository().findInviteUserById(email, classPage.getGooruOid(), null);
     inviteUsers = new ArrayList<InviteUser>();
     if (inviteUser == null) {
       inviteUsers.add(createInviteUserObj(email, classPage.getGooruOid(), CLASS, apiCaller));
     }
     Map<String, String> inviteMap = new HashMap<String, String>();
     inviteMap.put(EMAIL_ID, email);
     inviteMap.put(GOORU_OID, classPage.getGooruOid());
     inviteMap.put(STATUS, PENDING);
     invites.add(inviteMap);
     Profile profile = this.getUserRepository().getProfile(classPage.getUser(), false);
     String gender = "";
     String noun = "";
     if (profile.getGender() != null) {
       if (profile.getGender().getName().equalsIgnoreCase(FEMALE)) {
         gender = MS;
         noun = HER;
       } else if (profile.getGender().getName().equalsIgnoreCase(MALE)) {
         gender = MR;
         noun = HIS;
       }
     }
     this.getMailHandler()
         .sendMailToInviteUser(inviteMap, classPage.getUser(), classPage.getTitle(), gender, noun);
   }
   if (inviteUsers != null) {
     this.getInviteRepository().saveAll(inviteUsers);
   }
   return invites;
 }
  @Override
  public ActionResponseDTO<CollectionItem> createClasspageItem(
      String assignmentGooruOid,
      String classpageGooruOid,
      CollectionItem collectionItem,
      User user,
      String type)
      throws Exception {
    Classpage classpage = null;
    if (type != null && type.equalsIgnoreCase(CollectionType.USER_CLASSPAGE.getCollectionType())) {
      if (classpageGooruOid != null) {
        classpage = this.getClasspage(classpageGooruOid, null, null);
      } else {
        classpage =
            this.getCollectionRepository()
                .getUserShelfByClasspageGooruUid(
                    user.getGooruUId(), CollectionType.USER_CLASSPAGE.getCollectionType());
      }
      if (classpage == null) {
        classpage = new Classpage();
        classpage.setTitle(MY_CLASSPAGE);
        classpage.setCollectionType(CollectionType.USER_CLASSPAGE.getCollectionType());
        classpage.setClasspageCode(BaseUtil.base48Encode(7));
        classpage.setGooruOid(UUID.randomUUID().toString());
        ContentType contentType =
            (ContentType)
                this.getCollectionRepository().get(ContentType.class, ContentType.RESOURCE);
        classpage.setContentType(contentType);
        ResourceType resourceType =
            (ResourceType)
                this.getCollectionRepository()
                    .get(ResourceType.class, ResourceType.Type.CLASSPAGE.getType());
        classpage.setResourceType(resourceType);
        classpage.setLastModified(new Date(System.currentTimeMillis()));
        classpage.setCreatedOn(new Date(System.currentTimeMillis()));
        classpage.setSharing(Sharing.PRIVATE.getSharing());
        classpage.setUser(user);
        classpage.setOrganization(user.getPrimaryOrganization());
        classpage.setCreator(user);
        classpage.setDistinguish(Short.valueOf(ZERO));
        classpage.setRecordSource(NOT_ADDED);
        classpage.setIsFeatured(0);
        this.getCollectionRepository().save(classpage);
      }
      collectionItem.setItemType(ShelfType.AddedType.SUBSCRIBED.getAddedType());
    } else {
      classpage = this.getClasspage(classpageGooruOid, null, null);
      collectionItem.setItemType(ShelfType.AddedType.ADDED.getAddedType());
    }

    Collection collection =
        this.getCollectionRepository()
            .getCollectionByGooruOid(assignmentGooruOid, classpage.getUser().getGooruUId());
    Errors errors = validateClasspageItem(classpage, collection, collectionItem);
    if (collection != null) {
      if (!errors.hasErrors()) {
        collectionItem.setCollection(classpage);
        collectionItem.setResource(collection);
        int sequence =
            collectionItem.getCollection().getCollectionItems() != null
                ? collectionItem.getCollection().getCollectionItems().size() + 1
                : 1;
        collectionItem.setItemSequence(sequence);
        this.getCollectionRepository().save(collectionItem);
      }
    } else {
      throw new Exception("invalid assignmentId -" + assignmentGooruOid);
    }

    return new ActionResponseDTO<CollectionItem>(collectionItem, errors);
  }
  public ActionResponseDTO<Classpage> updateClasspage(
      Classpage newClasspage, String updateClasspageId, Boolean hasUnrestrictedContentAccess)
      throws Exception {
    Classpage classpage = this.getClasspage(updateClasspageId, null, null);
    Errors errors = validateUpdateClasspage(classpage, newClasspage);
    if (!errors.hasErrors()) {
      if (newClasspage.getVocabulary() != null) {
        classpage.setVocabulary(newClasspage.getVocabulary());
      }

      if (newClasspage.getTitle() != null) {
        classpage.setTitle(newClasspage.getTitle());
        UserGroup userGroup =
            this.getUserGroupService().findUserGroupByGroupCode(classpage.getClasspageCode());
        userGroup.setGroupName(newClasspage.getTitle());
        this.getUserRepository().save(userGroup);
      }
      if (newClasspage.getDescription() != null) {
        classpage.setDescription(newClasspage.getDescription());
      }
      if (newClasspage.getNarrationLink() != null) {
        classpage.setNarrationLink(newClasspage.getNarrationLink());
      }
      if (newClasspage.getEstimatedTime() != null) {
        classpage.setEstimatedTime(newClasspage.getEstimatedTime());
      }
      if (newClasspage.getNotes() != null) {
        classpage.setNotes(newClasspage.getNotes());
      }
      if (newClasspage.getGoals() != null) {
        classpage.setGoals(newClasspage.getGoals());
      }
      if (newClasspage.getKeyPoints() != null) {
        classpage.setGoals(newClasspage.getKeyPoints());
      }
      if (newClasspage.getLanguage() != null) {
        classpage.setLanguage(newClasspage.getLanguage());
      }
      if (newClasspage.getGrade() != null) {
        classpage.setGrade(newClasspage.getGrade());
      }
      if (newClasspage.getSharing() != null) {
        if (newClasspage.getSharing().equalsIgnoreCase(Sharing.PRIVATE.getSharing())
            || newClasspage.getSharing().equalsIgnoreCase(Sharing.PUBLIC.getSharing())
            || newClasspage.getSharing().equalsIgnoreCase(Sharing.ANYONEWITHLINK.getSharing())) {
          classpage.setSharing(newClasspage.getSharing());
        }
      }
      if (newClasspage.getLastUpdatedUserUid() != null) {
        classpage.setLastUpdatedUserUid(newClasspage.getLastUpdatedUserUid());
      }

      if (hasUnrestrictedContentAccess) {
        if (newClasspage.getCreator() != null && newClasspage.getCreator().getPartyUid() != null) {
          User user = userService.findByGooruId(newClasspage.getCreator().getPartyUid());
          classpage.setCreator(user);
        }

        if (newClasspage.getUser() != null && newClasspage.getUser().getPartyUid() != null) {
          User user = userService.findByGooruId(newClasspage.getUser().getPartyUid());
          classpage.setUser(user);
        }
      }

      this.getCollectionRepository().save(classpage);
    }
    return new ActionResponseDTO<Classpage>(classpage, errors);
  }