package com.qiwu.chatbotbackend.actiongenerate.servlet; import com.alibaba.fastjson.JSON; 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.CategoryUtils; import com.qiwu.chatbotbackend.actiongenerate.utils.ConfigTermUtils; import com.qiwu.chatbotbackend.db.SqlHelper; import com.qiwu.chatbotbackend.processing.SimpleUtil; 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.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import static com.qiwu.chatbotbackend.Config.Config.BOT_BACKUP_DIR; import static com.qiwu.chatbotbackend.Config.Config.SQL_CONNECTION_ERROR; @WebServlet(name = "BackupServlet", urlPatterns = {"/save2file"}) public class BackupServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { IntervalStatus intervalStatus = new IntervalStatus(1, ""); ServletContext context = request.getServletContext(); //读取并解析json数据 String operationType = "", botName = "", botCategoryName; String jsonStr = SimpleUtil.readJSONString(request), fileData; String auth = request.getHeader("authorization"); JSONObject responseJSONObject = new JSONObject(); Boolean flag = true; Integer botId = 0, categoryIndex; if (jsonStr != null && !jsonStr.equals("")) { System.out.println(jsonStr); try { JSONObject receiveParameters = JSONObject.parseObject(jsonStr); JSONObject backupFile = new JSONObject(); receiveParameters.put("Authorization", auth); operationType = receiveParameters.getString("type"); botId = receiveParameters.getInteger("botId"); botName = receiveParameters.getString("botName_yhm"); botCategoryName = receiveParameters.getString("catName"); EngineUtils engine = new EngineUtils(); SqlHelper helper = new SqlHelper(); BotInfo botInfo = null; List conditionList = new ArrayList(); List configTermList = new ArrayList(); List childCategoryIds = new ArrayList(); List childCategories = new ArrayList(); List variablesList = new ArrayList(); ActionUtils actionUtils = new ActionUtils(engine, helper); CategoryUtils categoryUtils = new CategoryUtils(helper); ConfigTermUtils configUtils = new ConfigTermUtils(botName, helper, context); //actionUtils. //如果需要保存condition,查询所有condition if (receiveParameters.containsKey("saveCondition") && receiveParameters.getBoolean("saveCondition")){ conditionList = helper.queryConditionAll(botId); } //如果需要保存配置文件,则从根目录开始保存配置文件 if(receiveParameters.containsKey("saveConfig") && receiveParameters.getBoolean("saveConfig")){ categoryIndex = receiveParameters.getInteger("indexCategory"); //查询某个类别下的所有类别 childCategoryIds = categoryUtils.findChildCategoryIds(botId, categoryIndex, childCategories); //查询属于以上类别列表的所有配置条目 configTermList = helper.queryConfigByCategoryIdList(botId, childCategoryIds); } if(!receiveParameters.containsKey("saveVariable") || receiveParameters.getBoolean("saveVariable")){ variablesList = helper.queryVariableAll(botId); } botInfo = helper.queryBotInfo(botId); Backup backup = new Backup(botId, botName, botCategoryName, conditionList, configTermList, childCategories, variablesList, botInfo); //; //backupFile.put("botId"); try { writeFile(BOT_BACKUP_DIR + botId + ".backup",JSON.toJSONString(backup)); // ReadFile("D:\\8.json"); System.out.println(); } catch (IOException e) { e.printStackTrace(); } helper.destroy(); engine.destroy(); } catch (SQLException e) { intervalStatus = new IntervalStatus(0, SQL_CONNECTION_ERROR); } } response.setCharacterEncoding("UTF-8"); response.setContentType("application/json; charset=utf-8"); //System.out.println("xx:" + responseJSONObject.toJSONString()); PrintWriter out = response.getWriter(); out.append( + botId + ".backup"); out.flush(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } /** * 写文件到.Json * @param filePath * @param sets * @throws IOException */ public static void writeFile(String filePath, String sets) throws IOException { FileWriter fw = new FileWriter(filePath); PrintWriter out = new PrintWriter(fw); out.write(sets); out.println(); fw.close(); out.close(); } }