/*
 *
 *  Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package org.wso2.carbon.apimgt.rest.api.publisher.impl;

import org.wso2.carbon.apimgt.api.APIManagementException;
import org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO;
import org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor;
import org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutorFactory;
import org.wso2.carbon.apimgt.impl.workflow.WorkflowStatus;
import org.wso2.carbon.apimgt.impl.workflow.WorkflowException;
import org.wso2.carbon.apimgt.rest.api.publisher.*;
import org.wso2.carbon.apimgt.rest.api.publisher.dto.*;
import org.wso2.carbon.apimgt.rest.api.publisher.dto.WorkflowDTO.*;
import org.wso2.carbon.apimgt.rest.api.util.RestApiConstants;
import org.wso2.carbon.apimgt.rest.api.util.utils.RestApiUtil;
import org.wso2.carbon.apimgt.rest.api.publisher.dto.ErrorDTO;
import org.wso2.carbon.apimgt.rest.api.publisher.dto.WorkflowDTO;
import org.wso2.carbon.context.PrivilegedCarbonContext;



import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


import javax.ws.rs.core.Response;

public class WorkflowsApiServiceImpl extends WorkflowsApiService {

    private static final Log log = LogFactory.getLog(WorkflowsApiService.class);

    @Override
    public Response workflowsUpdateWorkflowStatusPost(String workflowReferenceId, WorkflowDTO body) {

        ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();

        boolean isTenantFlowStarted = false;

        try {
            if (workflowReferenceId != null) {
                org.wso2.carbon.apimgt.impl.dto.WorkflowDTO workflowDTO = apiMgtDAO
                        .retrieveWorkflow(workflowReferenceId);
                if (workflowDTO == null) {
                    RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_WORKFLOW, workflowReferenceId,
                            log);
                } else {
                    String tenantDomain = workflowDTO.getTenantDomain();
                    if (tenantDomain != null
                            && !org.wso2.carbon.utils.multitenancy.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME
                                    .equals(tenantDomain)) {
                        isTenantFlowStarted = true;
                        PrivilegedCarbonContext.startTenantFlow();
                        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
                    }

                    workflowDTO.setWorkflowDescription(body.getDescription());
                    workflowDTO.setStatus(WorkflowStatus.valueOf(body.getStatus().toString()));
                    workflowDTO.setAttributes(body.getAttributes());
                    String workflowType = workflowDTO.getWorkflowType();
                    WorkflowExecutor workflowExecutor = WorkflowExecutorFactory.getInstance()
                            .getWorkflowExecutor(workflowType);

                    workflowExecutor.complete(workflowDTO);
                    return Response.ok().entity(body).build();
                    
                }                
            }
        } catch (APIManagementException e) {
            String msg = "Error while resuming workflow " + workflowReferenceId;
            RestApiUtil.handleInternalServerError(msg, e, log);
        } catch (WorkflowException e) {
            String msg = "Error while resuming workflow " + workflowReferenceId;
            RestApiUtil.handleInternalServerError(msg, e, log);
        } finally {
            if (isTenantFlowStarted) {
                PrivilegedCarbonContext.endTenantFlow();
            }
        }
        return null;
    }
}

@@ -1641,16 +1641,41 @@
 ctory;%0A%0A
+import java.util.Arrays;%0A
 %0Aimport 
@@ -1968,17 +1968,157 @@
 body) %7B%0A
+        RestApiUtil.handleBadRequest(%0A                %22Specified tier(s) %22 + Arrays.toString(invalidTiers.toArray()) + %22 are invalid%22, log);
 %0A
-
         
@@ -3191,252 +3191,938 @@
   %7D%0A
-%0A                    workflowDTO.setWorkflowDescription(body.getDescription());%0A                    workflowDTO.setStatus(WorkflowStatus.valueOf(body.getStatus().toString()));%0A                    workflowDTO.setAttributes(body.getAttributes());
+                    if(body == null)%7B%0A                        RestApiUtil.handleBadRequest(%0A                                %22Request payload is missing%22, log);%0A                    %7D%0A                    %0A                    if(body.getDescription() != null) %7B%0A                        workflowDTO.setWorkflowDescription(body.getDescription());%0A                    %7D%0A                    %0A                    if(body.getStatus() == null) %7B%0A                        RestApiUtil.handleBadRequest(%0A                                %22Workflow status is not defined%22, log);%0A                    %7D else %7B%0A                        workflowDTO.setStatus(WorkflowStatus.valueOf(body.getStatus().toString()));%0A                    %7D%0A                    %0A                    if(body.getAttributes() != null)%7B%0A                        workflowDTO.setAttributes(body.getAttributes());%0A                    %7D%0A                                               
 %0A   

add error handling for workflow rest api