How to Download A Document From the custom ECM for a process using BPM API as a RESTful service.
This guide provides instructions on how to download a document from custom ECM Upload for a process using BPM API as a Restful Service.
Applies to:
This Document Holds good for all CE 7.3 SP05 onwards. This service can be called from UI5 Screen as an Ajax call.
Summary:
This guide describes step-by-step how to download a document upload in the custom ECM for a process using BPM API as a rest full service.
About Me:
As a Sr. Netweaver Consultant, I’ve been undertaking consulting assignments leveraging on my undermentioned NetWeaver skills.
- Business Process Management (SAP NW BPM)
- Restful Services using BPM api to be used in UI5 Screens.
- SAP Web Dynpro Java (SAP WD4J)
- SAP Business Rules Management (SAP BRMS)
- SAP Composit Application Framework (SAP CAF)
- Master Data Management (SAP NW MDM)
- Enterprise Portal (SAP EP)
- Services creation using NWDS (SAP EJB)
- Enterprise SOA
Prerequisites:
You should have read through the document :
How to Start a BPM Process using BPM API as a RESTful service.
How To Upload A Document For A Task to design your custom ECM using BPM API as a RESTful service.
We will be discussing following points in detail in this document –
- Adding Libraries.
- Setting up the foundation for using Libraries.
- Creating Deploy-able Object.
- Accessing the methods exposed.
Adding Libraries:
Step 1 : Refer the document to add libraries.
Setting up the foundation for using Library:
Step 1 to Step 5.1 is same as mentioned in the document.
Step 5 : Create a restful service class file and write a code as shown below or if you are continuing from the document then just copy past the methods only.
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.URI;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.activation.DataHandler;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.CacheControl;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import org.apache.cxf.jaxrs.ext.multipart.MultipartBody;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import bo.FileDto;
import bo.Material;
import bo.MaterialCreation;
import bo.TaskHeader;
import bo.TaskHeaders;
import com.sap.bpm.api.BPMFactory;
import com.sap.bpm.exception.api.BPMException;
import com.sap.bpm.pm.api.ProcessDefinition;
import com.sap.bpm.pm.api.ProcessDefinitionManager;
import com.sap.bpm.pm.api.ProcessInstance;
import com.sap.bpm.pm.api.ProcessInstanceManager;
import com.sap.bpm.pm.api.ProcessStartEvent;
import com.sap.bpm.pm.api.ProcessStartManager;
import com.sap.bpm.tm.api.Status;
import com.sap.bpm.tm.api.TaskAbstract;
import com.sap.bpm.tm.api.TaskDetail;
import com.sap.bpm.tm.api.TaskInstanceManager;
import com.sap.tc.logging.Location;
import commonj.sdo.DataObject;
import commonj.sdo.helper.XMLHelper;
@Path("/MaterialCreationService")
@Produces({MediaType.APPLICATION_XML})
public class MaterialCreationToRestService {
private static final Location location = Location.getLocation(MaterialCreationToRestService.class);
private final String PRE_TASK_URI = "bpm://bpm.sap.com/task-instance/";
private String getProcessInstanceForTaskInstance(String taskInstanceId)
{
System.err.println("ArticleMaintenanceService -> getTaskDetails");
System.err.println("Task Instance ID: "+taskInstanceId);
try
{
URI taskInstId = new URI("bpm://bpm.sap.com/task-instance/" + taskInstanceId);
ProcessInstanceManager processInstanceManager = BPMFactory.getProcessInstanceManager();
ProcessInstance processInstance = processInstanceManager.getProcessInstanceForTaskInstanceId(taskInstId);
String processInstanceId = extractID(processInstance.getId().toString());
System.err.println("Process Instance ID : "+processInstanceId);
return processInstanceId;
}
catch (Exception e)
{
System.err.println("Exception : "+e.getMessage());
}
return null;
}
private String extractID(String instanceId)
{
if(instanceId == null || instanceId.trim().length() == 0)
return instanceId;
String [] ids = instanceId.split("/");
return ids[ids.length-1];
}
@Path("/downloadDocument/{requestId}")
@GET
@Produces({"application/txt","application/doc","application/docx","application/pdf"})
public Response downloadDocument(@Context HttpServletRequest req, @PathParam("requestId") String requestId) throws IOException
{
System.err.println("ECMDocumentService -> downloadDocument");
System.err.println("Request ID : "+requestId);
if(requestId == null || requestId.trim().length() == 0)
return null;
requestId = getProcessInstanceForTaskInstance(requestId);
ECMDocumentService ecmService = new ECMDocumentService();
FileDto fileDto = ecmService.downloadDocument(requestId);
if (fileDto != null && fileDto.getFile() != null)
{
System.err.println("File Name : "+fileDto.getDocumentId());
byte[] file = fileDto.getFile();
String fileName = fileDto.getDocumentId();
Response.ResponseBuilder response = Response.ok(file);
response.cacheControl(CacheControl.valueOf("private"));
response.header("Accept-Ranges", "none");
response.header("Content-Disposition", "attachment; filename="+ fileName);
return response.build();
}
else
{
Response.ResponseBuilder builder = Response.status(Response.Status.FORBIDDEN);
builder.type(MediaType.APPLICATION_JSON);
builder.entity("There is no download file for this request.");
return builder.build();
}
}
}
Step 6 & Step 7 is same as mentioned in the document. If you are continuing then you can ignore these steps.
Creating Deploy-able Object & Accessing the methods exposed.
Refer the document to “Creating Deploy-able Object” & “Accessing the methods exposed” as it is the same.