cziegeler    01/07/16 03:54:37

  Added:       src/org/apache/cocoon/acting ScriptAction.java
               webapp/docs/samples/script-action addknowncourse.js
                        addunknowncse.js addunknownte.js createuser.js
                        deleteknown.js deleteunknown.js login.js
                        updateinfo.js
  Log:
  Added script action. The source for an action can written with any
  supported scripting language!
  Submitted by: Jason Foster ([EMAIL PROTECTED])
  
  Revision  Changes    Path
  1.1                  xml-cocoon2/src/org/apache/cocoon/acting/ScriptAction.java
  
  Index: ScriptAction.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included  with this distribution in *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  
  package org.apache.cocoon.acting;
  
  // Cocoon imports
  
  import org.apache.cocoon.Constants;
  import org.apache.cocoon.environment.Request;
  import org.apache.cocoon.environment.Session;
  import org.apache.cocoon.environment.Redirector;
  import org.apache.cocoon.environment.Source;
  import org.apache.cocoon.environment.SourceResolver;
  import org.apache.cocoon.ProcessingException;
  import org.apache.cocoon.Roles;
  
  // Avalon imports
  
  import org.apache.avalon.framework.component.Component;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.framework.logger.AbstractLoggable;
  import org.apache.avalon.framework.parameters.Parameters;
  
  // Java runtime imports
  
  import java.io.Reader;
  import java.io.FileNotFoundException;
  import java.io.InputStreamReader;
  import java.util.Map;
  import java.util.Map;
  import java.util.HashMap;
  import java.util.Collections;
  
  // BSF imports
  
  import com.ibm.bsf.BSFManager;
  import com.ibm.bsf.util.IOUtils;
  import com.ibm.bsf.BSFException;
  
  // SAX imports
  
  import org.xml.sax.InputSource;
  import org.xml.sax.SAXException;
  
  /**
   * A simple action that executes any script that can be run by the BSF
   */
  
  public class ScriptAction extends ComposerAction implements Roles {
  
  
      public Map act( Redirector redirector, SourceResolver resolver, Map objectModel, 
String source, Parameters par ) throws Exception
      {
          try {
              // Figure out what script to open.  A missing script name is caught
              // by the resolver/SystemId grouping later on and causes an exception
  
              String scriptName = source;
  
              getLogger().debug("script source [" + scriptName + "]");
  
              // Locate the appropriate file on the filesytem
  
              Source src = resolver.resolve(scriptName);
              String systemID = src.getSystemId();
  
              getLogger().debug("script resolved to [" + systemID + "]");
  
              // TODO: why doesn't this work?
              // Reader in = src.getCharacterStream();
  
              Reader in = new InputStreamReader(src.getInputStream());
  
              // Set up the BSF manager and register relevant helper "beans"
  
              BSFManager mgr = new BSFManager();
              HashMap actionMap = new HashMap();
  
              // parameters to act(...)
              mgr.registerBean("resolver", resolver);
              mgr.registerBean("objectModel", objectModel);
              mgr.registerBean("parameters", par);
  
              // ScriptAction housekeeping
              mgr.registerBean("actionMap", actionMap);
  
              // helpers
              // TODO: should we check for a null request object here or let the 
script handle it?
  
              mgr.registerBean("logger", getLogger());
              mgr.registerBean("request", ( (Request) 
objectModel.get(Constants.REQUEST_OBJECT) ) );
              mgr.registerBean("scriptaction", this );
              mgr.registerBean("manager", this.manager );
  
              getLogger().debug("BSFManager execution begining");
  
              // Execute the script
  
              mgr.exec(BSFManager.getLangFromFilename(systemID), systemID, 0, 0,
                      IOUtils.getStringFromReader(in));
  
              getLogger().debug("BSFManager execution complete");
  
              // Figure out what to return
              // TODO: decide on a more robust communication method
  
              if ( actionMap.containsKey( "scriptaction-continue" ) )
              {
                  return ( Collections.unmodifiableMap(actionMap) );
              }
              else
              {
                  return ( null );
              }
          } catch (FileNotFoundException e) {
              throw new ProcessingException(
                      "Exception in ScriptAction.act()", e);
          } catch (BSFException e) {
              throw new ProcessingException(
                      "Exception in ScriptAction.act()", e);
          } catch (Exception e) {
              throw new ProcessingException(
                      "Exception in ScriptAction.act()", e);
          } // try/catch
      } // public Map act(...)
  } // public class ScriptAction
  
  
  
  1.1                  xml-cocoon2/webapp/docs/samples/script-action/addknowncourse.js
  
  Index: addknowncourse.js
  ===================================================================
  // Step 1 -- Retrieve helper "beans" from the BSF framework
  
  scriptaction = bsf.lookupBean( "scriptaction" )
  manager      = bsf.lookupBean( "manager" )            
  request      = bsf.lookupBean( "request" )
  logger       = bsf.lookupBean( "logger" )
  actionMap    = bsf.lookupBean( "actionMap" )
  
  // Step 2 -- Perform the action
  
  logger.debug( "START addknowncourse.js" )
  
  // Retrieve things from the session and request
  // NOTE: they are all of type java.lang.String
  
  session       = request.getSession( false )
  uwid          = session.getAttribute( "uwid" )
  term          = request.getParameter( "term" )
  course                = request.getParameter( "course" )
  distanceEd    = request.getParameter( "distanceEd" )
  extra                 = request.getParameter( "extra" )
  
  logger.debug( "Raw" )
  logger.debug( "  uwid          [" + uwid + "]" )
  logger.debug( "  term          [" + term + "]" )
  logger.debug( "  course        [" + course + "]" )
  logger.debug( "  distanceEd    [" + distanceEd + "]" )
  logger.debug( "  extra         [" + extra + "]" )
  
  // Cook things a little to make them palatable to the database
  
  if ( term == "0" ) { term = "" }
  if ( course == "0" ) { course = "" }
  if ( distanceEd == "on" ) { distanceEd = 2 } else { distanceEd = 1 }
  if ( extra == "on" ) { extra = 2 } else { extra = 1 }
  
  logger.debug( "Cooked" )
  logger.debug( "  uwid          [" + uwid + "]" )
  logger.debug( "  term          [" + term + "]" )
  logger.debug( "  course        [" + course + "]" )
  logger.debug( "  distanceEd    [" + distanceEd + "]" )
  logger.debug( "  extra         [" + extra + "]" )
  
  // Actually do the database work
  
  // We have the choice of declaring things out here and making them explicitly
  // null, or we have to use a different comparison in the "finally" block (defined?)
  
  dbselector = null
  datasource = null
  conn = null
  addStatement = null
  
  try
  {
      dbselector = manager.lookup( scriptaction.DB_CONNECTION )
      datasource = dbselector.select( "ceabplanner" )
      conn = datasource.getConnection()    
      addStatement = conn.prepareStatement(
      "INSERT INTO studentKnownCourseList ( STUDENT, KNOWN_COURSE, TERM_TAKEN, 
DISTANCE_ED, COURSE_EXTRA)" +
      "VALUES  ( ( SELECT id FROM students WHERE uw_userid = ? ), ?, ?, ?, ? )"
      )
      
      addStatement.setString( 1, uwid ); 
      addStatement.setString( 2, course ); 
      addStatement.setString( 3, term ); 
      addStatement.setString( 4, distanceEd ); 
      addStatement.setString( 5, extra ); 
      
      result = addStatement.executeUpdate()
      logger.debug( "Result #1 [" + result + "]" )
      
      conn.commit()
      
      actionMap.put( "scriptaction-continue", "" )
      session.setAttribute( "results", "<SUCCESS>Course added at " + Date() + 
"</SUCCESS>" )    
  }
  catch( ex )
  {
      logger.debug( "Caught Exception" )
      logger.debug( "  " + ex )
  }
  finally
  {
      if ( null != addStatement ) { addStatement.close() }
      if ( null != conn ) { conn.close() }
      if ( null != datasource ) { dbselector.release( datasource ) }
      if ( null != dbselector ) { manager.release( dbselector ) }
  }
  
  logger.debug( "END addknown.js" )
  
  
  
  1.1                  xml-cocoon2/webapp/docs/samples/script-action/addunknowncse.js
  
  Index: addunknowncse.js
  ===================================================================
  // Step 1 -- Retrieve helper "beans" from the BSF framework
  
  scriptaction = bsf.lookupBean( "scriptaction" )
  manager      = bsf.lookupBean( "manager" )            
  request      = bsf.lookupBean( "request" )
  logger       = bsf.lookupBean( "logger" )
  actionMap    = bsf.lookupBean( "actionMap" )
  
  // Step 2 -- Perform the action
  
  logger.debug( "START addunknown.js" )
  
  lectureHrs = 3
  labHrs = 0
  tutorialHrs = 0
  numWeeks = 12
  courseWeight = 1
  cseweight = 0
  math = 0
  sci = 0
  engsci = 0
  engdes = 0
  
  // Retrieve things from the session and request
  // NOTE: they are all of type java.lang.String
  
  session    = request.getSession( false )
  uwid       = session.getAttribute( "uwid" )
  term       = request.getParameter( "term" )
  dept       = request.getParameter( "dept" )
  number     = request.getParameter( "number" )
  letter     = request.getParameter( "letter" )
  name       = request.getParameter( "name" )
  lectures   = request.getParameter( "lectures" )
  labs       = request.getParameter( "labs" )
  tutorials  = request.getParameter( "tutorials" )
  weeks      = request.getParameter( "weeks" )
  csetype    = request.getParameter( "csetype" )
  weight     = request.getParameter( "weight" )
  distanceEd = request.getParameter( "distanceEd" )
  extra      = request.getParameter( "extra" )
  
  logger.debug( "Raw" )
  logger.debug( "  uwid          [" + uwid + "]" )
  logger.debug( "  term          [" + term + "]" )
  logger.debug( "  dept          [" + dept + "]" )
  logger.debug( "  number        [" + number + "]" )
  logger.debug( "  letter        [" + letter + "]" )
  logger.debug( "  name          [" + name + "]" )
  logger.debug( "  lectures      [" + lectures + "]" )
  logger.debug( "  labs          [" + labs + "]" )
  logger.debug( "  tutorials     [" + tutorials + "]" )
  logger.debug( "  weeks         [" + weeks + "]" )
  logger.debug( "  math          [" + math + "]" )
  logger.debug( "  sci           [" + sci + "]" )
  logger.debug( "  engsci        [" + engsci + "]" )
  logger.debug( "  engdes        [" + engdes + "]" )
  logger.debug( "  csetype       [" + csetype + "]" )
  logger.debug( "  weight        [" + weight + "]" )
  logger.debug( "  distanceEd    [" + distanceEd + "]" )
  logger.debug( "  extra         [" + extra + "]" )
  logger.debug( "  lectureHrs    [" + lectureHrs + "]" )
  logger.debug( "  labHrs        [" + labHrs + "]" )
  logger.debug( "  tutorialHrs   [" + tutorialHrs + "]" )
  logger.debug( "  numWeeks      [" + numWeeks + "]" )
  logger.debug( "  courseWeight  [" + courseWeight + "]" )
  logger.debug( "  cseweight     [" + cseweight + "]" )
  
  // Cook things a little to make them palatable to the database
  
  if (lectures != null) { lectureHrs = ( lectures + "" ) }
  if (labs != null) { labHrs = ( labs + "" ) }
  if (tutorials != null) { tutorialHrs = ( tutorials + "" ) }
  if (weeks != null) { numWeeks = ( weeks + "" ) }
  if (weight != null) { courseWeight = ( weight + "" ) }
  
  // The "/1" is vital to ensure a numeric context
  
  cseweight=(((lectureHrs/1)+(labHrs/2)+(tutorialHrs/2))*numWeeks*courseWeight)
  
  if ( distanceEd == "on" ) { distanceEd = 2 } else { distanceEd = 1 }
  if ( extra == "on" ) { extra = 2 } else { extra = 1 }
  
  logger.debug( "Cooked" )
  logger.debug( "  uwid          [" + uwid + "]" )
  logger.debug( "  term          [" + term + "]" )
  logger.debug( "  dept          [" + dept + "]" )
  logger.debug( "  number        [" + number + "]" )
  logger.debug( "  letter        [" + letter + "]" )
  logger.debug( "  name          [" + name + "]" )
  logger.debug( "  lectures      [" + lectures + "]" )
  logger.debug( "  labs          [" + labs + "]" )
  logger.debug( "  tutorials     [" + tutorials + "]" )
  logger.debug( "  weeks         [" + weeks + "]" )
  logger.debug( "  math          [" + math + "]" )
  logger.debug( "  sci           [" + sci + "]" )
  logger.debug( "  engsci        [" + engsci + "]" )
  logger.debug( "  engdes        [" + engdes + "]" )
  logger.debug( "  csetype       [" + csetype + "]" )
  logger.debug( "  weight        [" + weight + "]" )
  logger.debug( "  distanceEd    [" + distanceEd + "]" )
  logger.debug( "  extra         [" + extra + "]" )
  logger.debug( "  lectureHrs    [" + lectureHrs + "]" )
  logger.debug( "  labHrs        [" + labHrs + "]" )
  logger.debug( "  tutorialHrs   [" + tutorialHrs + "]" )
  logger.debug( "  numWeeks      [" + numWeeks + "]" )
  logger.debug( "  courseWeight  [" + courseWeight + "]" )
  logger.debug( "  cseweight     [" + cseweight + "]" )
  
  // Actually do the database work
  
  // We have the choice of declaring things out here and making them explicitly
  // null, or we have to use a different comparison in the "finally" block (defined?)
  
  dbselector = null
  datasource = null
  conn = null
  addStatement = null
  
  try
  {
      dbselector = manager.lookup( scriptaction.DB_CONNECTION )
      datasource = dbselector.select( "ceabplanner" )
      conn = datasource.getConnection()    
      addStatement = conn.prepareStatement(
         "INSERT INTO otherCourses( ID, STUDENT, DEPARTMENT, COURSENUMBER, 
COURSELETTER, COURSENAME, MATH, SCI, ENGSCI, ENGDES, CSETYPE, CSEWEIGHT, TERM_TAKEN, 
DISTANCE_ED, COURSE_EXTRA) VALUES  ( otherCourses_seq.nextval, ( SELECT id FROM 
students WHERE uw_userid = ? ), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )"
      )
  
      addStatement.setString( 1, uwid ); 
      addStatement.setString( 2, dept ); 
      addStatement.setString( 3, number ); 
      addStatement.setString( 4, letter ); 
      addStatement.setString( 5, name ); 
      addStatement.setString( 6, math ); 
      addStatement.setString( 7, sci ); 
      addStatement.setString( 8, engsci ); 
      addStatement.setString( 9, engdes ); 
      addStatement.setString( 10, csetype ); 
      addStatement.setString( 11, cseweight ); 
      addStatement.setString( 12, term ); 
      addStatement.setString( 13, distanceEd ); 
      addStatement.setString( 14, extra ); 
  
      result = addStatement.executeUpdate()
      logger.debug( "Result #1 [" + result + "]" )
  
      conn.commit()
  
      actionMap.put( "scriptaction-continue", "" )
      session.setAttribute( "results", "<SUCCESS>Unknown Complementary Studies course 
added at " + Date() + "</SUCCESS>" )
  }
  catch( ex )
  {
      logger.debug( "Caught Exception" )
      logger.debug( "  " + ex )
  }
  finally
  {
      if ( null != addStatement ) { addStatement.close() }
      if ( null != conn ) { conn.close() }
      if ( null != datasource ) { dbselector.release( datasource ) }
      if ( null != dbselector ) { manager.release( dbselector ) }
  }
  
  
  logger.debug( "END addunknown.js" )
  
  
  
  1.1                  xml-cocoon2/webapp/docs/samples/script-action/addunknownte.js
  
  Index: addunknownte.js
  ===================================================================
  // Step 1 -- Retrieve helper "beans" from the BSF framework
  
  scriptaction = bsf.lookupBean( "scriptaction" )
  manager      = bsf.lookupBean( "manager" )            
  request      = bsf.lookupBean( "request" )
  logger       = bsf.lookupBean( "logger" )
  actionMap    = bsf.lookupBean( "actionMap" )
  
  // Step 2 -- Perform the action
  
  logger.debug( "START addunknownte.js" )
  
  csetype = ""
  weight = 0
  
  lectureHrs = 3
  labHrs = 0
  tutorialHrs = 0
  numWeeks = 12
  
  courseWeight = 0
  mathWeight = 0
  sciWeight = 0
  engsciWeight = 0
  engdesWeight = 0
  
  mathWt = 0
  sciWt = 0
  engsciWt = 0
  engdesWt = 0
  
  // Retrieve things from the session and request
  // NOTE: they are all of type java.lang.String
  
  session    = request.getSession( false )
  uwid       = session.getAttribute( "uwid" )
  term       = request.getParameter( "term" )
  dept       = request.getParameter( "dept" )
  number     = request.getParameter( "number" )
  letter     = request.getParameter( "letter" )
  name       = request.getParameter( "name" )
  lectures   = request.getParameter( "lectures" )
  labs       = request.getParameter( "labs" )
  tutorials  = request.getParameter( "tutorials" )
  weeks      = request.getParameter( "weeks" )
  math       = request.getParameter( "math" )
  sci        = request.getParameter( "sci" )
  engsci     = request.getParameter( "engsci" )
  engdes     = request.getParameter( "engdes" )
  distanceEd = request.getParameter( "distanceEd" )
  extra      = request.getParameter( "extra" )
  
  logger.debug( "Raw" )
  logger.debug( "  uwid          [" + uwid + "]" )
  logger.debug( "  term          [" + term + "]" )
  logger.debug( "  dept          [" + dept + "]" )
  logger.debug( "  number        [" + number + "]" )
  logger.debug( "  letter        [" + letter + "]" )
  logger.debug( "  name          [" + name + "]" )
  logger.debug( "  lectures      [" + lectures + "]" )
  logger.debug( "  labs          [" + labs + "]" )
  logger.debug( "  tutorials     [" + tutorials + "]" )
  logger.debug( "  weeks         [" + weeks + "]" )
  logger.debug( "  math          [" + math + "]" )
  logger.debug( "  sci           [" + sci + "]" )
  logger.debug( "  engsci        [" + engsci + "]" )
  logger.debug( "  engdes        [" + engdes + "]" )
  logger.debug( "  distanceEd    [" + distanceEd + "]" )
  logger.debug( "  extra         [" + extra + "]" )
  logger.debug( "  courseWeight  [" + courseWeight + "]" )
  logger.debug( "  mathWt        [" + mathWt + "]" )
  logger.debug( "  sciWt         [" + sciWt + "]" )
  logger.debug( "  engsciWt      [" + engsciWt + "]" )
  logger.debug( "  engdesWt      [" + engdesWt + "]" )
  
  // Cook things a little to make them palatable to the database
  
  if (lectures != null) { lectureHrs = ( lectures + "" ) }
  if (labs != null) { labHrs = ( labs + "" ) }
  if (tutorials != null) { tutorialHrs = ( tutorials + "" ) }
  if (weeks != null) { numWeeks = ( weeks + "" ) }
  
  if (math != null) { mathWeight = ( math + "" ) }
  if (sci != null) { sciWeight = ( sci + "" ) }
  if (engsci != null) { engsciWeight = ( engsci + "" ) }
  if (engdes != null) { engdesWeight = ( engdes + "" ) }
  
  // The "/1" is vital to ensure a numeric context
  
  courseWeight=(((lectureHrs/1)+(labHrs/2)+(tutorialHrs/2))*numWeeks)
  mathWt = courseWeight*mathWeight
  sciWt = courseWeight*sciWeight
  engsciWt = courseWeight*engsciWeight
  engdesWt = courseWeight*engdesWeight
  
  if ( distanceEd == "on" ) { distanceEd = 2 } else { distanceEd = 1 }
  if ( extra == "on" ) { extra = 2 } else { extra = 1 }
  
  logger.debug( "Cooked" )
  logger.debug( "  uwid          [" + uwid + "]" )
  logger.debug( "  term          [" + term + "]" )
  logger.debug( "  dept          [" + dept + "]" )
  logger.debug( "  number        [" + number + "]" )
  logger.debug( "  letter        [" + letter + "]" )
  logger.debug( "  name          [" + name + "]" )
  logger.debug( "  lectures      [" + lectures + "]" )
  logger.debug( "  labs          [" + labs + "]" )
  logger.debug( "  tutorials     [" + tutorials + "]" )
  logger.debug( "  weeks         [" + weeks + "]" )
  logger.debug( "  math          [" + math + "]" )
  logger.debug( "  sci           [" + sci + "]" )
  logger.debug( "  engsci        [" + engsci + "]" )
  logger.debug( "  engdes        [" + engdes + "]" )
  logger.debug( "  distanceEd    [" + distanceEd + "]" )
  logger.debug( "  extra         [" + extra + "]" )
  logger.debug( "  courseWeight  [" + courseWeight + "]" )
  logger.debug( "  mathWt        [" + mathWt + "]" )
  logger.debug( "  sciWt         [" + sciWt + "]" )
  logger.debug( "  engsciWt      [" + engsciWt + "]" )
  logger.debug( "  engdesWt      [" + engdesWt + "]" )
  
  // Actually do the database work
  
  // We have the choice of declaring things out here and making them explicitly
  // null, or we have to use a different comparison in the "finally" block (defined?)
  
  dbselector = null
  datasource = null
  conn = null
  addStatement = null
  
  try
  {
      dbselector = manager.lookup( scriptaction.DB_CONNECTION )
      datasource = dbselector.select( "ceabplanner" )
      conn = datasource.getConnection()    
      addStatement = conn.prepareStatement(
         "INSERT INTO otherCourses( " +
           "ID, STUDENT, DEPARTMENT, COURSENUMBER, COURSELETTER, COURSENAME, MATH, 
SCI, ENGSCI, ENGDES,  TERM_TAKEN, DISTANCE_ED, COURSE_EXTRA, CSEWEIGHT)" +
         "VALUES  ( " +
           "otherCourses_seq.nextval, ( SELECT id FROM students WHERE uw_userid = ? ), 
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0)"
      )
  
      addStatement.setString( 1, uwid ); 
      addStatement.setString( 2, dept ); 
      addStatement.setString( 3, number ); 
      addStatement.setString( 4, letter ); 
      addStatement.setString( 5, name ); 
      addStatement.setString( 6, mathWt ); 
      addStatement.setString( 7, sciWt ); 
      addStatement.setString( 8, engsciWt ); 
      addStatement.setString( 9, engdesWt ); 
      addStatement.setString( 10, term ); 
      addStatement.setString( 11, distanceEd ); 
      addStatement.setString( 12, extra ); 
  
      result = addStatement.executeUpdate()
      logger.debug( "Result #1 [" + result + "]" )
  
      conn.commit()
  
      actionMap.put( "scriptaction-continue", "" )
      session.setAttribute( "results", "<SUCCESS>Unknown Technical Elective added at " 
+ Date() + "</SUCCESS>" )
  
  }
  catch( ex )
  {
      logger.debug( "Caught Exception" )
      logger.debug( "  " + ex )
  }
  finally
  {
      if ( null != addStatement ) { addStatement.close() }
      if ( null != conn ) { conn.close() }
      if ( null != datasource ) { dbselector.release( datasource ) }
      if ( null != dbselector ) { manager.release( dbselector ) }
  }
  
  logger.debug( "END addunknownte.js" )
  
  
  
  1.1                  xml-cocoon2/webapp/docs/samples/script-action/createuser.js
  
  Index: createuser.js
  ===================================================================
  // Step 1 -- Retrieve helper "beans" from the BSF framework
  
  scriptaction = bsf.lookupBean( "scriptaction" )
  manager      = bsf.lookupBean( "manager" )            
  request      = bsf.lookupBean( "request" )
  logger       = bsf.lookupBean( "logger" )
  actionMap    = bsf.lookupBean( "actionMap" )
  
  // Step 2 -- Perform the action
  
  logger.debug( "START createuser.js" )
  
  // Retrieve things from the session and request
  // NOTE: they are all of type java.lang.String
  
  session = request.getSession( false )
  uwid  = session.getAttribute( "uwid" )
  
  logger.debug( "Raw" )
  logger.debug( "  uwid [" + uwid + "]" )
  
  // We have the choice of declaring things out here and making them explicitly
  // null, or we have to use a different comparison in the "finally" block (defined?)
  
  dbselector = null
  datasource = null
  conn = null
  queryStatement = null
  insertStatement = null
  
  try
  {
      dbselector = manager.lookup( scriptaction.DB_CONNECTION )
      datasource = dbselector.select( "ceabplanner" )
      conn = datasource.getConnection()    
  
      // Check that the student exists
  
      queryStatement = conn.prepareStatement(
                "select count(*) here from students where uw_userid=? "
      )
                
      queryStatement.setString( 1, uwid )
        
      resultSet = queryStatement.executeQuery()
      resultSet.next()
         
      userExists = resultSet.getInt("here")
      
      logger.debug( "Result #1 [" + userExists + "]" )
  
      if (!userExists)
      {
  
          logger.debug( "User does not exist...creating and initializing" )
  
          insertStatement = conn.prepareStatement(
                   "INSERT INTO students (id, uw_userid, name, uw_id, current_term) 
values (students_seq.nextval, ?, '','', (SELECT id FROM terms WHERE description = 
'1A'))"
          )
  
          insertStatement.setString( 1, uwid ); 
  
          result = insertStatement.executeUpdate()
          logger.debug( "Result #2 [" + result + "]" )
  
          insertStatement = conn.prepareStatement(
                   "insert into studentNotes (student,note) values ( ( SELECT id FROM 
students WHERE uw_userid = ? ),'')"
          )
  
          insertStatement.setString( 1, uwid ); 
  
          result = insertStatement.executeUpdate()
          logger.debug( "Result #3 [" + result + "]" )
  
          insertStatement = conn.prepareStatement(
                   "INSERT INTO studentKnownCourseList (student, known_course, 
term_taken, distance_ed, course_extra) ( SELECT ( SELECT id FROM students WHERE 
uw_userid = ? ), course, term, '1', '1' FROM corecourses )"
          )
  
          insertStatement.setString( 1, uwid ); 
  
          result = insertStatement.executeUpdate()
          logger.debug( "Result #4 [" + result + "]" )
  
          conn.commit()
      }
      else
      {
          logger.debug( "User exists" )
      }
      actionMap.put( "scriptaction-continue", "" )
  }
  catch( ex )
  {
      logger.debug( "Caught Exception" )
      logger.debug( "  " + ex )
  }
  finally
  {
      if ( null != queryStatement ) { queryStatement.close() }
      if ( null != insertStatement ) { insertStatement.close() }
      if ( null != conn ) { conn.close() }
      if ( null != datasource ) { dbselector.release( datasource ) }
      if ( null != dbselector ) { manager.release( dbselector ) }
  }
  
  logger.debug( "END createuser.js" )
  
  
  
  1.1                  xml-cocoon2/webapp/docs/samples/script-action/deleteknown.js
  
  Index: deleteknown.js
  ===================================================================
  // Step 1 -- Retrieve helper "beans" from the BSF framework
  
  scriptaction = bsf.lookupBean( "scriptaction" )
  manager      = bsf.lookupBean( "manager" )            
  request      = bsf.lookupBean( "request" )
  logger       = bsf.lookupBean( "logger" )
  actionMap    = bsf.lookupBean( "actionMap" )
  
  // Step 2 -- Perform the action
  
  logger.debug( "START deleteknown.js" )
  
  // Retrieve things from the session and request
  // NOTE: they are all of type java.lang.String
  
  session = request.getSession( false )
  uwid  = session.getAttribute( "uwid" )
  id    = request.getParameter( "id" )
  
  logger.debug( "Raw" )
  logger.debug( "  uwid          [" + uwid + "]" )
  logger.debug( "  id            [" + id + "]" )
  
  // Actually do the database work
  
  // We have the choice of declaring things out here and making them explicitly
  // null, or we have to use a different comparison in the "finally" block (defined?)
  
  dbselector = null
  datasource = null
  conn = null
  addStatement = null
  
  try
  {
      dbselector = manager.lookup( scriptaction.DB_CONNECTION )
      datasource = dbselector.select( "ceabplanner" )
      conn = datasource.getConnection()    
      deleteStatement = conn.prepareStatement(
          "DELETE FROM studentknowncourselist WHERE student = ( SELECT id FROM 
students WHERE uw_userid = ? ) AND known_course = ?"
      )
  
      deleteStatement.setString( 1, uwid ); 
      deleteStatement.setString( 2, id ); 
  
      result = deleteStatement.executeUpdate()
      logger.debug( "Result #1 [" + result + "]" )
  
      conn.commit()
  
      actionMap.put( "scriptaction-continue", "" )
      session.setAttribute( "results", "<SUCCESS>Course deleted at " + Date() + 
"</SUCCESS>" )
  }
  catch( ex )
  {
      logger.debug( "Caught Exception" )
      logger.debug( "  " + ex )
  }
  finally
  {
      if ( null != addStatement ) { addStatement.close() }
      if ( null != conn ) { conn.close() }
      if ( null != datasource ) { dbselector.release( datasource ) }
      if ( null != dbselector ) { manager.release( dbselector ) }
  }
  
  logger.debug( "END deleteknown.js" )
  
  
  
  1.1                  xml-cocoon2/webapp/docs/samples/script-action/deleteunknown.js
  
  Index: deleteunknown.js
  ===================================================================
  // Step 1 -- Retrieve helper "beans" from the BSF framework
  
  scriptaction = bsf.lookupBean( "scriptaction" )
  manager      = bsf.lookupBean( "manager" )            
  request      = bsf.lookupBean( "request" )
  logger       = bsf.lookupBean( "logger" )
  actionMap    = bsf.lookupBean( "actionMap" )
  
  // Step 2 -- Perform the action
  
  logger.debug( "START deleteunknown.js" )
  
  // Retrieve things from the session and request
  // NOTE: they are all of type java.lang.String
  
  id      = request.getParameter( "id" )
  
  logger.debug( "Raw" )
  logger.debug( "  id            [" + id + "]" )
  
  // Actually do the database work
  
  // We have the choice of declaring things out here and making them explicitly
  // null, or we have to use a different comparison in the "finally" block (defined?)
  
  dbselector = null
  datasource = null
  conn = null
  addStatement = null
  
  try
  {
      dbselector = manager.lookup( scriptaction.DB_CONNECTION )
      datasource = dbselector.select( "ceabplanner" )
      conn = datasource.getConnection()    
      deleteStatement = conn.prepareStatement(
                "DELETE FROM otherCourses WHERE id = ?"
      )
  
      deleteStatement.setString( 1, id ); 
  
      result = deleteStatement.executeUpdate()
      logger.debug( "Result #1 [" + result + "]" )
  
      conn.commit()
  
      actionMap.put( "scriptaction-continue", "" )
  }
  catch( ex )
  {
      logger.debug( "Caught Exception" )
      logger.debug( "  " + ex )
  }
  finally
  {
      if ( null != addStatement ) { addStatement.close() }
      if ( null != conn ) { conn.close() }
      if ( null != datasource ) { dbselector.release( datasource ) }
      if ( null != dbselector ) { manager.release( dbselector ) }
  }
  
  logger.debug( "END deleteunknown.js" )
  
  
  
  1.1                  xml-cocoon2/webapp/docs/samples/script-action/login.js
  
  Index: login.js
  ===================================================================
  // Step 1 -- Retrieve helper "beans" from the BSF framework
  
  request      = bsf.lookupBean( "request" )
  logger       = bsf.lookupBean( "logger" )
  actionMap    = bsf.lookupBean( "actionMap" )
  
  // Step 2 -- Perform the action
  
  logger.debug( "START login.js" )
  
  // Retrieve things from the request
  // NOTE: they are all of type java.lang.String
  
  uwid     = request.getParameter( "uwid" )
  password = request.getParameter( "password" )
  
  logger.debug( "Raw" )
  logger.debug( "  uwid     [" + uwid + "]" )
  //logger.debug( "  password [" + password + "]" )
  
  // Hack since either the guy we stole the code from or
  // Bruce has made an implementation error
  
  password = password + "\0\0\0\0\0\0\0\0\0"
  
  //logger.debug( "Cooked" )
  //logger.debug( "  password [" + password + "]" )
  
  try
  {
  
      session = request.getSession(true)
  
      radius = new Packages.RadiusClient( "engine.uwaterloo.ca","1812","7sjU2xbHa31s2" 
)
      result = radius.Authenticate( uwid, password )
  
      if ( radius.ACCESS_ACCEPT == result )
      {
          logger.debug( "Authentication succeeded" )
          
          // Get (and optionally create) and populate the session
          
          session.setAttribute( "uwid", uwid )
          
          actionMap.put( "scriptaction-continue", "" )    
      }
      else
      {
          logger.debug( "Authentication failed" )
          session.setAttribute( "results", "<FAILURE>Incorrect User ID or 
Password</FAILURE>" )
      }
  
  }
  catch( ex )
  {
      logger.debug( "Caught Exception" )
      logger.debug( "  " + ex )
  }
  
  logger.debug( "END login.js" )
  
  
  
  1.1                  xml-cocoon2/webapp/docs/samples/script-action/updateinfo.js
  
  Index: updateinfo.js
  ===================================================================
  // Step 1 -- Retrieve helper "beans" from the BSF framework
  
  scriptaction = bsf.lookupBean( "scriptaction" )
  manager      = bsf.lookupBean( "manager" )            
  request      = bsf.lookupBean( "request" )
  logger       = bsf.lookupBean( "logger" )
  actionMap    = bsf.lookupBean( "actionMap" )
  
  // Step 2 -- Perform the action
  
  logger.debug( "START updateinfo.js" )
  
  // Retrieve things from the session and request
  // NOTE: they are all of type java.lang.String
  
  session       = request.getSession( false )
  uwid        = session.getAttribute( "uwid" )
  studentname   = request.getParameter( "studentname" )
  studentnumber = request.getParameter( "studentnumber" )
  current_term  = request.getParameter( "current_term" )
  note          = request.getParameter( "note" )
  
  logger.debug( "Raw" )
  logger.debug( "  uwid           [" + uwid + "]" )
  logger.debug( "  studentname    [" + studentname + "]" )
  logger.debug( "  studentnumber  [" + studentnumber + "]" )
  logger.debug( "  current_term   [" + current_term + "]" )
  logger.debug( "  note           [" + note + "]" )
  
  // Cook things to make them more palatable to the database
  
  note = ( note + "" ) // convert to a JS string
  if (note == " ") { note="" }
  
  if ( note.length > 255 )
  {
     note = note.substring( 0, 255 ); // take the leading 255 characters
  }
  
  logger.debug( "Cooked" )
  logger.debug( "  uwid           [" + uwid + "]" )
  logger.debug( "  studentname    [" + studentname + "]" )
  logger.debug( "  studentnumber  [" + studentnumber + "]" )
  logger.debug( "  current_term   [" + current_term + "]" )
  logger.debug( "  note           [" + note + "]" )
  
  // We have the choice of declaring things out here and making them explicitly
  // null, or we have to use a different comparison in the "finally" block (defined?)
  
  dbselector = null
  datasource = null
  conn = null
  updateStatement = null
  
  try
  {
      dbselector = manager.lookup( scriptaction.DB_CONNECTION )
      datasource = dbselector.select( "ceabplanner" )
      conn = datasource.getConnection()    
      updateStatement = conn.prepareStatement(
          "UPDATE students SET name = ?, current_term = ?, uw_id = ? WHERE uw_userid = 
? "
      )
                
      updateStatement.setString( 1, studentname ); 
      updateStatement.setString( 2, current_term ); 
      updateStatement.setString( 3, studentnumber ); 
      updateStatement.setString( 4, uwid ); 
        
      result = updateStatement.executeUpdate()
      logger.debug( "Result #1 [" + result + "]" )
  
      updateStatement = conn.prepareStatement(
          "UPDATE studentnotes SET note = ? WHERE student = ( SELECT id FROM students 
WHERE uw_userid = ? )"
      )
  
      updateStatement.setString( 1, note ); 
      updateStatement.setString( 2, uwid ); 
  
      result = updateStatement.executeUpdate()
      logger.debug( "Result #2 [" + result + "]" )
  
      conn.commit()
      
      actionMap.put( "scriptaction-continue", "" )
      session.setAttribute( "results", "<SUCCESS>Information Saved at " + Date() + 
"</SUCCESS>" )
  
  }
  catch( ex )
  {
      logger.debug( "Caught Exception" )
      logger.debug( "  " + ex )
  }
  finally
  {
      if ( null != updateStatement ) { updateStatement.close() }
      if ( null != conn ) { conn.close() }
      if ( null != datasource ) { dbselector.release( datasource ) }
      if ( null != dbselector ) { manager.release( dbselector ) }
  }
  
  logger.debug( "END updateinfo.js" )
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     [EMAIL PROTECTED]
To unsubscribe, e-mail:          [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to