cancel
Showing results for 
Search instead for 
Did you mean: 

Upload file in KM Prgramatically

Former Member
0 Kudos

Hi all,

I have to build this programatically in JSPDynpage, any ideas will be appreciated.

1. The iview will have a browse and an upload button.

2. When the user clicks on the browse button the user should be able to browse the excel file from the local machine.

3 when he clicks on the upload button, the file gets uploaded in the KM repository under the user's folder.

Please suggest any code snippets.

Thanks

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos
Former Member
0 Kudos

Hi Praveen,

This is really helpful. Thanks. Im building this with JSPDynpage and Im getting some errors. This is my code



import com.sapportals.htmlb.page.*;
import com.sapportals.htmlb.rendering.PageContext;
import com.sapportals.htmlb.rendering.PageContextFactory;

import com.sapportals.htmlb.unifiedrendering.controls.FileUpload;
import com.sapportals.htmlb.event.*;
import com.sapportals.portal.htmlb.page.*;
import com.sapportals.portal.prt.component.*;
import com.sapportals.portal.prt.resource.IResource;
import com.sapportals.portal.prt.session.*;

import java.io.FileInputStream;
import java.math.*;
import java.util.*;
import java.util.Calendar;
import com.sap.engine.lib.xml.signature.elements.RetrievalMethod;
import com.sap.security.api.IUser;

public class FileUploadUtility extends PageProcessorComponent {

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

  public static class FileUploadUtilityDynPage extends JSPDynPage{
  
    private fileUpload fileUploadBean = null;
    
    
	protected IPortalComponentRequest request;
	protected IPortalComponentSession session; 
	protected IPortalComponentContext context;
	protected IPortalComponentProfile profile;
	protected String userId;
	
	
	private final static int INITIAL_STATE = 0;    
	private final static int OUTPUT_STATE = 1;
	private final static int ERROR_STATE = 2;  
	private int state = INITIAL_STATE;
    
  
    public void doInitialization(){
		request = (IPortalComponentRequest) this.getRequest();
		session = ((IPortalComponentRequest)getRequest()).getComponentSession();
		context = request.getComponentContext();
	    
		profile = context.getProfile();
		IUserContext userContext = request.getUser();
		userId = userContext.getUserId();
					
		fileUploadBean = new fileUpload();
		session.putValue("fileUploadBean", fileUploadBean);
		uploadUtility();
    }

    public void uploadUtility(){
    	
		String mimetype;
		String resourceName;
		String repository;
		FileInputStream sourceFileInput;
		
		IUser epUser=(IUser)request.getUser().getUser();
		ResourceContext ctx=new ResourceContext(epUser);
		repository = "/documents/";
		RID rid=RID.getRID(repository);
		PageContext context = PageContextFactory.createPageContext(request, response);
		Event event=context.getCurrentEvent();
		
		if(null!=event)
		{
			String event_name=event.getAction();
			if(null!=event_name && event_name.trim().equalsIgnoreCase("upload_file"))
				{
				FileUpload fileUpload = (FileUpload)context.getComponentForId("fileupload");
				if(null !=fileUpload && null!=fileUpload.getFile())
					{
					mimetype = fileUpload.getFile().getContentType();
					resourceName = fileUpload.getFile().getFileName();
					sourceFileInput = new FileInputStream(fileUpload.getFile().getFile().getAbsolutePath());
					Content content = new Content(sourceFileInput, mimetype, -1L);
					IResource resource = (ResourceFactory.getInstance().getResource(rid, ctx));
					ICollection aCollection=(ICollection)resource;
					IResource newResource = aCollection.createResource(resourceName, null, content);
					}
				}
		}
	
		com.sapportals.portal.prt.resource.IResource res = request.getResource(com.sapportals.portal.prt.resource.IResource.JSP,"jsp/fileupload.jsp");
		request.dispatchRequest(res,response);
	
    	
    }



    public void doProcessAfterInput() throws PageException {
    }

    public void doProcessBeforeOutput() throws PageException {
      this.setJspName("File_Upload.jsp");
    }
  }
}



where can I get some Javadocs to read about some of the methods.

Edited by: S.B on May 22, 2008 5:05 PM

Former Member
0 Kudos

I tried

 https://www.sdn.sap.com/irj/sdn/javadocs 

it doesnt work.

Former Member
0 Kudos

Does anyone know where can I get KM API. My code is giving some errors and I need to import some KM JAR's.

Former Member
0 Kudos

Thanks Praveen,

Do you know where can I get the KM Jars. My code is giving errors and I think I need to import JAR's.

Im getting the errors for the following.



ResourceContext ctx=new ResourceContext(epUser);

RID rid=RID.getRID(repository);
PageContext context = PageContextFactory.createPageContext(request, response);



FileUpload fileUpload = (FileUpload)context.getComponentForId("fileupload");
				if(null !=fileUpload && null!=fileUpload.getFile())
					{
					mimetype = fileUpload.getFile().getContentType();
					resourceName = fileUpload.getFile().getFileName();
					sourceFileInput = new FileInputStream(fileUpload.getFile().getFile().getAbsolutePath());
					Content content = new Content(sourceFileInput, mimetype, -1L);
					IResource resource = (ResourceFactory.getInstance().getResource(rid, ctx));
					ICollection aCollection=(ICollection)resource;
					IResource newResource = aCollection.createResource(resourceName, null, content);
					}

I do not have any KM jars imported in my project as of now.

0 Kudos

Hi,

Did you set the project build path to point to KM jar files? If not how could you even compile the codes?

To know where the KM classes are in the jar files on your server use classlocator.

https://forums.sdn.sap.com/thread.jspa?threadID=763722

https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/4051

https://forums.sdn.sap.com/thread.jspa?threadID=83869

Also have sharing reference to the KM applications in your portalapp.xml.

Regards,

Praveen Gudapati

Former Member
0 Kudos

HI Praveen,

1. When you say :



Did you set the project build path to point to KM jar files? 

Do you mean to ask if Ive put KM jar's in "Project->Properties->Java Build Path->Libraries->Add External Jars ?"

2.

 Also have sharing reference to the KM applications in your portalapp.xml. 

How do I do this ?

My Portal app is below.



<?xml version="1.0" encoding="utf-8"?>
<application>
  <application-config>
    <property name="SharingReference" value="com.sap.portal.htmlb"/>
  </application-config>
  <components>
    <component name="FileUploadUtility">
      <component-config>
        <property name="ClassName" value="com.abc.FileUploadUtility"/>
      </component-config>
       <component-profile>
       <property name="tagLib" value="/SERVICE/htmlb/taglib/htmlb.tld">
       </property>  
      </component-profile>
     </component>
  </components>
  <services/>
</application>

Former Member
0 Kudos

I am missing some KM JARs on the NWDS for compiling my code.

Such as the following :

import com.sapportals.wcm.repository.Content;

import com.sapportals.wcm.repository.ICollection;

import com.sapportals.wcm.repository.IResource;

import com.sapportals.wcm.repository.IResourceFactory;

import com.sapportals.wcm.repository.ResourceContext;

import com.sapportals.wcm.repository.ResourceFactory;

import com.sapportals.wcm.util.uri.RID;

Any Idea where to put them on my local machine afrer I get them from the server.

0 Kudos

Hi,

You also have the required jar file in the plugins of your NWDS, so you only need to reference them like this:

Right click project -> Properties -> Java Build Path -> 'Libraries' tab -> Click on 'Add External Jars' and add the jars files.

The jar file can be found under:

C:\Program Files\SAP\IDE\IDE70\eclipse\plugins\com.sap.km.rfwizard_7.1.5\lib

Add the jar files that are required and Click Ok and rebuild the project.

The jar file that are required in your case for the import statements in your previous reply are:

bc.rf.framework_api.jar and bc.util.public_api.jar

