Package com.centraview.marketing.events

Source Code of com.centraview.marketing.events.EventsEJB

/*
* $RCSfile: EventsEJB.java,v $    $Revision: 1.1.1.1 $  $Date: 2005/04/28 20:22:40 $ - $Author: mking_cv $
*
* The contents of this file are subject to the Open Software License
* Version 2.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.centraview.com/opensource/license.html
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is: CentraView Open Source.
*
* The developer of the Original Code is CentraView.  Portions of the
* Original Code created by CentraView are Copyright (c) 2004 CentraView,
* LLC; All Rights Reserved.  The terms "CentraView" and the CentraView
* logos are trademarks and service marks of CentraView, LLC.
*/

package com.centraview.marketing.events;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.Context;
import javax.naming.InitialContext;

import com.centraview.administration.authorization.AuthorizationLocal;
import com.centraview.administration.authorization.AuthorizationLocalHome;
import com.centraview.common.AuthorizationFailedException;
import com.centraview.common.CVDal;
import com.centraview.common.CVUtility;
import com.centraview.marketing.EventAttendeeVO;
import com.centraview.marketing.EventDetails;

/**
*  This Ejb is Staleless SessionBean
*  Used for Add , Edit ,Delete ,Get Events details.
*   @author  Sunita
*/

public class EventsEJB implements SessionBean
{
  protected SessionContext context;
  protected Context ctx;
  private String dataSource = "MySqlDS";
  /**
  This method sets the context for this Bean
  */
  public void setSessionContext(SessionContext context)
  {
    this.context = context;
  }

  public void ejbPassivate() {}
  public void ejbActivate() {}
  public void ejbRemove() {}
  public void ejbCreate() {}

  /*
   *  Used to add new events
   *  @param mapEvent HashMap
   *  @return int
   */
  public int addEvent(HashMap mapEvent, int userID) throws AuthorizationFailedException
  {
    if(!CVUtility.isModuleVisible("Events",userID, this.dataSource))
      throw new AuthorizationFailedException("Event - getEventDetails");

    //System.out.println("[DEBUG] [EventsEJB]: In addEvent");
    int eventid=0;

    String strName         = (String)mapEvent.get("Name");
    String strDescription     = (String)mapEvent.get("Description");
    String whoshouldattend     = (String)mapEvent.get("WhoShouldAttend");
    int maxattendees      = Integer.parseInt(mapEvent.get("MaxAttendees").toString());
    int moderator         = Integer.parseInt(mapEvent.get("Moderator").toString());
    Timestamp startdate     = (Timestamp)mapEvent.get("StartDate");
    Timestamp enddate       = (Timestamp)mapEvent.get("EndDate");
    int creator         = Integer.parseInt(mapEvent.get("Creator").toString());

    CVDal cvdal = new CVDal(dataSource);
    try
    {
      cvdal.setSql("marketing.insertevent");
      cvdal.setString(1 , strName );
      cvdal.setString(2 , strDescription );
      cvdal.setTimestamp(3 , startdate );
      cvdal.setTimestamp(4 , enddate );
      cvdal.setString(5 , whoshouldattend );
      cvdal.setInt(6 , userID );
      cvdal.setInt(7 , creator );
      cvdal.setInt(8 , maxattendees );
      cvdal.executeUpdate();
      eventid = cvdal.getAutoGeneratedKey();
      cvdal.clearParameters();

      String[] attchmentids = (String[])mapEvent.get("Attachment");

      if (attchmentids != null && attchmentids.length != 0)
      {
        for (int i=0; i<attchmentids.length;i++)
        {
          String fileid = attchmentids[i];
          cvdal.setSqlQuery("insert into eventlink VALUES (?,?,?)");
          cvdal.setInt(1, eventid);
          cvdal.setInt(2, 6);
          cvdal.setInt(3, Integer.parseInt(fileid));
          cvdal.executeUpdate();
          cvdal.clearParameters();

        } //end of while loop(itt.hasNext())
      } //end of if statement ((attchmentids != null) && (attchmentids.size() != 0))

            InitialContext ic = CVUtility.getInitialContext();
            AuthorizationLocalHome authorizationHome = (AuthorizationLocalHome)ic.lookup("local/Authorization");
            AuthorizationLocal authorizationLocal = authorizationHome.create();
            authorizationLocal.setDataSource(dataSource);
            authorizationLocal.saveCurrentDefaultPermission("Events", eventid, userID);

    }
    catch(Exception exe)
    {
      exe.printStackTrace();
      eventid = -1;
    } finally {
          cvdal.destroy();
          cvdal = null;
    }
    return eventid;
  }


