package com.qiwu.chatbotbackend.actiongenerate.servlet; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.qiwu.chatbotbackend.actiongenerate.DB.EngineUtils; import com.qiwu.chatbotbackend.actiongenerate.entity.*; import com.qiwu.chatbotbackend.actiongenerate.utils.ActionUtils; import com.qiwu.chatbotbackend.actiongenerate.utils.ConfigTermUtils; import com.qiwu.chatbotbackend.actiongenerate.utils.VariableUtils; import com.qiwu.chatbotbackend.db.SqlHelper; import com.qiwu.chatbotbackend.entity.ConfigTermText; import com.qiwu.chatbotbackend.entity.UserInfo; import com.qiwu.chatbotbackend.processing.SimpleUtil; import com.qiwu.chatbotbackend.processing.UserInfoController; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.BufferedReader; import java.io.IOException; import java.io.PrintWriter; import java.io.StringReader; import java.sql.SQLException; import java.util.*; import static com.qiwu.chatbotbackend.Config.Config.*; @WebServlet(name = "RecoveryServlet") public class RecoveryServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context = request.getServletContext(); //读取并解析json数据 String operationType = "", botName = "", conditionName = "", botAccount; String jsonStr = SimpleUtil.readJSONString(request); String auth = request.getHeader("authorization"); JSONObject responseJSONObject = new JSONObject(), configTermJson, conditionJson; JSONArray conditionIdPair = new JSONArray(), idPair; Boolean flag = true; Integer botId = 0, newCategoryId = 0, len, id; IntervalStatus intervalStatus = new IntervalStatus(1, ""); StringBuffer completeConfigText = new StringBuffer(); ActionPostElem ape = null; System.out.println(jsonStr); if (jsonStr != null && !jsonStr.equals("")) { JSONObject receiveParameters = JSONObject.parseObject(jsonStr); receiveParameters.put("Authorization", auth); JSONObject data = receiveParameters.getJSONObject("data"); botId = receiveParameters.getInteger("botId"); botAccount = receiveParameters.getString("botName"); botName = receiveParameters.getString("botName_yhm"); SqlHelper helper = null; EngineUtils engine = null; try { Map idReplaceMap = new HashMap(); ConfigTermText configTermText = null; Set variableNameSet1, variableNameSet2, conditionNameSet1, conditionNameSet2; List actionTextList = new ArrayList(); //List originConditionList = null; //List<> List categoryList = data.getJSONArray("categoryList").toJavaList(Category.class); List originConditionList = null, conditionList = data.getJSONArray("conditionList").toJavaList(Condition.class); List configTermList = data.getJSONArray("configTermList").toJavaList(ConfigTerm.class); List originVariableList, variableList = data.getJSONArray("variablesList").toJavaList(Variable.class); BotInfo botInfo = data.getJSONObject("botInfo").toJavaObject(BotInfo.class); helper = new SqlHelper(); engine = new EngineUtils(); ActionUtils actionUtils = new ActionUtils(engine, helper); VariableUtils variableUtils = new VariableUtils(helper); ConfigTermUtils configTermUtils = new ConfigTermUtils(helper); //是否删除之前的数据 if (!data.containsKey("dataOverride") || data.getBoolean("dataOverride")) { //清空数据 helper.deleteUserData(botId); } originConditionList =helper.queryConditionAll(botId); originVariableList = helper.queryVariableAll(botId); conditionNameSet1 = actionUtils.getConditionNameSet(originConditionList); conditionNameSet2 = actionUtils.getConditionNameSet(conditionList); variableNameSet1 = variableUtils.getVariableNameSet(originVariableList); variableNameSet2 = variableUtils.getVariableNameSet(variableList); variableNameSet1.retainAll(variableNameSet2); conditionNameSet1.retainAll(conditionNameSet2); //监测是否重名 //求交集,如果交集大于0,则说明重复,返回重复列表 if(conditionNameSet1.size() > 0){ intervalStatus = new IntervalStatus(0, "dumplicate", JSONArray.toJSON(conditionNameSet1)); }else if(variableNameSet1.size() > 0){ intervalStatus = new IntervalStatus(0, "dumplicate", JSONArray.toJSON(variableNameSet1)); }else{ for(Category category:categoryList){ category.setBotId(botId); if(idReplaceMap.containsKey(category.getParentId())){ category.setParentId(idReplaceMap.get(category.getParentId())); } newCategoryId = helper.insertCategoryByBotId(category); idReplaceMap.put(category.getCategoryId(), newCategoryId); } //修改botid,并批量插入condition len = conditionList.size(); //替换配置文件相应类别的id for(int i =0;i < len;i++){ conditionList.get(i).setBotId(botId); conditionJson = JSON.parseObject(conditionList.get(i).getConditionJsonText()); if(conditionList.get(i).getActive().equals("1")){ completeConfigText.append(String.format(EXTRA_INFO_FORMAT, "activeAction", conditionJson.getString("conditionName"))); } idPair = new JSONArray(); idPair.add(conditionJson.getInteger("originConditionId")); idPair.add(conditionJson.getString("relationName").replace("0", "a0").replace("-", "b0")); conditionIdPair.add(idPair); actionTextList.add(conditionList.get(i).getActionText()); } helper.batchInsertCondition(conditionList); len = variableList.size(); //替换变量相应的botid for(int i =0;i < len;i++){ variableList.get(i).setBotId(botId); completeConfigText.append(String.format(VARIABLE_SAVE_FORMAT, variableList.get(i).getName(), variableList.get(i).getValue())); } helper.batchInsertVariable(botId, variableList); len = configTermList.size(); //替换配置文件相应类别的id for(ConfigTerm configTerm:configTermList){ if(idReplaceMap.containsKey(configTerm.getCategoryId())){ configTerm.setCategoryId(idReplaceMap.get(configTerm.getCategoryId())); }else{ configTerm.setCategoryId(0); } configTermJson = JSON.parseObject(configTerm.getConfigJsonText()); configTermText = configTermUtils.configPreprocessing(configTermJson); id = helper.insertConfig(botId, configTermJson, configTerm.getCategoryId()); completeConfigText.append(configTermText.toString()).append(id).append("\n"); } //helper.batchInsertConfig(botId, configTermList); batchUpdate(botAccount, completeConfigText.toString(), context, true); ape = new ActionPostElem(botId, botName, actionUtils.generateCompleteActionText(receiveParameters, actionTextList), auth, GAME_BOT_CATEGORY,conditionIdPair); actionUtils.postAction(ape, null); intervalStatus = actionUtils.intervalStatus; } }catch (SQLException e){ intervalStatus = new IntervalStatus(0, SQL_CONNECTION_ERROR); } finally{ engine.destroy(); helper.destroy(); } } switch(intervalStatus.getStatus()){ case 1: responseJSONObject.put("status", 1); break; case 0: responseJSONObject.put("status", 0); responseJSONObject.put("parameter", intervalStatus.getMessage()); break; } response.setCharacterEncoding("UTF-8"); response.setContentType("application/json; charset=utf-8"); //System.out.println("xx:" + responseJSONObject.toJSONString()); PrintWriter out = response.getWriter(); out.append(responseJSONObject.toString()); out.flush(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } public void batchUpdate(String userName, String data, ServletContext context, Boolean writeFile) throws IOException { BufferedReader br = new BufferedReader(new StringReader(data)); UserInfo userinfo = new UserInfo(userName); userinfo = UserInfoController.buildUserInfo(br, userinfo, true); context.setAttribute(userinfo.botAccount, userinfo); System.out.println("batchupdate"); if(writeFile){ UserInfoController.writeConfigText(userName, userinfo); } } }