All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.shoulder.web.template.crud.CrudController Maven / Gradle / Ivy

package org.shoulder.web.template.crud;

import org.shoulder.core.converter.ShoulderConversionService;
import org.shoulder.data.mybatis.template.entity.BaseEntity;
import org.shoulder.data.mybatis.template.entity.BizEntity;
import org.shoulder.data.mybatis.template.service.BaseService;
import org.shoulder.data.uid.BizIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.GenericTypeResolver;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;

/**
 * 提供基本的 CRUD API 能力
 *
 * @param    Service
 * @param         主键
 * @param     实体
 * @param  分页参数
 * @param    保存参数
 * @param  修改参数
 * @author lym
 */
@SuppressWarnings("rawtypes")
@RestController
public abstract class CrudController<
        SERVICE extends BaseService,
        ENTITY extends BaseEntity,
        ID extends Serializable,
        QueryResultDTO extends Serializable,
        PageQuery extends Serializable,
        SaveDTO extends Serializable,
        UpdateDTO extends Serializable>
    extends BaseControllerImpl
    implements
        SaveController,
        UpdateController,
        DeleteController,
        QueryController {

    protected Class[] genericClasses = GenericTypeResolver.resolveTypeArguments(getClass(), CrudController.class);

    @Autowired(required = false)
    protected BizIdGenerator bizIdGenerator;

    public CrudController(SERVICE service, ShoulderConversionService conversionService) {
        super(service, conversionService);
    }

    //@PostConstruct
    //@SuppressWarnings("unchecked")
    //public void registerConvertor() throws ClassNotFoundException {
    //    Class[] genericClasses = getGenericInfo();
    //    Class entityClass = (Class) genericClasses[1];
    //    Class pageQueryClass = (Class) genericClasses[4];
    //    ConvertUtil.addConverter(pageQueryClass, entityClass, this::convertToEntity);
    //}

    //protected Class[] getGenericInfo() throws ClassNotFoundException {
    //    if (genericClasses != null) {
    //        return genericClasses;
    //    }
    //    String genericInfo = getClass().getGenericSuperclass().toString();
    //    final String packagePrefix = "org.shoulder.web.template.crud.Crud";
    //    if (genericInfo.startsWith(packagePrefix)) {
    //        String[] genericClassesName = genericInfo.split("<", 2)[1].split(",");
    //        assert genericClassesName.length == 6;
    //        genericClassesName[1] = genericClassesName[1].substring(1);
    //        genericClassesName[2] = genericClassesName[2].substring(1);
    //        genericClassesName[3] = genericClassesName[3].substring(1);
    //        genericClassesName[4] = genericClassesName[4].substring(1);
    //        genericClassesName[5] = genericClassesName[5].substring(1, genericClassesName[5].length() - 1);
    //        Class[] classes = new Class[6];
    //        for (int i = 0; i < genericClassesName.length; i++) {
    //            classes[i] = Class.forName(genericClassesName[i]);
    //        }
    //        this.genericClasses = classes;
    //        return classes;
    //    } else {
    //        throw new ClassNotFoundException("getGenericInfo FAIL! Can't find genericInfo! superClass=" + genericInfo);
    //    }
    //}

    @Override
    @SuppressWarnings("unchecked")
    public String generateBizId(ENTITY entity) {
        getService().checkEntityAs(BizEntity.class);
        return bizIdGenerator.generateBizId((BizEntity) entity, (Class) entity.getClass());
    }

    @SuppressWarnings("unchecked")
    @Override public Class getQueryDtoClass() {
        return genericClasses[3];
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy