com.dm.platform.core.service.impl.BaseService.java Source code

Java tutorial

Introduction

Here is the source code for com.dm.platform.core.service.impl.BaseService.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dm.platform.core.service.impl;

import com.dm.platform.core.dao.impl.BaseDao;
import com.dm.platform.core.model.BaseModel;
import com.dm.platform.core.service.IService;
import com.dm.platform.core.util.QueryResult;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.LinkedHashMap;
import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Administrator
 * @param <Model>
 * @param <Dao>
 */
public abstract class BaseService<Model extends BaseModel, Dao extends BaseDao>
        implements IService<Model>, ApplicationContextAware {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    protected ApplicationContext applicationContext;
    protected Class<Model> modelClass;
    protected Class<Dao> daoClass;
    protected Dao dao;

    /**
     * ???
     *
     * @return
     */
    protected final Class<Model> getModelClass() {
        Type type = getClass().getGenericSuperclass();
        Type trueType = ((ParameterizedType) type).getActualTypeArguments()[0];
        return (Class<Model>) trueType;
    }

    /**
     * ???
     *
     * @return
     */
    protected final Class<Dao> getDaoClass() {
        Type type = getClass().getGenericSuperclass();
        Type trueType = ((ParameterizedType) type).getActualTypeArguments()[1];
        return (Class<Dao>) trueType;
    }

    @PostConstruct
    public void init() {
        logger.info("postContruct");
        modelClass = this.getModelClass();
        daoClass = this.getDaoClass();
        dao = applicationContext.getBean(daoClass);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        logger.info("setApplicationContext");
        this.applicationContext = applicationContext;
    }

    @Override
    @Transactional
    public void save(Model entity) {
        dao.save(entity);
    }

    @Override
    @Transactional
    public void clear() {

    }

    @Override
    @Transactional
    public void delete(Object entityid) {
        delete(new Object[] { entityid });
    }

    @Override
    @Transactional
    public void delete(Object[] entityids) {
    }

    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    @Override
    public Model find(Object entityId) {
        return null;
    }

    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    @Override
    public long getCount() {
        return 0;
    }

    @Override
    @Transactional
    public void update(Model entity) {

    }

    @SuppressWarnings("unchecked")
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    @Override
    public QueryResult<Model> getScrollData(int firstIndex, int maxResult, String whereJpql, Object[] queryParams,
            LinkedHashMap<String, String> orderBy) {
        return null;
    }

}