コード例 #1
0
 @Deprecated
 public List<?> getEntities(EntityReference ref, Search search) {
   // get the pollId
   Restriction pollRes = search.getRestrictionByProperty("pollId");
   if (pollRes == null || pollRes.getSingleValue() == null) {
     throw new IllegalArgumentException(
         "Must include a non-null pollId in order to retreive a list of votes");
   }
   Long pollId = null;
   try {
     pollId = developerHelperService.convert(pollRes.getSingleValue(), Long.class);
   } catch (UnsupportedOperationException e) {
     throw new IllegalArgumentException(
         "Invalid: pollId must be a long number: " + e.getMessage(), e);
   }
   // get the poll
   Poll poll = pollListManager.getPollById(pollId);
   if (poll == null) {
     throw new IllegalArgumentException(
         "pollId (" + pollId + ") is invalid and does not match any known polls");
   } else {
     boolean allowedPublic = pollListManager.isPollPublic(poll);
     if (!allowedPublic) {
       String userReference = developerHelperService.getCurrentUserReference();
       if (userReference == null) {
         throw new EntityException(
             "User must be logged in in order to access poll data",
             ref.getId(),
             HttpServletResponse.SC_UNAUTHORIZED);
       } else {
         boolean allowedManage = false;
         boolean allowedVote = false;
         allowedManage =
             developerHelperService.isUserAllowedInEntityReference(
                 userReference, PollListManager.PERMISSION_ADD, "/site/" + poll.getSiteId());
         allowedVote =
             developerHelperService.isUserAllowedInEntityReference(
                 userReference, PollListManager.PERMISSION_VOTE, "/site/" + poll.getSiteId());
         if (!(allowedManage || allowedVote)) {
           throw new SecurityException(
               "User (" + userReference + ") not allowed to access poll data: " + ref);
         }
       }
     }
   }
   // get the options
   List<Option> options = pollListManager.getOptionsForPoll(pollId);
   return options;
 }
コード例 #2
0
 public List<?> getEntities(EntityReference ref, Search search) {
   System.out.println("get entities");
   // get the setting which indicates if we are getting polls we can admin or polls we can take
   boolean adminControl = false;
   Restriction adminRes = search.getRestrictionByProperty("admin");
   if (adminRes != null) {
     adminControl = developerHelperService.convert(adminRes.getSingleValue(), boolean.class);
   }
   // get the location (if set)
   Restriction locRes =
       search.getRestrictionByProperty(
           CollectionResolvable
               .SEARCH_LOCATION_REFERENCE); // requestStorage.getStoredValueAsType(String.class,
   // "siteId");
   String[] siteIds = null;
   if (locRes != null) {
     String siteId = developerHelperService.getLocationIdFromRef(locRes.getStringValue());
     siteIds = new String[] {siteId};
   }
   // get the user (if set)
   Restriction userRes =
       search.getRestrictionByProperty(CollectionResolvable.SEARCH_USER_REFERENCE);
   String userId = null;
   if (userRes != null) {
     String currentUser = developerHelperService.getCurrentUserReference();
     String userReference = userRes.getStringValue();
     if (userReference == null) {
       throw new IllegalArgumentException(
           "Invalid request: Cannot limit polls by user when the value is null");
     }
     if (userReference.equals(currentUser) || developerHelperService.isUserAdmin(currentUser)) {
       userId =
           developerHelperService.getUserIdFromRef(
               userReference); // requestStorage.getStoredValueAsType(String.class, "userId");
     } else {
       throw new SecurityException(
           "Only the admin can get polls for other users, you requested polls for: "
               + userReference);
     }
   } else {
     userId = developerHelperService.getCurrentUserId();
     if (userId == null) {
       throw new EntityException(
           "No user is currently logged in so no polls data can be retrieved",
           ref.getId(),
           HttpServletResponse.SC_UNAUTHORIZED);
     }
   }
   String perm = PollListManager.PERMISSION_VOTE;
   if (adminControl) {
     perm = PollListManager.PERMISSION_ADD;
   }
   List<Poll> polls =
       pollListManager.findAllPollsForUserAndSitesAndPermission(userId, siteIds, perm);
   if (adminControl) {
     // add in options
     for (Poll p : polls) {
       List<Option> options = pollListManager.getOptionsForPoll(p.getPollId());
       p.setOptions(options);
     }
   } else {
     // add in the indicators that this user has replied
     Long[] pollIds = new Long[polls.size()];
     for (int i = 0; i < polls.size(); i++) {
       pollIds[i] = polls.get(i).getPollId();
     }
     Map<Long, List<Vote>> voteMap = pollVoteManager.getVotesForUser(userId, pollIds);
     for (Poll poll : polls) {
       Long pollId = poll.getPollId();
       List<Vote> l = voteMap.get(pollId);
       if (l != null) {
         poll.setCurrentUserVoted(true);
         poll.setCurrentUserVotes(l);
       } else {
         poll.setCurrentUserVoted(false);
       }
     }
   }
   return polls;
 }