com.sappenin.utils.appengine.customernotifications.framework.scheduler.payload.AggregatableNotificationTaskPayload.java Source code

Java tutorial

Introduction

Here is the source code for com.sappenin.utils.appengine.customernotifications.framework.scheduler.payload.AggregatableNotificationTaskPayload.java

Source

/**
 * Copyright (C) 2014 Sappenin Inc. (developers@sappenin.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sappenin.utils.appengine.customernotifications.framework.scheduler.payload;

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.ToString;
import lombok.experimental.Wither;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.googlecode.objectify.Key;
import com.sappenin.utils.appengine.customernotifications.NotificationType;
import com.sappenin.utils.appengine.tasks.aggregate.AggregatingTaskSchedulerPayload;

/**
 * A class that is serialized to/from JSON as the payload for the AggregateNotification task scheduler/handler. For
 * aggregate notification tasks that will use this payload, the task will be named so that only one (ish) will ever be
 * handled for a certain period. This allows "many" AggregatableNotificationEntities to be stored into the database
 * while being processed in bulk via the AggregateNotificationTaskHandler only once.
 */
@Getter
@ToString
@EqualsAndHashCode
@Wither
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
public class AggregatableNotificationTaskPayload implements AggregatingTaskSchedulerPayload, Serializable {
    // The ancestor key that all entities of type {@link AggregatableNotificationEntity} will share when they should be
    // aggregated together.
    @NonNull
    private final Key<NotificationType> ancestorGroupingKey;

    /**
     * This field will be use to both schedule the Task as well as enforce uniqueness via the taskname. Thus, it is
     * important to set this value to be some consistent future date, no matter what the current date is. For example,
     * something like "Next Friday at 6am" would always be consistent, no matter what the current date/time is.
     */
    @NonNull
    private final DateTime etaScheduledDateTime;

    /**
     * Required Args Constructor.
     * 
     * @param ancestorGroupingKey
     * @param etaScheduledDateTime
     */
    public AggregatableNotificationTaskPayload(
            @JsonProperty("ancestorGroupingKey") final Key<NotificationType> ancestorGroupingKey,
            @JsonProperty("etaScheduledDateTime") final DateTime etaScheduledDateTime) {
        this.ancestorGroupingKey = ancestorGroupingKey;
        this.etaScheduledDateTime = etaScheduledDateTime;
    }

    @JsonIgnore
    public Key<?> getNotificationTargetKey() {
        return this.ancestorGroupingKey.getParent();
    }

    @JsonIgnore
    public NotificationType getNotificationType() {
        NotificationType notificationType = new NotificationType(this.getAncestorGroupingKey().getName(), true);
        return notificationType;
    }

    /**
     * @return The unique task name for aggregating notifications. This is essentially the ancestorGroupingKey, as a
     *         String. In other words, for any NotificationTarget+NotificationType+AnticipatedSendDate, there should
     *         only be a single task in the queue.
     */
    @JsonIgnore
    @Override
    public String getAggregatedTaskName() {
        // We do this as part of the framework so that framework users don't have to worry abou it.
        DateTimeFormatter fmt = ISODateTimeFormat.dateTimeNoMillis();
        String timestamp = fmt.print(this.getEtaScheduledDateTime());

        // No Need to append the DateTime because the framework will handle this for us. See javadoc above.
        return (this.ancestorGroupingKey.getString() + timestamp).replace(":", "");
    }

}