Newer
Older
stockManagement.push / src / main / java / stockManagement / push / Handling.java
Shinji on 20 Jan 2023 4 KB gitignoreの追加
package stockManagement.push;

import java.util.*;
import javax.ws.rs.*;
import javax.ws.rs.client.*;
import javax.ws.rs.core.*;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.ObjectMapper;

import pushPullRefactor.*;

import com.fasterxml.jackson.core.JsonProcessingException;

@Path("/handling")
@Component
@Resource("handling")
public class Handling {
	private Client client = ClientBuilder.newClient();
	@Cache("available")
	private Map.Entry<String, Integer> available;
	@Cache("request")
	private Map.Entry<String, Integer> request;
	@PushReference("shipping")
	@Pullable("direct")
	private String shipping = "/shipping";
	
	@PushReference("stock")
	private String stock = "/stock";
	
	@State
	private Map.Entry<String, Map.Entry<Integer, Integer>> value;
	@Path("/available")
	@PUT
	@Message({"available", "shortage"})
	public void updateAvailable(@FormParam("available") String available_json, @FormParam("shortage") String shortage_json) throws JsonProcessingException {
		Map<String, Integer> shortage = new HashMap<>();
		{
			Map<String, Integer> i = new ObjectMapper().readValue(shortage_json, HashMap.class);
			shortage = i;
		}
		Map.Entry<String, Integer> available;
		{
			Map<String, Integer> i = new ObjectMapper().readValue(available_json, HashMap.class);
			available = new AbstractMap.SimpleEntry<>(i.entrySet().iterator().next().getKey(), i.entrySet().iterator().next().getValue());
		}
		this.available = available;
		Map.Entry<String, Map.Entry<Integer, Integer>> temp_if6;
		int temp_get14;
		if (shortage.get(available.getKey()) != null) {
			temp_get14 = shortage.get(available.getKey());
		} else {
			temp_get14 = 0;
		}
		if ((available.getValue()>=temp_get14)) {
			int temp_get15;
		if (shortage.get(available.getKey()) != null) {
			temp_get15 = shortage.get(available.getKey());
		} else {
			temp_get15 = 0;
		}
		int temp_get16;
		if (shortage.get(available.getKey()) != null) {
			temp_get16 = shortage.get(available.getKey());
		} else {
			temp_get16 = 0;
		}
			temp_if6 = new AbstractMap.SimpleEntry<>(available.getKey(), new AbstractMap.SimpleEntry<>(temp_get15, (available.getValue()-temp_get16)));
		} else {
			temp_if6 = new AbstractMap.SimpleEntry<>(available.getKey(), new AbstractMap.SimpleEntry<>(0, available.getValue()));
		}
		this.value = temp_if6;
		Form form = new Form();
		form.param("handling", new ObjectMapper().writeValueAsString(this.value));
		Entity<Form> entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED);
		String result = client.target("http://localhost:8080").path(shipping).request().put(entity, String.class);
				
		form = new Form();
		form.param("handling", new ObjectMapper().writeValueAsString(this.value));
		entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED);
		result = client.target("http://localhost:8080").path(stock).request().post(entity, String.class);
	}
	@Path("/request")
	@PUT
	@Message({"request", "stock"})
	public void updateRequest(@FormParam("request") String request_json, @FormParam("stock") String stock_json) throws JsonProcessingException {
		Map<String, Integer> stock = new HashMap<>();
		{
			Map<String, Integer> i = new ObjectMapper().readValue(stock_json, HashMap.class);
			stock = i;
		}
		Map.Entry<String, Integer> request;
		{
			Map<String, Integer> i = new ObjectMapper().readValue(request_json, HashMap.class);
			request = new AbstractMap.SimpleEntry<>(i.entrySet().iterator().next().getKey(), i.entrySet().iterator().next().getValue());
		}
		this.request = request;
		Map.Entry<String, Map.Entry<Integer, Integer>> temp_if7;
		int temp_get17;
		if (stock.get(request.getKey()) != null) {
			temp_get17 = stock.get(request.getKey());
		} else {
			temp_get17 = 0;
		}
		if ((temp_get17>=request.getValue())) {
			int temp_get18;
		if (stock.get(request.getKey()) != null) {
			temp_get18 = stock.get(request.getKey());
		} else {
			temp_get18 = 0;
		}
			temp_if7 = new AbstractMap.SimpleEntry<>(request.getKey(), new AbstractMap.SimpleEntry<>(request.getValue(), (temp_get18-request.getValue())));
		} else {
			int temp_get19;
		if (stock.get(request.getKey()) != null) {
			temp_get19 = stock.get(request.getKey());
		} else {
			temp_get19 = 0;
		}
			temp_if7 = new AbstractMap.SimpleEntry<>(request.getKey(), new AbstractMap.SimpleEntry<>(0, temp_get19));
		}
		this.value = temp_if7;
		Form form = new Form();
		form.param("handling", new ObjectMapper().writeValueAsString(this.value));
		Entity<Form> entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED);
		String result = client.target("http://localhost:8080").path(this.stock).request().post(entity, String.class);
		form = new Form();
		form.param("handling", new ObjectMapper().writeValueAsString(this.value));
		entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED);
		result = client.target("http://localhost:8080").path(shipping).request().put(entity, String.class);
	}
	@Produces(MediaType.APPLICATION_JSON)
	@GET
	@Getter
	public Map.Entry<String, Map.Entry<Integer, Integer>> getValue() {
		return value;
	}
}