Newer
Older
stockManagement.pull / src / main / java / stockManagement / pull / Available.java
Shinji on 25 Jan 2023 2 KB パッケージの修正
package stockManagement.pull;

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 com.fasterxml.jackson.core.JsonProcessingException;
import pushPullRefactor.Getter;
import pushPullRefactor.Message;
import pushPullRefactor.Resource;
import pushPullRefactor.State;

@Path("/available")
@Component
@Resource("available")
public class Available {
	private Client client = ClientBuilder.newClient();
	@State
	private Map.Entry<String, Integer> value;
	@PUT
	@Message({"arrival", "stock"})
	public void updateArrival(@FormParam("arrival") String arrival_json, @FormParam("stock") String stock_json) throws JsonProcessingException {
		Map<String, Integer> shortage_json = client.target("http://localhost:8080").path("/shortage").request().get(HashMap.class);
		Map<String, Integer> shortage = new HashMap<>();
		shortage = shortage_json;
		Map<String, Integer> stock = new HashMap<>();
		{
			Map<String, Integer> i = new ObjectMapper().readValue(stock_json, HashMap.class);
			stock = i;
		}
		Map.Entry<String, Integer> arrival;
		{
			Map<String, Integer> i = new ObjectMapper().readValue(arrival_json, HashMap.class);
			arrival = new AbstractMap.SimpleEntry<>(i.entrySet().iterator().next().getKey(), i.entrySet().iterator().next().getValue());
		}
		int temp_get20;
		if (stock.get(arrival.getKey()) != null) {
			temp_get20 = stock.get(arrival.getKey());
		} else {
			temp_get20 = 0;
		}
		this.value = new AbstractMap.SimpleEntry<>(arrival.getKey(), (arrival.getValue()+temp_get20));
		Form form = new Form();
		form.param("available", new ObjectMapper().writeValueAsString(this.value));
		Entity<Form> entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED);
		String result = client.target("http://localhost:8080").path("/shortage/available").request().post(entity, String.class);
		form = new Form();
		form.param("available", new ObjectMapper().writeValueAsString(this.value));
		form.param("shortage", new ObjectMapper().writeValueAsString(shortage));
		entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED);
		result = client.target("http://localhost:8080").path("/deriver/available").request().put(entity, String.class);
	}
	@Produces(MediaType.APPLICATION_JSON)
	@GET
	@Getter
	public Map.Entry<String, Integer> getValue() {
		return value;
	}
}