package au.org.aurin.wif.restclient.suitability; import static au.org.aurin.wif.io.RestAPIConstants.HEADER_USER_ID_KEY; import static au.org.aurin.wif.io.RestUtil.removeTrailingSlash; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpEntity; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.validation.BindException; import org.springframework.web.client.RestTemplate; import au.org.aurin.wif.controller.OWIURLs; import au.org.aurin.wif.exception.config.WifInvalidConfigException; import au.org.aurin.wif.exception.validate.WifInvalidInputException; import au.org.aurin.wif.io.ServiceException; import au.org.aurin.wif.model.suitability.Factor; /** * The Class FactorServiceClientImpl. */ public class FactorServiceClientImpl implements FactorServiceClient { /** The url. */ private String url; /** The rest template. */ @Autowired private RestTemplate restTemplate; /** * Sets the url. * * @param url * the new url */ public void setUrl(final String url) { this.url = url; } /* * (non-Javadoc) * @see * au.org.aurin.wif.restclient.FactorServiceClient#getFactorsForProject(java * .lang.String, java.lang.String) */ public List<Factor> getFactorsForProject(final String roleId, final String projectId) throws WifInvalidInputException { final MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>(); headers.add(HEADER_USER_ID_KEY, roleId); final HttpEntity<Object> requestEntity = new HttpEntity<Object>(headers); final ResponseEntity<List> response = restTemplate.exchange( removeTrailingSlash(url) + OWIURLs.PROJECT_SVC_URI + "/" + projectId + "/factors/", HttpMethod.GET, requestEntity, List.class); if (response.getStatusCode() != HttpStatus.OK) { throw new ServiceException("HTTP Response Status Code: " + response.getStatusCode() + " was thrown while running a request against " + removeTrailingSlash(url) + OWIURLs.PROJECT_SVC_URI + "/" + projectId + "/factors/."); } return response.getBody(); } /* * (non-Javadoc) * @see * au.org.aurin.wif.restclient.FactorServiceClient#getFactor(java.lang.String, * java.lang.String, java.lang.String) */ public Factor getFactor(final String roleId, final String projectId, final String id) throws WifInvalidInputException, WifInvalidConfigException { final MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>(); headers.add(HEADER_USER_ID_KEY, roleId); final HttpEntity<Object> requestEntity = new HttpEntity<Object>(headers); final ResponseEntity<Factor> response = restTemplate.exchange( removeTrailingSlash(url) + OWIURLs.PROJECT_SVC_URI + "/" + projectId + "/factors/" + id, HttpMethod.GET, requestEntity, Factor.class); if (response.getStatusCode() != HttpStatus.OK) { throw new ServiceException("HTTP Response Status Code: " + response.getStatusCode() + " was thrown while running a request against " + removeTrailingSlash(url) + OWIURLs.PROJECT_SVC_URI + "/" + projectId + "/factors/" + id + "."); } return response.getBody(); } /* * (non-Javadoc) * @see * au.org.aurin.wif.restclient.FactorServiceClient#createFactor(java.lang. * String, java.lang.String, au.org.aurin.wif.model.suitability.Factor) */ public String createFactor(final String roleId, final String projectId, final Factor factor) throws WifInvalidInputException, BindException, WifInvalidConfigException { final MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>(); headers.add(HEADER_USER_ID_KEY, roleId); final HttpEntity<Object> requestEntity = new HttpEntity<Object>(factor, headers); final ResponseEntity<Factor> response = restTemplate.exchange( removeTrailingSlash(url) + OWIURLs.PROJECT_SVC_URI + "/" + projectId + "/factors/", HttpMethod.POST, requestEntity, Factor.class); if (response.getStatusCode() != HttpStatus.CREATED) { throw new ServiceException("HTTP Response Status Code: " + response.getStatusCode() + " was thrown while creating a Factor."); } final Factor persistedFactor = response.getBody(); return persistedFactor.getId().toString(); } /* * (non-Javadoc) * @see * au.org.aurin.wif.restclient.FactorServiceClient#updateFactor(java.lang. * String, java.lang.String, java.lang.String, * au.org.aurin.wif.model.suitability.Factor) */ public void updateFactor(final String roleId, final String projectId, final String id, final Factor factor) throws WifInvalidInputException, BindException, WifInvalidConfigException { final MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>(); headers.add(HEADER_USER_ID_KEY, roleId); final HttpEntity<Object> requestEntity = new HttpEntity<Object>(factor, headers); restTemplate.exchange(removeTrailingSlash(url) + OWIURLs.PROJECT_SVC_URI + "/" + projectId + "/factors/" + id, HttpMethod.PUT, requestEntity, null); } /* * (non-Javadoc) * @see * au.org.aurin.wif.restclient.FactorServiceClient#deleteFactor(java.lang. * String, java.lang.String, java.lang.String) */ public void deleteFactor(final String roleId, final String projectId, final String id) throws WifInvalidInputException { final MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>(); headers.add(HEADER_USER_ID_KEY, roleId); final HttpEntity<Object> requestEntity = new HttpEntity<Object>(headers); restTemplate.exchange(removeTrailingSlash(url) + OWIURLs.PROJECT_SVC_URI + "/" + projectId + "/factors/" + id, HttpMethod.DELETE, requestEntity, null); } }