package processing; import entity.Condition; import entity.UserInfo; import javax.servlet.ServletContext; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class ConditionTableController { public static 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))) { Condition oldCondition = new Condition(); oldCondition = process.buildCondition(oldCondition, userinfo.id2config.get(Integer.parseInt(configId))); String conditionName = oldCondition.indexName; if (userinfo.conditionTable.get(conditionName) != null) { 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)); } else{ System.out.println("未找到要删除的configId"); } return true; } public static boolean batchDeleteConfig(String userName, List idGroup, UserInfo userInfo) throws IOException { Condition condition = new Condition(); ProcessQuestion process = new ProcessQuestion(); //data=configLine+"---"+conditionNameList for(int configId:idGroup) { if (userInfo.id2config.containsKey(configId)) { Condition oldCondition = new Condition(); oldCondition = process.buildCondition(oldCondition, userInfo.id2config.get(configId)); String conditionName = oldCondition.indexName; if (userInfo.conditionTable.get(conditionName) != null) { List conditionList = userInfo.conditionTable.get(conditionName); Iterator condIter = userInfo.conditionTable.get(conditionName).iterator(); while (condIter.hasNext()) { Condition c = condIter.next(); if (c.id == configId) { condIter.remove(); } } } userInfo.id2config.remove(configId); } else { System.out.println("未找到要删除的configId:" + configId); } } return true; } public static 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 static 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 static 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 static String writeConfigText(String userName, UserInfo userinfo) throws IOException { System.out.println("p"); System.out.println(userName); String CONFIG_DIR = System.getProperty("user.dir")+"/config", configText; 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; } }