The above paths vary depending on the NWDS version. So just check them.

Regards,

Praveen Gudapati

Former Member
0 Kudos

Hi Praveen,

I found the

C:\Program Files\SAP\IDE\IDE70\eclipse\plugins\com.sap.km.rfwizard_7.1.5\

But there is no lib folder under that.

And also, I imported the JAR "EPKMEclipsePlugins" and it still shows me errors.

0 Kudos

Hi,

If the jars are not present there, then just copy the necessary jars from server to this folder and reference them as I explained in my previous reply.

You will find the jar file here on server:

\usr\sap\J2E\JC01\j2ee\cluster\server0\apps\sap.com\irj\servlet_jsp\irj\root\WEB-INF\portal\portalapps\com.sap.netweaver.bc.rf\lib\bc.rf.framework_api.jar

\usr\sap\J2E\JC01\j2ee\cluster\server0\apps\sap.com\irj\servlet_jsp\irj\root\WEB-INF\portal\portalapps\com.sap.netweaver.bc.util\lib\bc.util.public_api.jar

Regards,

Praveen Gudapati

Former Member
0 Kudos

Hi Praveen,

Im still getting some errors :

this is my code



 public void uploadUtility(){
    	
		String mimetype;
		String resourceName;
		String repository;
		FileInputStream sourceFileInput;
		
		IUser epUser=(IUser)request.getUser().getUser();
		System.err.println("epUser - " + epUser);
		ResourceContext ctx=new ResourceContext(epUser);
		repository = "/documents/";
		RID rid=RID.getRID(repository);
		PageContext context = PageContextFactory.createPageContext(request, response);
		Event event=context.getCurrentEvent();
		
		if(null!=event)
		{
			String event_name=event.getAction();
			if(null!=event_name && event_name.trim().equalsIgnoreCase("upload_file"))
				{
				FileUpload fileUpload = (FileUpload)context.getComponentForId("fileupload");
				if(null !=fileUpload && null!=fileUpload.getFile())
					{
					mimetype = fileUpload.getFile().getContentType();
					resourceName = fileUpload.getFile().getFileName();
					sourceFileInput = new FileInputStream(fileUpload.getFile().getFile().getAbsolutePath());
					Content content = new Content(sourceFileInput, mimetype, -1L);
					IResource resource = (ResourceFactory.getInstance().getResource(rid, ctx));
					ICollection aCollection=(ICollection)resource;
					IResource newResource = aCollection.createResource(resourceName, null, content);
					}
				}
		}
	
		com.sapportals.portal.prt.resource.IResource res = request.getResource(com.sapportals.portal.prt.resource.IResource.JSP,"jsp/fileupload.jsp");
		request.dispatchRequest(res,response);
	
    	
    }// end of uploadUtil

following are the errors :

1. The constructor ResourceContext(Iuser) is undefined.


ResourceContext ctx=new ResourceContext(epUser);  

2. Cannot convert from IPagecontext to page context.


PageContext context = PageContextFactory.createPageContext(request, response);

3. Cannot cast from component to FileUpload


FileUpload fileUpload = (FileUpload)context.getComponentForId("fileupload");

4. The method getFile() is undefined for the type FileUpload


if(null !=fileUpload && null!=fileUpload.getFile())

Former Member
0 Kudos

Hi Praveen,

Any suggestions ?

Former Member
0 Kudos

Even when I add external Jars from :

plugins\com.sap.netweaver.bc.rf\lib\bc.rf.framework_api.jar

plugins\com.sap.netweaver.bc.util\lib\bc.util.public_api.jar

in my NWDS, the references are not getting resolved.

for example these 3 are still showing errors :

ResourceContext ctx=new ResourceContext(epUser);

RID rid=RID.getRID(repository);

PageContext context = PageContextFactory.createPageContext(request, response);

Any Ideas gurus ?

alexander_gtz
Participant
0 Kudos

Hi,

try the following. Perhaps it helps.

For the error with ResourceContext take another import for IUser Class (contained in com.sap.security.api.ep5.jar):

