/* EmployeeTrainingPerformer.java * Authors: Jocelyn Becker and Sagar * Last modified Sep 5 2000 */ /* This custom activity schedules a new employee orientation training. * Employees in different departments attend trainings on different days. * * An xml file specifies which departments attend training * on which day, for example: * * Engineering * monday * Marketing * tuesday * Human Resource * wednesday * Sales * thursday * * The custom activity writes an HTML file that welcomes * the new employee and tells them which day to attend training. * Trainings are held at 2 o'clock in the afternoon. * * The employee will attend training on the first * appropriate day after they start work. * For example, if their training day is tuesday * and they start work on monday, they will be scheduled * for training the following day on tuesday * but if they start on thursday, they will be scheduled * for training on tuesday the following week. * * The custom activity gets * the employee's name from the Emp_name input parameter * the start date from the Start_Date input paramater * and the department name from the Dept input parameter * * These input parameters are mapped to actual data field names * in the TrainingPerformer.xml file: * * * * getData("dfEmpName") * * getData("dfDeptName") * * getData("dfStartDate") * ... * * Additonal input parameters include the path to the applications directory * and the process instance id: * * getApplicationPath() * * getProcessInstance().getInstanceId() * */ package customer.activities; import java.util.*; import java.io.*; import java.net.*; //import java.sql.*; // This class schedules the training day for a new employee // based on which department the employee is in. public class EmployeeTrainingPerformer implements com.netscape.pm.model.ISimpleWorkPerformer { public final int SUN = 0; public final int MON = 1; public final int TUE = 2; public final int WED = 3; public final int THU = 4; public final int FRI = 5; public final int SAT = 6; public EmployeeTrainingPerformer (){ } // The init() method does initialization stuff when the application starts. // It is not called for each new process instance. public void init(Hashtable env){ // nothing to initialize here } // The perform() method defines what the custom activity does. // In this case, it reads the TrainingDays.xml file // to populate the trainingDays hash table // then it schedules the training. public void perform(Hashtable input, Hashtable output){ System.out.println("Entering perform"); // Get the employee's start date from the input hashtable Date startDate = (Date)input.get("Start_Date"); //String startDate = sqlDate.toString(); // startDate = startDate.replace('-', '/'); // Get the new employee's name and their department // from the input hashtable. String dept = (String) input.get( "Dept" ); String empName = (String) input.get( "Emp_Name" ); // Get the application path from the input hashtable String appPath = (String)input.get("path"); // Get the process instance id from the input hashtable int thisID = ((Double)input.get("id")).intValue(); // Read the schedule from the TrainingDays.xml file Hashtable trainingDays = readSchedule(appPath); // Figure out what day of the week the employee will go to training Date trainingDate = scheduleTraining(startDate, dept, trainingDays); // Write a welcome page containing the training info String filename = writeWelcomePage(empName, thisID, appPath, trainingDate); // Put the file name for the HTML page in the output hashtable output.put("welcomePage", filename); System.out.println("Exiting perform"); } // The readSchedule function reads and parses the TrainingDays.xml file // to find out which departments go to training on which days. // The results are stored in the trainingDays hashtable, keyed by dept. // This happens in perform, not init, so that any changes to the // schedule are reflected in the next process instance that is created. private Hashtable readSchedule(String appPath){ System.out.println("Entering readSchedule"); Hashtable trainingDays = new Hashtable(); //reading from file.. try{ int Pfound = 0; int MAX_LENGTH = 500; char xml[] = new char[MAX_LENGTH]; String Path = appPath + "\\" + "TrainingDays.xml"; java.io.File f = new java.io.File(Path); FileReader fr = new FileReader(f); BufferedReader in = new BufferedReader(fr); int count = 0; count = in.read(xml, count, MAX_LENGTH); String charSet = new String(xml); int charSetLength = charSet.length(); String temp = new String(); String dept = new String(); String day = new String(); count = 0; // parse the file. Look for or for(; count < charSetLength; count++) { if(charSet.charAt(count) == '<' ) { System.out.println("at <"); temp = ""; for(; charSet.charAt(count) != '>'; count++) { temp = temp + charSet.charAt(count); } temp = temp + charSet.charAt(count); count++; if(temp.equalsIgnoreCase("")) { for(dept = ""; charSet.charAt(count) != '<' ; dept = dept + charSet.charAt(count++)); trainingDays.put(dept, " "); } if(temp.equalsIgnoreCase("")) { for(day = ""; charSet.charAt(count) != '<' ; day = day + charSet.charAt(count++)); trainingDays.put(dept, day); } } } } catch(Exception ignore) { System.out.println("MY EXCEPTION IS: " + ignore.toString()); } System.out.println("Exiting readSchedule"); return trainingDays; } // Schedule the training public Date scheduleTraining (Date startDate, String dept, Hashtable trainingDays) { System.out.println("Entering schedule training"); // Get info about the start date int thisDay = startDate.getDay(); int dayOfMonth = startDate.getDate(); int month = startDate.getMonth(); int year = startDate.getYear(); System.out.println("The day is " + (String)trainingDays.get(dept)); // Using the dept as the key, get the value of the // training day from the the trainingDays hashtable if(((String)trainingDays.get(dept)).equals("monday") ){ dayOfMonth=IncrementForMonday(thisDay, dayOfMonth); } else if(((String)trainingDays.get(dept)).equals("tuesday") ){ dayOfMonth=IncrementForTuesday(thisDay, dayOfMonth); } else if(((String)trainingDays.get(dept)).equals("wednesday") ){ dayOfMonth=IncrementForWednesday(thisDay, dayOfMonth); } else if(((String)trainingDays.get(dept)).equals("thursday") ){ dayOfMonth=IncrementForThursday(thisDay, dayOfMonth); } else if(((String)trainingDays.get(dept)).equals("friday") ){ dayOfMonth=IncrementForFriday(thisDay, dayOfMonth); } if(((String)trainingDays.get(dept)).equals("saturday") ){ dayOfMonth=IncrementForSaturday(thisDay, dayOfMonth); } else if(((String)trainingDays.get(dept)).equals("sunday") ){ dayOfMonth=IncrementForSunday(thisDay, dayOfMonth); } Date trainingDate = new Date(year,month,dayOfMonth); System.out.println("Exiting schedule training"); return trainingDate; } // Create an HTML page that welcomes the // new employee and tells them when to attend training. // Trainings are held at 2 pm in the afternoon, so that // if the employee needs to go to training on their start date // they have time to find where to go and how to get there. public String writeWelcomePage(String employeeName, int thisID, String appPath, Date trainingDate) { // Format the date string System.out.println("Entering writeWelcomePage"); String finalDate = formatDateString(trainingDate); // File name is Employee name + ProcessInstance String fileName = employeeName+thisID+".html"; // Remove all white spaces from the filename fileName = fileName.replace(' ', '_'); // Path for this Application's folder String thisPath = appPath +fileName; // Make a file in this Application's folder try { RandomAccessFile HTMLfile = new RandomAccessFile(thisPath, "rw"); HTMLfile.writeUTF(""); HTMLfile.writeUTF(""); HTMLfile.writeUTF("New Employee Training"); HTMLfile.writeUTF(""); HTMLfile.writeUTF(""); HTMLfile.writeUTF("

