public void validateEndDate(FacesContext context, UIComponent component, Object value)
     throws ValidatorException {
   Date endDate = (Date) value;
   if (endDate == null) {
     ZVotesUtils.throwValidatorException("EndDateNotSet");
   }
   UIInput startDateComponent =
       (UIInput) (context.getViewRoot().findComponent("pollEditForm:startDate"));
   Date startDate = (Date) startDateComponent.getValue();
   if (startDate.after(endDate)) {
     ZVotesUtils.throwValidatorException("EndDateBeforeStartDate");
   }
   if (endDate.before(new Date())) {
     ZVotesUtils.throwValidatorException("EndDateBeforeNow");
   }
   if (current.getId() != null) {
     Date previousEndDate = getFacade().find(current.getId()).getEndDate();
     if ((current.getPollState().equals(PollState.PUBLISHED)
             || current.getPollState().equals(PollState.STARTED)
             || current.getPollState().equals(PollState.VOTING))
         && endDate.before(previousEndDate)) {
       ZVotesUtils.throwValidatorException("EndDateCantBeMovedToEarlierTime");
     }
   }
 }
 private void performDestroy() {
   try {
     getFacade().remove(current);
     ZVotesUtils.addInternationalizedInfoMessage("PollDeleted");
   } catch (Exception e) {
     ZVotesUtils.addInternationalizedErrorMessage("PersistenceErrorOccured");
   }
 }
 public String update() {
   try {
     getFacade().edit(current);
     ZVotesUtils.addInternationalizedInfoMessage("PollUpdated");
     return UrlsPy.POLL_LIST.getUrl(true);
   } catch (Exception e) {
     ZVotesUtils.addInternationalizedErrorMessage("PersistenceErrorOccured");
     return null;
   }
 }
 public String create() {
   try {
     getFacade().create(current);
     ZVotesUtils.addInternationalizedInfoMessage("PollCreated");
     return prepareList();
   } catch (Exception e) {
     ZVotesUtils.addInternationalizedErrorMessage("PersistenceErrorOccured");
     return null;
   }
 }
 public void validateDescription(FacesContext context, UIComponent component, Object value)
     throws ValidatorException {
   String description = (String) value;
   if (description.isEmpty()) {
     ZVotesUtils.throwValidatorException("DescriptionNotSet");
   }
 }
 public void validateStartDate(FacesContext context, UIComponent component, Object value)
     throws ValidatorException {
   Date startDate = (Date) value;
   if (startDate == null) {
     ZVotesUtils.throwValidatorException("StartDateNotSet");
   }
 }
 public void validateTitle(FacesContext context, UIComponent component, Object value)
     throws ValidatorException {
   String title = (String) value;
   if (title.isEmpty()) {
     ZVotesUtils.throwValidatorException("TitleNotSet");
   }
   List<Poll> polls_with_title = getFacade().findAllBy("title", title);
   int amt_polls_with_title = 0;
   for (Poll poll : polls_with_title) {
     if (!Objects.equals(poll.getId(), current.getId())) {
       amt_polls_with_title++;
     }
   }
   if (amt_polls_with_title >= 1) {
     ZVotesUtils.throwValidatorException("TitleAlreadyUsed");
   }
 }
 public boolean validate(Poll poll) {
   boolean result = true;
   if (poll.getItems().isEmpty()) {
     ZVotesUtils.addInternationalizedErrorMessage("PollNoItem");
     result = false;
   }
   if (poll.getStartDate() == null) {
     ZVotesUtils.addInternationalizedErrorMessage("StartDateNotSet");
     result = false;
   }
   if (poll.getEndDate() == null) {
     ZVotesUtils.addInternationalizedErrorMessage("EndDateNotSet");
     result = false;
   }
   if (poll.getEndDate().before(poll.getStartDate())) {
     ZVotesUtils.addInternationalizedErrorMessage("EndDateBeforeStartDate");
     result = false;
   }
   if (poll.getTitle().isEmpty()) {
     ZVotesUtils.addInternationalizedErrorMessage("TitleNotSet");
     result = false;
   }
   if (poll.getDescription().isEmpty()) {
     ZVotesUtils.addInternationalizedErrorMessage("DescriptionNotSet");
     result = false;
   }
   if (poll.getParticipants().size() < 3) {
     ZVotesUtils.addInternationalizedErrorMessage("PollLessThan3Participants");
     result = false;
   }
   for (Item item : poll.getItems()) {
     if (item.getType().equals(ItemType.YES_NO)) {
       item.setOwnOptions(false);
       itemFacade.edit(item);
     }
     if (item.getOptions().size() < 2 && !item.isOwnOptions()) {
       // replacing item's title
       HashMap<String, String> replaceMap = new HashMap<>();
       replaceMap.put("$item$", item.getTitle());
       ZVotesUtils.addInternationalizedErrorMessage("AnItemNeedsAtLeast2Options", replaceMap);
       result = false;
     }
     if (item.getOptions().size() < item.getM() && !item.isOwnOptions()) {
       HashMap<String, String> replaceMap = new HashMap<>();
       replaceMap.put("$item$", item.getTitle());
       ZVotesUtils.addInternationalizedErrorMessage("AnItemOptionsThanM", replaceMap);
       result = false;
     }
   }
   return result;
 }
  public String publish(Poll poll) {
    // validate Poll to see if it's ready to get public
    if (validate(poll)) {
      poll.setPollState(PollState.PUBLISHED);
      getFacade().edit(poll);
      // Start Tasks to set the Poll to state started and finished on the according dates
      taskManager.createStartPollTask(poll, getFacade());
      taskManager.createFinishPollTask(poll, getFacade());
      Locale locale = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();

      // create token for every participant
      for (Participant participant : poll.getParticipants()) {
        Token token = new Token();
        while (tokenFacade.countBy("tokenString", token.getTokenString()) > 0) {
          token = new Token();
        }
        token.setPoll(poll);
        tokenFacade.create(token);

        // only link if participationTracking is enabled
        if (poll.isParticipationTracking()) {
          token.setParticipant(participant);
          tokenFacade.edit(token);
          participant.setToken(token);
          participantFacade.edit(participant);
        }
      }
      HttpServletRequest request =
          (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
      String url =
          request.getScheme()
              + "://"
              + request.getServerName()
              + ":"
              + request.getServerPort()
              + "/";
      if (poll.isParticipationTracking() && poll.isReminderEmails()) {
        taskManager.createReminderMailTask(poll, eMailer, locale, url);
      }
      taskManager.createStartedMailTask(poll, eMailer, locale, url);
      ZVotesUtils.addInternationalizedInfoMessage("PollPublishedSuccessfully");
      return UrlsPy.POLL_LIST.getUrl(true);
    } else {
      return UrlsPy.POLL_LIST.getUrl(true);
    }
  }