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

group.rober.dataform.service.DataFormAdminServiceAbstract Maven / Gradle / Ivy

There is a newer version: 3.2.2
Show newest version
package group.rober.dataform.service;

import group.rober.dataform.DataFormConsts;
import group.rober.dataform.exception.DataFormException;
import group.rober.dataform.mapper.DataFormDBRowMapper;
import group.rober.dataform.mapper.DataFormElementDBRowMapper;
import group.rober.dataform.mapper.DataFormFilterDBRowMapper;
import group.rober.dataform.model.DataForm;
import group.rober.dataform.model.DataFormElement;
import group.rober.dataform.model.DataFormFilter;
import group.rober.dataform.model.DataFormStamp;
import group.rober.dataform.model.types.ElementDataEditStyle;
import group.rober.dataform.model.types.ElementDataFormat;
import group.rober.dataform.model.types.ElementDataType;
import group.rober.runtime.kit.FileKit;
import group.rober.runtime.kit.IOKit;
import group.rober.runtime.kit.JSONKit;
import group.rober.runtime.kit.ListKit;
import group.rober.runtime.kit.SQLKit;
import group.rober.runtime.kit.StringKit;
import group.rober.runtime.lang.MapData;
import group.rober.sql.core.DataQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.jdbc.core.JdbcTemplate;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;

/**
 * Created by luyu on 2018/5/31.
 */
public abstract class DataFormAdminServiceAbstract implements DataFormAdminService {

    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    DataFormPublicService dataFormPublicService;
    @Autowired
    private DataQuery dataQuery;
    @Autowired
    private DataFormDBRowMapper dataFormDBRowMapper;
    @Autowired
    private DataFormElementDBRowMapper dataFormElementDBRowMapper;
    @Autowired
    private DataFormFilterDBRowMapper dataFormFilterDBRowMapper;

    /**
     * 清空缓存
     */
    @CacheEvict(value= DataFormConsts.CACHE_KEY,allEntries=true,beforeInvocation=true)
    public void clearCacheAll(){

    }

    @Override
    /**
     * 把数据库中的显示模板提取到JSON文件中
     */
    public String dbTransferToJsonFile(){
        String directory = dataFormPublicService.getDataformDataDirectory().getAbsolutePath();


        List dataForms = this.getCompleteDataForms();
        dataForms.forEach(dataForm -> {
            String pack = dataForm.getPack();
            String code = dataForm.getCode();
//            File curDir = new File(directory+"/"+pack);
            File curDir = FileKit.getFile(directory,pack);
            if(!curDir.exists())curDir.mkdirs();
//            File jsonFile = new File(curDir.getAbsoluteFile()+"/"+code+".json");
            File jsonFile = FileKit.getFile(curDir,code+".json");
            if(!jsonFile.exists()){
                try {
                    FileKit.touchFile(jsonFile);
                } catch (IOException e) {
                    throw new DataFormException("创建json文件失败",e);
                }
            }

            String jsonText = JSONKit.toJsonString(dataForm,true);
            try {
//                System.out.println(jsonText);
                FileKit.write(jsonFile,jsonText, Charset.defaultCharset(),false);
            } catch (IOException e) {
                throw new DataFormException("写入json文件失败",e);
            }
        });
        return directory;
    }

    private List getCompleteDataForms() {
        List dataForms = dataQuery.exec(dataFormDBRowMapper,()->{
            return dataQuery.selectList(DataForm.class, "select * from FOWK_DATAFORM ORDER BY ID");
        });
        if (null == dataForms || dataForms.isEmpty()) return dataForms;


        List dataFormElements = dataQuery.exec(dataFormElementDBRowMapper,()->{
            return dataQuery.selectList(DataFormElement.class,
                    "SELECT * FROM FOWK_DATAFORM_ELEMENT ORDER BY DATAFORM_ID, SORT_CODE");
        });
        fillDataForm(dataForms, dataFormElements, (df, dfElement) -> df.addElement((DataFormElement) dfElement));


        List dataFormFilters = dataQuery.exec(dataFormFilterDBRowMapper,()->{
            return dataQuery.selectList(DataFormFilter.class,
                    "SELECT * FROM FOWK_DATAFORM_FILTER ORDER BY DATAFORM_ID, SORT_CODE");
        });

        fillDataForm(dataForms, dataFormFilters, (df, dfFilter) -> df.addFilter((DataFormFilter) dfFilter));
        return dataForms;
    }

    private void fillDataForm(List dataForms, List properties,
                              BiConsumer action) {
        DataForm currentDf;

        int lastJ = -1;
        for (int i = 0, j = lastJ + 1; i < dataForms.size(); ++i) {
            currentDf = dataForms.get(i);
            boolean dataformRefresh = true;
            for (; j < properties.size(); ++j) {
                String id = properties.get(j).getDataformId();
                if (null == id || id.isEmpty()) ;
                else if (id.equals(currentDf.getId())) {
                    lastJ = j;
                    action.accept(currentDf, properties.get(j));
                    dataformRefresh = false;
                } else {
                    if (!dataformRefresh)
                        break;
                }
            }
            if (j == properties.size() && j - lastJ > 1)
                j = lastJ + 1;
        }
    }


    @Override
    public List parseElementsFromTables(String dataFromId, String... tables) {
        List retList = ListKit.newArrayList();
        boolean singleTable = tables.length==1;
        for (String table : tables) {
            Connection connection = null;
            String tableName = table;
            String tableAlias = "";

            String tableExpr = StringKit.trim(table);
            String[] tableExprs = tableExpr.split("\\s+");
            if(tableExprs.length==2){
                tableName = tableExprs[0];
                tableAlias = tableExprs[1];
            }
            if(tableExprs.length==3){
                tableName = tableExprs[0];
                tableAlias = tableExprs[2];
            }

            try {
                connection = jdbcTemplate.getDataSource().getConnection();


                List dataList = SQLKit.getTableMeta(connection,tableName);

                //ORACLE使用SQL查询字段信息
                String dbName = connection.getMetaData().getDatabaseProductName();
                if("ORACLE".equalsIgnoreCase(dbName)){
                    String sql = "select  TABLE_NAME,COLUMN_NAME,COMMENTS from USER_COL_COMMENTS where TABLE_NAME = ?";
                    List> mapList = jdbcTemplate.queryForList(sql,tableName);
                    Map map = new HashMap<>();
                    mapList.forEach(row->{
                        map.put(row.get("COLUMN_NAME").toString(),row.get("COMMENTS").toString());
                    });

                    dataList.forEach(row -> {
                        String columnName = row.get("columnName",false).strValue();
                        String columnComment = row.get("comment",false).strValue();
                        String columnUpper = columnName.toUpperCase();
                        String comment = map.get(columnUpper);
                        if(StringKit.isNotBlank(comment)){
                            row.put("comment",comment);
                        }
                    });
                }
                fillElement(dataFromId,tableAlias,retList,dataList);

            } catch (SQLException e) {
                throw new DataFormException(e);
            } finally {
                IOKit.close(connection);
            }
        }

        //重新处理排序号
        DecimalFormat decimalFormat = new DecimalFormat("0000");
        if(retList.size()>100){
            decimalFormat = new DecimalFormat("00000");
        }
        for(int i=0;i retList,List dataList) throws SQLException {
        for(int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy