UserOrGuestLinkTag.java

/**
 * $Id: UserOrGuestLinkTag.java,v 1.3 2002/05/22 16:12:46 feldman Exp $
 *
 * VKBOBA - Versicherungs Kammer Bayern Online Beratung Assistent.
 * Copyright 2002 Think Tank Corporate Consulting. All Rights Reserved.
 *
 * Use is subject to license terms.
 *
 */
package de.vkb.oba.client.taglib;


import org.frameworks.bundle.ResourceBundle;
import org.frameworks.exception.runtime.FFRuntimeConfigurationException;

import de.vkb.oba.server.business.VKBUser;


/**
 * Shows "a href link depending on logged user: registered user or guest:
 * Shows link of tag attribute 'user' for registered user, or
 * link of tag attribute 'guest' for guest user.
 * Introduced: for show user contract for user and another link for guest.
 * Adds: additional/optional "a href" attributes like "class", etc.
 *
 * @author   Alexander Feldman (updated by $Author: feldman $)
 * @created  2002-05-21 Alexander Feldman
 * @version  $Revision: 1.3 $
 *
 */
public class UserOrGuestLinkTag extends BaseBodyTag
{
  /** URL for registered user. */
  private String userLink  = null;

  /** URL for guest user. */
  private String guestLink = null;

  /** Additional attributes to insert directly into resulting "a href" tag. */
  private String adds      = null;


  /**
   * Setter for jsp attribute: user - url used for registered user.
   *
   * @param val - value of user attribute.
   *
   */
  public void setUser(String val)
  {
    userLink = val;
  }


  /**
   * Setter for jsp attribute: guest - url used for guest user.
   *
   * @param val - value of guest attribute.
   *
   */
  public void setGuest(String val)
  {
    guestLink = val;
  }


  /**
   * Setter for jsp attribute: adds - additional attributes for "a href".
   *
   * @param val - value of adds attribute.
   *
   */
  public void setAdds(String val)
  {
    adds = val;
  }


  /**
   * Jsp Tag implementation.
   *
   */
  public void doStart()
  {
    checkAttributes();
    String url = getUser().isGuest() ?
      guestLink :
      userLink;
    adds = (adds != null) ?
      " " + adds :
      "";
    ResourceBundle rb = new ResourceBundle(RESOURCE_HTML);
    setPrefix(rb.get(HTML_TAG_A_HREF_OPEN_UA, new Object[] {url, adds}));
    setSuffix(rb.get(HTML_TAG_A_HREF_CLOSE));
  }


  /**
   * Checks that jsp tag attributes are required.
   *
   * It throws FFRuntimeConfigurationException if they are
   * not provided at tag.
   *
   */
  private void checkAttributes()
  {
    if (userLink == null)
    {
      throw new FFRuntimeConfigurationException(
        "UserOrGuestLinkTag attribute 'user' is null");
    }
    if (guestLink == null)
    {
      throw new FFRuntimeConfigurationException(
        "UserOrGuestLinkTag attribute 'guest' is null");
    }
  }


  /**
   * @return Current user (VKBUser) from session.
   *
   */
  private VKBUser getUser()
  {
    VKBUser user = null;
    try
    {
      user = (VKBUser) request.getSession().getObject(SESSION_USER);
    }
    catch (Exception e)
    {
      throw new FFRuntimeConfigurationException("Session timeout");
    }
    if (user == null)
    {
      throw new FFRuntimeConfigurationException("Session is not more valid");
    }
    return user;
  }


  /**
   * Default implementation of after-tag. We simply stop processing.
   *
   */
  public void doAfter()
  {
  }

}