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

group.rober.dataform.mapper.impl.DataFormMapperJSONFileImpl Maven / Gradle / Ivy

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

import group.rober.dataform.DataFormConsts;
import group.rober.dataform.autoconfigure.DataFormProperties;
import group.rober.dataform.exception.DataFormException;
import group.rober.dataform.mapper.DataFormMapper;
import group.rober.dataform.model.DataForm;
import group.rober.dataform.model.DataFormElement;
import group.rober.dataform.service.DataFormPublicService;
import group.rober.runtime.autoconfigure.RuntimeProperties;
import group.rober.runtime.kit.FileKit;
import group.rober.runtime.kit.StringKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Paths;
import java.text.MessageFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by luyu on 2018/6/1.
 */
public class DataFormMapperJSONFileImpl implements DataFormMapper {

    @Autowired
    private RuntimeProperties runtimeProperties;
    @Autowired
    private DataFormPublicService dataFormPublicService;
    @Autowired
    DataFormProperties dataFormProperties;
    @Autowired
    DataFormMapperJSONFileImpl self;
    ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();

    @Override
    public void clearCacheAll() {

    }

    @Override
    public void clearCacheItem(String formId) {

    }

    @Override
    public boolean exists(String id) {
        File file = dataFormPublicService.getFileByDataFormId(id);
        if (file.exists())
            return Boolean.TRUE;
        return Boolean.FALSE;
    }


    public DataForm getDataForm(String id) {
        if(runtimeProperties.isProductionModel()){
            return self.getDataFormMybeCache(id);  //通过代理调用,可能会使用缓存
        }else{
            return this.getDataFormMybeCache(id);  //直接调用,一定不会使用缓存
        }
    }

    /**
     * 获取显示模板,可能会取缓存
     *
     * @param id id
     * @return DataForm
     */
    @Cacheable(value= DataFormConsts.CACHE_KEY,key="#id")
    public DataForm getDataFormMybeCache(String id){
        DataForm dataForm = null;
        File file = dataFormPublicService.getFileByDataFormId(id);
        if(file.exists()){
            dataForm = dataFormPublicService.getDataFormFromFile(file);
        }

        if(dataForm == null){
            String urlStr = MessageFormat.format("classpath:{0}/{1}.json"
                    ,dataFormProperties.getDataformDataClasspath()
                    ,id.replace("-","/"));
            URL url = null;
            try {
                url = ResourceUtils.getURL(urlStr);
            } catch (FileNotFoundException e) {
                throw new DataFormException("",e);
            }
            dataForm = dataFormPublicService.getDataFormFrom(url);
        }

        return dataForm;
    }

    @Override
    public DataForm getDataForm(String pack, String code) {
        String dataFormId = StringKit.format("{0}-{1}", pack, code);
        return self.getDataForm(dataFormId);
    }

    public boolean isIgnoreResource(String str){
        List ignoreList = dataFormProperties.getDataformIgnoreResources();
        if(ignoreList == null || ignoreList.isEmpty())return false;
        for(String igItem : ignoreList){
            if(str.endsWith(igItem))return true;
        }
        return false;
    }

    @Override
    public List getDataFormsByPack(String pack) {
        try {
            pack = null == pack ? "" : pack;
            String directory = dataFormPublicService.getDataformDataDirectory().getAbsolutePath();
            Resource[] resources;
            if (resourcePatternResolver.getResource("file:" + directory).exists()) {
                resources = resourcePatternResolver.getResources(
                        "file:" + Paths.get(directory, pack).toString() + "/**/*.json");
            } else {
                String path = dataFormProperties.getDataformDataClasspath().replace(".", "/");
                path = path.endsWith("/") ? path : path + "/";
                resources = resourcePatternResolver.getResources(path + "**/*.json");
            }
            if (null == resources || resources.length == 0) return Collections.emptyList();
            List result = new ArrayList<>(resources.length);
            for (Resource jsonResource : resources) {
                if(jsonResource == null||jsonResource.getURI() == null)continue;
                String resourcePath = jsonResource.getURI().getPath();
                if(StringKit.isBlank(resourcePath)) continue;
                if(isIgnoreResource(resourcePath)) continue;

                String dataformId = getDataformId(jsonResource.getURL().getFile());
                DataForm dataForm = this.getDataForm(dataformId);

                if(dataForm == null || StringKit.isBlank(dataForm.getId()))continue;

                result.add(dataForm);
            }
            return result;
        } catch (IOException ex) {
            throw new RuntimeException("error when get dataform from json", ex);
        }
    }

    private String getDataformId(String jsonResourcePath) {
        jsonResourcePath = jsonResourcePath.replace("\\", "/");
        String[] pathSeg = jsonResourcePath.split("/");
        int len = pathSeg.length;
        return pathSeg[len - 2] + "-" + pathSeg[len - 1].replace(".json", "");
    }


    @Override
    public List getAllDataForms() {
        return getDataFormsByPack(null);
    }

    @Override
    public int save(DataForm dataForm) {
        self.clearCacheItem(dataForm.getId());
        String dataFormId = StringKit.format("{0}-{1}", dataForm.getPack(), dataForm.getCode());
        File jsonFile = FileKit.getFile(dataFormPublicService.getDataformDataDirectory().getAbsolutePath(),dataForm.getPack(),dataForm.getCode() + ".json");
        dataForm.getElements().stream().forEach(dataFormElement -> {
            dataFormElement.setDataformId(dataFormId);
        });
        return dataFormPublicService.saveDataForm(dataForm,jsonFile);
    }

    @Override
    public int saveDataFormElement(DataFormElement element) {
        String dataFmId = element.getDataformId();
        self.clearCacheItem(dataFmId);
        File file = dataFormPublicService.getFileByDataFormId(dataFmId);

        DataForm dataForm = self.getDataForm(dataFmId);
        List dataFormElements = dataForm.getElements();
        dataFormElements = dataFormElements.stream().filter(dataFormElement ->  !dataFormElement.getCode().equals(element.getCode())).collect(Collectors.toList());
        dataFormElements.add(element);
        dataForm.setElements(dataFormElements);
        return dataFormPublicService.saveDataForm(dataForm,file);
    }

    @Override
    public int delete(String id) {
        Integer result = 1;
        File file = dataFormPublicService.getFileByDataFormId(id);
        if (!file.exists())
            return result;
        Boolean isDeleted = file.delete();
        if (isDeleted)
            return result;
        return 0;
    }

    @Override
    public int delete(String pack, String code) {
        String dataFormId = StringKit.format("{0}-{1}", pack, code);
        return delete(dataFormId);
    }

    @Override
    public int deleteAll() {
        File directory = dataFormPublicService.getDataformDataDirectory().getAbsoluteFile();
        File[] dirs = directory.listFiles();
        if (dirs == null)
            return 1;
        for (int i = 0; i < dirs.length; i++) {
            dirs[i].delete();
        }
        return 1;
    }



}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy