package MSI.OP.SM.AOP.MEC.SIQ.SM.OSU.E; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.json.JSONObject; import MS.OP.SM.AOP.MEC.SIQ.cache.DetaDBBufferCache_M; import OP.SM.AOP.MEC.SIQ.SM.reflection.Cell; import OP.SM.AOP.MEC.SIQ.SM.reflection.Row; import OP.SM.AOP.MEC.SIQ.SM.reflection.Table; import PEU.P.cache.*; @SuppressWarnings("unchecked") //作者,著作权人: 罗瑶光,浏阳 public class IU_Rows_E { public static Map insertRowByTablePathAndIndex(String tablePath , String pageIndex, JSONObject culumnOfNewRow) throws FileNotFoundException, IOException { String[] sets = tablePath.split("/"); int rowInsertIndex = Integer.valueOf(pageIndex); File fileDBTable = new File(tablePath); if (fileDBTable.isDirectory()) { String DBTableRowsPath = tablePath + "/rows"; File fileDBTableRowsPath = new File(DBTableRowsPath); if (fileDBTableRowsPath.isDirectory()) { Row row = new Row(); ConcurrentHashMap cells = new ConcurrentHashMap<>(); row.I_Cells(cells); String DBTableRowIndexPath = DBTableRowsPath + "/row" + rowInsertIndex ; File readDBTableRowIndexFile = new File(DBTableRowIndexPath); if (!readDBTableRowIndexFile.exists()) { readDBTableRowIndexFile.mkdir(); Iterator it = culumnOfNewRow.keys(); while(it.hasNext()) { String culumnName = it.next(); String culumnValue = culumnOfNewRow.getString(culumnName); String needCreatCulumnPath = DBTableRowIndexPath + "/" + culumnName; File needCreatCulumn = new File(needCreatCulumnPath); needCreatCulumn.mkdir(); FileWriter fw = null; try { fw = new FileWriter(needCreatCulumnPath + "/value.lyg", true); fw.write(null==culumnValue?"":culumnValue); fw.close(); //buffer reflection update Cell cell = new Cell(); cell.I_CellValue(null == culumnValue ? "" : culumnValue); row.putCell(culumnName, cell); } catch (IOException e) { e.printStackTrace(); } } String needCreatCulumnPath = DBTableRowIndexPath + "/is_delete_0"; File needCreatCulumn = new File(needCreatCulumnPath); needCreatCulumn.mkdir(); } DetaDBBufferCache_M.db.getBase(sets[sets.length-2]) .getTable(sets[sets.length-1]).putRow("row" + rowInsertIndex, row); } } Map output = new HashMap<>(); output.put("totalPages", rowInsertIndex); return output; } public static Map insertRowByBaseName(String baseName , String tableName, JSONObject jsobj, boolean mod) throws Exception { Map output = new HashMap<>(); String tablePath = Cache_M.getCacheInfo("DBPath").getValue().toString(); tablePath += "/" + baseName + "/" + tableName; File fileDBTable = new File(tablePath); if (fileDBTable.isDirectory()) { String DBTableRowsPath = tablePath + "/rows"; Row row = new Row(); ConcurrentHashMap cells = new ConcurrentHashMap<>(); row.I_Cells(cells); File fileDBTableRowsPath = new File(DBTableRowsPath); if (fileDBTableRowsPath.isDirectory()) { int rowInsertIndex = fileDBTableRowsPath.list().length; output.put("totalPages", rowInsertIndex); String DBTableRowIndexPath = DBTableRowsPath + "/row" + rowInsertIndex ; File readDBTableRowIndexFile = new File(DBTableRowIndexPath); if (!readDBTableRowIndexFile.exists()) { readDBTableRowIndexFile.mkdir(); Iterator it = jsobj.keys(); while(it.hasNext()) { String culumnName = it.next(); String culumnValue = jsobj.get(culumnName).toString(); if(culumnValue.equalsIgnoreCase("random")){ culumnValue = "" + rowInsertIndex; } String needCreatCulumnPath = DBTableRowIndexPath + "/" + culumnName; File needCreatCulumn = new File(needCreatCulumnPath); if(!needCreatCulumn.exists()) { if(mod) { needCreatCulumn.mkdir(); } } File needCreatCulumnPathFile= new File(needCreatCulumnPath + "/value.lyg"); if(needCreatCulumnPathFile.exists() && !needCreatCulumnPathFile.canWrite()) { throw new Exception(); } if(mod) { FileWriter fw = null; try { fw = new FileWriter(needCreatCulumnPath + "/value.lyg", true); fw.write(null == culumnValue? "" : culumnValue); fw.close(); } catch (IOException e) { e.printStackTrace(); } } //add buffer Cell cell = new Cell(); cell.I_CellValue(null == culumnValue? "" : culumnValue); if(mod) { row.putCell(culumnName, cell); } } String needCreatCulumnPath = DBTableRowIndexPath + "/is_delete_0"; File needCreatCulumn = new File(needCreatCulumnPath); if(!needCreatCulumn.exists()) { if(mod){ needCreatCulumn.mkdir(); } } } Table table = DetaDBBufferCache_M.db.getBase(baseName).getTable(tableName); if(mod) { table.putRow("row" + rowInsertIndex, row); } } } return output; } public static void IU_RowByAttributes(Map object, boolean mod) throws Exception { JSONObject jsobj = new JSONObject(); //for late will make an exception record queue system, to control all of the db write; List culumnValues = (List)object.get("culumnValue"); Iterator iterator = culumnValues.iterator(); //list to json while(iterator.hasNext()) { String[] strings = iterator.next(); String string= ""; if(strings.length> 2) { string= strings[2]; } jsobj.put(strings[1], string); } insertRowByBaseName(object.get("baseName").toString(), object.get("tableName").toString() , jsobj, mod); } }