Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package eg.edu.alexu.csd.oop.Objects;
- import eg.edu.alexu.csd.oop.cachedTable;
- import eg.edu.alexu.csd.oop.queryParsedData;
- import eg.edu.alexu.csd.oop.XML.*;
- import org.xml.sax.SAXException;
- import javax.xml.parsers.ParserConfigurationException;
- import javax.xml.transform.TransformerException;
- import java.io.File;
- import java.io.IOException;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.Set;
- public class MyTable {
- static ArrayList<cachedTable> cachedTables = new ArrayList<>();
- static ArrayList<String> chashedPathes = new ArrayList<>();
- static Utilities util = new Utilities();
- static commandState cS = new commandState();
- public commandState create(queryParsedData data) throws SQLException {
- System.out.println("d5l hna");
- java.sql.SQLException ex = new java.sql.SQLException();
- commandState cS = new commandState();
- cachedTable table = null;
- String databasePath = data.getDataBaseName();
- String schemaPath= databasePath+System.getProperty("file.separator")+data.getTableName();
- String XMLpath = data.getDataBaseName()+System.getProperty("file.separator")+data.getTableName()+".xml";
- File xsd = new File(schemaPath+".xsd");
- if(databasePath != "") {
- if (!xsd.exists()) {
- XSDSchemaWriter schemaWriter = new XSDSchemaWriter();
- StaxXMLWriter xmlWriter = new StaxXMLWriter();
- String[][] atr;
- atr = new String[data.getData().size()][2];
- Map<String, String> prob = data.getData();
- int i = 0;
- for (String s : prob.keySet()) {
- atr[i][0] = s;
- atr[i][1] = prob.get(s);
- i++;
- }
- try {
- System.out.println("d5l hna");
- schemaWriter.schemaWriter(schemaPath, atr);
- table = new cachedTable();
- table.setCoulmnsTypes(new LinkedHashMap<>(data.getData()));
- prob.clear();
- xmlWriter.writeXML(schemaPath + ".xml", prob);
- table.setDatabaseName(data.getDataBaseName());
- table.setTableName(data.getTableName());
- table.setPath(XMLpath);
- System.out.println("DATA TYPES "+ prob);
- cS.setExecuted(true);
- chashedPathes.add(XMLpath.trim());
- System.out.println("XMLpath "+ XMLpath+" CACHED PATHES "+chashedPathes);
- cachedTables.add(table);
- System.out.println("name:"+table.getTableName());
- System.out.println("colTypes:"+table.getCoulmnsTypes());
- } catch (ParserConfigurationException e) {
- e.printStackTrace();
- } catch (TransformerException e) {
- e.printStackTrace();
- }
- return cS;
- } else {
- cS.setExecuted(false);
- return cS;
- }
- }
- else{
- throw ex;
- }
- }
- public commandState drop(queryParsedData data){
- String TableName = data.getDataBaseName()+System.getProperty("file.separator")+data.getTableName();
- File xml = new File(TableName+".xml");
- File xsd = new File(TableName+".xsd");
- int v = 0 ;
- for(int i = 0 ; i<cachedTables.size() ; i++){
- cachedTable Table = cachedTables.get(i);
- if(TableName == Table.getTableName()){
- v = i ;
- }
- }
- if(xsd.exists()) {
- // System.out.println("Table xsd Found");
- cachedTables.remove(v) ;
- chashedPathes.remove(v);
- xsd.delete();
- cS.setExecuted(true);
- }else {
- // System.out.println("Table xsd Not Found");
- cS.setExecuted(false);
- }if(xml.exists()) {
- //System.out.println("Table xml Found");
- if(xml.delete()){
- // System.out.println("Xml deleted");
- }else {
- // System.out.println("Not deleted");
- }
- cS.setExecuted(true);
- }else {
- // System.out.println("Table xml Not Found");
- cS.setExecuted(false);
- }
- return cS;
- }
- public commandState insert(queryParsedData data) throws SQLException {
- commandState cS = new commandState();
- java.sql.SQLException ex = new java.sql.SQLException();
- String XMLpath = data.getDataBaseName()+System.getProperty("file.separator")+data.getTableName()+".xml";
- System.out.println("XMLpath "+ XMLpath+" CACHED PATHES "+chashedPathes);
- cachedTable table = cachedTables.get(chashedPathes.indexOf(XMLpath));
- // System.out.println("=====================================================");
- // System.out.println("TABLE NAME " + table.getTableName());
- // System.out.println("DATA TYPES GIVEN " + data.getData());
- // System.out.println("TABLE DATA TYPES " + table.getCoulmnsTypes());
- // System.out.println("Cashed Pathes " +chashedPathes+ " XML PATH " + XMLpath+" INDEX " + chashedPathes.indexOf(XMLpath.trim()));
- // System.out.println("=====================================================");
- if(chashedPathes.indexOf(XMLpath)< 0){
- cS.setExecuted(false);
- System.out.println(XMLpath + " : Schema Not found");
- throw ex;
- }else {
- try {
- if(!util.validationByCaching(table.getCoulmnsTypes(),data)) {
- cS.setExecuted(false);
- System.out.println("Schema Not valid");
- throw ex;
- }
- else{
- for (String s : data.getData().keySet()) {
- String value = data.getData().get(s);
- if(util.isQuoted(value))
- value = data.getData().get(s).substring(1, data.getData().get(s).length() - 1);
- table.addCoulnm(s,value);
- }
- table.addRow(data.getData());
- cS.setUpdateCount(cS.getUpdateCount()+1);
- }
- } catch (ParserConfigurationException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return cS;
- }
- public commandState delete(queryParsedData data) throws SQLException, IOException, SAXException, ParserConfigurationException {
- commandState cS = new commandState();
- java.sql.SQLException ex = new java.sql.SQLException();
- String XMLpath = data.getDataBaseName()+System.getProperty("file.separator")+data.getTableName()+".xml";
- cachedTable table = cachedTables.get(chashedPathes.indexOf(XMLpath));
- System.out.println(data.getData());
- if(chashedPathes.indexOf(XMLpath)< 0){
- cS.setExecuted(false);
- System.out.println(XMLpath + " : Schema Not found");
- throw ex;
- }else {
- if(!((data.getCondition().contains("="))||(data.getCondition().contains(">"))||(data.getCondition().contains("<")))){
- cS.setUpdateCount(cS.getUpdateCount()+table.getRows().size());
- table.setRows(new ArrayList<>());
- Set s = table.getCoulmnsValues().keySet();
- for (int i =0;i<=s.size()+1;i++)
- table.getCoulmnsValues().remove(s.iterator().next());
- }
- else{
- ArrayList<Map> j = new ArrayList<>(table.getRows());
- System.out.println("TABLE "+j);
- ArrayList<Map> NEW = new ArrayList<>();
- for (int i=0;i<j.size();i++){
- boolean flag = false;
- Set kS = j.get(i).keySet();
- Object[] a = kS.toArray();
- for (int l=0;l<kS.size();l++) {
- if(util.conditionValidator((String) a[l],(String) j.get(i).get(a[l]),data)) {
- cS.setUpdateCount(cS.getUpdateCount()+1);
- flag = true;
- }
- }
- if(!flag)
- NEW.add(j.get(i));
- table.setRows(NEW);
- }
- }
- }
- System.out.println("ROWS "+table.getRows());
- System.out.println("VALUES "+table.getCoulmnsValues());
- return cS;
- }
- public commandState update(queryParsedData data) throws SQLException, IOException, SAXException, ParserConfigurationException {
- java.sql.SQLException ex = new java.sql.SQLException();
- String schemapath = data.getDataBaseName() + System.getProperty("file.separator") + data.getTableName() + ".xsd";
- String XMLpath = data.getDataBaseName() + System.getProperty("file.separator") + data.getTableName() + ".xml";
- System.out.println("Update ");
- boolean flagg = false ;
- File schemaFile = new File(schemapath);
- for(int g =0 ; g<chashedPathes.size() ; g++){
- if(XMLpath.equals(chashedPathes.get(g))) {
- flagg = true;
- break ;
- }
- }
- if ( flagg == false ) {
- throw ex ;
- }else {
- cachedTable table = cachedTables.get(chashedPathes.indexOf(XMLpath));
- System.out.print(table.getRows());
- if (chashedPathes.indexOf(XMLpath) < 0) {
- cS.setExecuted(false);
- System.out.println(XMLpath + " : Schema Not found");
- throw ex;
- } else {
- if (!((data.getCondition().contains("=")) || (data.getCondition().contains(">")) || (data.getCondition().contains("<")))) {
- cS.setUpdateCount(table.getRows().size());
- int p = table.getRows().size() ;
- table.setRows(new ArrayList<>());
- ArrayList<Map> j = new ArrayList<>(table.getRows());
- int i = 0;
- for (i = 0; i < p ; i++){
- j.add(i,data.getData());
- }
- table.setRows(j);
- return cS ;
- } else {
- ArrayList<Map> j = new ArrayList<>(table.getRows());
- System.out.println("TABLE " + j);
- int k = 0 ;
- ArrayList<Map> NEW = new ArrayList<>();
- for (int i = 0; i < j.size(); i++) {
- int z = 0;
- boolean flag = false;
- Set kS = j.get(i).keySet();
- Object[] a = kS.toArray();
- for (int l = 0; l < kS.size(); l++) {
- if (util.conditionValidator((String) a[l], (String) j.get(i).get(a[l]), data)) {
- cS.setUpdateCount(cS.getUpdateCount() + 1);
- flag = true;
- z = l;
- }
- }
- if (!flag) {
- NEW.add(j.get(i));
- } else {
- j.set(i, data.getData());
- NEW.add(j.get(i));
- k++ ;
- }
- table.setRows(NEW);
- }
- cS.setUpdateCount(k);
- }
- }
- System.out.println("ROWS " + table.getRows());
- System.out.println("VALUES " + table.getCoulmnsValues());
- return cS;
- }
- }
- // public commandState update(queryParsedData data) throws SQLException, IOException, SAXException, ParserConfigurationException {
- // java.sql.SQLException ex = new java.sql.SQLException();
- // String schemapath = data.getDataBaseName() + System.getProperty("file.separator") + data.getTableName() + ".xsd";
- // String XMLpath = data.getDataBaseName() + System.getProperty("file.separator") + data.getTableName() + ".xml";
- // System.out.println("Update ");
- // boolean flagg = false ;
- // File schemaFile = new File(schemapath);
- // for(int g =0 ; g<chashedPathes.size() ; g++){
- // if(XMLpath.equals(chashedPathes.get(g))) {
- // flagg = true;
- // break ;
- // }
- // }
- // if ( flagg == false ) {
- // throw ex ;
- // }else {
- // cachedTable table = cachedTables.get(chashedPathes.indexOf(XMLpath));
- // if (chashedPathes.indexOf(XMLpath) < 0) {
- // cS.setExecuted(false);
- // System.out.println(XMLpath + " : Schema Not found");
- // throw ex;
- // } else {
- // if (!((data.getCondition().contains("=")) || (data.getCondition().contains(">")) || (data.getCondition().contains("<")))) {
- // cS.setUpdateCount(table.getRows().size());
- // table.setRows(new ArrayList<>());
- // ArrayList<Map> j = new ArrayList<>(table.getRows());
- // int i = 0;
- // for (i = 0; i < table.getRows().size(); i++){
- // j.set(i, data.getData());
- // }
- // table.setRows(j);
- // return cS ;
- // } else {
- // if (!((data.getCondition().contains("=")) || (data.getCondition().contains(">")) || (data.getCondition().contains("<")))) {
- // for (int R = 0;R<table.getRows().size();R++) {
- // boolean flag = false;
- // for (Object s : table.getRows().get(R).keySet()) {
- // for (Object sData : data.getData().keySet()) {
- // if (s.equals(sData)){
- // flag = true;
- // table.getRows().get(R).put(s,data.getData().get(sData));
- // cS.setUpdateCount(cS.getUpdateCount() + 1);
- // }
- // }
- // }
- // if (flag)
- // cS.setUpdateCount(cS.getUpdateCount() + 1);
- // }
- // } else {
- // for (int R = 0;R<table.getRows().size();R++) {
- // boolean flag = false;
- // String conditionString = data.getCondition();
- // String[] condition = conditionString.split(" *= *| *< *| *> *");
- // for (Object s : table.getRows().get(R).keySet()) {
- //// for (Object sData : data.getData().keySet()) {
- // if (condition[0].equals(s)){
- // if (util.conditionValidator((String) s,(String) table.getRows().get(R).get(s),data)) {
- // table.getRows().get(R).put(s, data.getData().get(s));
- // flag = true;
- // }
- // }
- //// }
- // }
- // if (flag)
- // cS.setUpdateCount(cS.getUpdateCount() + 1);
- // }
- // }
- // }
- // }
- // System.out.println("ROWS " + table.getRows());
- // System.out.println("VALUES " + table.getCoulmnsValues());
- // return cS;
- // }
- // }
- public commandState select(queryParsedData data) throws SQLException, IOException, SAXException, ParserConfigurationException {
- commandState cS = new commandState();
- java.sql.SQLException ex = new java.sql.SQLException();
- String XMLpath = data.getDataBaseName()+System.getProperty("file.separator")+data.getTableName()+".xml";
- cachedTable table = cachedTables.get(chashedPathes.indexOf(XMLpath));
- if(chashedPathes.indexOf(XMLpath)< 0){
- cS.setExecuted(false);
- System.out.println(XMLpath + " : Schema Not found");
- throw ex;
- }else {
- ArrayList<ArrayList<Object>> selected = new ArrayList<>();
- if(!((data.getCondition().contains("="))||(data.getCondition().contains(">"))||(data.getCondition().contains("<")))){
- if (data.isSelectAll()){
- for (int i=0;i<table.getRows().size();i++) {
- ArrayList<Object> NEW = new ArrayList<>();
- String temp = new String();
- for(Object s : table.getRows().get(i).keySet())
- temp = temp.concat("\n").concat((String) table.getRows().get(i).get(s)) ;
- NEW.add(temp);
- selected.add(NEW);
- }
- }
- else{
- ArrayList<Object> NEW = new ArrayList<>();
- for (int i=0;i<table.getRows().size();i++) {
- String temp = new String();
- boolean flag = false;
- for(Object s : table.getRows().get(i).keySet()) {
- NEW = new ArrayList<>();
- for(Object S : data.getData().keySet()){
- if (s.equals(S)) {
- temp = temp.concat("\n").concat((String) table.getRows().get(i).get(s));
- flag = true;
- }
- }
- if(flag)
- NEW.add(temp);
- }
- }
- if (!NEW.isEmpty())
- selected.add(NEW);
- }
- cS.setArray(selected);
- }
- else{
- if (data.isSelectAll()){
- String conditionString = data.getCondition();
- String[] condition = conditionString.split(" *= *| *< *| *> *");
- boolean flag = false;
- ArrayList<Object> NEW = new ArrayList<>();
- for (int i=0;i<table.getRows().size();i++) {
- NEW = new ArrayList<>();
- String temp = new String();
- for(Object s : table.getRows().get(i).keySet()) {
- temp = new String();
- if (s.equals(condition[0]))
- if (util.conditionValidator((String) s,(String) table.getRows().get(i).get(s),data)) {
- for(Object F : table.getRows().get(i).keySet()) {
- temp = temp.concat("\n").concat((String) table.getRows().get(i).get(F));
- flag = true;
- }
- }
- if (flag) {
- NEW.add(temp);
- flag = false;
- }
- }
- if(!(NEW.size()==0))
- selected.add(NEW);
- }
- }
- else{
- ArrayList<Object> NEW = new ArrayList<>();
- for (int i=0;i<table.getRows().size();i++) {
- String temp = new String();
- boolean flag = false;
- for(Object s : table.getRows().get(i).keySet()) {
- NEW = new ArrayList<>();
- for(Object S : data.getData().keySet()){
- String conditionString = data.getCondition();
- String[] condition = conditionString.split(" *= *| *< *| *> *");
- System.out.println("SELECT " + condition[0]+ " "+ s);
- if (condition[0].equals(s)) {
- flag = false;
- System.out.println("SELECT " + condition[0]);
- System.out.println("KKKKKKKKKKKK ");
- if (util.conditionValidator((String) s,(String) table.getRows().get(i).get(s),data)) {
- temp = temp.concat("\n").concat((String) table.getRows().get(i).get(s));
- flag = true;
- }
- if(flag)
- NEW.add(temp);
- }
- }
- if (NEW.size()!=0)
- selected.add(NEW);
- }
- }
- }
- cS.setArray(selected);
- }
- }
- System.out.println("ROWS "+table.getRows());
- System.out.println("VALUES "+table.getCoulmnsValues());
- return cS;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement