Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.96 KB | None | 0 0
  1. package eg.edu.alexu.csd.oop.db.cs39;
  2.  
  3. import java.beans.XMLDecoder;
  4. import java.beans.XMLEncoder;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileNotFoundException;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10. import java.lang.*;
  11. import java.util.*;
  12.  
  13. import org.junit.runners.ParentRunner;
  14.  
  15. public class Table {
  16.  
  17. String ParentDB;
  18. Vector<String> types = new Vector<String>();
  19. Vector<String> names = new Vector<String>();
  20. Vector<String> names2 = new Vector<String>();
  21. Map<String, String> col_type = new HashMap<String, String>();
  22. Map<String, String> col_type2 = new HashMap<String, String>();
  23. String Table_Name;
  24. File file;
  25. ArrayList<Vector<Object>> items = new ArrayList<Vector<Object>>();
  26.  
  27. public Table(String Table_Name, Vector<String> names, Vector<String> types, String ParentDB) {
  28.  
  29. this.ParentDB = ParentDB;
  30. this.Table_Name = Table_Name;
  31. this.types = (Vector<String>) types.clone();
  32. this.names = (Vector<String>) names.clone();
  33. for(String temp : names) {this.names2.add(temp.toUpperCase());}
  34. this.file = new File(this.ParentDB + "\\" + this.Table_Name + ".xml");
  35. for (int i = 0; i < types.size(); i++) {
  36. col_type.put(this.names.get(i), this.types.get(i));
  37. col_type2.put(this.names.get(i).toUpperCase(), this.types.get(i));
  38. }
  39.  
  40. }
  41. public Map<String, String> getCol_type2() {
  42. return col_type2;
  43. }
  44.  
  45. public void setCol_type2(Map<String, String> col_type2) {
  46. this.col_type2 = col_type2;
  47. }
  48.  
  49. public void setNames2(Vector<String> names2) {
  50. this.names2 = names2;
  51. }
  52. public Table(String Table_Name, Vector<String> names, Vector<String> types) {
  53. this.Table_Name = Table_Name;
  54. this.types = (Vector<String>) types.clone();
  55. this.names = (Vector<String>) names.clone();
  56. for(String temp : names) {this.names2.add(temp.toUpperCase());}
  57. for (int i = 0; i < types.size(); i++) {
  58. col_type.put(this.names.get(i), this.types.get(i));
  59. col_type2.put(this.names.get(i).toUpperCase(), this.types.get(i));
  60. }
  61. }
  62.  
  63. public String getParentDB() {
  64. return ParentDB;
  65. }
  66.  
  67. public void setParentDB(String parentDB) {
  68. ParentDB = parentDB;
  69. }
  70.  
  71. public Vector<String> getTypes() {
  72. return types;
  73. }
  74.  
  75. public void setTypes(Vector<String> types) {
  76. this.types = types;
  77. }
  78.  
  79. public Vector<String> getNames() {
  80. return names;
  81. }
  82.  
  83. public Vector<String> getNames2() {
  84. return names2;
  85. }
  86.  
  87. public void setNames(Vector<String> names) {
  88. this.names = names;
  89. }
  90.  
  91. public Map<String, String> getCol_type() {
  92. return col_type;
  93. }
  94.  
  95. public void setCol_type(Map<String, String> col_type) {
  96. this.col_type = col_type;
  97. }
  98.  
  99. public String getTable_Name() {
  100. return Table_Name;
  101. }
  102.  
  103. public void setTable_Name(String table_Name) {
  104. Table_Name = table_Name;
  105. }
  106.  
  107. public void setItems(ArrayList<Vector<Object>> items) {
  108. this.items = items;
  109. }
  110.  
  111. public Table(String ParentDB) {
  112. this.ParentDB = ParentDB;
  113. }
  114.  
  115. public Table() {
  116.  
  117. }
  118.  
  119. public int InsertIntoTable(Vector<Object> input) throws Exception {
  120.  
  121. if (input == null || input.size() != names.size()) {
  122. return 0;
  123. }
  124.  
  125. for (int i = 0; i < types.size(); i++) {
  126. String a = input.get(i).getClass().getSimpleName();
  127. if (types.get(i).compareTo("VARCHAR")==0) {
  128. if (a.compareTo("String") != 0) {
  129. //System.out.println("Invalid Input");
  130. return 0;
  131. }
  132. } else {
  133. if (a.compareTo("Integer") != 0) {
  134. //System.out.println("Invalid Input");
  135. return 0;
  136. }
  137. }
  138. }
  139. items.add((Vector<Object>) input.clone());
  140. //System.out.println("Insert is done");
  141. return 1;
  142. }
  143.  
  144. public int DeleteFromTable(int Row)// Delete from table Where col=value and rows suppose to start from 1
  145. {
  146. if (Row == 0)// if WHERE isn't there then delete all items in the table
  147. { int x = items.size() ;
  148. items.clear();
  149. return x;
  150. }
  151. items.remove(Row - 1);
  152. return 0;
  153. }
  154.  
  155. public int DeleteFromTableWithCondition(String field, String ID)// example Delete From Table WHERE employeID=3
  156. { int X=names2.indexOf(field);
  157. int counter = 0;
  158. for (int i = 0; i < items.size(); i++) {
  159. String j = items.get(i).get(X).toString();
  160. if (j.compareToIgnoreCase(ID) == 0) {
  161. DeleteFromTable(i + 1);
  162. i--;
  163. counter++;
  164. }
  165. }
  166. return counter;
  167. }
  168.  
  169. /**
  170. *
  171. * @param col == to the names of columns(equal to the names when the * in the
  172. * command line
  173. * @return new table with the desired colomns
  174. * @throws Exception
  175. */
  176. public Table SelectFromTable(Vector<String> col) throws Exception {
  177. System.out.println(col);
  178. Table New = new Table(this.ParentDB);
  179. Vector<Integer> index = new Vector<Integer>();
  180. for (int i = 0; i < col.size(); i++) {
  181. String a = col.get(i);
  182. String b = col_type2.get(a.toUpperCase());
  183. New.names.add(a);
  184. New.types.add(b);
  185. index.add(names.indexOf(a));
  186. }
  187. for (int i = 0; i < items.size(); i++) {
  188. Vector<Object> input = new Vector<Object>();
  189. for (int j = 0; j < index.size(); j++) {
  190. int x = index.get(j);
  191. input.add(items.get(i).get(x));
  192. }
  193. New.InsertIntoTable(input);
  194. }
  195. return New;
  196. }
  197.  
  198. public Object[][] SelectFromTable2DArray(Vector<String> col) throws Exception {
  199. Table a = SelectFromTable(col);
  200. if(a.items.size()>0) {
  201. Object[][] out = new Object[a.items.size()][a.items.get(0).size()];
  202. for (int i = 0; i < a.items.size(); i++) {
  203. for (int j = 0; j < a.items.get(0).size(); j++) {
  204. out[i][j] = a.items.get(i).get(j);
  205. }
  206. }
  207. return out;
  208. }
  209. else{
  210.  
  211. Object[][] out1 = new Object[0][0];
  212. return out1 ;
  213. }
  214. }
  215.  
  216. public Object[][] SelectTable() throws Exception {
  217. return SelectFromTable2DArray(this.names);
  218. }
  219.  
  220. /**
  221. *
  222. * @param type =0 when equal type =1 when bigger type =-1 when smaller
  223. * @param field = colomn name
  224. * @param Condition
  225. * @return
  226. */
  227. public Object[][] SelectFromTableCondition(int type, String field, String Condition) {
  228. if (items.size() == 0) {
  229. return null;
  230. }
  231. ArrayList<Vector<Object>> a = new ArrayList<Vector<Object>>();
  232. int X = names2.indexOf(field);
  233.  
  234. for (int i = 0; i < items.size(); i++) {
  235.  
  236. String j = items.get(i).get(X).toString();
  237. if (this.col_type.get(names.get(X)).compareTo("INT") == 0) {
  238. int x = Integer.parseInt(j);
  239. int y = Integer.parseInt(Condition);
  240. if (type == 0 && x == y) {
  241. a.add((Vector<Object>) items.get(i).clone());
  242. } else if (type == 1 && x > y) {
  243. a.add((Vector<Object>) items.get(i).clone());
  244. } else if (type == -1 && x < y) {
  245. a.add((Vector<Object>) items.get(i).clone());
  246. }
  247. } else {
  248. if (type == 0) {
  249. if (j.compareTo(Condition) == 0) {
  250. a.add((Vector<Object>) items.get(i).clone());
  251. }
  252. } else if (type == -1) {
  253. if (j.compareTo(Condition) < 0) {
  254. a.add((Vector<Object>) items.get(i).clone());
  255. }
  256. } else {
  257. if (j.compareTo(Condition) > 0) {
  258. a.add((Vector<Object>) items.get(i).clone());
  259. }
  260. }
  261. }
  262.  
  263. }
  264. Object[][] out = new Object[a.size()][items.get(0).size()];
  265. for (int i = 0; i < a.size(); i++) {
  266. for (int j = 0; j < items.get(0).size(); j++) {
  267. out[i][j] = a.get(i).get(j);
  268. }
  269. }
  270. return out;
  271. }
  272. public Object[][] SelectFromTableMultipleCondition(String field,Vector<String>Condition)
  273. {
  274. //example SELECT * FROM Customers
  275. //WHERE Country IN ('Germany', 'France', 'UK');
  276. if (items.size() == 0) {
  277. return null;
  278. }
  279. ArrayList<Vector<Object>> a = new ArrayList<Vector<Object>>();
  280. int X = names2.indexOf(field);
  281.  
  282. for (int i = 0; i < items.size(); i++) {
  283.  
  284. String j = items.get(i).get(X).toString();
  285.  
  286. for(int k=0;k<Condition.size();k++)
  287. {
  288. if (this.col_type2.get(field).compareTo("INT") == 0) {
  289. int x = Integer.parseInt(j);
  290. int y = Integer.parseInt(Condition.get(k));
  291. if (x == y) {
  292. a.add((Vector<Object>) items.get(i).clone());
  293. break;
  294. }
  295. } else {
  296. if (j.compareToIgnoreCase(Condition.get(k)) == 0) {
  297. a.add((Vector<Object>) items.get(i).clone());
  298. break;
  299. }
  300. }
  301. }
  302. }
  303. Object[][] out = new Object[a.size()][items.get(0).size()];
  304. for (int i = 0; i < a.size(); i++) {
  305. for (int j = 0; j < items.get(0).size(); j++) {
  306. out[i][j] = a.get(i).get(j);
  307. }
  308. }
  309. return out;
  310.  
  311.  
  312. }
  313. /**
  314. *
  315. * @param field the column which will be selected
  316. * @param Condition the condition field
  317. * @param ID the condition field=ID
  318. */
  319. public Object[][] SelectCell(int type,String field,String Condition,String ID)
  320. {
  321. if(items.size()==0)
  322. {
  323. return null;
  324. }
  325. //System.out.println(field);
  326. // System.out.println(Condition);
  327. // System.out.println(ID);
  328. int X=0;
  329. int Y=0;
  330. X=names2.indexOf(Condition);
  331. Y=names2.indexOf(field);
  332. Object [][]out=new Object[items.size()][1];
  333. int k =0;
  334. for(int i=0;i<items.size();i++)
  335. {
  336. String j=items.get(i).get(X).toString();
  337. if (this.col_type.get(names.get(X)).compareTo("INT") == 0) {
  338. int x = Integer.parseInt(j);
  339. int y = Integer.parseInt(ID);
  340. if (type == 0 && x == y) {
  341. out[k][0]=items.get(i).get(Y);
  342. k++;
  343. } else if (type == 1 && x > y) {
  344. out[k][0]=items.get(i).get(Y);
  345. k++;
  346. } else if (type == -1 && x < y) {
  347. out[k][0]=items.get(i).get(Y);
  348. k++;
  349. }
  350. }
  351. else {
  352.  
  353. if (type == 0 && j.compareTo(ID)==0) {
  354. out[k][0]=items.get(i).get(Y);
  355. k++;
  356. } else if (type == 1 && j.compareTo(ID)>0) {
  357. System.out.println(ID+" and "+j);
  358. out[k][0]=items.get(i).get(Y);
  359. k++;
  360. } else if (type == -1 && j.compareTo(ID)<0) {
  361. out[k][0]=items.get(i).get(Y);
  362. k++;
  363. }
  364. }
  365. }
  366. Object [][]out1=new Object[k][1];
  367. for(int i=0;i<k;i++) {
  368. out1[i][0]=out[i][0];
  369. }
  370. return out1 ;
  371.  
  372. }
  373. public void Update(int Row, String field, String NewValue)// Rows starts with 1
  374. {
  375. int X = names2.indexOf(field);
  376. if (types.get(X).compareTo("VARCHAR") == 0) {
  377. items.get(Row - 1).setElementAt(NewValue, X);
  378. } else {
  379. int New = Integer.parseInt(NewValue);
  380. items.get(Row - 1).setElementAt(New, X);
  381. }
  382. }
  383.  
  384. public int UpdateWithCondition(int type, String Condition, String ID, String field, String NewValue) {// type=0
  385. // means
  386. // equal // type =1
  387. // means
  388. // bigger
  389. // type =-1
  390. // means
  391. // smaller
  392. int X=names2.indexOf(ID);
  393. int count = 0;
  394. for (int i = 0; i < items.size(); i++) {
  395. String j = items.get(i).get(X).toString().toUpperCase();
  396. if (this.col_type2.get(ID).compareTo("INT") == 0) {
  397. int x = Integer.parseInt(j);
  398. int y = Integer.parseInt(Condition);
  399. System.out.println("asdasdsa");
  400. if (type == 0 && x == y) {
  401. Update(i + 1, field, NewValue);
  402. count++;
  403. } else if (type == 1 && x > y) {
  404. Update(i + 1, field, NewValue);
  405. count++;
  406. } else if (type == -1 && x < y) {
  407. Update(i + 1, field, NewValue);
  408. count++;
  409. }
  410. }
  411. else
  412. {
  413. if (type == 0) {
  414. if (j.compareTo(Condition) == 0) {
  415. Update(i + 1, field, NewValue);
  416. count++;
  417. }
  418. } else if (type == -1) {
  419. if (j.compareTo(Condition) < 0) {
  420. Update(i + 1, field, NewValue);
  421. count++;
  422. }
  423. } else {
  424. if (j.compareTo(Condition) > 0) {
  425. Update(i + 1, field, NewValue);
  426. count++;
  427. }
  428. }
  429. }
  430. }
  431.  
  432. return count;
  433.  
  434. }
  435.  
  436. public int Updatecolumns (Vector<String> col,Vector<Object> values) {
  437. int count=0;
  438. // System.out.println("el size"+items.size());
  439. for(int i=0;i<items.size();i++) {
  440. count++;
  441. for(int j=0;j<items.get(0).size();j++) {
  442. Update(i + 1,col.get(j), (String) values.get(j));
  443. // items.get(i).setElementAt(values.get(j),j);
  444. }
  445. }
  446. System.out.println("el count"+count);
  447. return count;
  448. }
  449.  
  450. public int Updatecolumnscondition(Vector<String> col,Vector<Object> values,String column,String value) {
  451. int X=names2.indexOf(column),row=-1;
  452. int count=0;
  453.  
  454. for(int i=0;i<items.size();i++) {
  455. if(value.compareToIgnoreCase(items.get(i).get(X).toString())==0) {
  456.  
  457. row=i;
  458. count++;
  459. }
  460.  
  461. }
  462. if(row==-1) {return 0;}
  463. int i=0;
  464. for(Object O : values) {
  465. items.get(row).setElementAt(O,names2.indexOf(col.get(i++)));
  466.  
  467. }
  468. //System.out.println("count="+count);
  469. return count;
  470. }
  471.  
  472.  
  473.  
  474.  
  475. public ArrayList<Vector<Object>> getItems() {
  476. return items;
  477. }
  478.  
  479. public void SaveTable(String mainDirectoryPath) throws Exception// assumed that you have created a database that will contian the table
  480. {
  481. FileOutputStream file ;
  482. if(mainDirectoryPath==null) {
  483.  
  484. file = new FileOutputStream(ParentDB + "\\" + Table_Name + ".xml");
  485. }else {
  486.  
  487. file = new FileOutputStream(mainDirectoryPath+"\\"+ParentDB + "\\" + Table_Name + ".xml");
  488. }
  489. XMLEncoder a = new XMLEncoder(file);
  490. Table s = new Table(this.Table_Name, this.names, this.types, this.ParentDB);
  491. s.items=(ArrayList<Vector<Object>>) SelectFromTable(this.names).getItems().clone();
  492.  
  493. a.writeObject(s);
  494. a.close();
  495. file.close();
  496. }
  497.  
  498. public Table LoadTable(String path) throws IOException
  499. // Table h=new Table(); h=h.load(path);
  500. {
  501. FileInputStream file = new FileInputStream(path);
  502. XMLDecoder a = new XMLDecoder(file);
  503. Table s = (Table) a.readObject();
  504. s.file=new File(path);
  505. a.close();
  506. file.close();
  507. return s;
  508. }
  509.  
  510. public File getFile() {
  511. return file;
  512. }
  513.  
  514. public void DropTable() throws Exception// delete the table file and the table it self
  515. {
  516. File file = getFile();
  517. file.delete();
  518. }
  519. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement