Advertisement
Khadija_Assem

Untitled

Nov 29th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.65 KB | None | 0 0
  1. package eg.edu.alexu.csd.oop.Objects;
  2. import eg.edu.alexu.csd.oop.cachedTable;
  3. import eg.edu.alexu.csd.oop.queryParsedData;
  4. import eg.edu.alexu.csd.oop.XML.*;
  5. import org.xml.sax.SAXException;
  6.  
  7. import javax.xml.parsers.ParserConfigurationException;
  8. import javax.xml.transform.TransformerException;
  9. import java.io.File;
  10. import java.io.IOException;
  11. import java.sql.SQLException;
  12. import java.util.ArrayList;
  13. import java.util.LinkedHashMap;
  14. import java.util.Map;
  15. import java.util.Set;
  16.  
  17. public class MyTable {
  18.  
  19. static ArrayList<cachedTable> cachedTables = new ArrayList<>();
  20. static ArrayList<String> chashedPathes = new ArrayList<>();
  21. static Utilities util = new Utilities();
  22. static commandState cS = new commandState();
  23.  
  24. public commandState create(queryParsedData data) throws SQLException {
  25. System.out.println("d5l hna");
  26. java.sql.SQLException ex = new java.sql.SQLException();
  27. commandState cS = new commandState();
  28. cachedTable table = null;
  29. String databasePath = data.getDataBaseName();
  30. String schemaPath= databasePath+System.getProperty("file.separator")+data.getTableName();
  31. String XMLpath = data.getDataBaseName()+System.getProperty("file.separator")+data.getTableName()+".xml";
  32. File xsd = new File(schemaPath+".xsd");
  33. if(databasePath != "") {
  34. if (!xsd.exists()) {
  35. XSDSchemaWriter schemaWriter = new XSDSchemaWriter();
  36. StaxXMLWriter xmlWriter = new StaxXMLWriter();
  37. String[][] atr;
  38. atr = new String[data.getData().size()][2];
  39. Map<String, String> prob = data.getData();
  40. int i = 0;
  41. for (String s : prob.keySet()) {
  42. atr[i][0] = s;
  43. atr[i][1] = prob.get(s);
  44. i++;
  45. }
  46. try {
  47. System.out.println("d5l hna");
  48. schemaWriter.schemaWriter(schemaPath, atr);
  49. table = new cachedTable();
  50. table.setCoulmnsTypes(new LinkedHashMap<>(data.getData()));
  51. prob.clear();
  52. xmlWriter.writeXML(schemaPath + ".xml", prob);
  53. table.setDatabaseName(data.getDataBaseName());
  54. table.setTableName(data.getTableName());
  55. table.setPath(XMLpath);
  56. System.out.println("DATA TYPES "+ prob);
  57. cS.setExecuted(true);
  58. chashedPathes.add(XMLpath.trim());
  59. System.out.println("XMLpath "+ XMLpath+" CACHED PATHES "+chashedPathes);
  60. cachedTables.add(table);
  61. System.out.println("name:"+table.getTableName());
  62. System.out.println("colTypes:"+table.getCoulmnsTypes());
  63. } catch (ParserConfigurationException e) {
  64. e.printStackTrace();
  65. } catch (TransformerException e) {
  66. e.printStackTrace();
  67. }
  68. return cS;
  69. } else {
  70. cS.setExecuted(false);
  71. return cS;
  72. }
  73. }
  74. else{
  75. throw ex;
  76. }
  77. }
  78.  
  79. public commandState drop(queryParsedData data){
  80. String TableName = data.getDataBaseName()+System.getProperty("file.separator")+data.getTableName();
  81. File xml = new File(TableName+".xml");
  82. File xsd = new File(TableName+".xsd");
  83. int v = 0 ;
  84. for(int i = 0 ; i<cachedTables.size() ; i++){
  85. cachedTable Table = cachedTables.get(i);
  86. if(TableName == Table.getTableName()){
  87. v = i ;
  88. }
  89. }
  90. if(xsd.exists()) {
  91. // System.out.println("Table xsd Found");
  92. cachedTables.remove(v) ;
  93. chashedPathes.remove(v);
  94. xsd.delete();
  95. cS.setExecuted(true);
  96. }else {
  97. // System.out.println("Table xsd Not Found");
  98.  
  99. cS.setExecuted(false);
  100. }if(xml.exists()) {
  101. //System.out.println("Table xml Found");
  102. if(xml.delete()){
  103. // System.out.println("Xml deleted");
  104. }else {
  105. // System.out.println("Not deleted");
  106. }
  107. cS.setExecuted(true);
  108. }else {
  109. // System.out.println("Table xml Not Found");
  110. cS.setExecuted(false);
  111. }
  112. return cS;
  113. }
  114.  
  115. public commandState insert(queryParsedData data) throws SQLException {
  116. commandState cS = new commandState();
  117. java.sql.SQLException ex = new java.sql.SQLException();
  118. String XMLpath = data.getDataBaseName()+System.getProperty("file.separator")+data.getTableName()+".xml";
  119. System.out.println("XMLpath "+ XMLpath+" CACHED PATHES "+chashedPathes);
  120. cachedTable table = cachedTables.get(chashedPathes.indexOf(XMLpath));
  121. // System.out.println("=====================================================");
  122. // System.out.println("TABLE NAME " + table.getTableName());
  123. // System.out.println("DATA TYPES GIVEN " + data.getData());
  124. // System.out.println("TABLE DATA TYPES " + table.getCoulmnsTypes());
  125. // System.out.println("Cashed Pathes " +chashedPathes+ " XML PATH " + XMLpath+" INDEX " + chashedPathes.indexOf(XMLpath.trim()));
  126. // System.out.println("=====================================================");
  127. if(chashedPathes.indexOf(XMLpath)< 0){
  128. cS.setExecuted(false);
  129. System.out.println(XMLpath + " : Schema Not found");
  130. throw ex;
  131. }else {
  132. try {
  133. if(!util.validationByCaching(table.getCoulmnsTypes(),data)) {
  134. cS.setExecuted(false);
  135. System.out.println("Schema Not valid");
  136. throw ex;
  137. }
  138. else{
  139. for (String s : data.getData().keySet()) {
  140. String value = data.getData().get(s);
  141. if(util.isQuoted(value))
  142. value = data.getData().get(s).substring(1, data.getData().get(s).length() - 1);
  143. table.addCoulnm(s,value);
  144. }
  145. table.addRow(data.getData());
  146. cS.setUpdateCount(cS.getUpdateCount()+1);
  147. }
  148. } catch (ParserConfigurationException e) {
  149. e.printStackTrace();
  150. } catch (SAXException e) {
  151. e.printStackTrace();
  152. } catch (IOException e) {
  153. e.printStackTrace();
  154. }
  155. }
  156. return cS;
  157. }
  158.  
  159. public commandState delete(queryParsedData data) throws SQLException, IOException, SAXException, ParserConfigurationException {
  160. commandState cS = new commandState();
  161. java.sql.SQLException ex = new java.sql.SQLException();
  162. String XMLpath = data.getDataBaseName()+System.getProperty("file.separator")+data.getTableName()+".xml";
  163. cachedTable table = cachedTables.get(chashedPathes.indexOf(XMLpath));
  164. System.out.println(data.getData());
  165.  
  166. if(chashedPathes.indexOf(XMLpath)< 0){
  167. cS.setExecuted(false);
  168. System.out.println(XMLpath + " : Schema Not found");
  169. throw ex;
  170. }else {
  171. if(!((data.getCondition().contains("="))||(data.getCondition().contains(">"))||(data.getCondition().contains("<")))){
  172. cS.setUpdateCount(cS.getUpdateCount()+table.getRows().size());
  173. table.setRows(new ArrayList<>());
  174. Set s = table.getCoulmnsValues().keySet();
  175. for (int i =0;i<=s.size()+1;i++)
  176. table.getCoulmnsValues().remove(s.iterator().next());
  177. }
  178. else{
  179. ArrayList<Map> j = new ArrayList<>(table.getRows());
  180. System.out.println("TABLE "+j);
  181. ArrayList<Map> NEW = new ArrayList<>();
  182. for (int i=0;i<j.size();i++){
  183. boolean flag = false;
  184. Set kS = j.get(i).keySet();
  185. Object[] a = kS.toArray();
  186. for (int l=0;l<kS.size();l++) {
  187. if(util.conditionValidator((String) a[l],(String) j.get(i).get(a[l]),data)) {
  188. cS.setUpdateCount(cS.getUpdateCount()+1);
  189. flag = true;
  190. }
  191. }
  192. if(!flag)
  193. NEW.add(j.get(i));
  194. table.setRows(NEW);
  195. }
  196. }
  197. }
  198. System.out.println("ROWS "+table.getRows());
  199. System.out.println("VALUES "+table.getCoulmnsValues());
  200. return cS;
  201. }
  202.  
  203. public commandState update(queryParsedData data) throws SQLException, IOException, SAXException, ParserConfigurationException {
  204. java.sql.SQLException ex = new java.sql.SQLException();
  205. String schemapath = data.getDataBaseName() + System.getProperty("file.separator") + data.getTableName() + ".xsd";
  206. String XMLpath = data.getDataBaseName() + System.getProperty("file.separator") + data.getTableName() + ".xml";
  207. System.out.println("Update ");
  208. boolean flagg = false ;
  209. File schemaFile = new File(schemapath);
  210. for(int g =0 ; g<chashedPathes.size() ; g++){
  211. if(XMLpath.equals(chashedPathes.get(g))) {
  212. flagg = true;
  213. break ;
  214. }
  215. }
  216. if ( flagg == false ) {
  217. throw ex ;
  218. }else {
  219. cachedTable table = cachedTables.get(chashedPathes.indexOf(XMLpath));
  220. System.out.print(table.getRows());
  221. if (chashedPathes.indexOf(XMLpath) < 0) {
  222. cS.setExecuted(false);
  223. System.out.println(XMLpath + " : Schema Not found");
  224. throw ex;
  225. } else {
  226. if (!((data.getCondition().contains("=")) || (data.getCondition().contains(">")) || (data.getCondition().contains("<")))) {
  227. cS.setUpdateCount(table.getRows().size());
  228. int p = table.getRows().size() ;
  229. table.setRows(new ArrayList<>());
  230. ArrayList<Map> j = new ArrayList<>(table.getRows());
  231. int i = 0;
  232. for (i = 0; i < p ; i++){
  233. j.add(i,data.getData());
  234. }
  235. table.setRows(j);
  236. return cS ;
  237. } else {
  238. ArrayList<Map> j = new ArrayList<>(table.getRows());
  239. System.out.println("TABLE " + j);
  240. int k = 0 ;
  241. ArrayList<Map> NEW = new ArrayList<>();
  242. for (int i = 0; i < j.size(); i++) {
  243. int z = 0;
  244. boolean flag = false;
  245. Set kS = j.get(i).keySet();
  246. Object[] a = kS.toArray();
  247. for (int l = 0; l < kS.size(); l++) {
  248. if (util.conditionValidator((String) a[l], (String) j.get(i).get(a[l]), data)) {
  249. cS.setUpdateCount(cS.getUpdateCount() + 1);
  250. flag = true;
  251. z = l;
  252. }
  253. }
  254. if (!flag) {
  255. NEW.add(j.get(i));
  256. } else {
  257. j.set(i, data.getData());
  258. NEW.add(j.get(i));
  259. k++ ;
  260. }
  261. table.setRows(NEW);
  262. }
  263. cS.setUpdateCount(k);
  264. }
  265. }
  266. System.out.println("ROWS " + table.getRows());
  267. System.out.println("VALUES " + table.getCoulmnsValues());
  268. return cS;
  269. }
  270. }
  271.  
  272. // public commandState update(queryParsedData data) throws SQLException, IOException, SAXException, ParserConfigurationException {
  273. // java.sql.SQLException ex = new java.sql.SQLException();
  274. // String schemapath = data.getDataBaseName() + System.getProperty("file.separator") + data.getTableName() + ".xsd";
  275. // String XMLpath = data.getDataBaseName() + System.getProperty("file.separator") + data.getTableName() + ".xml";
  276. // System.out.println("Update ");
  277. // boolean flagg = false ;
  278. // File schemaFile = new File(schemapath);
  279. // for(int g =0 ; g<chashedPathes.size() ; g++){
  280. // if(XMLpath.equals(chashedPathes.get(g))) {
  281. // flagg = true;
  282. // break ;
  283. // }
  284. // }
  285. // if ( flagg == false ) {
  286. // throw ex ;
  287. // }else {
  288. // cachedTable table = cachedTables.get(chashedPathes.indexOf(XMLpath));
  289. // if (chashedPathes.indexOf(XMLpath) < 0) {
  290. // cS.setExecuted(false);
  291. // System.out.println(XMLpath + " : Schema Not found");
  292. // throw ex;
  293. // } else {
  294. // if (!((data.getCondition().contains("=")) || (data.getCondition().contains(">")) || (data.getCondition().contains("<")))) {
  295. // cS.setUpdateCount(table.getRows().size());
  296. // table.setRows(new ArrayList<>());
  297. // ArrayList<Map> j = new ArrayList<>(table.getRows());
  298. // int i = 0;
  299. // for (i = 0; i < table.getRows().size(); i++){
  300. // j.set(i, data.getData());
  301. // }
  302. // table.setRows(j);
  303. // return cS ;
  304. // } else {
  305. // if (!((data.getCondition().contains("=")) || (data.getCondition().contains(">")) || (data.getCondition().contains("<")))) {
  306. // for (int R = 0;R<table.getRows().size();R++) {
  307. // boolean flag = false;
  308. // for (Object s : table.getRows().get(R).keySet()) {
  309. // for (Object sData : data.getData().keySet()) {
  310. // if (s.equals(sData)){
  311. // flag = true;
  312. // table.getRows().get(R).put(s,data.getData().get(sData));
  313. // cS.setUpdateCount(cS.getUpdateCount() + 1);
  314. // }
  315. // }
  316. // }
  317. // if (flag)
  318. // cS.setUpdateCount(cS.getUpdateCount() + 1);
  319. // }
  320. // } else {
  321. // for (int R = 0;R<table.getRows().size();R++) {
  322. // boolean flag = false;
  323. // String conditionString = data.getCondition();
  324. // String[] condition = conditionString.split(" *= *| *< *| *> *");
  325. // for (Object s : table.getRows().get(R).keySet()) {
  326. //// for (Object sData : data.getData().keySet()) {
  327. // if (condition[0].equals(s)){
  328. // if (util.conditionValidator((String) s,(String) table.getRows().get(R).get(s),data)) {
  329. // table.getRows().get(R).put(s, data.getData().get(s));
  330. // flag = true;
  331. // }
  332. // }
  333. //// }
  334. // }
  335. // if (flag)
  336. // cS.setUpdateCount(cS.getUpdateCount() + 1);
  337. // }
  338. // }
  339. // }
  340. // }
  341. // System.out.println("ROWS " + table.getRows());
  342. // System.out.println("VALUES " + table.getCoulmnsValues());
  343. // return cS;
  344. // }
  345. // }
  346.  
  347. public commandState select(queryParsedData data) throws SQLException, IOException, SAXException, ParserConfigurationException {
  348. commandState cS = new commandState();
  349. java.sql.SQLException ex = new java.sql.SQLException();
  350. String XMLpath = data.getDataBaseName()+System.getProperty("file.separator")+data.getTableName()+".xml";
  351. cachedTable table = cachedTables.get(chashedPathes.indexOf(XMLpath));
  352.  
  353. if(chashedPathes.indexOf(XMLpath)< 0){
  354. cS.setExecuted(false);
  355. System.out.println(XMLpath + " : Schema Not found");
  356. throw ex;
  357. }else {
  358. ArrayList<ArrayList<Object>> selected = new ArrayList<>();
  359. if(!((data.getCondition().contains("="))||(data.getCondition().contains(">"))||(data.getCondition().contains("<")))){
  360. if (data.isSelectAll()){
  361. for (int i=0;i<table.getRows().size();i++) {
  362. ArrayList<Object> NEW = new ArrayList<>();
  363. String temp = new String();
  364. for(Object s : table.getRows().get(i).keySet())
  365. temp = temp.concat("\n").concat((String) table.getRows().get(i).get(s)) ;
  366. NEW.add(temp);
  367. selected.add(NEW);
  368. }
  369. }
  370. else{
  371. ArrayList<Object> NEW = new ArrayList<>();
  372. for (int i=0;i<table.getRows().size();i++) {
  373. String temp = new String();
  374. boolean flag = false;
  375. for(Object s : table.getRows().get(i).keySet()) {
  376. NEW = new ArrayList<>();
  377. for(Object S : data.getData().keySet()){
  378. if (s.equals(S)) {
  379. temp = temp.concat("\n").concat((String) table.getRows().get(i).get(s));
  380. flag = true;
  381. }
  382. }
  383. if(flag)
  384. NEW.add(temp);
  385. }
  386. }
  387. if (!NEW.isEmpty())
  388. selected.add(NEW);
  389. }
  390. cS.setArray(selected);
  391. }
  392. else{
  393. if (data.isSelectAll()){
  394. String conditionString = data.getCondition();
  395. String[] condition = conditionString.split(" *= *| *< *| *> *");
  396. boolean flag = false;
  397. ArrayList<Object> NEW = new ArrayList<>();
  398. for (int i=0;i<table.getRows().size();i++) {
  399. NEW = new ArrayList<>();
  400. String temp = new String();
  401. for(Object s : table.getRows().get(i).keySet()) {
  402. temp = new String();
  403. if (s.equals(condition[0]))
  404. if (util.conditionValidator((String) s,(String) table.getRows().get(i).get(s),data)) {
  405. for(Object F : table.getRows().get(i).keySet()) {
  406. temp = temp.concat("\n").concat((String) table.getRows().get(i).get(F));
  407. flag = true;
  408. }
  409. }
  410. if (flag) {
  411. NEW.add(temp);
  412. flag = false;
  413. }
  414. }
  415. if(!(NEW.size()==0))
  416. selected.add(NEW);
  417. }
  418. }
  419. else{
  420. ArrayList<Object> NEW = new ArrayList<>();
  421. for (int i=0;i<table.getRows().size();i++) {
  422. String temp = new String();
  423. boolean flag = false;
  424. for(Object s : table.getRows().get(i).keySet()) {
  425. NEW = new ArrayList<>();
  426. for(Object S : data.getData().keySet()){
  427. String conditionString = data.getCondition();
  428. String[] condition = conditionString.split(" *= *| *< *| *> *");
  429. System.out.println("SELECT " + condition[0]+ " "+ s);
  430. if (condition[0].equals(s)) {
  431. flag = false;
  432. System.out.println("SELECT " + condition[0]);
  433. System.out.println("KKKKKKKKKKKK ");
  434. if (util.conditionValidator((String) s,(String) table.getRows().get(i).get(s),data)) {
  435. temp = temp.concat("\n").concat((String) table.getRows().get(i).get(s));
  436. flag = true;
  437. }
  438. if(flag)
  439. NEW.add(temp);
  440. }
  441. }
  442. if (NEW.size()!=0)
  443. selected.add(NEW);
  444. }
  445. }
  446. }
  447. cS.setArray(selected);
  448. }
  449. }
  450. System.out.println("ROWS "+table.getRows());
  451. System.out.println("VALUES "+table.getCoulmnsValues());
  452. return cS;
  453. }
  454.  
  455. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement