Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.hframework.controller.ext.DbGeneratorController Maven / Gradle / Ivy
package com.hframework.controller.ext;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.hframe.domain.model.*;
import com.hframe.service.interfaces.*;
import com.hframework.base.service.CommonDataService;
import com.hframework.base.service.DataSetLoaderService;
import com.hframework.base.service.ModelLoaderService;
import com.hframework.beans.controller.ResultCode;
import com.hframework.beans.controller.ResultData;
import com.hframework.common.util.collect.CollectionUtils;
import com.hframework.common.util.collect.bean.Grouper;
import com.hframework.common.util.collect.bean.Merger;
import com.hframework.common.util.StringUtils;
import com.hframework.common.util.message.XmlUtils;
import com.hframework.web.controller.DefaultController;
import com.hframework.common.springext.datasource.DataSourceContextHolder;
import com.hframework.generator.util.CreatorUtil;
import com.hframework.generator.web.container.HfModelContainer;
import com.hframework.generator.web.container.HfModelContainerUtil;
import com.hframework.generator.web.sql.SqlGeneratorUtil;
import com.hframework.generator.web.sql.reverse.SQLParseUtil;
import com.hframework.web.context.WebContext;
import com.hframework.web.config.bean.Program;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by zhangquanhong on 2016/12/11.
*/
@Controller
@RequestMapping(value = "/extend")
public class DbGeneratorController extends ExtBaseController {
private static final Logger logger = LoggerFactory.getLogger(DbGeneratorController.class);
@Resource
private DataSetLoaderService dataSetLoaderService;
@Resource
private ModelLoaderService modelLoaderService;
@Resource
private CommonDataService commonDataService;
@Resource
private IHfpmProgramSV hfpmProgramSV;
@Resource
private IHfsecMenuSV hfsecMenuSV;
@Resource
private IHfsecUserSV hfsecUserSV;
@Resource
private IHfpmModuleSV hfpmModuleSV;
@Resource
private IHfcfgDbConnectSV iHfcfgDbConnectSV;
@Resource
private IHfpmProgramCfgSV iHfpmProgramCfgSV;
@Resource
private IHfusEntityStoreSV hfusEntityStoreSV;
@Resource
private IHfsecMenuSV iHfsecMenuSV;
@Resource
private IHfmdEntitySV iHfmdEntitySV;
@Resource
private IHfmdEntityAttrSV hfmdEntityAttrSV;
private static final String INSERT_HFSEC_USER_SQL =
"insert into hfsec_user(account,password, hfsec_user_name, avatar, status,hfsec_organize_id) values (\"{0}\",\"{1}\",\"{2}\",\"{3}\",1,1)";
private static final String SELECT_HFSEC_ROLE_SQL = "SELECT hfsec_role_id,hfsec_role_code,hfsec_role_name,hfsec_role_type,status FROM hfsec_role;";
private static final String SELECT_HFSEC_ROLE_2_SQL = "SELECT 2 AS hfsec_role_id," +
"'general_operator' AS hfsec_role_code,'普通操作员' AS hfsec_role_name," +
"1 AS hfsec_role_type,1 AS status " +
"UNION SELECT 1,'super_operator','超级管理员',1,1 " +
"UNION SELECT 3,'report_viewer','报表人员',2,1;";
private static final String SELECT_HFSEC_ORGANIZE_SQL = "SELECT hfsec_organize_id,hfsec_organize_code,hfsec_organize_name,hfsec_organize_type,hfsec_organize_level,status,parent_hfsec_organize_id FROM hfsec_organize;";
private static final String SELECT_HFSEC_ORGANIZE_2_SQL = " SELECT 1 AS hfsec_organize_id,'ROOT' AS hfsec_organize_code,'集团' AS hfsec_organize_name,1 AS hfsec_organize_type,1 AS hfsec_organize_level,1 AS status,-1 AS parent_hfsec_organize_id;";
private static final String SELECT_HFSEC_USER_AUTH_SQL = "SELECT hfsec_user_authorize_id, hfsec_user_id, hfsec_organize_id, hfsec_role_id, status FROM hfsec_user_authorize;";
private static final String SELECT_HFSEC_USER_AUTH_2_SQL = "SELECT 1 AS hfsec_user_authorize_id, 1 AS hfsec_user_id, 1 AS hfsec_organize_id, 1 AS hfsec_role_id, 1 AS status;";
private static final String SELECT_HFMD_ENUM_SQL = "SELECT hfmd_enum_value,CONCAT('',hfmd_enum_text,' ') as hfmd_enum_text," +
"hfmd_enum_desc,is_default,pri,hfmd_enum_class_id,hfmd_enum_class_code,del_flag FROM hfmd_enum t " +
"WHERE EXISTS (SELECT 1 FROM hfmd_enum_class c WHERE c.hfmd_enum_class_id = t.hfmd_enum_class_id)";
private static final String SELECT_HFMD_ENUM_CLASS_SQL = "SELECT hfmd_enum_class_id,hfmd_enum_class_name,CONCAT('',hfmd_enum_class_code,' ') " +
"as hfmd_enum_class_code,hfmd_enum_class_desc,del_flag FROM hfmd_enum_class t";
/**
* 重新加载(全量)
* @return
*/
@RequestMapping(value = "/model_diff.json")
@ResponseBody
public ResultData getModelDiff(HttpServletRequest request){
logger.debug("request : {}");
try{
Map pageContextParams = DefaultController.getPageContextParams(request);
WebContext.putContext(DefaultController.getPageContextRealyParams(pageContextParams));
Map pageFlowParams = WebContext.get(HashMap.class.getName());
String companyCode = "hframe";
String programCode = "hframe";
Long programId = -1L;
String programeName = "框架";
String moduleCode = "hframe";
String moduleName = "框架";
if(pageFlowParams != null) {
if(pageFlowParams.containsKey("hfpmProgramId") && StringUtils.isNotBlank(pageFlowParams.get("hfpmProgramId"))) {
HfpmProgram program = hfpmProgramSV.getHfpmProgramByPK(Long.parseLong(pageFlowParams.get("hfpmProgramId")));
programCode = program.getHfpmProgramCode();
programId = program.getHfpmProgramId();
programeName = program.getHfpmProgramName();
}
if(pageFlowParams.containsKey("hfpmModuleId") && StringUtils.isNotBlank(pageFlowParams.get("hfpmModuleId"))) {
HfpmModule module = hfpmModuleSV.getHfpmModuleByPK(Long.parseLong(pageFlowParams.get("hfpmModuleId")));
moduleCode = module.getHfpmModuleCode();
moduleName = module.getHfpmModuleName();
}
}
// final String hfcfgDbConnectId = request.getParameter("hfcfgDbConnectId");
String configSqlPath= modelLoaderService.load(companyCode, programCode, programeName, moduleCode, moduleName);
HfModelContainer targetModelContainer = SQLParseUtil.parseModelContainerFromSQLFile(
configSqlPath, programCode, programeName, moduleCode, moduleName);
startDynamicDataSource(pageFlowParams);
String dbSqlPath = SqlGeneratorUtil.createSqlFile(companyCode, programCode);
String projectBasePath = CreatorUtil.getTargetProjectBasePath(companyCode,
programCode, null);
Program program = XmlUtils.readValueFromAbsoluteFilePath(projectBasePath + "/basic/src/main/resources/program/program.xml", Program.class);
HfsecUser_Example example = new HfsecUser_Example();
example.createCriteria().andAccountEqualTo(program.getSuperManager().getCode());
int hfsecUserCountByExample = 0 ;
try{
hfsecUserCountByExample = hfsecUserSV.getHfsecUserCountByExample(example);
}catch(Exception e){}
DataSourceContextHolder.clear();
HfModelContainer curDbModelContainer = SQLParseUtil.parseModelContainerFromSQLFile(
dbSqlPath, programCode, programeName, moduleCode, moduleName);
final HfModelContainer[] resultModelContainers =
HfModelContainerUtil.mergerModelContainer(curDbModelContainer, targetModelContainer);
final List result = HfModelContainerUtil.getSql(resultModelContainers[0], resultModelContainers[1], false, true);
final List> sqls = new ArrayList>();
for (final String sql : result) {
sqls.add(new HashMap(){{
put("sql",sql.replaceAll("\n", ""));
}});
}
//添加枚举值变更sql语句
List enumSqls = getEnumSql(programId, pageFlowParams);
for (final String enumSql : enumSqls) {
sqls.add(new HashMap() {{
put("sql", enumSql);
}});
}
if(hfsecUserCountByExample == 0) {
final String sql = MessageFormat.format(INSERT_HFSEC_USER_SQL, program.getSuperManager().getCode(),
program.getSuperManager().getPassword(), program.getSuperManager().getName(), "http://pic.hanhande.com/files/141127/1283574_094432_8946.jpg");
sqls.add(new HashMap() {{
put("sql", sql);
}});
}
//这部内容已由具体的连带规则实现,无需再次买点处理
// generateDefaultDataSetIfNotExists(resultModelContainers, programCode, programeName, moduleCode, moduleName);
// WebContext.putContext("hfcfgDbConnectId", hfcfgDbConnectId);
return ResultData.success(new HashMap(){{
put("NewEntityId", new HashMap() {{
put("list", Lists.newArrayList(resultModelContainers[0].getEntityMap().values()));
}});
put("NewEntityAttrId", new HashMap() {{
put("list", Lists.newArrayList(resultModelContainers[0].getEntityAttrMap().values()));
}});
put("ModEntityId", new HashMap() {{
put("list", Lists.newArrayList(resultModelContainers[1].getEntityMap().values()));
}});
put("ModEntityAttrId", new HashMap() {{
put("list", Lists.newArrayList(resultModelContainers[1].getEntityAttrMap().values()));
}});
put("SelectDbConnector", new HashMap() {{
put("data", new HashMap(){{
put("hfcfgDbConnectId",null);
}});
}});
put("sql", new HashMap() {{
put("list", sqls);
}});
}});
}catch (Exception e) {
logger.error("error : ", e);
return ResultData.error(ResultCode.ERROR);
}finally {
DataSourceContextHolder.clear();
}
}
private List getEnumSql(final Long finalProgramId, Map pageFlowParams) throws Exception {
List sqls = new ArrayList();
startDynamicDataSource(pageFlowParams);
List> curEnumList = getDynamicTableDataSome(SELECT_HFMD_ENUM_SQL);
List> curEnumClassList = getDynamicTableDataSome(SELECT_HFMD_ENUM_CLASS_SQL);
List> curRoleList = getDynamicTableDataSome(SELECT_HFSEC_ROLE_SQL);
List> curUserAuthList = getDynamicTableDataSome(SELECT_HFSEC_USER_AUTH_SQL);
List> curOrganizeList = getDynamicTableDataSome(SELECT_HFSEC_ORGANIZE_SQL);
DataSourceContextHolder.clear();
List> enumList = getDynamicTableDataSome(SELECT_HFMD_ENUM_SQL + "and (t.hfpm_program_id IN (\" + finalProgramId + \") OR hfpm_program_id IS NULL )");
List> enumClassList = getDynamicTableDataSome(SELECT_HFMD_ENUM_CLASS_SQL + " where t.hfpm_program_id IN (" + finalProgramId + ") OR hfpm_program_id IS NULL");
List> roleList = getDynamicTableDataSome(SELECT_HFSEC_ROLE_2_SQL);
List> userAuthList = getDynamicTableDataSome(SELECT_HFSEC_USER_AUTH_2_SQL);
List> organizeList = getDynamicTableDataSome(SELECT_HFSEC_ORGANIZE_2_SQL);
CollectionUtils.remove(enumList, curEnumList, new Merger>() {
public K getKey(Map objectMap) {
return (K) (String.valueOf(objectMap.get("hfmd_enum_class_id")) + String.valueOf(objectMap.get("hfmd_enum_value")));
}
public K groupKey(Map objectMap) {
return (K) (String.valueOf(objectMap.get("hfmd_enum_class_id")) + String.valueOf(objectMap.get("hfmd_enum_value")));
}
});
CollectionUtils.remove(enumClassList, curEnumClassList, new Merger>() {
public K getKey(Map objectMap) {
return (K) (String.valueOf(objectMap.get("hfmd_enum_class_id")) + String.valueOf(objectMap.get("hfmd_enum_class_code")));
}
public K groupKey(Map objectMap) {
return (K) (String.valueOf(objectMap.get("hfmd_enum_class_id")) + String.valueOf(objectMap.get("hfmd_enum_class_code")));
}
});
CollectionUtils.remove(roleList, curRoleList, new Merger>() {
public K getKey(Map objectMap) {
return (K) String.valueOf(objectMap.get("hfsec_role_id"));
}
public K groupKey(Map objectMap) {
return (K) String.valueOf(objectMap.get("hfsec_role_id"));
}
});
CollectionUtils.remove(organizeList, curOrganizeList, new Merger>() {
public K getKey(Map objectMap) {
return (K) String.valueOf(objectMap.get("hfsec_organize_id"));
}
public K groupKey(Map objectMap) {
return (K) String.valueOf(objectMap.get("hfsec_organize_id"));
}
});
CollectionUtils.remove(userAuthList, curUserAuthList, new Merger>() {
public K getKey(Map objectMap) {
return (K) String.valueOf(objectMap.get("hfsec_user_authorize_id"));
}
public K groupKey(Map objectMap) {
return (K) String.valueOf(objectMap.get("hfsec_user_authorize_id"));
}
});
List roleSqls = HfModelContainerUtil.getSql(roleList, "hfsec_role", false);
for (String roleSql : roleSqls) {
sqls.add(roleSql + ";");
}
List organizeSqls = HfModelContainerUtil.getSql(organizeList, "hfsec_organize", false);
for (String organizeSql : organizeSqls) {
sqls.add(organizeSql + ";");
}
List userAuthorizeSqls = HfModelContainerUtil.getSql(userAuthList, "hfsec_user_authorize", false);
for (String userAuthorizeSql : userAuthorizeSqls) {
sqls.add(userAuthorizeSql + ";");
}
Map>> enumClassMap = CollectionUtils.group(enumList, new Grouper>() {
public K groupKey(Map objectMap) {
return (K) objectMap.get("hfmd_enum_class_id");
}
});
for (Map enumClass : enumClassList) {
List hfmd_enum_class = HfModelContainerUtil.getSql(Lists.newArrayList(enumClass), "hfmd_enum_class", false);
String mergeSql = "";
for (final String sql : hfmd_enum_class) {
mergeSql += (sql + ";");
}
if(enumClassMap.containsKey(enumClass.get("hfmd_enum_class_id"))) {
List> enums = enumClassMap.remove(enumClass.get("hfmd_enum_class_id"));
List hfmd_enum = HfModelContainerUtil.getSql(enums, "hfmd_enum", false);
for (final String sql : hfmd_enum) {
mergeSql += (" " + sql + ";");
}
}
sqls.add(mergeSql);
}
for (List> enumInfo : enumClassMap.values()) {
List hfmd_enum = HfModelContainerUtil.getSql(enumInfo, "hfmd_enum", false);
for (final String sql : hfmd_enum) {
sqls.add(sql + ";");
}
}
return sqls;
}
private List> getDynamicTableDataSome(final String sql) {
List> list = null;
try{
list = commonDataService.selectDynamicTableDataSome(new HashMap() {{
put("sql", sql);
}});
}catch (Exception e){};
return list;
}
/**
* 重新加载(全量)
* @return
*/
@RequestMapping(value = "/model_execute.json")
@ResponseBody
public ResultData modelExecute(@RequestParam(value="checkIds[]",required=false) String[] sqls, HttpServletRequest request){
if(sqls != null && sqls.length > 0 && !sqls[0].endsWith(";")) {
sqls = new String[]{Joiner.on(",").join(sqls)};
}
logger.debug("request : {}", sqls);
List result = new ArrayList();
for (String sql : sqls) {
if(sql.contains("insert into")) {
String[] split = sql.split(";[ ]*insert into");
for (String s : split) {
if(StringUtils.isNotBlank(s)) {
result.add((s.startsWith("insert into ") ? "" : "insert into ") + (s.endsWith(";") ? s : (s + ";")));
}
}
}else {
result.add(sql);
}
}
try{
Map pageContextParams = DefaultController.getPageContextParams(request);
WebContext.putContext(DefaultController.getPageContextRealyParams(pageContextParams));
Map pageFlowParams = WebContext.get(HashMap.class.getName());
startDynamicDataSource(pageFlowParams);
commonDataService.executeDBStructChange(result);
return ResultData.success();
}catch (Exception e) {
logger.error("error : ", e);
return ResultData.error(ResultCode.ERROR);
}finally {
DataSourceContextHolder.clear();
}
}
}