  /*
   *  Used to add new EventRegister
   *  @param mapEvents HashMap
   *  @return int
   */
  public int addEventRegister(HashMap mapEvent, int userID) throws AuthorizationFailedException
  {
    if(!CVUtility.isModuleVisible("Events",userID, this.dataSource))
      throw new AuthorizationFailedException("Event - getEventDetails");

    //System.out.println("In addEvent");
    int result=0;
    int eventId = Integer.parseInt(mapEvent.get("EventId").toString());
    String strAccepted = (String)mapEvent.get("Accepted");
    String strIndividualId[] = (String[])mapEvent.get("IndividualId");

    CVDal cvdal = new CVDal(dataSource);
    try
    {
      for (int i=0;i<strIndividualId.length;i++)
      {
        cvdal.setSql("marketing.inserteventregister");
        cvdal.setInt(1,eventId);
        cvdal.setInt(2,Integer.parseInt(strIndividualId[i]));
        cvdal.setString(3,strAccepted);
        cvdal.executeUpdate();
        cvdal.clearParameters();
      }
      cvdal.destroy();
    }
    catch(Exception exe)
    {
      exe.printStackTrace();
      result = -1;
    }
    //System.out.println("In addEvent " +result);
    return result;

  }

  /*
   *  Used to getEventdetails
   *  @param  eventid int
   *  @return EventDetails
   */
  public EventDetails getEventDetails(int userID, int eventID) throws AuthorizationFailedException
  {

    if(!CVUtility.isModuleVisible("Events",userID, this.dataSource))
      throw new AuthorizationFailedException("Event - getEventDetails");

    //System.out.println("In getEventDetails");
    EventDetails eventDetails = new EventDetails();
    CVDal cvdal = new CVDal(dataSource);
    Collection colDetails = null;
    try
    {
      cvdal.setSql("marketing.geteventdetails");
      cvdal.setInt(1,eventID);
      colDetails = cvdal.executeQuery();
      cvdal.clearParameters();

      Iterator it = colDetails.iterator();
      while (it.hasNext())
      {
        HashMap mapResult = (HashMap)it.next();
        eventDetails.setMaxattendees(Integer.parseInt(mapResult.get("maxattendees").toString()));
        eventDetails.setModeratorid(Integer.parseInt(mapResult.get("ownerid").toString()));
        eventDetails.setModeratorname((String)mapResult.get("ownername"));
        eventDetails.setStartdate((Timestamp)mapResult.get("startdate"));
        eventDetails.setEnddate((Timestamp)mapResult.get("enddate"));
        eventDetails.setWhoshouldattend((String)mapResult.get("formember"));
        eventDetails.setFormember((String)mapResult.get("formember"));
        eventDetails.setDetail((String)mapResult.get("detail"));
        eventDetails.setName((String)mapResult.get("title"));
        eventDetails.setCreateddate((Timestamp)mapResult.get("createddate"));
        eventDetails.setModifieddate((Timestamp)mapResult.get("modifieddate"));
      }

      ArrayList attachedFiles = new ArrayList();

      cvdal.setSql("customer.events.getEventAttachments");
      cvdal.setInt(1, eventID);
      Collection filesResults = cvdal.executeQuery();
      cvdal.clearParameters();

      if (filesResults != null)
      {
        Iterator filesIter = filesResults.iterator();
        while (filesIter.hasNext())
        {
          HashMap fileRow = (HashMap)filesIter.next();
          attachedFiles.add(fileRow);
        }
      }
      eventDetails.setAttachedFiles(attachedFiles);

      cvdal.destroy();
    }
    catch(Exception exe)
    {
      exe.printStackTrace();
      return null;
    }
    //System.out.println("In getEventDetails " +eventDetails);
    return eventDetails;
  }

  /*
   *  Used to editEvents
   *  @param  mapEvents HashMap
   *  @return int
   */
  public int editEvent(HashMap mapEvent, int userID) throws AuthorizationFailedException
  {
    if(!CVUtility.isModuleVisible("Events",userID, this.dataSource))
      throw new AuthorizationFailedException("Event - getEventDetails");

    //System.out.println("In editEvent");
    int eventid         = Integer.parseInt(mapEvent.get("eventid").toString());
    String strName         = (String)mapEvent.get("Name");
    String strDescription     = (String)mapEvent.get("Description");
    String whoshouldattend     = (String)mapEvent.get("WhoShouldAttend");
    int maxattendees      = Integer.parseInt(mapEvent.get("MaxAttendees").toString());
    int moderator         = Integer.parseInt(mapEvent.get("Moderator").toString());
    Timestamp startdate     = (Timestamp)mapEvent.get("StartDate");
    Timestamp enddate       = (Timestamp)mapEvent.get("EndDate");
    int creator         = Integer.parseInt(mapEvent.get("Creator").toString());
    CVDal cvdal = new CVDal(dataSource);
    try
    {
      cvdal.setSql("marketing.updateevent");
      cvdal.setString(1 , strName );
      cvdal.setString(2 , strDescription );
      cvdal.setTimestamp(3 , startdate );
      cvdal.setTimestamp(4 , enddate );
      cvdal.setString(5 , whoshouldattend );
      cvdal.setInt(6 , moderator );
      cvdal.setInt(7 , creator );
      cvdal.setInt(8 , maxattendees );
      cvdal.setInt(9 , eventid );
      cvdal.executeUpdate();
      cvdal.clearParameters();

      cvdal.setSqlQuery("delete from eventlink where EventID = "+eventid);
      cvdal.executeUpdate();
      cvdal.clearParameters();

      String[] attchmentids = (String[])mapEvent.get("Attachment");

      if (attchmentids != null && attchmentids.length != 0)
      {

        for (int i=0; i<attchmentids.length;i++)
        {
          String fileid = attchmentids[i];
          cvdal.setSqlQuery("insert into eventlink VALUES (?,?,?)");
          cvdal.setInt(1, eventid);
          cvdal.setInt(2, 6);
          cvdal.setInt(3, Integer.parseInt(fileid));
          cvdal.executeUpdate();
          cvdal.clearParameters();
        } //end of while loop(itt.hasNext())
      } //end of if statement ((attchmentids != null) && (attchmentids.size() != 0))


      cvdal.destroy();
    }
    catch(Exception exe)
    {
      exe.printStackTrace();
      eventid = -1;
    }
    //System.out.println("In editEvent " +eventid);
    return eventid;
  }

  /*
   *  Used to deleteEvent
   *  @param  eventid
   *  @return int
   */
  public int deleteEvent(int eventid)
  {
    //System.out.println("In deleteEvent");
    int result=1;

    CVDal cvdal = new CVDal(dataSource);
    try
    {
      cvdal.setSql("marketing.deleteeventregister");
      cvdal.setInt(1 , eventid );
      cvdal.executeUpdate();
      cvdal.clearParameters();

      cvdal.setSql("marketing.deleteevent");
      cvdal.setInt(1 , eventid );
      cvdal.executeUpdate();
      cvdal.clearParameters();


      cvdal.setSqlQuery("delete from eventlink where EventID = "+eventid);
      cvdal.executeUpdate();
      cvdal.clearParameters();

      cvdal.destroy();
    }
    catch(Exception exe)
    {
      exe.printStackTrace();
      result = -1;
    }
    return result;
  }

  /*
   *  Used to deleteEventRegister
   *  @param  eventid
   *  @return int
   */
  public int deleteEventRegister(HashMap mapEvent)
  {
    //System.out.println("In deleteEventRegister");
    int result=1;

    int eventid = Integer.parseInt(mapEvent.get("eventid").toString());

    String strIndividualId[] = (String[])mapEvent.get("individualid");
    CVDal cvdal = new CVDal(dataSource);
    try
    {
      for (int i=0;i<strIndividualId.length;i++)
      {

        //System.out.println("deleteEventRegister~~~eventid="+eventid+"~~indID="+strIndividualId[i]);

        cvdal.setSql("marketing.deleteeventindregister");
        cvdal.setInt(1 , eventid );
        cvdal.setInt(2,Integer.parseInt(strIndividualId[i]));
        cvdal.executeUpdate();
        cvdal.clearParameters();
      }
      cvdal.destroy();
    }
    catch(Exception exe)
    {
      exe.printStackTrace();
      result = -1;
    }
    //System.out.println("In deleteEventRegister " +result);
    return result;
  }

