package org.ngbw.web.actions; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.log4j.Logger; import org.apache.struts2.ServletActionContext; import org.ngbw.sdk.database.UserSuAllocationService; import org.ngbw.sdk.database.UserSuAllocationService.UserSuAllocation; import org.ngbw.sdk.database.UserSuAllocationService.UserSuBalance; import org.ngbw.sdk.UserAuthenticationException; import org.ngbw.sdk.Workbench; import org.ngbw.sdk.common.util.StringUtils; import org.ngbw.sdk.common.util.ValidationResult; import org.ngbw.sdk.core.shared.UserRole; import org.ngbw.sdk.database.Country; import org.ngbw.sdk.database.Group; import org.ngbw.sdk.database.User; import org.ngbw.sdk.phoenix.CipresPhoenixUserAccountManager; import org.ngbw.sdk.tool.CipresNotifier; import org.ngbw.web.controllers.FolderController; /** * Struts action class to process all user profile-related requests in the NGBW * web application. * */ @SuppressWarnings("serial") public class SUManager extends SessionManager { private static final Logger logger = Logger.getLogger(SUManager.class); private static final String COMMA = ","; private static final String SPACE = " "; private String userNames = null; private String suToBeTransfered = null; private List allocationTransactions = null; public SUManager() { } public String getUserNames() { return userNames; } public void setUserNames(String userNames) { this.userNames = userNames; } public String getSuToBeTransfered() { return suToBeTransfered; } public void setSuToBeTransfered(String suToBeTransfered) { this.suToBeTransfered = suToBeTransfered; } @Override public String input () { return INPUT; } /* public String showTransactionActivities() { }*/ public String transfterSU() { logger.info("userNames = " + userNames + " suToBeTransfered = " + suToBeTransfered); List users = new ArrayList(); List sus = new ArrayList(); /* String[] nameList = null; String[] suList = null; if (userNames.contains(COMMA)) nameList = userNames.split(COMMA); else if (userNames.contains(SPACE)) nameList = userNames.split(SPACE); if (suToBeTransfered.contains(COMMA)) suList = suToBeTransfered.split(COMMA); else if (suToBeTransfered.contains(SPACE)) suList = suToBeTransfered.split(SPACE); */ //if (assignUsersSUs(nameList, suList, users, sus)) if (validateTransferSU(userNames, suToBeTransfered, users, sus)) { int allocatedTotal = 0; for (Integer aInt : sus) allocatedTotal += aInt.intValue(); if (allocatedTotal > getSuAllocationTransferable ()) { super.reportUserError( "Total CPU hours to be transfered is bigger than your transferable CPU hours"); clear(); return INPUT; } for(int idx = 0; idx < users.size(); ++idx) { logger.info("Transfering to user: " + users.get(idx).getUsername() + " CPU hours: " + sus.get(idx)); transfterSUSingle(users.get(idx), sus.get(idx)); } clear(); } else return INPUT; return SUCCESS; } public boolean transfterSUSingle(User user, int suTBT) { /* User user = null; try { user = User.findUser(userNames); } catch (Exception ex) { } int suTBT = Integer.parseInt(suToBeTransfered);*/ if (suTBT <= 0 ) { super.reportUserError(String.format( "SUs to be transfered must be bigger than 0 while your input is '%s'", suTBT)); //clear(); return false; } if (user == null) { super.reportUserError(String.format( "No user with username '%s' can be found in the database.", user.getUsername())); //clear(); return false; } boolean adjustSuccessful = false; boolean hasPassed = false; try { UserSuAllocationService.adjustSuWithNote( super.getAuthenticatedUser(), -suTBT, super.getAuthenticatedUser(), "Transfer to " + user.getUsername()); hasPassed = true; try { UserSuAllocationService.adjustSuWithNote( user, suTBT, super.getAuthenticatedUser(), "Transfer from " + super.getAuthenticatedUser().getUsername()); } catch (Throwable e) { String msg = String.format("Error occurred when trying to transfer %s CPU hours from your account to user %s", suTBT, user.getUsername()); super.reportUserError(msg); logger.error(msg, e); UserSuAllocationService.adjustSuWithNote( super.getAuthenticatedUser(), suTBT, super.getAuthenticatedUser(), "Transfer back from " + user.getUsername() + " because of error"); logger.info("Has transfered back CPU hours " + suTBT + " from " + user.getUsername()); adjustSuccessful = false; throw e; } if (suTBT > 1) super.reportUserMessage(String.format("%s CPU Hours have been transfered from your account to user %s", suTBT, user.getUsername())); else super.reportUserMessage(String.format("%s CPU Hour has been transfered from your account to user %s", suTBT, user.getUsername())); adjustSuccessful = true; } catch (Throwable e) { if (!hasPassed) { String msg = String.format("Error occurred when trying to transfer %s CPU hours from your account to user %s", suTBT, user.getUsername()); super.reportUserError(msg); logger.error(msg, e); } adjustSuccessful = false; } //clear(); return adjustSuccessful; } private boolean validateTransferSU(String userNamesL, String suToBeTransferedL, List users, List sus) { if (userNamesL == null || userNamesL.isEmpty() || suToBeTransferedL == null || suToBeTransferedL.isEmpty()) { super.reportUserError("Both User Name(s) and CPU Hrs to be Transfered fields require non-empty values"); return false; } String[] nameList = null; String[] suList = null; if (userNamesL.contains(COMMA)) nameList = userNamesL.split(COMMA); else if (userNamesL.contains(SPACE)) nameList = userNamesL.split(SPACE); if (suToBeTransferedL.contains(COMMA)) suList = suToBeTransferedL.split(COMMA); else if (suToBeTransferedL.contains(SPACE)) suList = suToBeTransferedL.split(SPACE); if ((userNamesL.contains(COMMA) || userNamesL.contains(SPACE)) && (suToBeTransferedL.contains(COMMA) || suToBeTransferedL.contains(SPACE))) { if (nameList.length != suList.length) { super.reportUserError("Number of User Names and number of CPU Hrs must be the same"); return false; } int idx = 0; for (String name : nameList) { try { User user = User.findUser(name.trim()); Integer su = Integer.parseInt(suList[idx].trim()); if (user != null) users.add(user); else { super.reportUserError(String.format( "No user with username '%s' can be found in the database.", name)); return false; } if (su != null && su.intValue() > 0) sus.add(su); else { super.reportUserError(String.format( "CPU hours '%s' should be bigger than 0 and in correct format.", suList[idx])); return false; } } catch (IOException | SQLException ex) { super.reportUserError(String.format( "No user with username '%s' can be found in the database due to database error.", name)); return false; } catch (NumberFormatException nfe) { super.reportUserError(String.format( "Specified CPU hour '%s' is not in the correct format.", suList[idx])); return false; } ++idx; } } else if ((userNamesL.contains(COMMA) || userNamesL.contains(SPACE)) && (!suToBeTransferedL.contains(COMMA) && !suToBeTransferedL.contains(SPACE))) { suList = new String[]{suToBeTransferedL}; //Integer su = Integer.parseInt(suToBeTransferedL); if (!assignUsersSUs(nameList, suList, users, sus)) return false; } else if ((!userNamesL.contains(COMMA) && !userNamesL.contains(SPACE)) && (suToBeTransferedL.contains(COMMA) || suToBeTransferedL.contains(SPACE))) { super.reportUserError( "User Names field has only one item while CPU Hours field have multiple items."); return false; } else { nameList = new String[] {userNamesL}; suList = new String[] {suToBeTransferedL}; if (!assignUsersSUs(nameList, suList, users, sus)) return false; } return true; } private boolean assignUsersSUs(String[] nameList, String[] suList, List users, List sus) { if (nameList != null && suList != null) { int idx = 0; for (String name : nameList) { try { User user = User.findUser(name); Integer su = Integer.parseInt(suList[idx]); if (user != null) users.add(user); else { super.reportUserError(String.format( "No user with username '%s' can be found in the database.", name)); return false; } if (su != null && su.intValue() > 0) sus.add(su); else { super.reportUserError(String.format( "CPU hours '%s' should be bigger than 0 and in correct format.", suList[idx])); return false; } } catch (IOException | SQLException ex) { super.reportUserError(String.format( "No user with username '%s' can be found in the database due to database error.", name)); return false; } ++idx; } } return true; } private void clear() { userNames = null; suToBeTransfered = null; } }