Uploading multiple files in SAP portal KM folder
Portal allows us to upload one file at a time in the Knowledge management.
Sometimes the requirement is to upload more than one file, maybe one full folder having multiple folders and files inside this.
This can be achieved by using the portal drive or webdav client and putting the folder in the desired location in the KM.
SAP Portal 7.3 has the feature of multiple files but there also you cannot upload folder with complete structures.
So, for this kind of requirement where you cannot use Portal Drive or Webdav Client because of security issues and all, we can use a utility like this which allows us to upload a zip file and then open the zip file and extract the files and upload the files.
We will use KM APIs with web dynpro java to upload a zip file of the folders and then using ZIP java classes to extract the files from zip file and creating the files and folders in KM in the same structure.
We need to create a File upload UI element and one button in the web dynpro view.
Create the context like below
Where filedata is of type “binary”, filepath is of type “String”
KMpath “String” -> this context will be used to store the path where we want to store the extracted files.
View layout should be like below.
Bind the property “data” to filenode.filedata
Create one event for button “Upload”. In the event handler we need to write the code to upload zip file and then extracting it.
Full code with eventhandlers and other hook method coding is shown below.
a. In the wddoinit method write the following code
//@@begin wdDoInit() try { IFilenodeElement fileelement = wdContext.nodeFilenode().createFilenodeElement();
if (fileelement != null) {
wdContext.nodeFilenode().bind(fileelement);
IWDAttributeInfo attInfo = wdContext.nodeFilenode().getNodeInfo().getAttribute( “filedata” );
ISimpleTypeModifiable type = attInfo.getModifiableSimpleType();
IWDModifiableBinaryType binaryType = (IWDModifiableBinaryType) type;
} else {
wdComponentAPI.getMessageManager().reportWarning(“error”);
} }
catch (Exception e) {
wdComponentAPI.getMessageManager().reportSuccess(” catch error” + e);
}
wdContext.currentContextElement().setKMpath(“/wpccontent/Sites/~~~ /”); // setting the KM path where the zip file will bw uploaded and extracted. //@@end
b. Write the following code in the others section in the implementation which contains some methods and we can create these methods separately also.
//@@begin others
IFilenodeElement fileelement = null;
// getzipfiles method is the method which opens the zip file and places the content in the specified location
public boolean getZipFiles(InputStream is, String kmpath)
{
boolean flag = false;
try
{
IWDClientUser wdClientUser = WDClientUser.getCurrentUser();
com.sap.security.api.IUser sapUser = wdClientUser.getSAPUser();
IUser ep5User = WPUMFactory.getUserFactory().getEP5User(sapUser);
ResourceContext resourseContext = new ResourceContext(ep5User);
byte[] buf = new byte[1024];
ZipInputStream zipinputstream = null;
ZipEntry zipentry; zipinputstream = new ZipInputStream(is);
System.err.println(“file read ” + is); zipentry = zipinputstream.getNextEntry();
while (zipentry != null)
{
//for each entry to be extracted
String entryName = zipentry.getName();
System.err.println(“entryname “+entryName);
int n; FileOutputStream fileoutputstream;
File newFile = new File(entryName);
System.err.println(“directory” + entryName + newFile.isDirectory());
String directory = newFile.getParent(); String[] filen = entryName.split(“/”);
String filekmpath = “”;
String currentfolder = kmpath;
String fileKmpath1 = “” ;
for(int i1=0; i1<filen.length; i1++ )
{
fileKmpath1 = “” ;
if(filen[i1].trim().length() >0)
{
String file_name = filen[i1];
System.err.println(“filn name ” + filen[i1] + “—–index” + file_name.indexOf(“.”));
if((i1+1 == filen.length )&&(!(entryName.endsWith(“/”))))
{
RID pathRID = RID.getRID(currentfolder);
IResourceFactory resourseFactory = ResourceFactory.getInstance();
System.err.println(“pathRID ” + pathRID); System.err.println(“resourseContext ” + resourseContext);
ICollection collection = (ICollection) resourseFactory.getResource(pathRID,resourseContext);
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((n = zipinputstream.read(buf, 0, 1024)) > -1)
out.write(buf, 0, n);
ByteArrayInputStream bin = new ByteArrayInputStream(out.toByteArray());
Content con = new Content( bin , “application/octet-stream”, -1,”UTF-16″);
IResourceList old_resourcesList = collection.getChildren();
IResource resource = null;
if(!(IsSameNameResourceExist(old_resourcesList, file_name)))
{
resource = collection.createResource(file_name, null, con);
} else
{
String new_name = “new_” + file_name;
int i =0;
while(IsSameNameResourceExist(old_resourcesList, new_name))
{
i = i+1;
new_name = “new_” +i +”_”+ file_name;
}
resource = collection.createResource(new_name, null, con);
long contentLen = resource.getContent().getContentLength() ;
out.close();
System.err.println(“file uploaded is ” + entryName);
if ( contentLen < 0){
}else
{
System.err.println(“File uploaded is “+entryName);
}
} else {
fileKmpath1 = filen[i1] + “/” ;
System.err.println(“filekmpath1” + fileKmpath1);
filekmpath = filekmpath + filen[i1]+ “/”;
System.err.println(“filekmpath1” + filekmpath);
if(!(IsfolderPresent(kmpath+ filekmpath)))
{
System.err.println(“filekmpath1 inside if” + filekmpath);
Createfolder(currentfolder, filen[i1]);
}
currentfolder = kmpath+ filekmpath;
System.err.println(“current folder” + currentfolder);
} } }
zipinputstream.closeEntry();
zipentry = zipinputstream.getNextEntry();
}//while
zipinputstream.close();
flag = true;
}
catch (Exception e) {
System.err.println(“error uploading KM file “+e.getLocalizedMessage());
e.printStackTrace();
wdComponentAPI.getMessageManager().reportWarning(“error uploading KM file ” +e.getLocalizedMessage());
flag = false;
}
finally { return flag; }
}
public boolean IsSameNameResourceExist(IResourceList reslist, String name)
{
boolean flag = false;
IResource resi = null;
try { for (int i = 0; i < reslist.size(); i++)
{
resi = reslist.get(i);
if(name.trim().equalsIgnoreCase(resi.getName().trim()))
{
flag = true;
break;
}
}
}
catch(Exception e)
{
System.err.println(“exception occured while same name resource check folder”); // first we need to create this folder
e.printStackTrace();
flag = false;
}
return flag;
}
public boolean IsfolderPresent(String path)
{
boolean flag = false;
try {
IWDClientUser wdClientUser = WDClientUser.getCurrentUser();
com.sap.security.api.IUser sapUser = wdClientUser.getSAPUser();
IUser ep5User = WPUMFactory.getUserFactory().getEP5User(sapUser);
ResourceContext resourseContext = new ResourceContext(ep5User);
RID pathRID = RID.getRID(path);
IResourceFactory resourseFactory = ResourceFactory.getInstance();
ICollection collection = (ICollection) resourseFactory.getResource(pathRID,resourseContext);
if (collection !=null)
{ flag = true; }
else
{ flag = false; }
}
catch(Exception e)
{
System.err.println(“exception occured while checking folder”);
// first we need to create this folder
e.printStackTrace();
flag = false;
}
return flag;
}
public boolean Createfolder(String path, String Name)
{
boolean flag = false;
try {
IWDClientUser wdClientUser = WDClientUser.getCurrentUser();
com.sap.security.api.IUser sapUser = wdClientUser.getSAPUser();
IUser ep5User = WPUMFactory.getUserFactory().getEP5User(sapUser);
ResourceContext resourseContext = new ResourceContext(ep5User);
RID pathRID = RID.getRID(path);
IResourceFactory resourseFactory = ResourceFactory.getInstance();
ICollection collection = (ICollection) resourseFactory.getResource(pathRID,resourseContext);
IResourceList old_resourcesList = collection.getChildren();
ICollection newfolder = null;
if(!(IsSameNameResourceExist(old_resourcesList, Name))) {
newfolder = collection.createCollection(Name,null,null); }
else {
String new_name = “new_” + Name;
newfolder = collection.createCollection(Name,null,null);
} else {
String new_name = “new_” + Name;
int i =0;
while(IsSameNameResourceExist(old_resourcesList, new_name))
{
i = i+1;
new_name = “new_” +i +”_”+ Name;
} newfolder = collection.createCollection(new_name,null,null); }
flag = true;
} catch(Exception e){
System.err.println(“exception occured while checking folder”); // first we need to create this folder
e.printStackTrace(); flag = false;
} return flag;
}
And that’s it.
You can upload multiple files or many folders to any specified locations.
Hi Sarbjeet, thanks for this article which seems very useful to me. But I have several problems with the code:
1. The binary class has been deprecated and was replaced by the Resource class.
Is the code also working with the resource class?
2. The second Else at the end is probably too much:
else {
String new_name = "new_" + Name;
newfolder = collection.createCollection(Name,null,null);
3. I can't find the IResourceList type in my NWDS 7.3.
4. What is the code in the Upload button event handler (I guess in some way the getZipFiles method is called)?
Thanks and greetings
Stefan
Hi Sarbjeet,
May I know what would be the security issue related to usage of portal drive?
Regards,
Mahesh