User: schaefera
  Date: 02/03/19 15:12:31

  Added:       src/main/org/jboss/survey/ejb/session
                        SurveyStatisticsBean.java
  Log:
  Some Adjustments to Survey and adding JBoss 3.0 Beta to the download
  page of www.jboss.org.
  
  Revision  Changes    Path
  1.1                  
website-survey/src/main/org/jboss/survey/ejb/session/SurveyStatisticsBean.java
  
  Index: SurveyStatisticsBean.java
  ===================================================================
  /*
   * JBoss, the OpenSource J2EE webOS
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.survey.ejb.session;
  
  import org.jboss.survey.ejb.Debug;
  import org.jboss.survey.exception.InvalidValueException;
  import org.jboss.survey.exception.ServiceUnavailableException;
  
  import java.sql.Connection;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Statement;
  import java.util.Hashtable;
  import java.rmi.RemoteException;
  import javax.ejb.CreateException;
  import javax.ejb.EJBException;
  // import javax.ejb.FinderException;
  // import javax.ejb.RemoveException;
  import javax.ejb.SessionBean;
  import javax.ejb.SessionContext;
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  import javax.sql.DataSource;
  
  /**
   * Contains all the business logic around the Surveys Statistics.
   *
   * @author  <a href="mailto:[EMAIL PROTECTED]";>Andreas Schaefer</a>.
   * @version $Revision: 1.1 $
   *   
   * <p><b>Revisions:</b>
   *
   * <p><b>20011126 Andreas Schaefer:</b>
   * <ul>
   * <li> Creation
   * </ul>
   *
   * @ejb:bean name="jboss/survey/SurveyStatistics"
   *           display-name="JBoss Survey Statistics Logic Session Bean"
   *           type="Stateless"
   *           jndi-name="ejb/jboss/survey/SurveyStatistics"
   *
   * @ejb:resource-ref res-name="jdbc/SurveyDS"
   *                   res-type="javax.sql.DataSource"
   *                   res-auth="Container"
   *
   * @jboss:resource-ref res-ref-name="jdbc/SurveyDS"
   *                     resource-name="SurveyDS"
   * @jboss:resource-manager res-man-class=""
   *                         res-man-name="SurveyDS"
   *                         res-man-jndi-name="java:/SurveyDS"
   **/
  public class SurveyStatisticsBean
     implements SessionBean
  {
  
     // -------------------------------------------------------------------------
     // Static
     // -------------------------------------------------------------------------
     
     // -------------------------------------------------------------------------
     // Members 
     // -------------------------------------------------------------------------
     
     private SessionContext mContext;
     
     // -------------------------------------------------------------------------
     // Methods
     // -------------------------------------------------------------------------  
     
     /**
     * Returns the statistics of all the Surveys. 
     *
     * @return List of statistics values
     *
     * @throws ServiceUnavailable If the service is unaccessible or unusable
     * @throws RemoteException Necessary for a EJB
     *
     * @ejb:interface-method view-type="remote"
     **/
     public Hashtable getStatistics()
        throws
           ServiceUnavailableException,
           RemoteException
     {
        Connection lConnection = null;
        try {
           lConnection = getDBConnection();
           
           Statement lStatement = lConnection.createStatement();
           ResultSet lResults = lStatement.executeQuery(
              "SELECT ui.industry_type, ui.nr_of_employees " +
              "  FROM user_info ui, " +
              "       survey s " +
              " WHERE s.user_id = ui.id "
           );
           int ucs1 = 0;
           int ucs2 = 0;
           int ucs3 = 0;
           int ucs4 = 0;
           int ucs5 = 0;
           int ucs6 = 0;
           int ucs7 = 0;
           int ucs8 = 0;
           int ucs9 = 0;
           int ucs10 = 0;
           int ucs11 = 0;
           int cs1 = 0;
           int cs2 = 0;
           int cs3 = 0;
           int cs4 = 0;
           int cs5 = 0;
           int lSize = 0;
           while( lResults.next() ) {
              if( ( lResults.getInt( 1 ) & 1 ) > 0 ) {
                 ucs1++;
              }
              if( ( lResults.getInt( 1 ) & 2 ) > 0 ) {
                 ucs2++;
              }
              if( ( lResults.getInt( 1 ) & 4 ) > 0 ) {
                 ucs3++;
              }
              if( ( lResults.getInt( 1 ) & 8 ) > 0 ) {
                 ucs4++;
              }
              if( ( lResults.getInt( 1 ) & 16 ) > 0 ) {
                 ucs5++;
              }
              if( ( lResults.getInt( 1 ) & 32 ) > 0 ) {
                 ucs6++;
              }
              if( ( lResults.getInt( 1 ) & 64 ) > 0 ) {
                 ucs7++;
              }
              if( ( lResults.getInt( 1 ) & 128 ) > 0 ) {
                 ucs8++;
              }
              if( ( lResults.getInt( 1 ) & 256 ) > 0 ) {
                 ucs9++;
              }
              if( ( lResults.getInt( 1 ) & 512 ) > 0 ) {
                 ucs10++;
              }
              if( ( lResults.getInt( 1 ) & 1024 ) > 0 ) {
                 ucs11++;
              }
              switch( lResults.getInt( 2 ) ) {
                 case 0:
                    cs1++;
                    break;
                 case 1:
                    cs2++;
                    break;
                 case 2:
                    cs3++;
                    break;
                 case 4:
                    cs4++;
                    break;
                 case 40:
                    cs5++;
                    break;
              }
              lSize++;
           }
           Hashtable lReturn = new Hashtable();
           lReturn.put( "NOU", lSize + "" );
           lReturn.put( "UCS1", ( 100 * ucs1 / lSize ) + " %" );
           lReturn.put( "UCS2", ( 100 * ucs2 / lSize ) + " %" );
           lReturn.put( "UCS3", ( 100 * ucs3 / lSize ) + " %" );
           lReturn.put( "UCS4", ( 100 * ucs4 / lSize ) + " %" );
           lReturn.put( "UCS5", ( 100 * ucs5 / lSize ) + " %" );
           lReturn.put( "UCS6", ( 100 * ucs6 / lSize ) + " %" );
           lReturn.put( "UCS7", ( 100 * ucs7 / lSize ) + " %" );
           lReturn.put( "UCS8", ( 100 * ucs8 / lSize ) + " %" );
           lReturn.put( "UCS9", ( 100 * ucs9 / lSize ) + " %" );
           lReturn.put( "UCS10", ( 100 * ucs10 / lSize ) + " %" );
           lReturn.put( "UCS11", ( 100 * ucs11 / lSize ) + " %" );
           lReturn.put( "CS1", ( 100 * cs1 / lSize ) + " %" );
           lReturn.put( "CS2", ( 100 * cs2 / lSize ) + " %" );
           lReturn.put( "CS3", ( 100 * cs3 / lSize ) + " %" );
           lReturn.put( "CS4", ( 100 * cs4 / lSize ) + " %" );
           lReturn.put( "CS5", ( 100 * cs5 / lSize ) + " %" );
           
           lStatement.close();
           
           lStatement = lConnection.createStatement();
           lResults = lStatement.executeQuery(
              "SELECT s.jboss_usage, s.jboss_first_time, s.jboss_other_server " +
              "  FROM survey s "
           );
           int jbu1 = 0;
           int jbu2 = 0;
           int jbu3 = 0;
           int jbu4 = 0;
           int jbu5 = 0;
           int ftu = 0;
           int sf1 = 0;
           int sf2 = 0;
           int sf3 = 0;
           int sf4 = 0;
           int sf5 = 0;
           int sf6 = 0;
           int sf7 = 0;
           int sf8 = 0;
           int sf9 = 0;
           int sf10 = 0;
           lSize = 0;
           while( lResults.next() ) {
              if( lResults.getString( 1 ).equals( "Evaluation" ) ) {
                 jbu1++;
              }
              if( lResults.getString( 1 ).equals( "Development" ) ) {
                 jbu2++;
              }
              if( lResults.getString( 1 ).equals( "Reference" ) ) {
                 jbu3++;
              }
              if( lResults.getString( 1 ).equals( "Production" ) ) {
                 jbu4++;
              }
              if( lResults.getString( 1 ).equals( "Packaged" ) ) {
                 jbu5++;
              }
              if( lResults.getBoolean( 2 ) ) {
                 ftu++;
              } else {
                 switch( lResults.getInt( 3 ) ) {
                    case 0:
                       sf1++;
                       break;
                    case 1:
                       sf2++;
                       break;
                    case 2:
                       sf3++;
                       break;
                    case 3:
                       sf4++;
                       break;
                    case 4:
                       sf5++;
                       break;
                    case 5:
                       sf6++;
                       break;
                    case 6:
                       sf7++;
                       break;
                    case 7:
                       sf8++;
                       break;
                    case 8:
                       sf9++;
                       break;
                    case 9:
                       sf10++;
                       break;
                 }
              }
              lSize++;
           }
           lReturn.put( "NOS", lSize + "" );
           lReturn.put( "JBU1", ( 100 * jbu1 / lSize ) + " %" );
           lReturn.put( "JBU2", ( 100 * jbu2 / lSize ) + " %" );
           lReturn.put( "JBU3", ( 100 * jbu3 / lSize ) + " %" );
           lReturn.put( "JBU4", ( 100 * jbu4 / lSize ) + " %" );
           lReturn.put( "JBU5", ( 100 * jbu5 / lSize ) + " %" );
           lReturn.put( "FTU", ( 100 * ftu / lSize ) + " %" );
           lReturn.put( "NFTU", ( 100 * ( lSize - ftu ) / lSize ) + " %" );
           lReturn.put( "SF1", ( 100 * sf1 / lSize ) + " %" );
           lReturn.put( "SF2", ( 100 * sf2 / lSize ) + " %" );
           lReturn.put( "SF3", ( 100 * sf3 / lSize ) + " %" );
           lReturn.put( "SF4", ( 100 * sf4 / lSize ) + " %" );
           lReturn.put( "SF5", ( 100 * sf5 / lSize ) + " %" );
           lReturn.put( "SF6", ( 100 * sf6 / lSize ) + " %" );
           lReturn.put( "SF7", ( 100 * sf7 / lSize ) + " %" );
           lReturn.put( "SF8", ( 100 * sf8 / lSize ) + " %" );
           lReturn.put( "SF9", ( 100 * sf9 / lSize ) + " %" );
           lReturn.put( "SF10", ( 100 * sf10 / lSize ) + " %" );
           
           return lReturn;
        }
        catch( SQLException se ) {
           se.printStackTrace();
           throw new ServiceUnavailableException( "Database problem reported" );
        }
     }
     
     /**
     * Create the Session Bean
     *
     * @throws CreateException 
     *
     * @ejb:create-method view-type="remote"
     **/
     public void ejbCreate()
        throws
           CreateException
     {
     }
     
     /**
     * Describes the instance and its content for debugging purpose
     *
     * @return Debugging information about the instance and its content
     **/
     public String toString()
     {
        return "SurveyStatisticsBean [ " + " ]";
     }
     
     private Connection getDBConnection()
        throws ServiceUnavailableException,
           SQLException
     {
        try {
           // Get the Data Source
           Context lContext = new InitialContext();
           Object lResource = lContext.lookup( "java:comp/env/jdbc/SurveyDS" );
           DataSource lSource = (DataSource) lResource;
           
           return lSource.getConnection();
        }
        catch( NamingException ne ) {
           throw new ServiceUnavailableException( "Service Lookup is broken" );
        }
     }
     
     // -------------------------------------------------------------------------
     // Framework Callbacks
     // -------------------------------------------------------------------------  
     
     /**
     * Set the associated session context. The container invokes this method on 
     * an instance after the instance has been created. 
     * <p>This method is called with no transaction context.
     *
     * @param aContext A SessionContext interface for the instance. The instance 
     *  should store the reference to the context in an instance variable.
     * @throws EJBException Should something go wrong while seting the context,
     *  an EJBException will be thrown.
     **/
     public void setSessionContext( SessionContext aContext )
        throws
           EJBException
     {
        mContext = aContext;
     }
     
     
     /**
     * The activate method is called when the instance is activated from its 
     * "passive" state. The instance should acquire any resource that it has 
     * released earlier in the ejbPassivate() method. 
     * <p>This method is called with no transaction context.
     *
     * @throws EJBException Thrown by the method to indicate a failure caused 
     *  by a system-level error
     **/
     public void ejbActivate()
        throws
           EJBException
     {
     }
     
     
     /**
     * The passivate method is called before the instance enters the "passive" 
     * state. The instance should release any resources that it can re-acquire 
     * later in the ejbActivate() method. 
     * <p>After the passivate method completes, the instance must be in a state 
     * that allows the container to use the Java Serialization protocol to 
     * externalize and store away the instance's state. 
     * <p>This method is called with no transaction context.
     *
     * @throws EJBException Thrown by the method to indicate a failure caused 
     *  by a system-level error
     **/
     public void ejbPassivate()
        throws
           EJBException
     {
     }
     
     
     /**
     * A container invokes this method before it ends the life of the session 
     * object. This happens as a result of a client's invoking a remove 
     * operation, or when a container decides to terminate the session object 
     * after a timeout. 
     * <p>This method is called with no transaction context.
     *
     * @throws EJBException Thrown by the method to indicate a failure caused 
     *  by a system-level error
     **/
     public void ejbRemove()
        throws
           EJBException
     {
     }
     
  }
  
  // ----------------------------------------------------------------------------
  // EOF
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to