Package org.jresponder.domain

Examples of org.jresponder.domain.Subscription


      Subscriber mySubscriber = subscriberService.lookupSubscriber(aEmail);
     
      if (mySubscriber == null) return webApiUtil.result404("Couldn't find subscriber");
     
      Subscription mySubscription = null;
      for (Subscription myTempSubscription: mySubscriber.getSubscriptions()) {
        if (myTempSubscription.getMessageGroupName().equals(aMessageGroupName)) {
          mySubscription = myTempSubscription;
        }
      }
View Full Code Here


       
        // find existing subscriber
        Subscriber mySubscriber = mainDao.getSubscriberByEmail(aEmail);
      logger().debug("Looked up subscriber with email ({}) and got: {}", aEmail, mySubscriber);
       
      Subscription mySubscription = null;
     
        // doesn't exist, create it
        if (mySubscriber == null) {
         
          logger().debug("No subscriber for this email, making a new record");
         
          // create subscriber
          mySubscriber = new Subscriber();
          mySubscriber.setEmail(aEmail);
          mySubscriber.setPropsMap(aSubscriberPropsMap);
          mySubscriber.setSubscriberStatus(SubscriberStatus.OK);
          mainDao.persist(mySubscriber);
         
          logger().debug("Now making a new subscription record");

            // create subscription
            mySubscription = new Subscription(mySubscriber, aMessageGroupName);
            mySubscription.setNextSendDate(new Date());
            mySubscription.setToken(tokenUtil.generateToken());
           
            // send opt in confirm message if applicable
            if (myOptInConfirmMessageRef != null) {
              logger().debug("About to send opt-in confirm message...");
              doOptInConfirmMessage(myOptInConfirmMessageRef, myMessageGroup, mySubscriber, mySubscription);
            }
            // if no opt in, then just mark active
            else {
              logger().debug("No opt-in confirm message, just marking as active");
              mySubscription.setSubscriptionStatus(SubscriptionStatus.ACTIVE);
            }
           
          logger().debug("Saving subscription");
            mainDao.persist(mySubscription);
           
        /* ========================================================== */
        /* Make LogEntry                                              */
        /* ========================================================== */
            mainDao.logEntry
                      (
                          LogEntryType.SUBSCRIBED,
                          mySubscriber,
                          aMessageGroupName,
                          defaultLogEntryProps()
                      );
           
        }
       
        // already there, merge
        else {
         
          logger().debug("Already have a Subscriber object for email address ({}): {}", aEmail, mySubscriber);
         
          // update attributes
          mySubscriber.setPropsMap(
              (Map<String,Object>)PropUtil.getInstance().propMerge(mySubscriber.getPropsMap(), aSubscriberPropsMap)
              );
         
          if (logger().isDebugEnabled()) {
            logger().debug("Saving updated properties: {}", mySubscriber.getPropsMap());
          }
         
            mainDao.persist(mySubscriber);
         
            // see if the subscription is there
            mySubscription =
                mainDao.getSubscriptionBySubscriberAndMessageGroupName
                  (
                      mySubscriber,
                      myMessageGroup.getName()
                  );
           
        logger().debug("Looking for corresponding Subscription record for subscriber and message group ({}) found: {}", myMessageGroup.getName(), mySubscription);
           
            // no subscription, create it
            if (mySubscription == null) {
             
              mySubscription = new Subscription(mySubscriber, aMessageGroupName);
                mySubscription.setNextSendDate(new Date());
                mySubscription.setToken(tokenUtil.generateToken());

                // send opt in confirm message if applicable
                if (myOptInConfirmMessageRef != null) {
                  logger().debug("About to send opt-in confirm message...");
                  doOptInConfirmMessage(myOptInConfirmMessageRef, myMessageGroup, mySubscriber, mySubscription);
                }
                // if no opt in, then just mark active
                else {
                  logger().debug("No opt-in confirm message, just marking as active");
                  mySubscription.setSubscriptionStatus(SubscriptionStatus.ACTIVE);
                }

              logger().debug("Saving subscription");
                mainDao.persist(mySubscription);
               
          /* ========================================================== */
          /* Make LogEntry                                              */
          /* ========================================================== */
              mainDao.logEntry
                        (
                            LogEntryType.SUBSCRIBED,
                            mySubscriber,
                            aMessageGroupName,
                            defaultLogEntryProps()
                        );
            }
           
            // we do already have a subscription
            else {
             
              // see if it's active
              if (mySubscription.getSubscriptionStatus() != SubscriptionStatus.ACTIVE) {
               
                    // send opt in confirm message if applicable
                    if (myOptInConfirmMessageRef != null) {
                      doOptInConfirmMessage(myOptInConfirmMessageRef, myMessageGroup, mySubscriber, mySubscription);
                    }
                    // if no opt in, then just mark active
                    else {
                      mySubscription.setSubscriptionStatus(SubscriptionStatus.ACTIVE);
                    }
                   
              }
             
              // save subscription
              mainDao.persist(mySubscription);
             

          /* ========================================================== */
          /* Make LogEntry                                              */
          /* ========================================================== */
              mainDao.logEntry
                        (
                            LogEntryType.RESUBSCRIBED,
                            mySubscriber,
                            aMessageGroupName,
                            defaultLogEntryProps()
                        );
            }
           
        }
       
        // now that we've done all the work -
        // re-read subscriber, so we get the latest (probably not
        // necessary, but it makes it me feel better ;)
        mySubscriber = mainDao.getSubscriberById(mySubscriber.getId());
       
        // log an info, just for politeness
      logger().info("User '{}' subscribed to message group '{}' ({} opt-in confirm)",
              new Object[] { mySubscriber.getEmail(), mySubscription.getMessageGroupName(),
                  (myOptInConfirmMessageRef != null ? "with" : "without")});

        return mySubscriber;
       
      }
