| | |
| | | /** |
| | | * 插入文件 |
| | | */ |
| | | public void insertFiles(MetaEntity entity, List<MetaFileEntity> list, List<TabMapperEntity> tabs) { |
| | | public void insertFiles(MetaEntity entity, List<MetaFileEntity> list, List<TabMapperEntity> tabList) { |
| | | try { |
| | | String temp = pathHelper.getConfig().getTempPath(); |
| | | String full = pathHelper.getUploadFullPath(); |
| | | String root = pathHelper.getConfig().getUploadPath(); |
| | | |
| | | for (MetaFileEntity mf : list) { |
| | | String filePath = getFilePath(temp, full, mf); |
| | | if (null == filePath) { |
| | | File file = new File(temp + File.separator + mf.getPath()); |
| | | File newFile = new File(root + File.separator + mf.getGuid()); |
| | | |
| | | String type = getType(mf.getName().toLowerCase()); |
| | | if (null == type) { |
| | | file.delete(); |
| | | continue; |
| | | } |
| | | |
| | | // 元数据 |
| | | MetaEntity me = createMetaEntity(entity, mf); |
| | | if (metaService.insert(me) < 1) { |
| | | // 获取表映射 |
| | | List<TabMapperEntity> tabs = getTabs(mf, tabList); |
| | | if (tabs.isEmpty()) { |
| | | file.delete(); |
| | | continue; |
| | | } |
| | | |
| | | // 元数据文件 |
| | | MetaFileEntity mfe = createMetaFileEntity(me, mf, filePath); |
| | | if (metaFileService.insert(mfe) < 1) { |
| | | continue; |
| | | MetaFileEntity old = metaFileService.selectByGuid(mf.getGuid()); |
| | | String filePath = null == old ? newFile.getPath() : old.getPath(); |
| | | |
| | | int count = 0; |
| | | for (TabMapperEntity tab : tabs) { |
| | | if (null == tab || StringHelper.isEmpty(tab.getEntity())) { |
| | | return; |
| | | } |
| | | switch (type) { |
| | | case "shp": |
| | | case "gdb": |
| | | case "mdb": |
| | | insertDb(type, temp, mf, tab); |
| | | break; |
| | | // case "xls": |
| | | default: |
| | | insertXls(temp, mf, tab); |
| | | break; |
| | | } |
| | | int rows = tab.getRows(); |
| | | if (rows == 0) { |
| | | continue; |
| | | } |
| | | |
| | | MetaEntity me = createMetaEntity(entity, mf); |
| | | // me.setTab,me.setRow |
| | | if (metaService.insert(me) == 0) { |
| | | continue; |
| | | } |
| | | |
| | | MetaFileEntity mfe = createMetaFileEntity(me, mf, filePath); |
| | | if (metaFileService.insert(mfe) == 0) { |
| | | metaService.delete(me.getId()); |
| | | continue; |
| | | } |
| | | |
| | | count += rows; |
| | | } |
| | | |
| | | // 入库 |
| | | switch (me.getType()) { |
| | | case "xls": |
| | | insertXls(root, mfe, tabs); |
| | | break; |
| | | case "shp": |
| | | case "gdb": |
| | | case "mdb": |
| | | insertDb(me.getType(), temp, mfe, tabs); |
| | | break; |
| | | default: |
| | | break; |
| | | if (count == 0 || null != old) { |
| | | file.delete(); |
| | | } else { |
| | | file.renameTo(newFile); |
| | | } |
| | | } |
| | | } catch (Exception ex) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取文件路径 |
| | | * 获取 Tabs |
| | | */ |
| | | private String getFilePath(String temp, String root, MetaFileEntity mf) { |
| | | // 移动文件 |
| | | File file = new File(temp + File.separator + mf.getPath()); |
| | | File newFile = new File(root + File.separator + mf.getGuid()); |
| | | private List<TabMapperEntity> getTabs(MetaFileEntity mfe, List<TabMapperEntity> tabs) { |
| | | List<TabMapperEntity> list = new ArrayList<>(); |
| | | for (TabMapperEntity tab : tabs) { |
| | | if (!tab.getFileName().equals(mfe.getName()) || StringHelper.isEmpty(tab.getEntity())) { |
| | | continue; |
| | | } |
| | | |
| | | // 文件类型 |
| | | String metaType = getType(mf.getName().toLowerCase()); |
| | | if (null == metaType) { |
| | | file.delete(); |
| | | return null; |
| | | list.add(tab); |
| | | } |
| | | |
| | | MetaFileEntity old = metaFileService.selectByGuid(mf.getGuid()); |
| | | return list; |
| | | } |
| | | |
| | | String filePath = null; |
| | | if (null == old) { |
| | | filePath = newFile.getPath(); |
| | | file.renameTo(newFile); |
| | | } else { |
| | | filePath = old.getPath(); |
| | | file.delete(); |
| | | /** |
| | | * 获取文件类型 |
| | | */ |
| | | private String getType(String name) { |
| | | if (name.contains(XLS)) { |
| | | return "xls"; |
| | | } |
| | | if (name.contains(MDB)) { |
| | | return "mdb"; |
| | | } |
| | | if (name.contains(SHP_ZIP)) { |
| | | return "shp"; |
| | | } |
| | | if (name.contains(GDB_ZIP)) { |
| | | return "gdb"; |
| | | } |
| | | |
| | | return filePath; |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取文件类型 |
| | | */ |
| | | private String getType(String name) { |
| | | if (name.contains(XLS)) { |
| | | return "xls"; |
| | | } |
| | | if (name.contains(MDB)) { |
| | | return "mdb"; |
| | | } |
| | | if (name.contains(SHP_ZIP)) { |
| | | return "shp"; |
| | | } |
| | | if (name.contains(GDB_ZIP)) { |
| | | return "gdb"; |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 插入Excel |
| | | */ |
| | | private void insertXls(String root, MetaFileEntity mfe, List<TabMapperEntity> tabs) { |
| | | TabMapperEntity tab = getTabEntity(mfe, tabs); |
| | | if (null == tab || StringHelper.isEmpty(tab.getEntity())) { |
| | | return; |
| | | } |
| | | |
| | | private void insertXls(String root, MetaFileEntity mf, TabMapperEntity tab) { |
| | | BasicMapper basicMapper = ClassHelper.getBasicMapper(tab.getEntity()); |
| | | if (null == basicMapper) { |
| | | return; |
| | |
| | | return; |
| | | } |
| | | |
| | | List<?> list = ExcelHelper.readExcel(clazz, root + File.separator + mfe.getPath()); |
| | | List<?> list = ExcelHelper.readExcel(clazz, root + File.separator + mf.getPath()); |
| | | if (list.isEmpty()) { |
| | | return; |
| | | } |
| | | setCreateInfo(list, mfe); |
| | | setCreateInfo(list, mf); |
| | | |
| | | int rows = 0; |
| | | for (int i = 0, c = list.size(); i < c; i++) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取表映射实体类 |
| | | */ |
| | | private TabMapperEntity getTabEntity(MetaFileEntity mfe, List<TabMapperEntity> tabs) { |
| | | for (TabMapperEntity tab : tabs) { |
| | | if (tab.getFileName().equals(mfe.getName())) { |
| | | return tab; |
| | | } |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 设置创建信息 |
| | | */ |
| | | private <T> void setCreateInfo(List<T> list, MetaFileEntity mfe) { |
| | | private <T> void setCreateInfo(List<T> list, MetaFileEntity mf) { |
| | | try { |
| | | for (T t : list) { |
| | | Field cuField = t.getClass().getDeclaredField("createuser"); |
| | | cuField.setAccessible(true); |
| | | cuField.set(t, mfe.getCreateUser()); |
| | | cuField.set(t, mf.getCreateUser()); |
| | | |
| | | Field ctField = t.getClass().getDeclaredField("createtime"); |
| | | ctField.setAccessible(true); |
| | | ctField.set(t, mfe.getCreateTime()); |
| | | ctField.set(t, mf.getCreateTime()); |
| | | } |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | |
| | | /** |
| | | * 插入DB |
| | | */ |
| | | private void insertDb(String type, String root, MetaFileEntity mfe, List<TabMapperEntity> tabs) { |
| | | for (TabMapperEntity tab : tabs) { |
| | | if (!tab.getFileName().equals(mfe.getName()) || StringHelper.isEmpty(tab.getEntity())) { |
| | | continue; |
| | | } |
| | | |
| | | BasicMapper basicMapper = ClassHelper.getBasicMapper(tab.getEntity()); |
| | | if (null == basicMapper) { |
| | | continue; |
| | | } |
| | | |
| | | String className = ClassHelper.getClassName(basicMapper); |
| | | Class clazz = ClassHelper.getEntityClass(className); |
| | | if (null == clazz) { |
| | | continue; |
| | | } |
| | | |
| | | String filePath = StringEscapeUtils.escapeJava(root + File.separator + tab.getSubPath()); |
| | | List<?> list = null; |
| | | switch (type) { |
| | | case "shp": |
| | | list = ShpHelper.readData(clazz, filePath); |
| | | break; |
| | | case "gdb": |
| | | list = GdbHelper.readData(clazz, filePath, tab.getTab()); |
| | | break; |
| | | case "mdb": |
| | | list = MdbHelper.readData(clazz, filePath, tab.getTab()); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | if (null == list || list.isEmpty()) { |
| | | continue; |
| | | } |
| | | setCreateInfo(list, mfe); |
| | | |
| | | Integer rows = basicMapper.insertBatch(list); |
| | | |
| | | tab.setRows(rows); |
| | | private void insertDb(String type, String root, MetaFileEntity mf, TabMapperEntity tab) { |
| | | BasicMapper basicMapper = ClassHelper.getBasicMapper(tab.getEntity()); |
| | | if (null == basicMapper) { |
| | | return; |
| | | } |
| | | |
| | | String className = ClassHelper.getClassName(basicMapper); |
| | | Class clazz = ClassHelper.getEntityClass(className); |
| | | if (null == clazz) { |
| | | return; |
| | | } |
| | | |
| | | String filePath = StringEscapeUtils.escapeJava(root + File.separator + tab.getSubPath()); |
| | | List<?> list = null; |
| | | switch (type) { |
| | | case "shp": |
| | | list = ShpHelper.readData(clazz, filePath); |
| | | break; |
| | | case "gdb": |
| | | list = GdbHelper.readData(clazz, filePath, tab.getTab()); |
| | | break; |
| | | case "mdb": |
| | | list = MdbHelper.readData(clazz, filePath, tab.getTab()); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | if (null == list || list.isEmpty()) { |
| | | return; |
| | | } |
| | | |
| | | setCreateInfo(list, mf); |
| | | int rows = basicMapper.insertBatch(list); |
| | | |
| | | tab.setRows(rows); |
| | | } |
| | | |
| | | /** |