org.craftercms.commerce.server.BaseCRUDService.java Source code

Java tutorial

Introduction

Here is the source code for org.craftercms.commerce.server.BaseCRUDService.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.server;

import java.lang.reflect.ParameterizedType;
import java.util.Date;
import java.util.Set;
import java.util.UUID;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import org.craftercms.commerce.api.BaseType;
import org.craftercms.commerce.api.CrafterCommerceException;
import org.craftercms.commerce.api.service.CRUDService;
import org.craftercms.commerce.api.service.ServiceResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;

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

    @Autowired
    private Validator validator;

    protected Validator getValidator() {
        return validator;
    }

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

    protected void prepareEntityForSaving(@RequestBody T entity) {
        if (null == entity.getId())
            entity.setId(UUID.randomUUID().toString());
        Date now = new Date();
        if (null == entity.getCreationDate())
            entity.setCreationDate(now);
        entity.setLastModifiedDate(now);
    }

    protected void validate(T entity) throws CrafterCommerceException {
        Set<ConstraintViolation<T>> violations = getValidator().validate(entity);
        if (violations.size() > 0) {
            StringBuilder sb = new StringBuilder("Entity was not valid for the following reason(s):");
            int i = 0;
            for (ConstraintViolation<T> _violation : violations) {
                sb.append("\n");
                sb.append(_violation.getPropertyPath());
                sb.append(" ");
                sb.append(_violation.getMessage());
                i++;
                if (i < violations.size()) {
                    sb.append(", ");
                }
            }
            throw new CrafterCommerceException(sb.toString());
        }
    }

    /**
     * Note that the below abstract methods have to be defined in order for Spring
     * to read the request mappings annotations on the same methods defined in CRUDService
     */

    public abstract ServiceResponse<T> save(T entity) throws CrafterCommerceException;

    public abstract ServiceResponse<T> saveAll(T[] entities) throws CrafterCommerceException;

    public abstract ServiceResponse<T> findById(String id) throws CrafterCommerceException;

    public abstract ServiceResponse<T> findByQuery(String query, int offset, int maxResults)
            throws CrafterCommerceException;

    public abstract ServiceResponse<T> findAll() throws CrafterCommerceException;

    public abstract ServiceResponse<T> count() throws CrafterCommerceException;

    public abstract ServiceResponse<T> delete(T entity) throws CrafterCommerceException;

    public abstract ServiceResponse<T> deleteAll() throws CrafterCommerceException;

    public abstract ServiceResponse<T> exists(String id) throws CrafterCommerceException;

}