View Full Code Here

   * @return true if found and marked, false if not found
   */
  @Transactional(propagation=Propagation.REQUIRED)
  public boolean unsubscribeFromToken(String aToken) {
   
    Subscription mySubscription = mainDao.getSubscriptionByToken(aToken);
    if (mySubscription == null) { return false; }
   
    mySubscription.setSubscriptionStatus(SubscriptionStatus.INACTIVE);
    mySubscription.setNextSendDate(null);
   
    mainDao.persist(mySubscription);
   
    Subscriber mySubscriber = mySubscription.getSubscriber();
    if (mySubscriber == null) {
      throw new IllegalStateException("Subscription record found but no corresponding subscriber record - this is an internal database error");
    }
   
    logger().info("Subscription corresponding to (email={} messageGroupName={}) was marked as INACTIVE (this particular email/message group combination is now not active)", mySubscriber.getEmail(), mySubscription.getMessageGroupName());

    return true;
  }
View Full Code Here

   * @return true if found and marked, false if not found
   */
  @Transactional(propagation=Propagation.REQUIRED)
  public boolean removeFromToken(String aToken) {
   
    Subscription mySubscription = mainDao.getSubscriptionByToken(aToken);
    if (mySubscription == null) { return false; }
   
    Subscriber mySubscriber = mySubscription.getSubscriber();
    if (mySubscriber == null) {
      throw new IllegalStateException("Subscription record found but no corresponding subscriber record - this is an internal database error");
    }
   
    mySubscriber.setSubscriberStatus(SubscriberStatus.REMOVED);
View Full Code Here

   *         funky state.
   */
  @Transactional(propagation=Propagation.REQUIRED)
  public void confirmFromToken(String aToken) throws ServiceException {
   
    Subscription mySubscription = mainDao.getSubscriptionByToken(aToken);
    if (mySubscription == null) {
      throw new ServiceException(ServiceExceptionType.NO_SUCH_SUBSCRIPTION,
          propUtil.mkprops("token", aToken));
    }
   
    Subscriber mySubscriber = mySubscription.getSubscriber();
    if (mySubscriber == null) {
      throw new ServiceException(ServiceExceptionType.NO_SUCH_SUBSCRIBER,
          propUtil.mkprops("token", aToken, "jr_subscription_id", mySubscription.getId()));
    }
   
    logger().debug("Attempting to confirm subscription (token={}) for subscriber (email={})",
        new Object[] { mySubscription.getToken(), mySubscriber.getEmail() });
   
    // make sure subscriber is marked as OK
    if (mySubscriber.getSubscriberStatus() != SubscriberStatus.OK) {
      logger().debug("Could not confirm, subscriber status was not OK, instead it was: {}", mySubscriber.getSubscriberStatus() );
      throw new ServiceException(ServiceExceptionType.SUBSCRIBER_NOT_OK,
          propUtil.mkprops("token", aToken));
    }
   
    // make sure subscription is marked as confirm wait or is already active
    if (!
        (
            mySubscription.getSubscriptionStatus() == SubscriptionStatus.CONFIRM_WAIT ||
            mySubscription.getSubscriptionStatus() == SubscriptionStatus.ACTIVE
        )
      ) {
      logger().debug("Could not confirm, subscription status was not CONFIRM_WAIT or ACTIVE, instead it was: {}", mySubscription.getSubscriptionStatus() );
      throw new ServiceException(ServiceExceptionType.UNEXPECTED_SUBSCRIPTION_STATUS,
          propUtil.mkprops("token", aToken));
    }
   
    mySubscription.setSubscriptionStatus(SubscriptionStatus.ACTIVE);
    // if next send date is null, then set it to now - so it goes to the
    // engine to be scheduled properly
    if (mySubscription.getNextSendDate() == null) {
      mySubscription.setNextSendDate(new Date());
    }
    mainDao.persist(mySubscription);
   
    logger().info("Subscription (email={},message_group_name={},token={}) was confirmed!",
        new Object[] { mySubscriber.getEmail(), mySubscription.getMessageGroupName(), mySubscription.getToken() });

  }
View Full Code Here

  public boolean engineLoopProcessOne() {
   
    // 1. select next batch size (maybe just 1 for now) where next
    //    consideration date is older than now
   
    Subscription mySubscription = mainDao.getNextSendableSubscription(SubscriptionStatus.ACTIVE);
   
    // if no result, then bail out, nothing to do
    if (mySubscription == null) {
      logger().debug("No more results, exiting loop");
      return false;
    }
   
    // the subscription we are now processing
    logger().debug("Got subscription with id: {}", mySubscription.getId());
   
    // look up the subscriber too
    Subscriber mySubscriber = mySubscription.getSubscriber();
   
    if (mySubscriber == null) {
      throw new RuntimeException("Data error, subscription with id " + mySubscription.getId() + " has null subscriber, should not be possible");
    }
   
    // Make sure we don't send to people who are asked off
   
    // if status is not okay...
    if (mySubscriber.getSubscriberStatus() != SubscriberStatus.OK) {
      // end this subscription
      endSubscription(mySubscription);
      // log for posterity
      logger().info("Was going to send to subscriber (email={}) but status is not OK, it's '{}', not sending and ending subscription.", mySubscriber.getEmail(), mySubscriber.getSubscriberStatus());
      // bail out
      return true;
    }
   
    // 2. for this Subscription, find last message processed in
    //    the list for that group
   
    // first make sure we have a corresponding message group
    MessageGroup myMessageGroup = messageGroupSource.getMessageGroupByName(mySubscription.getMessageGroupName());
   
    // if no such message group, then we just get rid of the
    // subscription (lose the date so it doesn't get pulled back up);
    // also catch the case where no subscriber record, should never
    // happen, but adding scenario for robustness)
    if (myMessageGroup == null || mySubscriber == null) {
     
      logger().warn(
          "Either message group or subscriber was null, " +
          "bailing out (messageGroup: {}, subscriber: {})",
          myMessageGroup, mySubscriber);
     
      /* ====================================================== */
      /* clear next send date of subscription                   */
      /* ====================================================== */
      endSubscription(mySubscription);
      return true;
    }
   
    // get the name of the last message
    String myLastMessageName = mySubscription.getLastMessageName();
    logger().debug("myLastMessageName: {}", myLastMessageName);
   
    // if last message exists
    int mySendMessageRefIndex = myMessageGroup.indexOfName(myLastMessageName);
    if (mySendMessageRefIndex >= 0) {
      // increment to next one to get the message we should send
      mySendMessageRefIndex++;
    }
    // if last message name was null, then start at zero
    if (myLastMessageName == null) {
      mySendMessageRefIndex = 0;
    }
   
    logger().debug("myLastMessageRefIndex: {}", mySendMessageRefIndex);
   
   
    // 3. get the next message
   
    List<MessageRef> myMessageRefList = myMessageGroup.getMessageRefList();
   
    // the MessageRef that we are to send
    MessageRef mySendMessageRef = null;
    if (mySendMessageRefIndex >= 0) {
      // make sure we haven't gone beyond the end of the messages
      if (myMessageRefList.size() > mySendMessageRefIndex) {
        mySendMessageRef = myMessageRefList.get(mySendMessageRefIndex);
      }
    }
    logger().debug("about to send mySendMessageRef: {}", mySendMessageRef);

   
    // no message to send (either couldn't find last message or
    // there is no next message); also check for the case where
    // the last send date is not set (should never happen, but just
    // to keep it from breaking the system we treat it like end
    // of list)
    if (mySendMessageRef == null || mySubscription.getLastSendDate() == null) {
      logger().debug("no more messages to send for this subscription");
      /* ====================================================== */
      /* clear next send date of subscription                   */
      /* ====================================================== */
      endSubscription(mySubscription);
      return true;
    }
   

    if (mySubscription.getLastSendDate() == null) {
      logger().warn("last_send_date was null on subscription, should never happen");
    }
   
    // 3a.read the time on it and recalculate, confirm that
    //    time is now past
   
    Long myWait = mySendMessageRef.getWaitAfterLastMessage();
    if (myWait == null) {
      /* ====================================================== */
      /* clear next send date of subscription                   */
      /* ====================================================== */
      logger().warn("'wait after last message' value was not set for this message, ending the subscription");
      endSubscription(mySubscription);
      return true;
    }
   
    // 3b.if not past then set next consideration date to recalced date
    //    and kick back
   
    // see if it's time to send yet
    long myLastSendDate = mySubscription.getLastSendDate().getTime();
    if (myLastSendDate + myWait > new Date().getTime()) {
      logger().debug("it is not yet time to send this message, putting it back for later");
      // not time to send (possibly message changed since
      // this subscription record was last edited - also first
      // subscription looks like this);
      // reset the nextSendDate and move on
      mySubscription.setNextSendDate(new Date(myLastSendDate + myWait));
      mainDao.persist(mySubscription);
      return true;
    }
   
   
    // 4. render and send message
   
    boolean mySendWorked = sendMessage(
            mySendMessageRef,
            myMessageGroup,
            mySubscriber,
            mySubscription,
            LogEntryType.MESSAGE_SENT
        );
   
    // if the sending was skipped...
    if (!mySendWorked) {
     
      // write a log entry for the skipping
      mainDao.logEntry
            (
                 LogEntryType.MESSAGE_SKIPPED,
                 mySubscriber,
                 myMessageGroup.getName(),
                 propUtil.mkprops(
                     "message_name", mySendMessageRef.getName()
                     )
            );

    }
   
   
   
   
    // 5. get next message in the list and recalculate date based on that,
    //    update last message name and save
   
    // update the last sent info
    mySubscription.setLastMessageName(mySendMessageRef.getName());
    mySubscription.setLastSendDate(new Date());
   
    // index of next message
    int myNextMessageRefIndex = mySendMessageRefIndex + 1;
   
   
    // 5a.if no more messages, then next consideration date is set to null
   
    if (myNextMessageRefIndex >= myMessageRefList.size()) {
      mySubscription.setNextSendDate(null);
    }
   
    // if we have a next message, then calc when it fires
    else {
     
      MessageRef myNextMessageRef = myMessageRefList.get(myNextMessageRefIndex);
      Long myNextWait = myNextMessageRef.getWaitAfterLastMessage();
      mySubscription.setNextSendDate(
            new Date(mySubscription.getLastSendDate().getTime() + myNextWait)
          );

    }
   
    // save the changes to the subscription
    mainDao.persist(mySubscription);
   
    if (mySubscription.getNextSendDate() == null) {
     
      // write a log entry for the completion of the subscription
      mainDao.logEntry
          (
               LogEntryType.SUBSCRIPTION_DONE,
View Full Code Here

    Assert.assertEquals(1, wiser.getMessages().size());
   
    wiser.dumpMessages(System.out);
   
    Subscriber mySubscriber = mainDao.getSubscriberByEmail("joey@example.com");
    Subscription mySubscription = mainDao.getSubscriptionBySubscriberAndMessageGroupName(mySubscriber, "list1");
   
    String myToken = mySubscription.getToken();
   
    System.out.println("Token: " + myToken);
   
    Assert.assertEquals(SubscriptionStatus.CONFIRM_WAIT, mySubscription.getSubscriptionStatus());
   
    // try confirm
    subscriberService.confirmFromToken(myToken);
   
    // get the subscription again directly from the db and check the status
    mySubscription = mainDao.getSubscriptionBySubscriberAndMessageGroupName(mySubscriber, "list1");
    Assert.assertEquals(SubscriptionStatus.ACTIVE, mySubscription.getSubscriptionStatus());
   
    // now try to unsubscribe
    subscriberService.unsubscribeFromToken(myToken);
   
    // check database
    mySubscription = mainDao.getSubscriptionBySubscriberAndMessageGroupName(mySubscriber, "list1");
    Assert.assertEquals(SubscriptionStatus.INACTIVE, mySubscription.getSubscriptionStatus());
   
    // now try remove
    subscriberService.removeFromToken(myToken);
   
    // check database
View Full Code Here

TOP

Related Classes of org.jresponder.domain.Subscription

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.