org.craftercms.commerce.client.AbstractRestCRUDService.java Source code

Java tutorial

Introduction

Here is the source code for org.craftercms.commerce.client.AbstractRestCRUDService.java

Source

/*
 * Copyright (C) 2007-2013 Crafter Software Corporation.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.craftercms.commerce.client;

import java.lang.reflect.ParameterizedType;

import org.craftercms.commerce.api.BaseType;
import org.craftercms.commerce.api.CrafterCommerceConstants;
import org.craftercms.commerce.api.CrafterCommerceException;
import org.craftercms.commerce.api.TypeToPathMapper;
import org.craftercms.commerce.api.service.CRUDService;
import org.craftercms.commerce.api.service.ServiceResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

public abstract class AbstractRestCRUDService<T extends BaseType> implements CRUDService<T> {

    private static Logger LOGGER = LoggerFactory.getLogger("org.craftercms.commerce.RestCRUDService");

    private String crafterCommerceServerUrl;
    private RestTemplate restTemplate;
    private HttpHeaders httpHeaders;

    @SuppressWarnings("unchecked")
    protected Class<? extends BaseType> getTypeArgument() {
        ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
        return (Class<? extends BaseType>) parameterizedType.getActualTypeArguments()[0];
    }

    public String getCrafterCommerceServerUrl() {
        return crafterCommerceServerUrl;
    }

    public void setCrafterCommerceServerUrl(String crafterCommerceServerUrl) {
        this.crafterCommerceServerUrl = crafterCommerceServerUrl;
    }

    protected RestTemplate getRestTemplate() {
        return restTemplate;
    }

    protected HttpHeaders getHttpHeaders() {
        return httpHeaders;
    }

    public AbstractRestCRUDService(String crafterCommerceServerUrl) {
        this.crafterCommerceServerUrl = crafterCommerceServerUrl;
        restTemplate = new RestTemplate();
        httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public ServiceResponse<T> save(T entity) throws CrafterCommerceException {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("About to save entity: " + entity.toString());
        }
        try {
            String restUrl = crafterCommerceServerUrl + TypeToPathMapper.path(getTypeArgument())
                    + CrafterCommerceConstants.SAVE_ACTION_REST_MAPPING + CrafterCommerceConstants.JSON_EXTENSION;
            HttpEntity<T> httpEntity = new HttpEntity<T>(entity, httpHeaders);
            ResponseEntity<? extends ServiceResponse> responseEntity = restTemplate.postForEntity(restUrl,
                    httpEntity, new ServiceResponse<T>().getClass());
            ServiceResponse<T> response = responseEntity.getBody();
            if (response.isSuccess() == false) {
                throw new CrafterCommerceException(response.getMessage());
            }
            return response;
        } catch (Exception e) {
            throw new CrafterCommerceException(e);
        }
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public ServiceResponse<T> saveAll(T[] entities) throws CrafterCommerceException {
        try {
            String restUrl = crafterCommerceServerUrl + TypeToPathMapper.path(getTypeArgument())
                    + CrafterCommerceConstants.SAVE_ALL_ACTION_REST_MAPPING
                    + CrafterCommerceConstants.JSON_EXTENSION;
            HttpEntity<T[]> httpEntity = new HttpEntity<T[]>(entities, httpHeaders);
            ResponseEntity<? extends ServiceResponse> responseEntity = restTemplate.postForEntity(restUrl,
                    httpEntity, new ServiceResponse<T>().getClass());
            ServiceResponse<T> response = responseEntity.getBody();
            if (response.isSuccess() == false) {
                throw new CrafterCommerceException(response.getMessage());
            }
            return response;
        } catch (Exception e) {
            throw new CrafterCommerceException(e);
        }
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public ServiceResponse<T> findById(String id) throws CrafterCommerceException {
        try {
            String restUrl = crafterCommerceServerUrl + TypeToPathMapper.path(getTypeArgument())
                    + CrafterCommerceConstants.FIND_BY_ID_ACTION_REST_MAPPING
                    + CrafterCommerceConstants.JSON_EXTENSION;
            ResponseEntity<? extends ServiceResponse> responseEntity = restTemplate.getForEntity(restUrl,
                    new ServiceResponse<T>().getClass(), id);
            ServiceResponse<T> response = responseEntity.getBody();
            if (response.isSuccess() == false) {
                throw new CrafterCommerceException(response.getMessage());
            }
            return response;
        } catch (Exception e) {
            throw new CrafterCommerceException(e);
        }
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public ServiceResponse<T> findByQuery(String query, int offset, int maxResults)
            throws CrafterCommerceException {
        try {
            String restUrl = crafterCommerceServerUrl + TypeToPathMapper.path(getTypeArgument())
                    + CrafterCommerceConstants.FIND_BY_QUERY_ACTION_REST_MAPPING
                    + CrafterCommerceConstants.JSON_EXTENSION;
            ResponseEntity<? extends ServiceResponse> responseEntity = restTemplate.getForEntity(restUrl,
                    new ServiceResponse<T>().getClass(), query, offset, maxResults);
            ServiceResponse<T> response = responseEntity.getBody();
            if (response.isSuccess() == false) {
                throw new CrafterCommerceException(response.getMessage());
            }
            return response;
        } catch (Exception e) {
            throw new CrafterCommerceException(e);
        }
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public ServiceResponse<T> findByQuery(String query, int offset, int maxResults, String... facet)
            throws CrafterCommerceException {
        try {
            String restUrl = crafterCommerceServerUrl + TypeToPathMapper.path(getTypeArgument())
                    + CrafterCommerceConstants.FIND_BY_QUERY_WITH_FACETS_ACTION_REST_MAPPING
                    + CrafterCommerceConstants.JSON_EXTENSION;
            String facetConcat = "";
            for (int i = 0; i < facet.length; i++) {
                facetConcat = facetConcat + facet[i];
                if (i + 1 < facet.length) {
                    facetConcat = facetConcat + ",";
                }
            }
            ResponseEntity<? extends ServiceResponse> responseEntity = restTemplate.getForEntity(restUrl,
                    new ServiceResponse<T>().getClass(), query, offset, maxResults, facetConcat);
            ServiceResponse<T> response = responseEntity.getBody();
            if (response.isSuccess() == false) {
                throw new CrafterCommerceException(response.getMessage());
            }
            return response;
        } catch (Exception e) {
            throw new CrafterCommerceException(e);
        }
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public ServiceResponse<T> findAll() throws CrafterCommerceException {
        try {
            String restUrl = crafterCommerceServerUrl + TypeToPathMapper.path(getTypeArgument())
                    + CrafterCommerceConstants.FIND_ALL_ACTION_REST_MAPPING
                    + CrafterCommerceConstants.JSON_EXTENSION;
            ResponseEntity<? extends ServiceResponse> responseEntity = restTemplate.getForEntity(restUrl,
                    new ServiceResponse<T>().getClass());
            ServiceResponse<T> response = responseEntity.getBody();
            if (response.isSuccess() == false) {
                throw new CrafterCommerceException(response.getMessage());
            }
            return response;
        } catch (Exception e) {
            throw new CrafterCommerceException(e);
        }
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public ServiceResponse<T> count() throws CrafterCommerceException {
        try {
            String restUrl = crafterCommerceServerUrl + TypeToPathMapper.path(getTypeArgument())
                    + CrafterCommerceConstants.COUNT_ACTION_REST_MAPPING + CrafterCommerceConstants.JSON_EXTENSION;
            ResponseEntity<? extends ServiceResponse> responseEntity = restTemplate.getForEntity(restUrl,
                    new ServiceResponse<T>().getClass());
            ServiceResponse<T> response = responseEntity.getBody();
            if (response.isSuccess() == false) {
                throw new CrafterCommerceException(response.getMessage());
            }
            return response;
        } catch (Exception e) {
            throw new CrafterCommerceException(e);
        }
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public ServiceResponse<T> delete(T entity) throws CrafterCommerceException {
        try {
            String restUrl = crafterCommerceServerUrl + TypeToPathMapper.path(getTypeArgument())
                    + CrafterCommerceConstants.DELETE_ACTION_REST_MAPPING + CrafterCommerceConstants.JSON_EXTENSION;
            ResponseEntity<? extends ServiceResponse> responseEntity = restTemplate.postForEntity(restUrl, entity,
                    new ServiceResponse<T>().getClass());
            ServiceResponse<T> response = responseEntity.getBody();
            if (response.isSuccess() == false) {
                throw new CrafterCommerceException(response.getMessage());
            }
            return response;
        } catch (Exception e) {
            throw new CrafterCommerceException(e);
        }
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public ServiceResponse<T> deleteAll() throws CrafterCommerceException {
        try {
            String restUrl = crafterCommerceServerUrl + TypeToPathMapper.path(getTypeArgument())
                    + CrafterCommerceConstants.DELETE_ALL_ACTION_REST_MAPPING
                    + CrafterCommerceConstants.JSON_EXTENSION;
            ResponseEntity<? extends ServiceResponse> responseEntity = restTemplate.postForEntity(restUrl, null,
                    new ServiceResponse<T>().getClass());
            ServiceResponse<T> response = responseEntity.getBody();
            if (response.isSuccess() == false) {
                throw new CrafterCommerceException(response.getMessage());
            }
            return response;
        } catch (Exception e) {
            throw new CrafterCommerceException(e);
        }
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public ServiceResponse<T> exists(String id) throws CrafterCommerceException {
        try {
            String restUrl = crafterCommerceServerUrl + TypeToPathMapper.path(getTypeArgument())
                    + CrafterCommerceConstants.EXISTS_ACTION_REST_MAPPING + CrafterCommerceConstants.JSON_EXTENSION;
            ResponseEntity<? extends ServiceResponse> responseEntity = restTemplate.getForEntity(restUrl,
                    new ServiceResponse<T>().getClass(), id);
            ServiceResponse<T> response = responseEntity.getBody();
            if (response.isSuccess() == false) {
                throw new CrafterCommerceException(response.getMessage());
            }
            return response;
        } catch (Exception e) {
            throw new CrafterCommerceException(e);
        }
    }

}