Search in sources :

Example 1 with HumanTaskClientAPIServiceClient

use of org.wso2.carbon.humantask.ui.clients.HumanTaskClientAPIServiceClient in project carbon-business-process by wso2.

the class AttachmentUploadExecutor method execute.

@Override
public boolean execute(HttpServletRequest request, HttpServletResponse response) throws IOException {
    PrintWriter out = response.getWriter();
    String webContext = (String) request.getAttribute(CarbonConstants.WEB_CONTEXT);
    String serverURL = (String) request.getAttribute(CarbonConstants.SERVER_URL);
    String cookie = (String) request.getAttribute(ServerConstants.ADMIN_SERVICE_COOKIE);
    Map<String, ArrayList<String>> formFieldsMap = getFormFieldsMap();
    String taskID = null;
    String redirect = null;
    try {
        if (formFieldsMap.get("taskID") != null) {
            taskID = formFieldsMap.get("taskID").get(0);
        }
        if (formFieldsMap.get("redirect") != null) {
            redirect = formFieldsMap.get("redirect").get(0);
        }
        ArrayList<FileItemData> fileItemsMap = getFileItemsMap().get("fileToUpload");
        FileItemData fileToBeUpload = fileItemsMap.get(0);
        AttachmentUploadClient attachmentUploadClient = new AttachmentUploadClient(configurationContext, serverURL, cookie);
        HumanTaskClientAPIServiceClient taskOperationClient = new HumanTaskClientAPIServiceClient(cookie, serverURL, configurationContext);
        response.setContentType("text/html; charset=utf-8");
        String attachmentID = attachmentUploadClient.addUploadedFileItem(fileToBeUpload);
        String attachmentName = fileToBeUpload.getDataHandler().getName();
        String contentType = fileToBeUpload.getDataHandler().getContentType();
        boolean isAdded = taskOperationClient.addAttachment(taskID, attachmentName, contentType, attachmentID);
        String msg = "Your attachment has been uploaded successfully.";
        if (!isAdded) {
            throw new Exception("Attachment was added successfully with id:" + attachmentID + ". But the task " + "with id: " + taskID + " was not associated with it correctly.");
        } else {
            if (redirect != null && redirect.contains("humantask/basic_task_view.jsp")) {
                // redirection is going to the carbon mgt console
                CarbonUIMessage.sendCarbonUIMessage(msg, CarbonUIMessage.INFO, request, response, getContextRoot(request) + "/" + webContext + "/" + redirect);
            } else if (redirect != null) {
                // redirection exists, not to carbon mgt console
                out.write(msg);
                response.sendRedirect(redirect);
            } else {
                CarbonUIMessage.sendCarbonUIMessage(msg, CarbonUIMessage.INFO, request);
            }
            return true;
        }
    } catch (Exception ex) {
        String errMsg = "File upload failed. Reason :" + ex.getLocalizedMessage();
        log.error(errMsg, ex);
        if (redirect != null) {
            CarbonUIMessage.sendCarbonUIMessage(errMsg, CarbonUIMessage.ERROR, request, response, getContextRoot(request) + "/" + webContext + "/" + redirect);
        } else {
            CarbonUIMessage.sendCarbonUIMessage(errMsg, CarbonUIMessage.ERROR, request);
        }
    }
    return false;
}
Also used : HumanTaskClientAPIServiceClient(org.wso2.carbon.humantask.ui.clients.HumanTaskClientAPIServiceClient) FileItemData(org.wso2.carbon.utils.FileItemData) ArrayList(java.util.ArrayList) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Aggregations

IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 HumanTaskClientAPIServiceClient (org.wso2.carbon.humantask.ui.clients.HumanTaskClientAPIServiceClient)1 FileItemData (org.wso2.carbon.utils.FileItemData)1