import com.sapportals.portal.security.usermanagement.IUser;

I tried your import in one of my codes and I get then the same error as you. I know this one is deprecated but it works, perhaps someone else could tell the 100% right import to build the ResourceContext.

For the last issue change your line

PageContext context = PageContextFactory.createPageContext(request, response);

to

IPageContext context = PageContextFactory.createPageContext(request,response);

The problem with your RID is curious because the bc.util.public_api.jar is the right jar. Did you rebuild the project after import? What is the error message that NWDS tells you for that?

Let me know if it helped

Regards

Alex

Former Member
0 Kudos

Hi Alexander,

1. Yes I already used the "import com.sapportals.portal.security.usermanagement.IUser;" and it fixed the ResourceContext error.

2. for the second error (IPageContext context = PageContextFactory.createPageContext(request,response);

) that you mentioned : are you using this import ---> import com.sapportals.htmlb.rendering.PageContext;? This is also fixed now.

3. I rebuilt the project and Im left with one error only which is :



FileUpload fileUpload = (FileUpload)context.getComponentForId("fileupload");

The error says : Cannot cast from Component to FileUpload.

Any Ideas?

alexander_gtz
Participant
0 Kudos

Hi,

no I use

com.sapportals.htmlb.rendering.IPageContext;

for my KM Upload.

So how do you build your context now if you say your error is fixed? Could you please paste your code you changed?

Greetings

Alex

Former Member
0 Kudos

Hi Alexander,

Here we are :




import java.io.FileInputStream;
import java.util.List;

import com.sapportals.htmlb.event.Event;
import com.sapportals.htmlb.page.DynPage;
import com.sapportals.htmlb.page.PageException;
import com.sapportals.htmlb.rendering.IPageContext;

import com.sapportals.htmlb.rendering.PageContextFactory;
import com.sapportals.htmlb.unifiedrendering.controls.FileUpload;
import com.sapportals.portal.htmlb.page.JSPDynPage;
import com.sapportals.portal.htmlb.page.PageProcessorComponent;

import com.sapportals.portal.prt.component.*;

import com.sapportals.portal.prt.session.IUserContext;
import com.sapportals.wcm.repository.Content;
import com.sapportals.wcm.repository.ICollection;
import com.sapportals.wcm.repository.IResource;
import com.sapportals.wcm.repository.ResourceContext;
import com.sapportals.wcm.repository.ResourceFactory;
import com.sapportals.wcm.util.uri.RID;
import com.sapportals.portal.security.usermanagement.IUser;


public class FileUploadUtility extends PageProcessorComponent {

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

  public static class FileUploadUtilityDynPage extends JSPDynPage{
  
    private fileUpload fileUploadBean = null;
    
    
	protected IPortalComponentRequest request;
	protected IPortalComponentResponse response;
	protected IPortalComponentSession session; 
	protected IPortalComponentContext context;
	protected IPortalComponentProfile profile;
	protected String userId;
	
    public void doInitialization(){
		request = (IPortalComponentRequest) this.getRequest();
		session = ((IPortalComponentRequest)getRequest()).getComponentSession();
		context = request.getComponentContext();
	    
		profile = context.getProfile();
		IUserContext userContext = request.getUser();
		userId = userContext.getUserId();
					
		fileUploadBean = new fileUpload();
		session.putValue("fileUploadBean", fileUploadBean);
		//uploadUtility();
    }

