All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fivefaces.cloud.workflow.function.CreateStructure Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package com.fivefaces.cloud.workflow.function;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fivefaces.common.util.JsonUtils;
import org.json.JSONObject;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponentsBuilder;

public class CreateStructure extends Function{

    public static final String FUNCTION_NAME = "CREATE_STRUCTURE";

    public CreateStructure(RestTemplateBuilder restTemplateBuilder) {
        super(restTemplateBuilder);
    }

    @Override
    public void setup(JSONObject jsonObject) {
    }

    @Override
    public JSONObject process(JSONObject jsonParameters) throws JsonProcessingException {
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8081/api/v1/structure/insertFromWorkflow");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        MultiValueMap requestMap = new LinkedMultiValueMap<>();

        JSONObject inputJson = (JSONObject) jsonParameters.get("insert");
        requestMap.add("queryJSON", new JsonUtils().marshall(inputJson.toString()));
        HttpEntity> request = new HttpEntity<>(requestMap, headers);
        ResponseEntity response = restTemplateBuilder.build().postForEntity( builder.build().encode().toUri(), request , String.class );

        JSONObject result = new JSONObject(new JsonUtils().unmarshall(response.getBody()));
        return result.getJSONObject("jsonData");
    }

    @Override
    public void cleanup(JSONObject jsonObject) {

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy