Skip to Content
0
Former Member
Jun 10, 2008 at 10:42 AM

Problem in accessing images in the KM from Portal code.

23 Views

Hi All,

I need to develop a portal application that accesses the KM and displays the images that are stored in the KM to the user. There are 8 images which are stored in the /documents/Images directory in the KM. The user should be able to see the next image in the KM by clicking on the 'Next' button of the JSP and the previous image in the KM by clicking on the 'Previous' button of the JSP.

Below is the code which reads the KM and displays the images. However, the images that are displayed are not in a proper sequence. Also when the user clicks on the 'Next' button and arrives to the last image, although i disable the 'Next' button, when the user clicks on the 'Previous' button the user is not able to see the previous image. Infact the KM tries to display the next image which is not present and hence throws an IndexOutOfBoundsException. This happens vice versa for the 'Previous' button as well.

Any help would be highly appreciated and rewarded.

-


JSP Dynpage

-


package com.ltitl.image;

import com.ltitl.bean.ImageBean;
import com.sap.security.api.IUser;
import com.sapportals.htmlb.event.Event;
import com.sapportals.htmlb.page.DynPage;
import com.sapportals.htmlb.page.PageException;
import com.sapportals.portal.htmlb.page.JSPDynPage;
import com.sapportals.portal.htmlb.page.PageProcessorComponent;
import com.sapportals.portal.prt.component.IPortalComponentProfile;
import com.sapportals.portal.prt.component.IPortalComponentRequest;
import com.sapportals.portal.prt.component.IPortalComponentSession;
import com.sapportals.portal.security.usermanagement.UserManagementException;
import com.sapportals.wcm.repository.ICollection;
import com.sapportals.wcm.repository.IResource;
import com.sapportals.wcm.repository.IResourceContext;
import com.sapportals.wcm.repository.IResourceList;
import com.sapportals.wcm.repository.ResourceContext;
import com.sapportals.wcm.repository.ResourceException;
import com.sapportals.wcm.repository.ResourceFactory;
import com.sapportals.wcm.util.uri.RID;
import com.sapportals.wcm.util.usermanagement.WPUMFactory;

public class ImageControl extends PageProcessorComponent {

  public DynPage getPage(){
    return new ImageControlDynPage();
  }

  public static class ImageControlDynPage extends JSPDynPage{
  
    public static ImageBean imageBean = null;
    public static IResource resource = null;
    public static IResourceContext resourceContext = null;
    public static IPortalComponentSession componentSession = null;
    public static IPortalComponentRequest request = null;
    public static IPortalComponentProfile profile = null;
    public static IUser user1 = null;
    public static RID rid = null;
    public static int count = 0;
    public static int total = 0;
    public static IResourceList children = null;
  
    public void doInitialization() throws PageException{
      request = (IPortalComponentRequest)this.getRequest();	
      componentSession = request.getComponentSession();
      profile = request.getComponentContext().getProfile();
      user1 = request.getUser();
	  
      imageBean = new ImageBean();
       try 
        {
               com.sapportals.portal.security.usermanagement.IUser user =  WPUMFactory.getUserFactory().getEP5User(user1); 

	resourceContext = new ResourceContext(user);
  	
	String imagepath = profile.getProperty("PathToFolder");	
	rid = RID.getRID(imagepath); 

	resource = ResourceFactory.getInstance().getResource(rid,resourceContext);
	if(resource != null)
	{	
	        if(resource.isCollection())
	         {	
				  ICollection collection = (ICollection)resource;
				  total = collection.getChildrenCount(true,false,false);
				  imageBean.setTotal(total);
					
				  children = collection.getChildren();
				  accessResource();
			  }
			  else
			  {
				  imageBean.setMsg_txt("resource " + resource.getName() + " is not a collection");
			  }
		  }
		  else
		  {
			  imageBean.setMsg_txt("resource " + resource.getRID() + " does not exist");
		  }

		componentSession.putValue("imageBean",imageBean);

	  } catch (UserManagementException ume) {
		  imageBean.setMsg_txt("exception:" + ume.getLocalizedMessage());	
	  } 
	  catch(ResourceException ue) {
		  imageBean.setMsg_txt("exception:" + ue.getLocalizedMessage());	
	  }
	  
   }

    public void doProcessAfterInput() throws PageException {
		IPortalComponentSession session = ((IPortalComponentRequest)this.getRequest()).getComponentSession();
		imageBean = (ImageBean)session.getValue("imageBean");
		if(null != imageBean) {
			accessResource();
		}
		else
		{
			imageBean.setMsg_txt("Image Bean null");
		}
    }

    public void doProcessBeforeOutput() throws PageException {
      this.setJspName("ImageOutput.jsp");
    }
    
    public void onPrevious(Event event) throws PageException {
    	--count;
    }
    
	public void onNext(Event event) throws PageException {
		++count;
	}
	
public void accessResource() throws PageException {
try {
			
if(count >= 0 && count < total)
{
IResource resImg = children.get(count);
imageBean.setCount(count);
imageBean.setMsg_txt("count: " + count);
imageBean.setInitialPath("/irj/go/km/docs");
imageBean.setImageName("" + resImg.getRID());
}
else
{
imageBean.setMsg_txt("out of bounds count:" + count);
}			
			
} catch (ResourceException e) {
	imageBean.setMsg_txt("resource exception:" + e.getLocalizedMessage());
		} 
	}
  } 
}