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

com.mycomm.dao.dao4comm.framework.impl.SimpleOrmObjectLoader Maven / Gradle / Ivy

There is a newer version: 1.0.11
Show newest version
/*
 * 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.mycomm.dao.dao4comm.framework.impl;

import com.mycomm.IProtocol.cache.TableCacheService;
import com.mycomm.dao.dao4comm.ResultHelp;
import com.mycomm.dao.dao4comm.framework.AnnotationStructureStrategy;
import com.mycomm.dao.dao4comm.framework.OrmObjectLoader;
import com.mycomm.dao.dao4comm.util.AnnotationParser;
import com.mycomm.dao.dao4comm.util.InstanceBuilder;
import com.mycomm.dao.dao4comm.util.Utils;
import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

/**
 *
 * @author jw362j
 */
public class SimpleOrmObjectLoader implements OrmObjectLoader {

    private static final Logger log = LoggerFactory.getLogger(SimpleOrmObjectLoader.class);

    public ResultHelp getScrollData(final AnnotationStructureStrategy annotationStructureStrategy, final JdbcTemplate jdbcTemplate, final Class clz, long firstindex, int maxresult, String[] fields, String wherejpql, Object[] queryParams, int[] queryParamsTypes, Map orderby, TableCacheService myTableCacheService) {
        if (jdbcTemplate == null) {
            throw new RuntimeException("JdbcTemplate is null ,game over!");
        }
        String sqlQuery = "SELECT "
                + ((fields != null && fields.length > 0) ? Utils.buildProjection(fields) : " * ")
                + " FROM " + annotationStructureStrategy.getTableName()
                + (wherejpql == null || "".equals(wherejpql.trim()) ? " " : " WHERE " + wherejpql)
                + Utils.buildOrderby(orderby)
                + ((firstindex > -1 && maxresult > -1) ? (" LIMIT " + firstindex + ", " + maxresult) : "");
        log.info(sqlQuery);
        if (queryParams == null || queryParams.length <= 0) {
            if (queryParamsTypes != null) {
                return null;
            }
        }
        if (queryParamsTypes == null || queryParamsTypes.length <= 0) {
            if (queryParams != null) {
                return null;
            }
        }

        List list;
        final Field[] fs = clz.getDeclaredFields();
        if (queryParams != null && queryParams.length > 0 && queryParamsTypes != null && queryParamsTypes.length > 0) {
            list = (List) jdbcTemplate.query(sqlQuery,
                    queryParams,
                    queryParamsTypes,
                    new RowMapper() {
                @Override
                public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                    try {
                        Object instance = clz.newInstance();
//                        Field[] fs = clz.getDeclaredFields();
                        InstanceBuilder.buildInstance(rs, instance, fs, annotationStructureStrategy.getIdColumName());
                        return instance;
                    } catch (InstantiationException ex) {
                        log.error(ex.getMessage());
                        return null;
                    } catch (IllegalAccessException ex) {
                        log.error(ex.getMessage());
                        return null;
                    }
                }
            });
        } else {
            list = jdbcTemplate.query(sqlQuery, new RowMapper() {
                public Object mapRow(ResultSet rs, int i) throws SQLException {
                    try {
                        Object instance = clz.newInstance();
                        InstanceBuilder.buildInstance(rs, instance, fs, annotationStructureStrategy.getIdColumName());
                        return instance;
                    } catch (InstantiationException ex) {
                        log.error(ex.getMessage());
                        return null;
                    } catch (IllegalAccessException ex) {
                        log.error(ex.getMessage());
                        return null;
                    }
                }
            });
        }
        if (myTableCacheService != null) {
            //long id_flag = AnnotationParser.getIdValue(getModel());
            if(list != null && list.size() > 0){
                for(Object dataItem : list){
                    if(dataItem == null){
                        continue;
                    }
                    long id_flag = AnnotationParser.getIdValue(dataItem);
                    if(id_flag > 0){
                        myTableCacheService.put(clz, dataItem, id_flag);
                    }
                }
            }
        }

        ResultHelp qr = new ResultHelp();
        qr.setResultlist(list);
        return qr;
    }

}