Package com.nordija.portal

Source Code of com.nordija.portal.PortalBean

/**
* Copyright 1999-2001 by Nordija <www.nordija.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
**/


package com.nordija.portal;

import java.rmi.RemoteException;
import java.util.*;
import java.io.*;
import java.text.*;

import javax.ejb.*;
import javax.naming.*;

import com.nordija.midtier.utilitybeans.sequence.sessionbean.SequenceServiceHome;
import com.nordija.midtier.utilitybeans.sequence.sessionbean.SequenceService;
import com.nordija.midtier.utilitybeans.sequence.entitybean.Range;

import com.nordija.portal.entity.*;

/**
* This stateless session EJBean is the interface to
* the Portal Service
*/
public class PortalBean
  extends com.nordija.midtier.ejb.BaseSessionBean
  implements PortalBusiness {

  // TODO: Move to properties
  private String jndi_userhome = "nordija.portal.UserHome";
  private String jndi_choicehome = "nordija.portal.ChoiceHome";
  private String jndi_Portlethome = "nordija.portal.PortletHome";
  private String jndi_layouthome = "nordija.portal.LayoutHome";

  private transient UserHome userHome;
  private transient ChoiceHome choiceHome;
  private transient PortletHome PortletHome;
  private transient LayoutHome layoutHome;

  private Range choiceRange;

  public void ejbCreate() {
  }

  public void ejbPostCreate() {
  }

  /**
   * Fetches the Portlets used by the specified user
   */
  public PortletInfo[] getAllSelectedPortlets(String userName) throws FinderException, NamingException, RemoteException {
    PortletInfo[] mi;
    User user = fetchUserHome().findByUserName(userName);
    Enumeration enum = fetchChoiceHome().findByUserId(user.getId());
    Vector result = new Vector();
    while(enum.hasMoreElements()) {
      Choice c = (Choice)enum.nextElement();
      Portlet m = fetchPortletHome().findByPrimaryKey(new PortletPK(c.getPortletId()));
      result.addElement(new PortletInfo(c.getChoiceId(), m.getName(), c.getLayoutLocation(), m.getJspfile()
  , m.getDescription(), m.getDeletable(), m.getEditable()));
    }
    mi = new PortletInfo[result.size()];
    result.copyInto(mi);
    return mi;
  }

  /**
   * Fetches the narrow Portlets used by the specified user
   */
  public PortletInfo[] getSelectedNarrowPortlets(String userName) throws FinderException, NamingException, RemoteException {
    PortletInfo[] mi;
    User user = fetchUserHome().findByUserName(userName);
    Enumeration enum = fetchChoiceHome().findByUserId(user.getId());
    Vector result = new Vector();
    while(enum.hasMoreElements()) {
      Choice c = (Choice)enum.nextElement();
      Portlet m = fetchPortletHome().findByPrimaryKey(new PortletPK(c.getPortletId()));
      if(m.isNarrow())
  result.addElement(new PortletInfo(c.getChoiceId(), m.getName(), c.getLayoutLocation(), m.getJspfile()
           , m.getDescription(), m.getDeletable(), m.getEditable()));
    }
    mi = new PortletInfo[result.size()];
    result.copyInto(mi);
    return mi;
  }

  /**
   * Fetches the wide Portlets used by the specified user
   */
  public PortletInfo[] getSelectedWidePortlets(String userName) throws FinderException, NamingException, RemoteException {
    PortletInfo[] mi;
    User user = fetchUserHome().findByUserName(userName);
    Enumeration enum = fetchChoiceHome().findByUserId(user.getId());
    Vector result = new Vector();
    while(enum.hasMoreElements()) {
      Choice c = (Choice)enum.nextElement();
      Portlet m = fetchPortletHome().findByPrimaryKey(new PortletPK(c.getPortletId()));
      if(m.isWide())
  result.addElement(new PortletInfo(c.getChoiceId(), m.getName(), c.getLayoutLocation(), m.getJspfile()
           , m.getDescription(), m.getDeletable(), m.getEditable()));
    }
    mi = new PortletInfo[result.size()];
    result.copyInto(mi);
    return mi;
  }

  /**
   * Retrieves the name of an jsp page with the users
   * preferred layout.
   */
  public String getLayout(String userName) throws RemoteException {
    try {
      User user = fetchUserHome().findByUserName(userName);
      return new String("portal.jsp");
    } catch(Exception e) {
      throw new RemoteException(e.toString());
    }
  }

  public Enumeration listPortlets(String username)
    throws RemoteException {
    try {
      return fetchPortletHome().findAll();//TODO: Should be changed to only see allowed Portlets
    } catch(Exception e) {
      throw new RemoteException(e.toString());
    }
  }

  public Enumeration listLayouts()
    throws RemoteException {
    try {
      return fetchLayoutHome().findAll();
    } catch(Exception e) {
      throw new RemoteException(e.toString());
    }
  }

  public Enumeration listUsers()
    throws RemoteException {
    try {
      return fetchUserHome().findAll();
    } catch(Exception e) {
      throw new RemoteException(e.toString());
    }
  }

  public void removePortlet(String userName, String PortletName)
    throws CreateException, FinderException, NamingException, RemoteException, RemoveException {
    User user = fetchUserHome().findByUserName(userName);
    Portlet Portlet = fetchPortletHome().findByPortletName(PortletName);
//      fetchChoiceHome().remove(new ChoicePK(user.getId(), Portlet.getId(), 0));
//      Choice choice = fetchChoiceHome().findByPrimaryKey(new ChoicePK(user.getId(), Portlet.getId(), 0));
//      choice.remove();
  }

  public void removePortlet(String userName, long choiceId)
    throws CreateException, FinderException, NamingException, RemoteException, RemoveException {
    User user = fetchUserHome().findByUserName(userName);
    Choice choice = fetchChoiceHome().findByPrimaryKey(new ChoicePK(choiceId));
    if(choice.getUserId()!=user.getId())
      throw new RemoteException(userName+" couldn't remove choice with id "+choiceId);
    choice.remove();
  }

  /**
   * Adds a Portlet to the users list of selected Portlets on his/hers portal page
   * @param userName name of user
   * @param PortletName name of Portlet
   */
  public void addPortlet(String userName, String PortletName)
    throws CreateException, FinderException, NamingException, RemoteException {

    // Fetch user and Portlet information
    User user = fetchUserHome().findByUserName(userName);
    Portlet Portlet = fetchPortletHome().findByPortletName(PortletName);

    // Generate unique id for choice
    if(choiceRange == null) {
      Context ctx = new InitialContext();
      SequenceServiceHome home = (SequenceServiceHome)ctx.lookup("nordija.portal.sequence");
      SequenceService seq = home.create();
      choiceRange = seq.getSequenceRange("Choice",true,1);
    }

    ChoicePK pk = new ChoicePK(choiceRange.getNextUniqueNumber());
    System.out.println("Adding Choice with key: "+pk.choiceId);
    Choice choice = fetchChoiceHome().create(pk, Portlet.getId(), user.getId());
  }

  /**
   * Obtains a reference to the PortletHome
   * @return The obtained object reference
   */
  protected PortletHome fetchPortletHome() throws NamingException, RemoteException {
    if(PortletHome == null) {
      Context ctx = new InitialContext();
      PortletHome = (PortletHome)ctx.lookup(jndi_Portlethome);
    }
    return PortletHome;
  }


  /**
   * Obtains a reference to the LayoutHome
   * @return The obtained object reference
   */
  protected LayoutHome fetchLayoutHome() throws NamingException, RemoteException {
    if(layoutHome == null) {
      Context ctx = new InitialContext();
      layoutHome = (LayoutHome)ctx.lookup(jndi_layouthome);
    }
    return layoutHome;
  }


  /**
   * Obtains a reference to the Userhome
   * @return The obtained object reference
   */
  protected UserHome fetchUserHome() throws NamingException, RemoteException {
    if(userHome == null) {
      Context ctx = new InitialContext();
      userHome = (UserHome)ctx.lookup(jndi_userhome);
    }
    return userHome;
  }


  /**
   * Obtains a reference to the Choicehome
   * @return The obtained object reference
   */
  protected ChoiceHome fetchChoiceHome() throws NamingException, RemoteException {
    if(choiceHome == null) {
      Context ctx = new InitialContext();
      choiceHome = (ChoiceHome)ctx.lookup(jndi_choicehome);
    }
    return choiceHome;
  }
}
TOP

Related Classes of com.nordija.portal.PortalBean

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.