User: user57 Date: 01/10/02 14:34:56 Added: src/main/org/jboss/survey/ejb/session SequenceGeneratorBean.java SurveyManagementBean.java Log: o moved jboss-website/website/survey to jboss-website/survey (it's own module) Revision Changes Path 1.1 website-survey/src/main/org/jboss/survey/ejb/session/SequenceGeneratorBean.java Index: SequenceGeneratorBean.java =================================================================== /* * JBoss, the OpenSource J2EE webOS * * Distributable under LGPL license. * See terms of license at gnu.org. */ package org.jboss.survey.ejb.session; import java.rmi.RemoteException; import java.sql.Connection; import java.sql.Statement; import java.sql.ResultSet; import java.sql.SQLException; import javax.ejb.CreateException; import javax.ejb.EJBException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; import org.jboss.survey.ejb.Debug; import org.jboss.survey.exception.ServiceUnavailableException; /** * Encapsulates the retrival of DB data * * @author Andreas Schaefer * @version $Revision: 1.1 $ * * @ejb:bean name="jboss/survey/SequenceGenerator" * display-name="Generates unique Identifier for an Entity" * type="Stateless" * jndi-name="ejb/jboss/survey/SequenceGenerator" * @ejb:env-entry name="DataSource_Name" * value="SurveyDS" * @ejb:resource_ref res-name="jdbc/SurveyDS" */ public class SequenceGeneratorBean implements SessionBean { // ------------------------------------------------------------------------- // Static // ------------------------------------------------------------------------- // ------------------------------------------------------------------------- // Members // ------------------------------------------------------------------------- private SessionContext mContext; // Only for test purposes private int mNextNumber = 0; // ------------------------------------------------------------------------- // Methods // ------------------------------------------------------------------------- /** * Delivers the next sequence number from the given Sequence Name * * @param pSequenceName Name of the Sequence Generator * * @return Next sequence number * * @throws RemoteException * * @ejb:interface-method view-type="remote" **/ public int getNextNumber( String pSequenceName ) throws ServiceUnavailableException, RemoteException { Connection aConnection = null; Statement aStatement = null; try { // Get the Home Interface Context aJNDIContext = new InitialContext(); String lDataSourceName = (String) aJNDIContext.lookup( "java:comp/env/DataSource_Name" ); // Get the Datasource DataSource aSource = (DataSource) aJNDIContext.lookup( "java:/" + lDataSourceName ); // Get JDBC Connection, create statement and get the result to return aConnection = aSource.getConnection(); aStatement = aConnection.createStatement(); String aSql = "SELECT Nextval( '" + pSequenceName + "' ) "; if( Debug.LEVEL >= Debug.REGULAR ) System.err.println( "Sql Statement: " + aSql ); ResultSet aResult = aStatement.executeQuery( aSql ); if( aResult.next() ) { return aResult.getInt( 1 ); } else { return -1; } /* AS Hypersonic Way // Get the Home Interface Context aJNDIContext = new InitialContext(); // Get the Datasource DataSource aSource = (DataSource) aJNDIContext.lookup( "java:/DefaultDS" ); // Get JDBC Connection, create statement and get the result to return aConnection = aSource.getConnection(); aStatement = aConnection.createStatement(); //AS This is only working for a demo because two threads could get the same //AS Sequence Number because of multi threading String aSql = "SELECT MAX( id ) FROM " + pSequenceName; if( Debug.LEVEL >= Debug.REGULAR ) System.err.println( "Sql Statement: " + aSql ); ResultSet aResult = aStatement.executeQuery( aSql ); int lResult = -1; if( aResult.next() ) { lResult = aResult.getInt( 1 ); if( Debug.LEVEL >= Debug.REGULAR ) System.out.println( "Found maximum ID ffrom Survey: " + lResult ); if( lResult <= 0 ) { lResult = 1; } } return lResult + 1; */ } catch( SQLException se ) { se.printStackTrace( System.err ); throw new ServiceUnavailableException ( "Sequence number is broken" ); } catch( NamingException ne ) { ne.printStackTrace( System.err ); throw new ServiceUnavailableException ( "JNDI Lookup broken" ); } finally { try { if( aStatement != null ) { aStatement.close(); } if( aConnection != null ) { aConnection.close(); } } catch( Exception e ) { e.printStackTrace( System.err ); try { if( aConnection != null ) { aConnection.close(); } } catch( Exception e2 ) { e2.printStackTrace( System.err ); } } } } /** * 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 "SequenceGeneratorBean [ " + " ]"; } // ------------------------------------------------------------------------- // 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 1.1 website-survey/src/main/org/jboss/survey/ejb/session/SurveyManagementBean.java Index: SurveyManagementBean.java =================================================================== // ---------------------------------------------------------------------------- // File: SurveyManagementBean.java // Copyright ( c ) 2001 eBuilt, Inc. All rights reserved. // Version: $Revision: 1.1 $ // Last Checked In: $Date: 2001/10/02 21:34:56 $ // Last Checked In By: $Author: user57 $ // ---------------------------------------------------------------------------- package org.jboss.survey.ejb.session; import org.jboss.survey.ejb.Debug; import org.jboss.survey.ejb.entity.Survey; import org.jboss.survey.ejb.entity.SurveyBean; import org.jboss.survey.ejb.entity.SurveyData; import org.jboss.survey.ejb.entity.SurveyHome; import org.jboss.survey.ejb.entity.SurveyPK; import org.jboss.survey.ejb.entity.User; import org.jboss.survey.ejb.entity.UserBean; import org.jboss.survey.ejb.entity.UserData; import org.jboss.survey.ejb.entity.UserHome; import org.jboss.survey.ejb.entity.UserPK; import org.jboss.survey.exception.InvalidValueException; import org.jboss.survey.exception.ServiceUnavailableException; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.rmi.RemoteException; import javax.mail.Session; import javax.mail.internet.MimeMessage; import javax.mail.internet.InternetAddress; import javax.mail.Transport; import javax.mail.Address; import javax.mail.Message; 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; /** * Contains all the business logic around the Surveys management. * * @author Andreas Schaefer * @version $Revision: 1.1 $ * * @ejb:bean name="jboss/survey/SurveyManagement" * display-name="JBoss Survey Business Logic Session Bean" * type="Stateless" * jndi-name="ejb/jboss/survey/SurveyManagement" * @ejb:ejb-ref ejb-name="jboss/survey/Survey" * @ejb:ejb-ref ejb-name="jboss/survey/User" * @ejb:resource-ref res-name="mail/Mail" * res-type="javax.mail.Session" * res-auth="Container" * * @--ejb:jndi-name ejb/jboss/survey/SurveyManagement * @jboss:resource-manager res-man-class="javax.mail.Session" * res-man-jndi-name="mail/Mail" * res-man-name="Mail" **/ public class SurveyManagementBean implements SessionBean { // ------------------------------------------------------------------------- // Static // ------------------------------------------------------------------------- // ------------------------------------------------------------------------- // Members // ------------------------------------------------------------------------- private SessionContext mContext; // ------------------------------------------------------------------------- // Methods // ------------------------------------------------------------------------- /** * Returns the all the available Surveys * * @return Survey Value Object List * * @throws ServiceUnavailable If the service is unaccessible or unusable * @throws RemoteException Necessary for a EJB * * @ejb:interface-method view-type="remote" **/ public Collection getSurveys() throws ServiceUnavailableException, RemoteException { try { // Get the Home Interface Context aJNDIContext = new InitialContext(); SurveyHome aHome = (SurveyHome) aJNDIContext.lookup( "java:comp/env/ejb/jboss/survey/Survey" ); Iterator i = ( (Collection) aHome.findAll() ).iterator(); Collection lReturn = new ArrayList(); while( i.hasNext() ) { Survey lSurvey = (Survey) i.next(); lReturn.add( lSurvey.getValueObject() ); } return lReturn; } catch( NamingException ne ) { throw new ServiceUnavailableException( "Service Lookup is broken" ); } catch( FinderException fe ) { throw new ServiceUnavailableException( "Could not find all Surveys" ); } } /** * Returns the requested Survey * * @param pSurveyId Id of the requested Survey * * @return Survey Value Object * * @throws InvalidValueException If the requested Survey could not be found * @throws ServiceUnavailable If the service is unaccessible or unusable * @throws RemoteException Necessary for a EJB * * @ejb:interface-method view-type="remote" **/ public SurveyData getSurvey( int pSurveyId ) throws InvalidValueException, ServiceUnavailableException, RemoteException { try { // Get the Home Interface Context aJNDIContext = new InitialContext(); SurveyHome aHome = (SurveyHome) aJNDIContext.lookup( "java:comp/env/ejb/jboss/survey/Survey" ); // Get the pae by its id Survey aSurvey = (Survey) aHome.findByPrimaryKey( new SurveyPK( pSurveyId ) ); return aSurvey.getValueObject(); } catch( NamingException ne ) { throw new ServiceUnavailableException( "Service Lookup is broken" ); } catch( FinderException fe ) { throw new InvalidValueException( "id.invalid", new String[] { "Survey", "Id" } ); } } /** * Saves the given Survey (either create or edit the existing one) * * @param pSurvey Survey Value Object containing the Survey Information * * @return Updated Survey Data * * @throws InvalidValueException If the given Survey content is invalid * @throws ServiceUnavailable If the service is unaccessible or unusable * @throws RemoteException Necessary for a EJB * * @ejb:interface-method view-type="remote" **/ public SurveyData saveSurvey( SurveyData pSurvey ) throws InvalidValueException, ServiceUnavailableException, RemoteException { try { // Get the Home Interface Context aJNDIContext = new InitialContext(); SurveyHome aHome = (SurveyHome) aJNDIContext.lookup( "java:comp/env/ejb/jboss/survey/Survey" ); // Get the Entity Bean and save the Survey Survey aSurvey; if( pSurvey.getId() <= 0 ) { aSurvey = aHome.create( pSurvey ); } else { SurveyPK aPK = new SurveyPK( pSurvey.getId() ); aSurvey = (Survey) aHome.findByPrimaryKey( aPK ); aSurvey.setValueObject( pSurvey ); } try { UserHome aUserHome = (UserHome) aJNDIContext.lookup( "java:comp/env/ejb/jboss/survey/User" ); User aUser = aUserHome.findByPrimaryKey( new UserPK( pSurvey.getUserId() ) ); UserData aUserData = aUser.getValueObject(); if( aUserData.getEmail() != null && !aUserData.getEmail().equals( "" ) ) { Session lSession = (Session) new InitialContext().lookup( "java:Mail" );; MimeMessage lMessage = new MimeMessage( lSession ); lMessage.setFrom(); Address[] lTo = new InternetAddress[] { new InternetAddress( aUserData.getEmail() ) }; lMessage.setRecipients( Message.RecipientType.TO, lTo ); lMessage.setSubject( "Thank you for your support" ); lMessage.setSentDate( new java.util.Date() ); lMessage.setContent( "You added this information in our survey:\n " + pSurvey + "\n\nSee you later, the JBoss Open-Source project.", "text/plain" ); Transport.send( lMessage ); } } catch( Exception e ) { e.printStackTrace(); } return aSurvey.getValueObject(); } catch( NamingException ne ) { ne.printStackTrace( System.err ); throw new ServiceUnavailableException( "Service Lookup is broken" ); } catch( CreateException ce ) { ce.printStackTrace(); throw new ServiceUnavailableException( "Survey could not be created" ); } catch( FinderException fe ) { throw new InvalidValueException( "id.invalid", new String[] { "Survey", "Id" } ); } } /** * Removes the given Survey * * @param pSurveyId Survey ID of the Survey to be delete * * @throws InvalidValueException If the given Survey ID is not valid or Survey not found * @throws ServiceUnavailable If the service is unaccessible or unusable * @throws RemoteException Necessary for a EJB * * @ejb:interface-method view-type="remote" **/ public void removeSurvey( int pSurveyId ) throws InvalidValueException, ServiceUnavailableException, RemoteException { try { if( pSurveyId <= 0 ) { throw new InvalidValueException( "wrong.id", "Survey Id" ); } // Get the Home Interface Context aJNDIContext = new InitialContext(); SurveyHome aHome = (SurveyHome) aJNDIContext.lookup( "java:comp/env/ejb/jboss/survey/Survey" ); // Get the Survey Bean and remove it SurveyPK aPK = new SurveyPK( pSurveyId ); Survey aSurvey = (Survey) aHome.findByPrimaryKey( aPK ); // Now remove it aSurvey.remove(); } catch( NamingException ne ) { throw new ServiceUnavailableException( "Service Lookup is broken" ); } catch( FinderException fe ) { throw new InvalidValueException( "id.invalid", new String[] { "Survey", "Id" } ); } catch( RemoveException re ) { throw new ServiceUnavailableException( "Survey could not be removed" ); } } /** * Returns the requested User * * @param pUserId Id of the requested User * * @return User Value Object * * @throws InvalidValueException If the requested User could not be found * @throws ServiceUnavailable If the service is unaccessible or unusable * @throws RemoteException Necessary for a EJB * * @ejb:interface-method view-type="remote" **/ public UserData getUser( int pUserId ) throws InvalidValueException, ServiceUnavailableException, RemoteException { try { // Get the Home Interface Context aJNDIContext = new InitialContext(); UserHome aHome = (UserHome) aJNDIContext.lookup( "java:comp/env/ejb/jboss/survey/User" ); // Get the pae by its id User aUser = (User) aHome.findByPrimaryKey( new UserPK( pUserId ) ); return aUser.getValueObject(); } catch( NamingException ne ) { throw new ServiceUnavailableException( "Service Lookup is broken" ); } catch( FinderException fe ) { throw new InvalidValueException( "id.invalid", new String[] { "User", "Id" } ); } } /** * Returns a list of Users according to the given Search Criteria * * @param pSupportCriteria * @param pSizeCriteria * @param pTypeCriteria * * @return List of found users which can be empty but never null * * @throws InvalidValueException If the requested User could not be found * @throws ServiceUnavailable If the service is unaccessible or unusable * @throws RemoteException Necessary for a EJB * * @ejb:interface-method view-type="remote" **/ public Collection getUsers( int pSupportCriteria, int pSizeCriteria, int pTypeCriteria ) throws InvalidValueException, ServiceUnavailableException, RemoteException { try { // Get the Home Interface Context aJNDIContext = new InitialContext(); UserHome aHome = (UserHome) aJNDIContext.lookup( "java:comp/env/ejb/jboss/survey/User" ); //AS ToDo: Right now I have no idea how to make this seach happeing except // gettting all users and selected them here Collection lUsers = aHome.findAll(); Iterator i = lUsers.iterator(); Collection lReturn = new ArrayList(); while( i.hasNext() ) { UserData lUser = ( (User) i.next() ).getValueObject(); if( pSupportCriteria > 0 ) { if( ( lUser.getSupport() & pSupportCriteria ) <= 0 ) { i.remove(); continue; } } if( pSizeCriteria > 0 ) { switch( lUser.getNrOfEmployees() ) { case 0: if( ( pSizeCriteria & 1 ) == 0 ) { i.remove(); continue; } break; case 1: if( ( pSizeCriteria & 2 ) == 0 ) { i.remove(); continue; } break; case 2: if( ( pSizeCriteria & 4 ) == 0 ) { i.remove(); continue; } break; case 4: if( ( pSizeCriteria & 8 ) == 0 ) { i.remove(); continue; } break; case 40: if( ( pSizeCriteria & 16 ) == 0 ) { i.remove(); continue; } break; } } if( pTypeCriteria > 0 ) { if( ( lUser.getIndustryType() & pTypeCriteria ) <= 0 ) { i.remove(); continue; } } lReturn.add( lUser ); } return lReturn; } catch( NamingException ne ) { throw new ServiceUnavailableException( "Service Lookup is broken" ); } catch( FinderException fe ) { throw new InvalidValueException( "id.invalid", new String[] { "User", "Id" } ); } } /** * Returns the requested User * * @param pEmail Email address of the user * @param pPassword Password of the user * * @return User Value Object * * @throws InvalidValueException If the requested User could not be found * @throws ServiceUnavailable If the service is unaccessible or unusable * @throws RemoteException Necessary for a EJB * * @ejb:interface-method view-type="remote" **/ public UserData getUser( String pEmail, String pPassword ) throws InvalidValueException, ServiceUnavailableException, RemoteException { try { // Get the Home Interface Context aJNDIContext = new InitialContext(); UserHome aHome = (UserHome) aJNDIContext.lookup( "java:comp/env/ejb/jboss/survey/User" ); // Get the pae by its id User aUser = (User) aHome.findByEmailAndPassword( pEmail, pPassword ); return aUser.getValueObject(); } catch( NamingException ne ) { throw new ServiceUnavailableException( "Service Lookup is broken" ); } catch( FinderException fe ) { fe.printStackTrace(); throw new InvalidValueException( "user.notFound", new String[] { "User", pEmail, pPassword } ); } } /** * Saves the given User (either create or edit the existing one) * * @param pUser User Value Object containing the User Information * * @return Updated User Data * * @throws InvalidValueException If the given User content is invalid * @throws ServiceUnavailable If the service is unaccessible or unusable * @throws RemoteException Necessary for a EJB * * @ejb:interface-method view-type="remote" **/ public UserData saveUser( UserData pUser ) throws InvalidValueException, ServiceUnavailableException, RemoteException { try { // Get the Home Interface Context aJNDIContext = new InitialContext(); UserHome aHome = (UserHome) aJNDIContext.lookup( "java:comp/env/ejb/jboss/survey/User" ); // Get the Entity Bean and save the User User aUser; if( pUser.getId() <= 0 ) { aUser = aHome.create( pUser ); } else { UserPK aPK = new UserPK( pUser.getId() ); aUser = (User) aHome.findByPrimaryKey( aPK ); aUser.setValueObject( pUser ); } return aUser.getValueObject(); } catch( NamingException ne ) { ne.printStackTrace( System.err ); throw new ServiceUnavailableException( "Service Lookup is broken" ); } catch( CreateException ce ) { ce.printStackTrace(); throw new ServiceUnavailableException( "User could not be created" ); } catch( FinderException fe ) { throw new InvalidValueException( "id.invalid", new String[] { "User", "Id" } ); } } /** * Removes the given User * * @param pUserId User ID of the User to be delete * * @throws InvalidValueException If the given User ID is not valid or User not found * @throws ServiceUnavailable If the service is unaccessible or unusable * @throws RemoteException Necessary for a EJB * * @ejb:interface-method view-type="remote" **/ public void removeUser( int pUserId ) throws InvalidValueException, ServiceUnavailableException, RemoteException { try { if( pUserId <= 0 ) { throw new InvalidValueException( "wrong.id", "User Id" ); } // Get the Home Interface Context aJNDIContext = new InitialContext(); UserHome aHome = (UserHome) aJNDIContext.lookup( "java:comp/env/ejb/jboss/survey/User" ); // Get the User Bean and remove it UserPK aPK = new UserPK( pUserId ); User aUser = (User) aHome.findByPrimaryKey( aPK ); // Now remove it aUser.remove(); } catch( NamingException ne ) { throw new ServiceUnavailableException( "Service Lookup is broken" ); } catch( FinderException fe ) { throw new InvalidValueException( "id.invalid", new String[] { "User", "Id" } ); } catch( RemoveException re ) { throw new ServiceUnavailableException( "User could not be removed" ); } } /** * Sends an email to the given User * * @ejb:interface-method view-type="remote" **/ public void sendEmail( String pFromAddress, String pFromName, String pToAddress, String pSubject, String pEmail ) throws RemoteException { try { Session lSession = (Session) new InitialContext().lookup( "java:Mail" );; MimeMessage lMessage = new MimeMessage( lSession ); lMessage.setFrom( new InternetAddress( pFromAddress ) ); Address[] lTo = new InternetAddress[] { new InternetAddress( pToAddress ) }; lMessage.setRecipients( Message.RecipientType.TO, lTo ); lMessage.setSubject( pSubject ); lMessage.setSentDate( new java.util.Date() ); lMessage.setContent( pEmail, "text/plain" ); Transport.send( lMessage ); } catch( Exception e ) { throw new RemoteException( e.getMessage() ); } } /** * 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 "SurveyManagementBean [ " + " ]"; } // ------------------------------------------------------------------------- // 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