   /*
    *  Used to registerAttendee
    *  @param  eventid , individual
    *  @return int
    */
   public int registerAttendee(HashMap mapEvent)
   {
     //System.out.println("In registerAttendee");
     int result=1;

     int eventid = Integer.parseInt(mapEvent.get("eventid").toString());
     int individualId = Integer.parseInt(mapEvent.get("individualid").toString());
    String acceptedStatus = mapEvent.get("accepted").toString();
     CVDal cvdal = new CVDal(dataSource);
     try
     {
       cvdal.setSql("marketing.registerattendee");
      cvdal.setString(1, acceptedStatus);
       cvdal.setInt(2 , eventid );
       cvdal.setInt(3 , individualId);
       cvdal.executeUpdate();
     }
     catch(Exception exe)
     {
       exe.printStackTrace();
       result = -1;
     }
     finally
     {
      cvdal.clearParameters();
      cvdal.destroy();
     }
    //System.out.println("Out registerAttendee " +result);
    return result;
   }

   /**
    * Returns whether the User has accepted their
    * invitation to the event passed to the method.
    *
    * @param eventID The Event ID.
    * @param userID The Individual ID of the User.
    *
    * @return Whether or not the user
    * has accepted the invitation
    *
    * @throws IndividualNotInvitedException This User
    * has not been invited to the event.
    */
   public boolean hasUserAcceptedEvent(int eventID, int userID) throws AuthorizationFailedException, IndividualNotInvitedException
   {
      if(!CVUtility.isModuleVisible("Events",userID, this.dataSource))
        throw new AuthorizationFailedException("Event - getEventDetails");

      //System.out.println("[DEBUG] [EventsEJB]: In hasUserAcceptedEvent");
      CVDal cvdal = new CVDal(dataSource);
      Collection colDetails = null;
      boolean returnValue = false;
      try
      {
        cvdal.setSqlQuery("select accepted from eventregister where EventID = ? and IndividualID = ?");
        cvdal.setInt(1, eventID);
        cvdal.setInt(2, userID);
        colDetails = cvdal.executeQuery();

        Iterator it = colDetails.iterator();
        if (it.hasNext())
        {
          HashMap mapResult = (HashMap)it.next();
          String acceptedString = (String) mapResult.get("accepted");
          //System.out.println("[DEBUG] [EventsEJB] acceptedString: " + acceptedString);
          returnValue = (acceptedString == null || acceptedString.equals(""))
            ?false:acceptedString.equalsIgnoreCase("YES");
        } //end of if statement (it.hasNext())
        else
        {
          throw new IndividualNotInvitedException(eventID, userID);
        } //end of else statement (it.hasNext())
      } //end of try block
      catch (IndividualNotInvitedException inie)
      {
        throw inie;
      } //end of catch block (IndividualNotInvitedException)
      catch(Exception exe)
      {
        System.out.println("[Exception] EventsEJB.hasUserAcceptedEvent: " + exe.toString());
        //exe.printStackTrace();
      } //end of catch block (Exception)
      finally
      {
        cvdal.clearParameters();
        cvdal.destroy();
      } //end of finally block
      //System.out.println("[DEBUG] [EventsEJB]: Exit hasUserAcceptedEvent");
      return returnValue;
    } //end of hasUserAcceptedEvent method


   /*
    *  Used to getEventAttendeesForMail
    *  @param  eventid
    *  @return String
    */

   public String getEventAttendeesForMail(int eventid, int userID) throws AuthorizationFailedException
   {
     if(!CVUtility.isModuleVisible("Events",userID, this.dataSource))
       throw new AuthorizationFailedException("Event - getEventDetails");

     String result = "";

     CVDal cvdl = new CVDal(dataSource);
     try
     {
      String strSQL = "select moc.Content emailAddress from methodofcontact moc left join mocrelate mocrel on mocrel.MOCID=moc.MOCID and moc.MOCType=1 ,eventregister er"
              +" where mocrel.ContactID=er.IndividualID and mocrel.ContactType=2 and er.EventID="+eventid;
       cvdl.setSqlQuery( strSQL );
       Collection v = cvdl.executeQuery();

       cvdl.destroy();

       String emailList = "";

       Iterator it = v.iterator();

         while( it.hasNext() )
         {
           HashMap hm = ( HashMap  )it.next();
           String emailAddress = (String)hm.get( "emailAddress" );
           if (emailAddress != null && !emailAddress.equals("")){
            if ( emailList.equals("") )
            {
              emailList = emailAddress;
            }
            else
            {
              emailList = emailList + "," + emailAddress;
            }
          }
         }

         result = emailList;
     }
     catch(Exception exe)
     {
       exe.printStackTrace();
       result = "";
     }
     //System.out.println("In getEventAttendeesForMail " +result);
     return result;
   }

   /**
    *  This method returns a Collection of EventAttendeeVO objects.
    *
    * @param eventID The eventID to get the Event Attendees from.
    *
    * @return A Collection of EventAttendeeVO objects.
    */
   public Collection getAttendeesForEvent(int eventID, int userID) throws AuthorizationFailedException
   {
     if(!CVUtility.isModuleVisible("Events",userID, this.dataSource))
       throw new AuthorizationFailedException("Event - getEventDetails");

     Collection returnCollection = new ArrayList();
     String sqlCommand = "select EventID, IndividualID, accepted from eventregister where EventID = ?";
     CVDal dataConnection = new CVDal(dataSource);
     try
     {
       dataConnection.setSqlQuery(sqlCommand);
       dataConnection.setInt(1, eventID);
       Collection resultsCollection = dataConnection.executeQuery();
       if (resultsCollection != null)
       {
         Iterator resultsIterator = resultsCollection.iterator();
         while (resultsIterator.hasNext())
         {
           HashMap resultHashMap = (HashMap) resultsIterator.next();
           //System.out.println("[DEBUG] [EventsEJB] HashMap Results: " + resultHashMap.toString());
           EventAttendeeVO eventAttendee = new EventAttendeeVO();
           eventAttendee.setEventID(Integer.parseInt(resultHashMap.get("EventID").toString()));
           eventAttendee.setIndividualID(Integer.parseInt(resultHashMap.get("IndividualID").toString()));
           eventAttendee.setAcceptedString(resultHashMap.get("accepted").toString());
           //System.out.println("[DEBUG] [EventsEJB] eventAttendee: " + eventAttendee.toString());
           returnCollection.add(eventAttendee);
         } //end of while loop (resultsIterator.hasNext())
       } //end of if statement (resultsCollection != null)
     } //end of try block
     catch (Exception ex)
     {
       System.out.println("[Exception] EventsEJB.getAttendeesForEvent: " + ex.toString());
       ex.printStackTrace();
     } //end of catch block (Exception)
     finally
     {
       dataConnection.destroy();
       dataConnection = null;
     } //end of finally block
     //get the eventAttendees
     return returnCollection;
   } //end of getAttendeesForEvent method

  /**
   * Returns an EventDetails object containing the details
   * of a given Event record for the given eventID.
   * @param eventID The ID of the event record whose details we want
   * @return EventDetails object
   */
  public EventDetails getCustomerEventDetails(int eventID)
  {
    EventDetails eventDetails = new EventDetails();

    CVDal cvdl = new CVDal(dataSource);

    try
    {
      cvdl.setSql("customerview.events.geteventdetails");
      cvdl.setInt(1, eventID);
      Collection sqlResults = cvdl.executeQuery();
      cvdl.clearParameters();

      if (sqlResults != null)
      {
        Iterator iter = sqlResults.iterator();
        HashMap mapResult = (HashMap)iter.next();

        eventDetails.setMaxattendees(Integer.parseInt(mapResult.get("maxAttendees").toString()));
        eventDetails.setModeratorname((String)mapResult.get("moderator"));
        eventDetails.setStartdate((Timestamp)mapResult.get("startDate"));
        eventDetails.setEnddate((Timestamp)mapResult.get("endDate"));
        eventDetails.setWhoshouldattend((String)mapResult.get("whoAttends"));
        eventDetails.setDetail((String)mapResult.get("detail"));
        eventDetails.setName((String)mapResult.get("title"));
      }   // end if (sqlResults != null)

      ArrayList attachedFiles = new ArrayList();

      cvdl.setSql("customer.events.getEventAttachments");
      cvdl.setInt(1, eventID);
      Collection filesResults = cvdl.executeQuery();
      cvdl.clearParameters();

      if (filesResults != null)
      {
        Iterator filesIter = filesResults.iterator();
        while (filesIter.hasNext())
        {
          HashMap fileRow = (HashMap)filesIter.next();
          attachedFiles.add(fileRow);
        }
      }
      eventDetails.setAttachedFiles(attachedFiles);

      cvdl.destroy();
    }catch(Exception e){
      System.out.println("[Exception][EventsEJB] Exception thrown in getCustomerEventDetails(): " + e);
      e.printStackTrace();
    }
    return(eventDetails);
  }   // end getCustomerEventDetails() method

  /**
   * @author Kevin McAllister <kevin@centraview.com>
   * This simply sets the target datasource to be used for DB interaction
   * @param ds A string that contains the cannonical JNDI name of the datasource
   */
   public void setDataSource(String ds) {
     this.dataSource = ds;
   }
}
TOP

Related Classes of com.centraview.marketing.events.EventsEJB

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.