package org.ngbw.utils; import java.util.Date; import java.util.Iterator; import java.util.List; import org.apache.commons.cli.CommandLine; import org.apache.commons.lang3.exception.ExceptionUtils; import org.apache.log4j.Logger; import org.ngbw.sdk.Workbench; import org.ngbw.sdk.common.util.StringUtils; import org.ngbw.sdk.database.User; import org.ngbw.sdk.database.UserSuAllocationService; import org.ngbw.sdk.database.UserSuAllocationService.UserSuAllocation; import org.ngbw.sdk.tool.CipresNotifier; import org.ngbw.sdk.tool.DateUtils; /** * * * @author Tony Chen * * @version 1.0.0 */ public class UserSuSubscriptionExpiration { private static final Logger logger = Logger.getLogger(UserSuSubscriptionExpiration.class); private static final String VERSION = "1.0.0"; private String log4jFile = null; private static CommandLine cmdln = null; private List argsList = null; public UserSuSubscriptionExpiration () {} public static void main ( String[] args ) { int status = 0; UserSuSubscriptionExpiration subscriptionExpiration = null; try { Workbench.getInstance(); subscriptionExpiration = new UserSuSubscriptionExpiration(); subscriptionExpiration.checkExpirationDates(); } catch ( Throwable t ) { status = 1; System.err.println(t.getMessage()); logger.error(t.getMessage(), t); } subscriptionExpiration = null; System.exit(status); } private void checkExpirationDates () throws Throwable { try { // Retrieve ALL SU Allocations which expire ON or AFTER today. // List allocations = UserSuAllocation.filterByExpireTime( null, DateUtils.setTimeToBeginOfDay(new Date()), // expireTimeFrom null, // expireTimeTo UserSuAllocationService.SORT_ASCENDING); final String WILL_EXPIRE = "ID[%d] User[%d - %s] SU subscription will expire on [%s] in [%d] days."; final String HAS_EXPIRED = "ID[%d] User[%d - %s] SU subscription has expired on [%s], [%d] days ago."; if (allocations != null && !allocations.isEmpty()) { int emailsSent = 0; final Iterator itr = allocations.iterator(); while (itr != null && itr.hasNext()) { final UserSuAllocation allocation = itr.next(); final Date expiresDate = allocation.getSuExpireTime(); final long dayCount = DateUtils.daysDidff(expiresDate, new Date(), false); if (dayCount > 0) { System.out.println(String.format(WILL_EXPIRE, allocation.getId(), allocation.getUserId(), allocation.getUser().getUsername(), DateUtils.formatDate("yyyy/MM/dd HH:mm:ss", expiresDate), dayCount)); } else { System.out.println(String.format(HAS_EXPIRED, allocation.getId(), allocation.getUserId(), allocation.getUser().getUsername(), DateUtils.formatDate("yyyy/MM/dd HH:mm:ss", expiresDate), Math.abs(dayCount))); } // Send a reminder email to the user iff the allocation will expires in exactly 30 days. if (dayCount == 30) { emailUser(allocation, dayCount); emailsSent += 1; } } final String body = String.format( "Total %d SU allocations inspected for expiring subscriptions." + "\n\n" + "Total %d emails sent.", allocations.size(), emailsSent); CipresNotifier.emailSysAdmin("Expiring SU Subscriptions Alert Sent", body, false); } else { System.err.println("List is null or empty."); } } catch ( Throwable t ) { System.err.println("ERROR: " + StringUtils.coalesceNotNullNotEmpty( t.getMessage(), t.toString(), ExceptionUtils.getStackTrace(t))); logger.error(t.getMessage(), t); throw t; } } private void emailUser ( final UserSuAllocation allocation, final long dayCount ) { try { final User user = allocation.getUser(); final String body = String.format( "Dear %s," + "\n\n" + "Your current SU subscription will expire in %d days.", user.getUsername(), dayCount); logger.debug(String.format( "Sending email to User[%d - %s] Email[%s] ...", allocation.getUserId(), allocation.getUser().getUsername(), allocation.getUser().getEmail())); System.out.println(String.format( "Sending email to User[%d - %s] Email[%s] ...", allocation.getUserId(), allocation.getUser().getUsername(), allocation.getUser().getEmail())); CipresNotifier.emailUser( allocation.getUser(), "Alert: Your current subscription will expire soon!", body, false); } catch ( Throwable t ) { logger.error(t.getMessage(), t); } } }