package servlet; import entity.Condition; import entity.UserInfo; import processing.ProcessQuestion; 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.*; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; @WebServlet(name = "UpdateSingleConfigServlet",urlPatterns = {"/update2"}) public class UpdateSingleConfigServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); ServletContext context = this.getServletContext(); String userName = request.getParameter("userName"); String processType = request.getParameter("type"); String configId = request.getParameter("configId"); String data = request.getParameter("configData"); System.out.println("userName:"+userName + " " + "type:" + processType + " " + "configId:" + "configId:" + configId + " " + "configData:" + data); switch(processType){ case "insert_config": insertConfig(userName, configId, data, context); break; case "update_config": deleteConfig(userName, configId, data, context); insertConfig(userName, configId, data, context); break; case "delete_config": deleteConfig(userName, configId, data, context); break; case "extra_info": updateExtraInfo(userName, data, context); break; case "batch_update": batchUpdate(userName, data, context); } writeConfig(userName, context); response.setCharacterEncoding("utf-8"); response.setContentType( "text/plain"); PrintWriter out = response.getWriter(); System.out.println("cc"); out.write("success"); out.flush(); out.close(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } public boolean insertConfig(String userName, String configId, String data, ServletContext context) throws IOException { Condition condition = new Condition(); ProcessQuestion process = new ProcessQuestion(); //data=configLine+"---"+conditionNameList String dataPair[] = data.split("---"); String configLine = dataPair[0]; String conditionNameList[]; if(dataPair.length > 1) conditionNameList = dataPair[1].split(","); else conditionNameList = new String[]{"emptySet",}; condition = process.buildCondition(condition, configLine); if(context.getAttribute(userName) == null){ process.userConfigInit(userName, context); } UserInfo userinfo = (UserInfo) context.getAttribute(userName); if(userinfo.conditionTable.get(conditionNameList[0]) == null){ List conditionList = new ArrayList(); userinfo.conditionTable.put(conditionNameList[0], conditionList); } userinfo.conditionTable.get(conditionNameList[0]).add(condition); userinfo.id2config.put(Integer.parseInt(configId), configLine); return true; } public boolean updateConfig(String userName, String configId, String data, ServletContext context) throws IOException { Condition condition = new Condition(); ProcessQuestion process = new ProcessQuestion(); //data=configLine+"---"+conditionNameList String dataPair[] = data.split("---"); String configLine = dataPair[0]; String conditionNameList[] = dataPair[1].split(","); condition = process.buildCondition(condition, configLine); if(context.getAttribute(userName) == null){ process.userConfigInit(userName, context); } UserInfo userinfo = (UserInfo) context.getAttribute(userName); if (userinfo.id2config.get(condition.id) == null){ }else { for (String conditionName : conditionNameList) { if (userinfo.conditionTable.get(conditionName) == null) { continue; } List conditionList = userinfo.conditionTable.get(conditionName); int condLength = conditionList.size(); for (int i = 0; i < condLength; i++) { if (conditionList.get(i).id == Integer.parseInt(configId)) { conditionList.set(i, condition); } } } userinfo.id2config.put(Integer.parseInt(configId), configLine); } return true; } public boolean deleteConfig(String userName, String configId, String data, ServletContext context) throws IOException { Condition condition = new Condition(); ProcessQuestion process = new ProcessQuestion(); //data=configLine+"---"+conditionNameList String conditionNameList[] = data.split(","); if(context.getAttribute(userName) == null){ process.userConfigInit(userName, context); } UserInfo userinfo = (UserInfo) context.getAttribute(userName); if (userinfo.id2config.containsKey(Integer.parseInt(configId))) { String currentPremises[] = userinfo.id2config.get(Integer.parseInt(configId)).split("\\|")[0].split(","); for (String premise : currentPremises) { String conditionName = premise.split("=")[0]; if (userinfo.conditionTable.get(conditionName) == null) { continue; } List conditionList = userinfo.conditionTable.get(conditionName); Iterator condIter = userinfo.conditionTable.get(conditionName).iterator(); while (condIter.hasNext()) { Condition c = condIter.next(); if (c.id == Integer.parseInt(configId)) { condIter.remove(); } } } userinfo.id2config.remove(Integer.parseInt(configId)); } return true; } public boolean updateExtraInfo(String userName, String data, ServletContext context) throws IOException { ProcessQuestion process = new ProcessQuestion(); if(context.getAttribute(userName) == null){ if(context.getAttribute(userName) == null){ process.userConfigInit(userName, context); } } UserInfo userinfo = (UserInfo) context.getAttribute(userName); String extraElements[] = data.split("\\|"); userinfo.extraInfo.put(extraElements[1],extraElements[2]); return true; } public String writeConfig(String userName, ServletContext context) throws IOException { System.out.println("write"); UserInfo userinfo = (UserInfo) context.getAttribute(userName); String CONFIG_DIR = System.getProperty("user.dir")+"/config", configText; context.setAttribute(userName,userinfo); String path = CONFIG_DIR + "/" + userName + ".txt"; File file = new File(path); File dir = new File(System.getProperty("user.dir")+"/config"); String returnMessage = "success"; if(!dir.exists()) dir.mkdir(); if(!file.exists()){ file.createNewFile(); } FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); try { Iterator keyIter = userinfo.extraInfo.keySet().iterator(); while (keyIter.hasNext()) { String info = keyIter.next(); if(!userinfo.extraInfo.get(info).equals("")) bw.write("-extraInfo-|" + info + "|" + userinfo.extraInfo.get(info) + "\n"); } Iterator idIter = userinfo.id2config.keySet().iterator(); while (idIter.hasNext()) { int id = idIter.next(); bw.write(userinfo.id2config.get(id) + "\n"); } bw.write("\n"); }catch(Exception e){ e.printStackTrace(); returnMessage = "config file write error"; }finally { bw.close(); fw.close(); } return returnMessage; } public void batchUpdate(String userName, String data, ServletContext context) throws IOException { BufferedReader br = new BufferedReader(new StringReader(data)); UserInfo userinfo = new UserInfo(userName); userinfo = ProcessQuestion.buildUserInfo(br, userinfo); context.setAttribute(userinfo.userName,userinfo); System.out.println("batchupdate"); } }