package org.ngbw.web.actions; import java.io.IOException; import java.sql.SQLException; import java.text.ParseException; import java.util.Collections; import java.util.Date; import java.util.List; import java.util.regex.Pattern; import org.apache.log4j.Logger; import org.apache.struts2.interceptor.validation.SkipValidation; 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.database.UserSuAllocationService.UserSuAllocationTransaction; import org.ngbw.sdk.database.UserSuAllocationService.UserSuBalance; import org.ngbw.sdk.tool.DateUtils; /** * Struts action class to manage user tasks in the NGBW web application. * * @author mzhuang */ @SuppressWarnings("serial") public class UserSuAllocationManager extends ManageTasks { private static final Logger logger = Logger.getLogger(UserSuAllocationManager.class); // session attribute key constants public static final String USER_SU_INFO = "userSuInfo"; private long userId = 0; private long credits = 0; private long usages = 0; private Date dateRangeFrom = null; private Date dateRangeTo = null; private String userName = null; private String suAmount = null; // from input form private String userSuSearchStr = ""; // from input form private String transTimeFrom = null; // from input form private String transTimeTo = null; // from input form private UserSuBalance suBalance = null; private UserSuAllocation suAllocation = null; private List allocationTransactions = null; private boolean myselfSuInfo = false; // public String // getUserSuSearchStr () // { // return userSuSearchStr; // } public long getUserId () { return userId; } public long getCredits () { return credits; } public long getUsages () { return usages; } public Date getDateRangeFrom () { return dateRangeFrom; } public Date getDateRangeTo () { return dateRangeTo; } public String getUserName () { return userName; } public String getSuAmount () { return suAmount; } public UserSuBalance getSuBalance () { return suBalance; } public UserSuAllocation getSuAllocation () { return suAllocation; } public List getAllocationTransactions () { return allocationTransactions; } public void setUserSuSearchStr ( String str ) { this.userSuSearchStr = str; } public void setTransTimeFrom ( String str ) { logger.debug("Setting transTimeFrom: " + str); this.transTimeFrom = str; } public void setTransTimeTo ( String str ) { logger.debug("Setting transTimeTo: " + str); this.transTimeTo = str; } public void setUserId ( long id ) { this.userId = id; } public void setSuAmount ( String amt ) { this.suAmount = amt; } public void setSuBalance ( UserSuBalance suBalance ) { this.suBalance = suBalance; } public void setSuAllocation ( UserSuAllocation allocation ) { this.suAllocation = allocation; } public void setAllocationTransactions ( List transactions ) { this.allocationTransactions = transactions; } public boolean hasSuAllocation () { return (null != suAllocation); } public boolean hasAllocationTransactions () { return (null != allocationTransactions && !allocationTransactions.isEmpty()); } public boolean getMyselfSuInfo() { return myselfSuInfo; } public void setMyselfSuInfo(boolean myselfSuInfo) { logger.debug("Setting MyselfSuInfo: " + myselfSuInfo); this.myselfSuInfo = myselfSuInfo; } @Override public String execute () { return SUCCESS; } private User findUser ( String searchStr ) throws IOException, SQLException { User user = null; if (searchStr.contains("@")) { user = User.findUserByEmail(searchStr); if (null == user) { super.reportUserError(String.format("No user with email address '%s' can be found in the database.", searchStr)); } } else if (StringUtils.hasPattern(searchStr, Pattern.compile("^[0-9]+$"))) { user = User.findUser(Long.parseLong(searchStr)); if (null == user) { super.reportUserError(String.format("No user with ID '%s' can be found in the database.", searchStr)); } } else if (StringUtils.hasPattern(searchStr, Pattern.compile("^[a-fA-F0-9\\-]{32,36}$"))) { user = User.findUserByUUID(searchStr); if (null == user) { super.reportUserError(String.format("No user with UUID '%s' can be found in the database.", searchStr)); } } else { user = User.findUser(searchStr); if (null == user) { super.reportUserError(String.format("No user with username '%s' can be found in the database.", searchStr)); } } return user; } private void loadSuAllocation ( long userId ) { try { suAllocation = UserSuAllocationService.getUserSuAllocation(userId); } catch ( Throwable t ) { logger.error(t.getMessage(), t); } } private void setTransactionTimeRanges ( Date startTime ) { logger.debug(String.format("'transTimeFrom' from input form = [%s]", transTimeFrom)); if (!StringUtils.isNullOrEmpty(this.transTimeFrom, true)) { try { this.dateRangeFrom = DateUtils.getDate(this.transTimeFrom); } catch ( ParseException pe ) { this.dateRangeFrom = null; logger.error(pe.getMessage(), pe); } } logger.debug(String.format("'transTimeTo' from input form = [%s]", transTimeTo)); if (!StringUtils.isNullOrEmpty(this.transTimeTo, true)) { try { this.dateRangeTo = DateUtils.getDate(this.transTimeTo); } catch ( ParseException pe ) { this.dateRangeTo = DateUtils.getDate(); logger.error(pe.getMessage(), pe); } } else { this.dateRangeTo = DateUtils.getDate(); } if (null == this.dateRangeFrom) { this.dateRangeFrom = (Date) StringUtils.most(DateUtils.addDays(this.dateRangeTo, -60), startTime); } } @SkipValidation public String searchMyselfSuInfo () throws Throwable { myselfSuInfo = true; logger.debug("BEGIN: searchMyselfSuInfo()::String"); return searchUserSuInfoBase(true); } @SkipValidation public String searchUserSuInfo () throws Throwable { return searchUserSuInfoBase(false); } private String searchUserSuInfoBase (boolean myself) throws Throwable { logger.debug("BEGIN: searchUserSuInfo()::String"); logger.info("searchUserSuINfo(): myself = " + myself); String resultName = SUCCESS; try { if (myself || !StringUtils.isNullOrEmpty(userSuSearchStr, true)) { User targetUser = null; if (myself) { targetUser = super.getAuthenticatedUser(); logger.info("Show SU inof of myself: " + targetUser.getUsername()); } else targetUser = this.findUser(userSuSearchStr.trim()); if (null != targetUser) { this.userName = targetUser.getUsername(); this.userSuSearchStr = targetUser.getUsername(); logger.debug("User found: ID=" + targetUser.getUserId() + ", " + targetUser.getUsername()); this.loadSuAllocation(targetUser.getUserId()); if (null != suAllocation) { setTransactionTimeRanges(suAllocation.getStartTime()); logger.debug( String.format( "Retrieving Alloc Trans for user [%s] filter [%s] - [%s] ...", targetUser.getUsername(), DateUtils.formatDate(this.dateRangeFrom), DateUtils.formatDate(this.dateRangeTo))); suBalance = UserSuAllocationService.getUserSuBalance( targetUser.getUserId(), null, // transaction type this.getDateRangeFrom(), this.getDateRangeTo(), 0, 0); suBalance.calculate(true); allocationTransactions = suBalance.getAllocationTransactions(); if (null != allocationTransactions) { Collections.sort( allocationTransactions, UserSuAllocationService.UserSuAllocationTransaction.SORT_BY_TRANSACTION_TIME_DESC); } } else { resultName = ERROR; super.reportUserError( String.format( "User '%s' does not have SU balance record.", targetUser.getUsername())); } } } else { resultName = INPUT; //super.reportUserError("Please enter a valid user's ID, UUID, username, or email address."); } } catch ( Throwable t ) { logger.error(t.getMessage(), t); resultName = ERROR; super.reportUserError("Unable to retrieve user's SU information."); } logger.debug("END: searchUserSuInfo()::String"); return resultName; } @SkipValidation public String filterUserSuTransactions () throws Throwable { logger.debug("BEGIN: filterUserSuTransactions()::String"); String resultName = SUCCESS; try { if (!myselfSuInfo && userId <= 0) { resultName = ERROR; super.reportUserError("Please enter a valid user's ID."); } else { User targetUser = null; if (myselfSuInfo) { targetUser = super.getAuthenticatedUser(); logger.info("Filter SU inof of myself: " + targetUser.getUsername()); } else targetUser = User.findUser(userId); if (null != targetUser) { this.userName = targetUser.getUsername(); this.loadSuAllocation(targetUser.getUserId()); setTransactionTimeRanges(null); this.loadSuAllocation(targetUser.getUserId()); logger.debug( String.format( "Retrieving Alloc Trans for user [%s] filter [%s] - [%s] ...", targetUser.getUsername(), DateUtils.formatDate(this.dateRangeFrom), DateUtils.formatDate(this.dateRangeTo))); suBalance = UserSuAllocationService.getUserSuBalance( targetUser.getUserId(), null, // transaction type this.getDateRangeFrom(), this.getDateRangeTo(), 0, 0); suBalance.calculate(true); allocationTransactions = suBalance.getAllocationTransactions(); if (null != allocationTransactions) { Collections.sort( allocationTransactions, UserSuAllocationService.UserSuAllocationTransaction.SORT_BY_TRANSACTION_TIME_DESC); } } else { resultName = ERROR; super.reportUserError("User not found."); } } } catch ( Throwable t ) { logger.error(t.getMessage(), t); resultName = ERROR; super.reportUserError("Unable to retrieve user's SU information."); } logger.debug("END: filterUserSuTransactions()::String"); this.searchUserSuInfo(); return resultName; } @SkipValidation public String adjustUserSu () throws Throwable { logger.debug("BEGIN: adjustUserSu()::String"); String resultName = SUCCESS; logger.debug( String.format( "Adjusting SU Allocation amount [%s] for user [%s]...", suAmount, userId)); User targetUser = User.findUser(userId); try { if (userId <= 0) { resultName = ERROR; super.reportUserError("Please enter a valid user's ID."); } else if (StringUtils.isNullOrEmpty(suAmount, true)) { resultName = ERROR; super.reportUserError("Please enter an integer for SU amount."); } else if (!StringUtils.hasPattern(suAmount.trim(), Pattern.compile("^(-)?[0-9]+$"))) { resultName = ERROR; super.reportUserError("Please enter an integer for SU amount."); } else { if (null != targetUser) { userSuSearchStr = targetUser.getUsername(); this.loadSuAllocation(targetUser.getUserId()); if (null != suAllocation) { UserSuAllocationService.adjustSu( targetUser, Integer.parseInt(suAmount), super.getAuthenticatedUser()); suAmount = null; super.reportUserMessage("User's SU has been adjusted."); } else { super.reportUserError( String.format( "User '%s' does not have SU transaction record.", targetUser.getUsername())); } } else { resultName = ERROR; super.reportUserError("User not found."); } } } catch ( Throwable t ) { logger.error(t.getMessage(), t); resultName = ERROR; super.reportUserError("Unable to adjust user's SU."); } finally { if (null != targetUser) { this.loadSuAllocation(targetUser.getUserId()); setTransactionTimeRanges(suAllocation.getStartTime()); suBalance = UserSuAllocationService.getUserSuBalance( targetUser.getUserId(), null, // transaction type this.getDateRangeFrom(), this.getDateRangeTo(), 0, 0); suBalance.calculate(true); allocationTransactions = suBalance.getAllocationTransactions(); if (null != allocationTransactions) { Collections.sort( allocationTransactions, UserSuAllocationService.UserSuAllocationTransaction.SORT_BY_TRANSACTION_TIME_DESC); } } } this.searchUserSuInfo(); logger.debug("END: adjustUserSu()::String"); return resultName; } }