    public void uploadUtility(){
    	
		String mimetype;
		String resourceName;
		String repository;
		FileInputStream sourceFileInput;
		
		
		
		IUser epUser=(IUser)request.getUser().getUser();
		System.err.println("epUser - " + epUser);
		ResourceContext ctx = new ResourceContext(epUser);
		repository = "/documents/";
		RID rid=RID.getRID(repository);
		IPageContext context = PageContextFactory.createPageContext(request,response);

	//	PageContext context = PageContextFactory.createPageContext(request, response);
		Event event=context.getCurrentEvent();
		
		if(null!=event)
		{
			String event_name=event.getAction();
			if(null!=event_name && event_name.trim().equalsIgnoreCase("upload_file"))
				{
				FileUpload fileUpload = (FileUpload)context.getComponentForId("fileupload");
				if(null !=fileUpload && null!=fileUpload.getFile())
					{
					mimetype = fileUpload.getFile().getContentType();
					resourceName = fileUpload.getFile().getFileName();
					sourceFileInput = new FileInputStream(fileUpload.getFile().getFile().getAbsolutePath());
					Content content = new Content(sourceFileInput, mimetype, -1L);
					//IResource resource = (ResourceFactory.getInstance().getResource(rid, ctx));
					IResource resource = (IResource) (ResourceFactory.getInstance().getResource(rid, ctx));
					ICollection aCollection=(ICollection)resource;
					//IResource newResource = aCollection.createResource(resourceName, null, content);
					IResource newResource = (IResource) aCollection.createResource(resourceName, null, content);
					}
				}
		}
	
		com.sapportals.portal.prt.resource.IResource res = request.getResource(com.sapportals.portal.prt.resource.IResource.JSP,"jsp/fileupload.jsp");
		request.dispatchRequest(res,response);
	
    	
    }// end of uploadUtil


    public void doProcessAfterInput() throws PageException {
    }

    public void doProcessBeforeOutput() throws PageException {
      this.setJspName("File_Upload.jsp");
    }
  }
}


I just recompiled the code after reorganizing my Imports and it resolved the context error.

alexander_gtz
Participant
0 Kudos

Hi,

I could reproduce your error in my coding.

Please take another import for your FileUpload control it is:

import com.sapportals.htmlb.FileUpload;

If I take your import I get exactly the same error as you.

Regards

Alex

Former Member
0 Kudos

Hi Alexander,

Thanks so much, I have assigned the points. The error is no more.

But now it requires a lot of try and catch blocks.

Can you please put your code here ?

Regds,

alexander_gtz
Participant
0 Kudos

Hi,

my coding is a bit different but I think if you change the following your code is ok

		if(null!=event)
		{
			String event_name=event.getAction();
			if(null!=event_name && event_name.trim().equalsIgnoreCase("upload_file"))
				{
				FileUpload fileUpload = (FileUpload)context.getComponentForId("fileupload");
				if(null !=fileUpload && null!=fileUpload.getFile())
					{
						try{
						
							mimetype = fileUpload.getFile().getContentType();
							resourceName = fileUpload.getFile().getFileName();
							sourceFileInput = new FileInputStream(fileUpload.getFile().getFile().getAbsolutePath());
							Content content = new Content(sourceFileInput, mimetype, -1L);
							//IResource resource = (ResourceFactory.getInstance().getResource(rid, ctx));
							IResource resource = (IResource) (ResourceFactory.getInstance().getResource(rid, ctx));
							ICollection aCollection=(ICollection)resource;
							//IResource newResource = aCollection.createResource(resourceName, null, content);
							IResource newResource = (IResource) aCollection.createResource(resourceName, null, content);
						}catch(FileNotFoundException fex){
							//do sth eg response.write(fex.getMessage())
						}catch(ResourceException rex){
							//do sth eg response.write(rex.getMessage())
						}catch(Exception ex){
							//do sth eg response.write(ex.getMessage())
						}
					}
				}
		}

You also need the imports:

import java.io.FileNotFoundException;

import com.sapportals.wcm.repository.ResourceException;

If there are other exceptions NWDS wants, import this exception and integrate it the same way.

Greetings

Alex

Former Member
0 Kudos

Hi Alexander,

Thanks. I think I got what I was looking for from this thread. I have assigned you points and Im closing this thread. I have opened another thread for other errors that Im getting.

It is https://forums.sdn.sap.com/message.jspa?messageID=5487854#&threadID=5487854

It will be good if you can share your expertise there.

Thanks

Answers (0)