package com.product.data.sync.controller; import com.alibaba.fastjson.JSONArray; import com.product.common.lang.StringUtils; import com.product.core.config.CoreConst; import com.product.core.controller.support.AbstractBaseController; import com.product.core.entity.DataTableEntity; import com.product.core.entity.FieldSetEntity; import com.product.core.entity.RequestParameterEntity; import com.product.core.exception.BaseException; import com.product.core.spring.context.SpringMVCContextHolder; import com.product.data.sync.config.CmnConst; import com.product.data.sync.config.SystemCode; import com.product.data.sync.service.ConnectionConfigurationService; import com.product.data.sync.service.ide.IConnectionConfigurationService; import com.product.data.sync.util.ResultInfo; import com.product.data.sync.util.ScheduledTaskExecution; import com.product.module.sys.version.ApiVersion; import com.product.util.BaseUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import java.sql.Connection; /** * Copyright LX-BASE * * @Title: ConnectionConfigurationController * @Project: LX-BASE-SERVER * @Date: 2021-08-10 11:30:48 * @Author: luoxin * @Description: 数据库连接配置Constoller * */ @RequestMapping("/api/connectionConfiguration") @RestController public class ConnectionConfigurationController extends AbstractBaseController { @Autowired public ConnectionConfigurationService connectionConfigurationService; @Autowired public ScheduledTaskExecution scheduledTaskExecution; /** * 保存数据库连接配置 * @param request * @return */ @RequestMapping(value = "/save-connectConfigure/{version}", method = RequestMethod.POST) public String saveConnectionConfiguration(HttpServletRequest request) { try { //获取参数 FieldSetEntity fse=null; Object bean=request.getAttribute(CoreConst.API_POST_REQUEST_DATA); if(bean != null){ RequestParameterEntity reqp=(RequestParameterEntity)bean; fse = reqp.getFormData(); } //判断参数是否为空 if(bean == null || fse == null) { SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); } //判断表名是否正常 if (!CmnConst.PRODUCT_SYS_DATABASE_CONNECTION_CONFIG.equals(fse.getTableName())) { SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText()); return this.error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText()); } IConnectionConfigurationService service = (IConnectionConfigurationService) getProxyInstance(connectionConfigurationService); String uuid = service.saveConnectionConfiguration(fse); if (!BaseUtil.strIsNull(uuid)) { return OK_Add(uuid); } SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_SAVE_CONNECT_CONFIGURE_FAIL.getValue(), SystemCode.SYSTEM_SAVE_CONNECT_CONFIGURE_FAIL.getText()); return error(SystemCode.SYSTEM_SAVE_CONNECT_CONFIGURE_FAIL.getValue(), SystemCode.SYSTEM_SAVE_CONNECT_CONFIGURE_FAIL.getText()); }catch(BaseException e) { SpringMVCContextHolder.getSystemLogger().error(e); return this.error(e); }catch (Exception e) { SpringMVCContextHolder.getSystemLogger().error(e); return this.error(SystemCode.SYSTEM_SAVE_CONNECT_CONFIGURE_FAIL.getValue(), SystemCode.SYSTEM_SAVE_CONNECT_CONFIGURE_FAIL.getText()); } } /** * 删除数据库连接配置 * @param request * @return */ @RequestMapping(value = "/delete-connectConfigure/{version}", method = RequestMethod.POST) @ApiVersion(1) public String delConnectionConfiguration(HttpServletRequest request) { try { //获取参数 FieldSetEntity fse=null; Object bean=request.getAttribute(CoreConst.API_POST_REQUEST_DATA); if(bean != null) { RequestParameterEntity reqp=(RequestParameterEntity)bean; fse = reqp.getFormData(); } //判断参数是否为空 if(bean == null || fse == null) { SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); } //判断表名是否正常 if (!CmnConst.PRODUCT_SYS_DATABASE_CONNECTION_CONFIG.equals(fse.getTableName())) { SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText()); return this.error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText()); } //判断uuid是否为空 if (StringUtils.isEmpty(fse.getString("uuid"))) { SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_FORM_COUNT.getValue(), SystemCode.SYSTEM_FORM_COUNT.getText()); return this.error(SystemCode.SYSTEM_FORM_COUNT.getValue(), SystemCode.SYSTEM_FORM_COUNT.getText()); } IConnectionConfigurationService service = (IConnectionConfigurationService) getProxyInstance(connectionConfigurationService); boolean success=service.delConnectionConfiguration(fse); if(success) { return OK(); } SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_DELETE_CONNECT_CONFIGURE_FAIL.getValue(),SystemCode.SYSTEM_DELETE_CONNECT_CONFIGURE_FAIL.getText()); return error(SystemCode.SYSTEM_DELETE_CONNECT_CONFIGURE_FAIL.getValue(),SystemCode.SYSTEM_DELETE_CONNECT_CONFIGURE_FAIL.getText()); }catch(BaseException e) { SpringMVCContextHolder.getSystemLogger().error(e); return this.error(e); }catch (Exception e) { SpringMVCContextHolder.getSystemLogger().error(e); return this.error(SystemCode.SYSTEM_DELETE_CONNECT_CONFIGURE_FAIL.getValue(),SystemCode.SYSTEM_DELETE_CONNECT_CONFIGURE_FAIL.getText()); } } /** * 获取数据库连接配置列表 * @param request * @return */ @RequestMapping(value = "/list-connectConfigure/{version}", method = RequestMethod.POST) public String listConnectionConfiguration(HttpServletRequest request) { try { // 获取参数 FieldSetEntity fse = null; Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA); if (bean != null) { RequestParameterEntity reqp = (RequestParameterEntity) bean; fse = reqp.getFormData(); } // 判断参数是否为空 if (bean == null || fse == null) { SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText()); return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); } // 判断表名是否正常 if (!CmnConst.PRODUCT_SYS_DATABASE_CONNECTION_CONFIG.equals(fse.getTableName())) { SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText()); return this.error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText()); } // 判断分页参数 if (fse.getString(CmnConst.PAGESIZE) == null || fse.getString(CmnConst.CPAGE) == null) { return this.error(SystemCode.SYSTEM_CPAGES_NOT_NULL.getValue(), SystemCode.SYSTEM_CPAGES_NOT_NULL.getText()); } DataTableEntity dt = connectionConfigurationService.listConnectionConfiguration(fse); return OK_List(dt); } catch (BaseException e) { e.printStackTrace(); SpringMVCContextHolder.getSystemLogger().error(e); return this.error(e); } catch (Exception e) { e.printStackTrace(); SpringMVCContextHolder.getSystemLogger().error(e); return this.error(SystemCode.SYSTEM_LIST_CONNECT_CONFIGURE_FAIL.getValue(), SystemCode.SYSTEM_LIST_CONNECT_CONFIGURE_FAIL.getText()); } } /** * 查询数据库连接配置详情 * @param request * @return */ @RequestMapping(value = "/get-connectConfigure/{version}", method = RequestMethod.POST) @ApiVersion(1) public String getConnectionConfiguration(HttpServletRequest request) { try { // 获取参数 FieldSetEntity fse = null; Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA); if (bean != null) { RequestParameterEntity reqp = (RequestParameterEntity) bean; fse = reqp.getFormData(); } // 判断参数是否为空 if (bean == null || fse == null) { SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); } // 判断表名是否正常 if (!CmnConst.PRODUCT_SYS_DATABASE_CONNECTION_CONFIG.equals(fse.getTableName())) { SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText()); return this.error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText()); } // 判断uuid是否为空 if (StringUtils.isEmpty(fse.getString("uuid"))) { SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText()); return this.error(SystemCode.SYSTEM_FORM_COUNT.getValue(), SystemCode.SYSTEM_FORM_COUNT.getText()); } FieldSetEntity fs = connectionConfigurationService.getConnectionConfiguration(fse); return OK_List(fs); } catch (BaseException e) { e.printStackTrace(); SpringMVCContextHolder.getSystemLogger().error(e); return this.error(e); } catch (Exception e) { e.printStackTrace(); SpringMVCContextHolder.getSystemLogger().error(e); return this.error(SystemCode.SYSTEM_GET_CONNECT_CONFIGURE_FAIL.getValue(), SystemCode.SYSTEM_GET_CONNECT_CONFIGURE_FAIL.getText()); } } /** * 运行数据库连接配置 * @param request * @return */ @RequestMapping(value = "/run-connectConfigure/{version}", method = RequestMethod.POST) @ApiVersion(1) public String runConnectionConfiguration(HttpServletRequest request) { try { // 获取参数 FieldSetEntity fse = null; Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA); if (bean != null) { RequestParameterEntity reqp = (RequestParameterEntity) bean; fse = reqp.getFormData(); } // 判断参数是否为空 if (bean == null || fse == null) { SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); } // 判断表名是否正常 if (!CmnConst.PRODUCT_SYS_DATABASE_CONNECTION_CONFIG.equals(fse.getTableName())) { SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText()); return this.error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText()); } Connection conn = connectionConfigurationService.runConnectionConfiguration(fse); if(conn != null){ conn.close(); return OK(); } return this.error(SystemCode.SYSTEM_RUN_CONNECT_CONFIGURE_FAIL.getValue(), SystemCode.SYSTEM_RUN_CONNECT_CONFIGURE_FAIL.getText()); } catch (BaseException e) { e.printStackTrace(); SpringMVCContextHolder.getSystemLogger().error(e); return this.error(e); } catch (Exception e) { e.printStackTrace(); SpringMVCContextHolder.getSystemLogger().error(e); return this.error(SystemCode.SYSTEM_RUN_CONNECT_CONFIGURE_FAIL.getValue(), SystemCode.SYSTEM_RUN_CONNECT_CONFIGURE_FAIL.getText()); } } /** * 获取并保存字段 * @param request * @return */ @RequestMapping(value = "/saveField/{version}", method = RequestMethod.POST) @ApiVersion(1) public String saveField(HttpServletRequest request) { try { // 获取参数 FieldSetEntity fse = null; Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA); if (bean != null) { RequestParameterEntity reqp = (RequestParameterEntity) bean; fse = reqp.getFormData(); } // 判断参数是否为空 if (bean == null || fse == null) { SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); } // 判断表名是否正常 if (!CmnConst.PRODUCT_SYS_DATABASE_CONNECTION_CONFIG.equals(fse.getTableName())) { SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText()); return this.error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText()); } if(connectionConfigurationService.saveField(fse)){ return OK(); } return this.error(SystemCode.SYSTEM_RUN_CONNECT_CONFIGURE_FAIL.getValue(), SystemCode.SYSTEM_RUN_CONNECT_CONFIGURE_FAIL.getText()); } catch (BaseException e) { e.printStackTrace(); SpringMVCContextHolder.getSystemLogger().error(e); return this.error(e); } catch (Exception e) { e.printStackTrace(); SpringMVCContextHolder.getSystemLogger().error(e); return this.error(SystemCode.SYSTEM_RUN_CONNECT_CONFIGURE_FAIL.getValue(), SystemCode.SYSTEM_RUN_CONNECT_CONFIGURE_FAIL.getText()); } } /** * 获取数据同步配置树 * @param request * @return */ @RequestMapping(value = "/tree/{version}", method = RequestMethod.POST) @ApiVersion(1) public String getTree(HttpServletRequest request) { try { // 获取参数 FieldSetEntity fse = null; Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA); if (bean != null) { RequestParameterEntity reqp = (RequestParameterEntity) bean; fse = reqp.getFormData(); } // 判断参数是否为空 if (bean == null || fse == null) { SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); return this.error(SystemCode.SYSTEM_FORM_NODATA.getValue(), SystemCode.SYSTEM_FORM_NODATA.getText()); } // 判断表名是否正常 if (!CmnConst.PRODUCT_SYS_DATABASE_CONNECTION_CONFIG.equals(fse.getTableName())) { SpringMVCContextHolder.getSystemLogger().error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText()); return this.error(SystemCode.SYSTEM_TABLE_NODATA.getValue(), SystemCode.SYSTEM_TABLE_NODATA.getText()); } JSONArray array = connectionConfigurationService.getSyncTree(); return ResultInfo.success(array); } catch (BaseException e) { e.printStackTrace(); SpringMVCContextHolder.getSystemLogger().error(e); return this.error(e); } catch (Exception e) { e.printStackTrace(); SpringMVCContextHolder.getSystemLogger().error(e); return this.error(SystemCode.SYSTEM_RUN_CONNECT_CONFIGURE_FAIL.getValue(), SystemCode.SYSTEM_RUN_CONNECT_CONFIGURE_FAIL.getText()); } } }