001    package biz.wedoit4u;
002    
003    import biz.wedoit4u.databeans.*;
004    
005    /**
006     * A class that is used to store sessional information about a user
007     * that is using the CDA.
008     *
009     * <p>Copyright 2003, Rakesh Vidyadharan and wedoit4u.biz</p>
010     *
011     * @author Rakesh Vidyadharan 18<sup><small>th</small></sup> November 2003
012     *
013     * @version $Id: CDASession.java,v 1.4 2004/05/26 11:42:29 rakesh Exp $
014     */
015    public class CDASession extends Session
016    {
017      /**
018       * A flag that is used to indicate that a user has successfully logged
019       * on to the service.
020       */
021      private boolean loggedOn = false;
022    
023      /**
024       * The username of the user currently logged on to the CDA.
025       */
026      private String username = "";
027    
028      /**
029       * The site category that the user is currently browsing.
030       */
031      private int categoryId = 0;
032    
033      /**
034       * The instance of {@link biz.wedoit4u.databeans.CustomerBean} that is
035       * associated with the user logged in.
036       */
037      CustomerBean cb = null;
038    
039      /**
040       * The instance of {@link biz.wedoit4u.databeans.SitePropertyBean} 
041       * that is associated with the user logged in.
042       */
043      SitePropertyBean spb = null;
044    
045      /**
046       * The default constructor.  Does nothing special.
047       */
048      public CDASession() {}
049    
050      /**
051       * Log the specified user into the CDA.  Just ensure that the password
052       * specified matches the username specified.
053       *
054       * @param username - The <code>username</code> that is to be
055       *   matched.
056       * @param password - The <code>password</code> that is to be
057       *   matched.
058       * @return boolean - Return <code>true</code> if the username exists
059       *   and matches the password.
060       */
061      public boolean login( String username, String password )
062      {
063        try
064        {
065          cb = CustomerBean.getInstance( username, password );
066          spb = SitePropertyBean.getInstance( cb.getCustomerId() );
067          loggedOn = true;
068          this.username = username;
069        }
070        catch ( CMAException cex )
071        {
072          Logger.error( "Error logging in user with username " + username + " and password " + password + " in CDASession.login.", cex );
073        }
074    
075        return loggedOn;
076      }
077    
078      /**
079       * Returns the value of {@link 
080       * biz.wedoit4u.databeans.CustomerBean#customerId}.
081       */
082      public int getCustomerId() { return cb.getCustomerId(); }
083    
084      /**
085       * Returns the value of {@link 
086       * biz.wedoit4u.databeans.SitePropertyBean#topRail}.
087       *
088       * @return int - The value of <code>top_rail</code>.
089       */
090      public int getTopRail() { return spb.topRail; }
091    
092      /**
093       * Returns the value of {@link 
094       * biz.wedoit4u.databeans.SitePropertyBean#leftRail}.
095       *
096       * @return int - The value of <code>left_rail</code>.
097       */
098      public int getLeftRail() { return spb.leftRail; }
099    
100      /**
101       * Returns the value of {@link 
102       * biz.wedoit4u.databeans.SitePropertyBean#rightRail}.
103       *
104       * @return int - The value of <code>right_rail</code>.
105       */
106      public int getRightRail() { return spb.rightRail; }
107    
108      /**
109       * Returns the value of {@link 
110       * biz.wedoit4u.databeans.SitePropertyBean#bottomRail}.
111       *
112       * @return int - The value of <code>bottom_rail</code>.
113       */
114      public int getBottomRail() { return spb.bottomRail; }
115    
116      /**
117       * Returns the value of {@link 
118       * biz.wedoit4u.databeans.SitePropertyBean#header}.
119       *
120       * @return String - The value of <code>header</code>.
121       */
122      public String getHeader() { return spb.header; }
123    
124      /**
125       * Returns the value of {@link 
126       * biz.wedoit4u.databeans.SitePropertyBean#footer}.
127       *
128       * @return String - The value of <code>footer</code>.
129       */
130      public String getFooter() { return spb.footer; }
131      
132      /**
133       * Returns {@link #loggedOn}.
134       *
135       * @return boolean - The value/reference of/to loggedOn.
136       */
137      public final boolean getLoggedOn()
138      {
139        return loggedOn;
140      }
141      
142      /**
143       * Returns {@link #categoryId}.
144       *
145       * @return int - The value/reference of/to categoryId.
146       */
147      public final int getCategoryId()
148      {
149        return categoryId;
150      }
151      
152      /**
153       * Set {@link #categoryId} based upon the <code>String</code> value
154       * specified.  If the value is <code>null</code> or <code>empty</code>
155       * then the category with highest priority for the user is set.
156       *
157       * @param category - The value based upon which to find the
158       *   appropriate category.
159       * @throws CMAException - If errors are encountered while fetching 
160       *   the categories for the customer.
161       */
162      public final void setCategoryId( String category ) throws CMAException
163      {
164        if ( category == null || category.equals( "" ) )
165        {
166          CategoryBean[] cbArray = getCategoryList( cb.getCustomerId() );
167          categoryId = cbArray[0].getCategoryId();
168        }
169        else
170        {
171          try
172          {
173            categoryId = Integer.parseInt( category );
174          }
175          catch ( NumberFormatException nfex )
176          {
177            Logger.error( "Error parsing categoryId in CDASession.setCategoryId( " + category + " ) for customerId " + cb.getCustomerId() + ".", nfex );
178            CategoryBean[] cbArray = getCategoryList( cb.getCustomerId() );
179            categoryId = cbArray[0].getCategoryId();
180          }
181        }
182      }
183    
184      /**
185       * Parse the content id out of the specified string value.
186       *
187       * @param content - The content id string value to parse.
188       * @return int - The contentId value.  If the specified string is
189       *   <code>null</code> or <code>empty</code>, this method returns
190       *   <code>0</code>.
191       */
192      public final int getContentId( String content )
193      {
194        int contentId = 0;
195    
196        if ( content != null && content.length() > 0 )
197        {
198          try
199          {
200            contentId = Integer.parseInt( content );
201          }
202          catch ( NumberFormatException nfex )
203          {
204            Logger.error( "Error parsing contentId in CDASession.getContentId( " + content + " ) for customerId " + cb.getCustomerId() + ".", nfex );
205          }
206        }
207    
208        return contentId;
209      }
210    
211      /**
212       * Return the {@link biz.wedoit4u.databeans.ContentBean} instance
213       * identified by the <code>contentId</code> primary key value
214       * specified.
215       *
216       * @see biz.wedoit4u.databeans.ContentBean#getInstance( int )
217       * @param contentId - The primary key value based upon which to
218       *   find the appropriate bean instance.
219       * @return ContentBean - The java bean instance.
220       * @throws CMAException - If the bean instance cannot be found.
221       */
222      public final ContentBean getContentBean( int contentId )
223        throws CMAException
224      {
225        return ContentBean.getInstance( contentId );
226      }
227    
228      /**
229       * Return the <code>CSS properties</code> for the customer as a
230       * <code>String</code>.  Create a new instance of the {@link
231       * CSSGenerator} class for the <code>customer_id</code> and
232       * fetch the <code>CSS contents</code>.
233       *
234       * @see CSSGenerator#getCssContents()
235       * @return String - The CSS properties string.
236       * @throws CMAException - If errors are encountered while fetching
237       *   the CSS contents for the customer.
238       */
239      public final String getCSSContents() throws CMAException
240      {
241        return ( new CSSGenerator( cb.getCustomerId() ) ).getCssContents();
242      }
243    }