Hello " + employeeName + "

"); HTMLfile.writeUTF("

Welcome to our company.

"); HTMLfile.writeUTF("

Please attend new employee orientation training on "); HTMLfile.writeUTF("" + finalDate + " at 2 pm. in Room B3, which is above the cafeteria.

"); HTMLfile.writeUTF("

We'll have a tee-shirt and cap and other corporate goodies for you at the training!

"); HTMLfile.writeUTF(""); HTMLfile.writeUTF(""); HTMLfile.close(); } catch (Exception e) { System.out.println ("Trouble with writing welcome page: " + e); } System.out.println("Exiting writeWelcomePage"); return fileName; } private String formatDateString (Date trainingDate){ System.out.println("Entering formatDateString"); String finalDay ="?"; try{ // Logic to rearrange date String // eg Mon Oct 16 2000 String dateStr = trainingDate.toString(); int strIndx = dateStr.indexOf("00:00"); String tmpDay1 = dateStr.substring(0, strIndx); int endIndx = dateStr.indexOf("2000"); String tmpDay2 = dateStr.substring(endIndx); finalDay = tmpDay1+ " " + tmpDay2; // 09:00:00 PDT/PST Remove PDT/PST String time = dateStr.substring(strIndx, endIndx); int i = time.indexOf("P"); time = time.substring(0, i); // end of string rearrangement logic } catch(Exception e){ System.out.println("Error : "+e); } System.out.println("Exiting formatDateString"); return finalDay; } public void destroy(){ } // helper functions to find training date private int IncrementForMonday(int thisDay, int dayOfMonth ){ if(thisDay == SUN) // for Monday just Increment Once from Sunday dayOfMonth = dayOfMonth+1; if(thisDay == TUE) // for Monday just Increment 6 from Tue dayOfMonth = dayOfMonth+6; if(thisDay == WED) // for Monday just Increment 5 from Wed dayOfMonth = dayOfMonth+5; if(thisDay == THU) // for Monday just Increment 4 from Thursday dayOfMonth = dayOfMonth+4; if(thisDay == FRI) // for Monday just Increment 3 from Friday dayOfMonth = dayOfMonth+3; if(thisDay == SAT) // for Monday just Increment 2 from Saturday dayOfMonth = dayOfMonth+2; return dayOfMonth; } private int IncrementForTuesday(int thisDay, int dayOfMonth ){ if(thisDay == SUN) dayOfMonth = dayOfMonth+2; if(thisDay == MON) dayOfMonth = dayOfMonth+1; if(thisDay == WED) dayOfMonth = dayOfMonth+6; if(thisDay == THU) dayOfMonth = dayOfMonth+5; if(thisDay == FRI) dayOfMonth = dayOfMonth+4; if(thisDay == SAT) dayOfMonth = dayOfMonth+3; return dayOfMonth; } private int IncrementForWednesday(int thisDay, int dayOfMonth ){ if(thisDay == SUN) dayOfMonth = dayOfMonth+3; if(thisDay == MON) dayOfMonth = dayOfMonth+2; if(thisDay == TUE) dayOfMonth = dayOfMonth+1; if(thisDay == THU) dayOfMonth = dayOfMonth+6; if(thisDay == FRI) dayOfMonth = dayOfMonth+5; if(thisDay == SAT) dayOfMonth = dayOfMonth+4; return dayOfMonth; } private int IncrementForThursday(int thisDay, int dayOfMonth ){ if(thisDay == SUN) dayOfMonth = dayOfMonth+4; if(thisDay == MON) dayOfMonth = dayOfMonth+3; if(thisDay == TUE) dayOfMonth = dayOfMonth+2; if(thisDay == WED) dayOfMonth = dayOfMonth+1; if(thisDay == FRI) dayOfMonth = dayOfMonth+6; if(thisDay == SAT) dayOfMonth = dayOfMonth+5; return dayOfMonth; } private int IncrementForFriday(int thisDay, int dayOfMonth ){ if(thisDay == SUN) dayOfMonth = dayOfMonth+5; if(thisDay == MON) dayOfMonth = dayOfMonth+4; if(thisDay == TUE) dayOfMonth = dayOfMonth+3; if(thisDay == WED) dayOfMonth = dayOfMonth+2; if(thisDay == THU) dayOfMonth = dayOfMonth+1; if(thisDay == SAT) dayOfMonth = dayOfMonth+6; return dayOfMonth; } private int IncrementForSaturday(int thisDay, int dayOfMonth ){ if(thisDay == SUN) dayOfMonth = dayOfMonth+6; if(thisDay == MON) dayOfMonth = dayOfMonth+5; if(thisDay == TUE) dayOfMonth = dayOfMonth+4; if(thisDay == WED) dayOfMonth = dayOfMonth+3; if(thisDay == THU) dayOfMonth = dayOfMonth+2; if(thisDay == FRI) dayOfMonth = dayOfMonth+1; return dayOfMonth; } private int IncrementForSunday(int thisDay, int dayOfMonth ){ if(thisDay == MON) dayOfMonth = dayOfMonth+6; if(thisDay == TUE) dayOfMonth = dayOfMonth+5; if(thisDay == WED) dayOfMonth = dayOfMonth+4; if(thisDay == THU) dayOfMonth = dayOfMonth+3; if(thisDay == FRI) dayOfMonth = dayOfMonth+2; if(thisDay == SAT) dayOfMonth = dayOfMonth+1; return dayOfMonth; } // end of class }