Advertisement
Guest User

Untitled

a guest
Sep 11th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.43 KB | None | 0 0
  1. package DataClasses.UserData;
  2.  
  3. import DataClasses.ItemData.Item;
  4. import DataClasses.ItemData.Location;
  5. import com.mongodb.*;
  6. import com.mongodb.client.MongoDatabase;
  7. import org.bson.Document;
  8. import com.mongodb.client.MongoCollection;
  9.  
  10. import com.mongodb.util.JSON;
  11.  
  12. import java.io.*;
  13. import java.text.SimpleDateFormat;
  14. import java.time.LocalDateTime;
  15. import java.time.format.DateTimeFormatter;
  16. import java.util.ArrayList;
  17. import java.util.Calendar;
  18. import java.util.List;
  19. import java.util.Locale;
  20.  
  21. import org.json.simple.JSONObject;
  22. import org.json.simple.JSONArray;
  23. import org.json.simple.parser.JSONParser;
  24. import org.json.simple.parser.ParseException;
  25.  
  26. public class User {
  27. private String username; //gia tous users apo ta .xml arxeia einai idio me to _UserID
  28. private String password; //gia tous users apo ta .xml arxeia einai "1" gia olous
  29. private String _UserID;
  30. private String verified;
  31.  
  32. private String surname;
  33. private String first_name;
  34.  
  35. private String email;
  36. private String afm;
  37. private String phone_number;
  38.  
  39. private DataClasses.ItemData.Location Location;
  40. private String Country;
  41.  
  42. private String Bidder_Rating;
  43. private String Seller_Rating;
  44.  
  45. public User() {
  46. this.username = null;
  47. this.password = null;
  48. }
  49. public User(String sUserName, String sUserPassword) {
  50. this.username = sUserName;
  51. this.password = sUserPassword;
  52. }
  53. public User(String sUserName) {
  54. this.username = sUserName;
  55. }
  56. public User(String sUserName, String sUserPassword, String _UserID,
  57. String surname, String first_name, String email, String afm, String phone_number,
  58. String Location, String Longitude, String Latitude,String Country,
  59. String Bidder_Rating, String Seller_Rating){
  60. this.username = sUserName;
  61. this.password = sUserPassword;
  62. this._UserID = _UserID;
  63.  
  64. this.surname = surname;
  65. this.first_name = first_name;
  66. this.email = email;
  67. this.afm = afm;
  68. this.phone_number = phone_number;
  69.  
  70. this.Location = new Location();
  71. this.Location.set__text(Location);
  72. this.Location.set_Latitude(Latitude);
  73. this.Location.set_Longitude(Longitude);
  74. this.Country = Country;
  75.  
  76. this.Bidder_Rating = Bidder_Rating;
  77. this.Seller_Rating = Seller_Rating;
  78. }
  79.  
  80. public boolean isValidUserCredentials(String sUserName, String sUserPassword){
  81. Mongo mongo = new Mongo("localhost", 27017);
  82. DB db = mongo.getDB("ergasia");
  83. DBCollection collection = db.getCollection("user");
  84.  
  85. BasicDBObject query = new BasicDBObject();;
  86. query.put("username", sUserName);
  87. query.put("password", sUserPassword);
  88. query.put("verified", "true");
  89. DBCursor cursor = collection.find(query);
  90. if (cursor.hasNext()) {
  91. cursor.next();
  92. System.out.println(cursor.curr().toString());
  93. return true;
  94. }
  95. // username doesn't exists
  96. return false;
  97. }
  98.  
  99. public boolean UserVerified(String sUserName, String sUserPassword){
  100. Mongo mongo = new Mongo("localhost", 27017);
  101. DB db = mongo.getDB("ergasia");
  102. DBCollection collection = db.getCollection("user");
  103.  
  104. BasicDBObject query = new BasicDBObject();;
  105. query.put("username", sUserName);
  106. query.put("password", sUserPassword);
  107. query.put("verified", "false");
  108. DBCursor cursor = collection.find(query);
  109. if (cursor.hasNext()) {
  110. cursor.next();
  111. System.out.println(cursor.curr().toString());
  112. return true;
  113. }
  114. // username doesn't exists
  115. return false;
  116. }
  117.  
  118. public String ValidateRegistration(String sUserName, String sUserPassword, String sConPassword){
  119. MongoClient mongoClient = new MongoClient("localhost", 27017);
  120. MongoDatabase database = mongoClient.getDatabase("ergasia");
  121. MongoCollection<Document> collection = database.getCollection("user");
  122.  
  123. List<Document> users = collection.find().into(new ArrayList<Document>());
  124. //elegxos egkurothtas kwdikou
  125. if(!sUserPassword.equals(sConPassword)){ //failare to confirm password
  126. return "Confirmation Error";
  127. }
  128.  
  129. //prepei na brei ean yparxei xrhsths me idio name
  130. if(this.name_exists().equals("name already exists")){
  131. return "name already exists";
  132. }
  133.  
  134. Mongo mongo = new Mongo("localhost", 27017);
  135. DB db = mongo.getDB("ergasia");
  136. DBCollection collection1 = db.getCollection("user");
  137.  
  138. BasicDBObject user = new BasicDBObject();
  139. user.put("username",this.username);
  140. user.put("password", this.password);
  141. user.put("verified",this.verified);
  142.  
  143. user.put("surname",this.surname);
  144. user.put("first_name",this.first_name);
  145.  
  146. user.put("email",this.email);
  147. user.put("afm",this.afm);
  148. user.put("phone_number",this.phone_number);
  149.  
  150. if(this.Location.get_Latitude().equals("")) {
  151. user.put("Location", this.Location.get__text());
  152. }
  153. else {
  154. JSONObject Location = new JSONObject();
  155.  
  156. Location.put("_Latitude", this.Location.get_Latitude());
  157. Location.put("_Longitude", this.Location.get_Longitude());
  158. Location.put("__text", this.Location.get__text());
  159.  
  160. user.put("Location", Location);
  161. }
  162. user.put("Country",this.Country);
  163.  
  164. user.put("Seller_Rating",this.Seller_Rating);
  165. user.put("Bidder_Rating",this.Bidder_Rating);
  166. user.put("_UserID", this._UserID);
  167.  
  168. collection1.insert(user);
  169.  
  170. return "OK";
  171. }
  172.  
  173. public String GetUsers() throws IOException {
  174. MongoClient mongoClient = new MongoClient("localhost", 27017);
  175. MongoDatabase database = mongoClient.getDatabase("ergasia");
  176. MongoCollection<Document> collection = database.getCollection("user");
  177.  
  178. List<Document> users = collection.find().into(new ArrayList<Document>());
  179. JSONArray list = new JSONArray();
  180. for (Document user : users) {
  181. JSONObject obj = new JSONObject();
  182. obj.put("_id",user.getObjectId("_id"));
  183. obj.put("username",user.getString("username"));
  184. obj.put("password",user.getString("password"));
  185.  
  186. list.add(obj);
  187. }
  188.  
  189. JSONObject mainObj = new JSONObject();
  190. mainObj.put("users", list);
  191.  
  192. StringWriter out = new StringWriter();
  193. mainObj.writeJSONString(out);
  194.  
  195. String jsonText = out.toString();
  196. System.out.print(jsonText);
  197. return jsonText;
  198. }
  199.  
  200. public String name_exists(){
  201. Mongo mongo = new Mongo("localhost", 27017);
  202. DB db = mongo.getDB("ergasia");
  203. DBCollection collection = db.getCollection("user");
  204.  
  205. BasicDBObject query = new BasicDBObject();
  206. query.put("username", this.username);
  207. DBCursor cursor = collection.find(query);
  208.  
  209. if (cursor.hasNext()){
  210. return "name already exists";
  211. }
  212.  
  213. return "name doesn't exist";
  214. }
  215.  
  216. public String get_username(){
  217. return this.username;
  218. }
  219. public String get_UserID(){
  220. return this._UserID;
  221. }
  222. public String get_password(){
  223. return this.password;
  224. }
  225. public String get_Location(){
  226. return this.Location.get__text();
  227. }
  228. public String get_Country(){
  229. return this.Country;
  230. }
  231. public String get_Bidder_Rating(){
  232. return this.Bidder_Rating;
  233. }
  234. public String get_Seller_Rating(){
  235. return this.Seller_Rating;
  236. }
  237.  
  238. public String find_UserID(){
  239. // String UserID = null;
  240. // JSONArray array = new JSONArray();
  241. Mongo mongo = new Mongo("localhost", 27017);
  242. DB db = mongo.getDB("ergasia");
  243. DBCollection collection = db.getCollection("user");
  244.  
  245. BasicDBObject Query = new BasicDBObject();
  246. Query.put("username", this.username);
  247. DBCursor cursor = collection.find(Query);
  248. if (cursor.hasNext()) {
  249. System.out.println(cursor.next());
  250. return (String)(cursor.curr().get("_UserID"));
  251. }
  252. return null;
  253. }
  254.  
  255. public String find_Seller_Rating(){
  256. // JSONArray array = new JSONArray();
  257. Mongo mongo = new Mongo("localhost", 27017);
  258. DB db = mongo.getDB("ergasia");
  259. DBCollection collection = db.getCollection("user");
  260.  
  261. BasicDBObject Query = new BasicDBObject();
  262. Query.put("username", this.username);
  263. DBCursor cursor = collection.find(Query);
  264. if (cursor.hasNext()) {
  265. System.out.println(cursor.next());
  266. return (String)(cursor.curr().get("Seller_Rating"));
  267. }
  268. return null;
  269. }
  270.  
  271. public String get_user(){
  272. //prepei na brei ean yparxei xrhsths me idio name
  273. if(this.name_exists().equals("name doesn't exist")){
  274. return "name doesn't exist";
  275. }
  276.  
  277. Mongo mongo = new Mongo("localhost", 27017);
  278. DB db = mongo.getDB("ergasia");
  279. DBCollection collection = db.getCollection("user");
  280.  
  281. BasicDBObject query = new BasicDBObject();
  282. query.put("username", this.username);
  283. DBCursor cursor = collection.find(query);
  284.  
  285. JSONObject user = new JSONObject();
  286. if (cursor.hasNext()){
  287. cursor.next();
  288.  
  289. user.put("username",cursor.curr().get("username"));
  290. user.put("password",cursor.curr().get("password"));
  291. user.put("_UserID",cursor.curr().get("_UserID"));
  292.  
  293. user.put("surname",cursor.curr().get("surname"));
  294. user.put("first_name",cursor.curr().get("first_name"));
  295.  
  296. user.put("email",cursor.curr().get("email"));
  297. user.put("afm",cursor.curr().get("afm"));
  298. user.put("phone_number",cursor.curr().get("phone_number"));
  299.  
  300. user.put("Location",cursor.curr().get("Location"));
  301. // user.put("",cursor.curr().get(""));
  302. // user.put("",cursor.curr().get(""));
  303. user.put("Country",cursor.curr().get("Country"));
  304.  
  305. user.put("Seller_Rating",cursor.curr().get("Seller_Rating"));
  306. user.put("Bidder_Rating",cursor.curr().get("Bidder_Rating"));
  307. }
  308.  
  309. return user.toJSONString();
  310. }
  311.  
  312. public String verify_user(){
  313. //prepei na brei ean yparxei xrhsths me idio name
  314. if(this.name_exists().equals("name doesn't exist")){
  315. return "name doesn't exist";
  316. }
  317.  
  318. Mongo mongo = new Mongo("localhost", 27017);
  319. DB db = mongo.getDB("ergasia");
  320. DBCollection collection = db.getCollection("user");
  321.  
  322.  
  323. BasicDBObject newDocument = new BasicDBObject();
  324. JSONObject temp_object = new JSONObject();
  325. temp_object.put("username", this.username);
  326. newDocument.append("$set", new BasicDBObject().append("verified", "true"));
  327. BasicDBObject searchQuery = new BasicDBObject().append("username", this.username);
  328. collection.update(searchQuery, newDocument);
  329.  
  330. JSONObject obj = new JSONObject();
  331. obj.put("message", "ok");
  332. return obj.toJSONString();
  333. }
  334.  
  335. public String users_paging(int page, int n, String Verified){
  336. Mongo mongo = new Mongo("localhost", 27017);
  337. DB db = mongo.getDB("ergasia");
  338. DBCollection collection = db.getCollection("user");
  339.  
  340. int count=0;
  341. JSONArray array = new JSONArray();
  342. BasicDBObject Query = new BasicDBObject();
  343.  
  344. Query.put("verified", "false");
  345. DBCursor cursor = collection.find(Query);
  346. if( Verified.equals("All") ){
  347. cursor = collection.find();
  348. }
  349. else if( Verified.equals("Yes") ){
  350. Query.put("verified", "true");
  351. cursor = collection.find(Query);
  352. }
  353.  
  354. cursor.skip(n*(page-1));
  355. while(cursor.hasNext()) {
  356. System.out.println(cursor.next());
  357.  
  358. JSONObject temp_user = new JSONObject();
  359. temp_user.put("username", cursor.curr().get("username"));
  360. temp_user.put("verified", cursor.curr().get("verified"));
  361. array.add(temp_user);
  362. count++;
  363. if (count == n) { break; }
  364. }
  365. JSONObject result = new JSONObject();
  366. result.put("n", count);
  367. result.put("items",array);
  368.  
  369. return result.toJSONString();
  370. // return array.toJSONString();
  371. }
  372.  
  373. public String get_auctions(String active) throws ParseException {
  374. int n=0;
  375. int count=0;
  376.  
  377. //prepei na brei ean yparxei xrhsths me idio name
  378. if(this.name_exists().equals("name doesn't exist")){
  379. return "name doesn't exist";
  380. }
  381.  
  382. // get current date
  383. DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMM-dd-yy kk:mm:ss", Locale.ENGLISH);
  384. LocalDateTime now = LocalDateTime.now();
  385. // String current_date = dtf.format(now);
  386.  
  387. Mongo mongo = new Mongo("localhost", 27017);
  388. DB db = mongo.getDB("ergasia");
  389. DBCollection collection = db.getCollection("user");
  390.  
  391. BasicDBObject query = new BasicDBObject();
  392. query.put("username", this.username);
  393. DBCursor cursor = collection.find(query);
  394.  
  395. JSONArray items_array = new JSONArray();
  396. if (cursor.hasNext()) {
  397. cursor.next();
  398.  
  399. if(cursor.curr().get("SoldItems")!=null){
  400. String sold_items = cursor.curr().get("SoldItems").toString();
  401. JSONParser parser = new org.json.simple.parser.JSONParser();
  402. JSONArray SoldItems = (JSONArray) parser.parse(sold_items);
  403. n = SoldItems.size();
  404. for (Object o : SoldItems){
  405. JSONObject sold_item_obj = (JSONObject)o;
  406. // find item
  407. String item_id = (String)sold_item_obj.get("_ItemID");
  408. Item temp_item = new Item(item_id);
  409. String item_info = temp_item.get_item();
  410. // create item object
  411. JSONParser parser1 = new org.json.simple.parser.JSONParser();
  412. JSONObject item = (JSONObject) parser1.parse(item_info);
  413.  
  414. JSONObject temp_item_obj = new JSONObject();
  415. temp_item_obj.put("Name",item.get("Name"));
  416. temp_item_obj.put("Category",item.get("Category"));
  417. temp_item_obj.put("Currently",item.get("Currently"));
  418. if( item.get("Buy_Price") != null ) {
  419. temp_item_obj.put("Buy_Price", item.get("Buy_Price"));
  420. }
  421. temp_item_obj.put("First_Bid",item.get("First_Bid"));
  422. temp_item_obj.put("Number_of_Bids",item.get("Number_of_Bids"));
  423. if( item.get("Bids") != null ) {
  424. temp_item_obj.put("Bids", item.get("Bids"));
  425. }
  426. temp_item_obj.put("Location",item.get("Location"));
  427. temp_item_obj.put("Country",item.get("Country"));
  428. temp_item_obj.put("Started",item.get("Started"));
  429. temp_item_obj.put("Ends",item.get("Ends"));
  430. temp_item_obj.put("Seller",item.get("Seller"));
  431. temp_item_obj.put("Description",item.get("Description"));
  432. temp_item_obj.put("_ItemID",item.get("_ItemID"));
  433.  
  434. // compare dates, active inactive
  435. String date_str = (String)item.get("Ends");
  436. LocalDateTime ends = LocalDateTime.parse(date_str, dtf);
  437.  
  438. if (ends.compareTo(now) > 0) {
  439. temp_item_obj.put("active","true");
  440. System.out.println("ends > now");
  441. if(active.equals("true") || active.equals("All")){
  442. items_array.add(temp_item_obj);
  443. count++;
  444. }
  445. } else if (ends.compareTo(now) < 0) {
  446. temp_item_obj.put("active","false");
  447. System.out.println("ends > now");
  448. if(active.equals("false") || active.equals("All")){
  449. items_array.add(temp_item_obj);
  450. count++;
  451. }
  452. } else if (ends.compareTo(now) == 0) {
  453. temp_item_obj.put("active","false");
  454. System.out.println("ends == now");
  455. if(active.equals("false") || active.equals("All")){
  456. items_array.add(temp_item_obj);
  457. count++;
  458. }
  459. }
  460. }
  461. }
  462. }
  463. JSONObject obj = new JSONObject();
  464. obj.put("SoldItems",items_array);
  465. obj.put("n",String.valueOf(n));
  466. obj.put("count",String.valueOf(count));
  467. return obj.toJSONString();
  468. }
  469.  
  470. public String delete_sold_item(String item_id){
  471. Mongo mongo = new Mongo("localhost", 27017);
  472. DB db = mongo.getDB("ergasia");
  473. DBCollection collection = db.getCollection("user");
  474.  
  475. BasicDBObject newDocument = new BasicDBObject();
  476. JSONObject temp_object = new JSONObject();
  477. temp_object.put("_ItemID", item_id);
  478. newDocument.append("$pull", new BasicDBObject().append("SoldItems", temp_object));
  479. BasicDBObject searchQuery = new BasicDBObject().append("username", this.username);
  480. collection.update(searchQuery, newDocument);
  481.  
  482. return "OK!";
  483. }
  484.  
  485. public void add_visited_item(String item_id){
  486. Mongo mongo = new Mongo("localhost", 27017);
  487. DB db = mongo.getDB("ergasia");
  488. DBCollection collection = db.getCollection("user");
  489.  
  490. // update user VisitedItems
  491. JSONObject temp_obj = new JSONObject();
  492. temp_obj.put("_ItemID", item_id);
  493. BasicDBObject newDocument = new BasicDBObject();
  494. newDocument.append("$push", new BasicDBObject().append("VisitedItems", temp_obj));
  495. BasicDBObject searchQuery = new BasicDBObject().append("_UserID", this.username);
  496. collection.update(searchQuery, newDocument);
  497. }
  498. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement