Facebook
From Marcin, 5 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 241
  1.  
  2. package serwer;
  3.  
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.text.DateFormat;
  12. import java.text.SimpleDateFormat;
  13. import java.util.ArrayList;
  14. import java.util.Calendar;
  15. import java.util.Date;
  16. import java.util.List;
  17.  
  18. /*
  19.  * Klasa implementujaca operacje obslugujace polecenia przychodzacych do serwera
  20.  * Operacje jako argumenty wejsciowe otrzymuje wartosci dostarczone w poleceniu
  21.  * Operacje wykonuja zapytania do bazy danych
  22.  * nastepnie zwraca odpowiedz jako true/false lub pobrane z bazy rekordy
  23.  *
  24.  * @author Marcin Celejewski
  25.  * @version v1.0
  26.  * @see Serwer.java
  27.  * @see FILReply.java
  28.  * @see FILReplyElement.java
  29.  * @param c polaczenie z baza danych
  30.  * @param url url bazy danych
  31.  * @param user uzytkownik laczacy sie z baza danych
  32.  * @param password haslo sluzace do laczenia sie z baza danych
  33.  * @param logfile sciezka do zapisania loga jednego polaczenia
  34.  *
  35.  */
  36. public class Operations {
  37.         private Connection c;
  38.         // private final String url = "jdbc:postgresql://192.168.1.10/cogdziekiedy";
  39.         private final String url = "jdbc:postgresql://127.0.0.1/postgres";
  40.         private final String user = "serwer";
  41.         private final String password = "bananapi";
  42.         private String logfile;
  43.  
  44.         /*
  45.          * Konstruktor tworzacy nazwe kolejnego loga
  46.          * @param ip ip uzytkownika ktory wyslal polecenie
  47.          * @return nothing
  48.          *
  49.          */
  50.         public Operations(String ip) {
  51.                 DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
  52.                 Date date = new Date();
  53.  
  54.                 System.out.println("D:\\logiserwera\\" + dateFormat.format(date) + "_" + ip + ".log");
  55.                 logfile = "D:\\logiserwera\\" + dateFormat.format(date) + "_" + ip + ".log";
  56.  
  57.         }
  58.  
  59.         /*
  60.          * Metoda zapisujaca logi do pliku
  61.          * @param s string ktory chcemy zapisac do logow
  62.          * @return nothing
  63.          * @exception IOException On input error.
  64.          * @see IOException
  65.          *
  66.          */
  67.         void log(String s) {
  68.                 FileWriter fw;
  69.                 try {
  70.                         fw = new FileWriter(logfile, true);
  71.  
  72.                         fw.write("\n" + s + "\n");
  73.                         fw.close();
  74.                         System.out.println(s);
  75.  
  76.                 } catch (IOException e) {
  77.                         e.printStackTrace();
  78.                 }
  79.  
  80.         }
  81.  
  82.         /*
  83.          * Metoda sluzaca do polaczenia sie z baza danych
  84.          * @param nothing
  85.          * @return ifconnected
  86.          * @exception Exception
  87.          * @see Exception
  88.          *
  89.          */
  90.         public boolean connect() {
  91.                 try {
  92.                         Class.forName("org.postgresql.Driver");
  93.  
  94.                         c = DriverManager.getConnection(url, user, password);
  95.                 } catch (Exception e) {
  96.                         e.printStackTrace();
  97.                         log("Cannot connect to databsase. " + e.toString());
  98.                         return false;
  99.                 }
  100.                 log("Opened database successfully");
  101.                 return true;
  102.         }
  103.         /*
  104.          * Metoda obsluzaca logowanie sie do systemu
  105.          * @param login login uzytkownika
  106.          * @param password haslo uzytkownika
  107.          * @return iflogged
  108.          * @exception SQLException e
  109.          * @see SQLException e
  110.          *
  111.          */
  112.         public boolean login(String login, String password) {
  113.                 boolean correct = false;
  114.                 try {
  115.                         PreparedStatement ps = c.prepareStatement("SELECT * FROM Moderators WHERE login = ? AND password = ?;");
  116.                         ps.setString(1, login);
  117.                         ps.setInt(2, Integer.parseInt(password));
  118.                         ResultSet rs = ps.executeQuery();
  119.                         if (rs.next()) {
  120.                                 correct = true;
  121.                         }
  122.                         rs.close();
  123.                         ps.close();
  124.                         c.close();
  125.  
  126.                         log("Loged in succussfully");
  127.  
  128.                 } catch (SQLException e) {
  129.                         log("Cannot execute this login statment. " + e.toString());
  130.                 }
  131.                 return correct;
  132.         }
  133.         /*
  134.          * Metoda pobierajaca okreslone lokacje z bazy danych
  135.          * @param latitude szerokosc geograficzna uzytkownika
  136.          * @param longitude dlugosc geograficzna uzytkownika
  137.          * @param radius promien w jakim nalezy szukac lokacji
  138.          * @param tags tablica tagow z lokacjami, ktore nalezy wyszukac
  139.          * @return tosend
  140.          * @exception SQLException e
  141.          * @see SQLException e
  142.          * @see FILReply.java
  143.          */
  144.         public FILReply findPlace(double latitude, double longitude, double radius, String[] tags) {
  145.                 List<Long> tags_ID = new ArrayList<Long>();
  146.                 List<FILReplyElement> result = new ArrayList<FILReplyElement>();
  147.                 FILReply tosend = new FILReply();
  148.  
  149.                 if (tags.length > 0) {
  150.                         tags_ID = findTags(tags);
  151.                 }
  152.  
  153.                 double deltaLat = radius * 0.00001451;
  154.                 double deltaLong = radius * 0.00000878;
  155.  
  156.                 String query = "SELECT Location_ID, Location_Name, Latitude_cord, Longitude_cord, Tags, Description "
  157.                                 + "FROM Location " + "WHERE (Latitude_cord BETWEEN ? AND ?) AND (Longitude_cord BETWEEN ? AND ?)";
  158.                 if (tags_ID.size() > 0) {
  159.                         query = query.concat(" AND (Tags LIKE '%|" + Long.toString(tags_ID.get(0)) + "|%'");
  160.                         if (tags_ID.size() > 1) {
  161.                                 for (int i = 1; i < tags_ID.size(); i++) {
  162.                                         query = query.concat(" OR Tags LIKE '%|" + Long.toString(tags_ID.get(i)) + "|%'");
  163.                                 }
  164.  
  165.                         }
  166.                         query = query.concat(") GROUP BY Location_ID;");
  167.                 } else {
  168.                         query = query.concat(" GROUP BY Location_ID;");
  169.                 }
  170.  
  171.                 PreparedStatement ps = null;
  172.                 ResultSet rs = null;
  173.                 try {
  174.  
  175.                         ps = c.prepareStatement(query);
  176.                         ps.setDouble(1, latitude - deltaLat);
  177.                         ps.setDouble(2, latitude + deltaLat);
  178.                         ps.setDouble(3, longitude - deltaLong);
  179.                         ps.setDouble(4, longitude + deltaLong);
  180.                         rs = ps.executeQuery();
  181.  
  182.                 } catch (SQLException e) {
  183.                         log("findPlace " + e.toString());
  184.                 }
  185.  
  186.                 try {
  187.                         while (rs.next()) {
  188.                                 FILReplyElement t = new FILReplyElement();
  189.                                 t.name = rs.getString("Location_Name");
  190.                                 t.latitude = rs.getDouble("Latitude_cord");
  191.                                 t.longitude = rs.getDouble("Longitude_cord");
  192.                                 t.description = rs.getString("description");
  193.                                 result.add(t);
  194.                         }
  195.                 } catch (SQLException e) {
  196.                         log("findPlace " + e.toString());
  197.                 }
  198.                 FILReplyElement[] tab = new FILReplyElement[result.size()];
  199.                 for (int i = 0; i < result.size(); i++) {
  200.                         tab[i] = result.get(i);
  201.  
  202.                 }
  203.                 tosend.arr = tab;
  204.                 try {
  205.                         rs.close();
  206.                         ps.close();
  207.                         c.close();
  208.                 } catch (SQLException e) {
  209.                         log("findPlace " + e.toString());
  210.                 }
  211.  
  212.                 log("findedPlace successfully");
  213.                 return tosend;
  214.         }
  215.         /*
  216.          * Metoda pobierajaca okreslone wydarzenia z bazy danych
  217.          * @param latitude szerokosc geograficzna uzytkownika
  218.          * @param longitude dlugosc geograficzna uzytkownika
  219.          * @param radius promien w jakim nalezy szukac wydarzen
  220.          * @param tags tablica tagow z wydrzeniami, ktore nalezy wyszukac
  221.          * @return result
  222.          * @exception SQLException e
  223.          * @see SQLException e
  224.          * @see FILReplyElement.java
  225.          */
  226.         public FILReplyElement[] findEvent(double latitude, double longitude, double radius, String[] tags) {
  227.  
  228.                 List<Long> tags_ID = new ArrayList<Long>();
  229.                 List<FILReplyElement> result = new ArrayList<FILReplyElement>();
  230.                 if (tags.length > 0) {
  231.                         tags_ID = findTags(tags);
  232.                 }
  233.  
  234.                 double deltaLat = radius * 0.00001451;
  235.                 double deltaLong = radius * 0.00000878;
  236.  
  237.                 try {
  238.                         String query = "SELECT Event_ID, Event_Name, Latitude_cord, Longitude_cord, Date, Time, Tags, Description "
  239.                                         + "FROM Events "
  240.                                         + "WHERE Time >= ? AND Date == ? AND (Latitude_cord BETWEEN ? AND ?) AND (Longitude_cord BETWEEN ? AND ?)";
  241.                         if (tags_ID.size() > 0) {
  242.                                 query = query.concat(" AND (Tags LIKE '%|" + Long.toString(tags_ID.get(0)) + "|%'");
  243.                                 if (tags_ID.size() > 1) {
  244.                                         for (int i = 1; i < tags_ID.size(); i++) {
  245.                                                 query = query.concat(" OR Tags LIKE '%|" + Long.toString(tags_ID.get(i)) + "|%'");
  246.                                         }
  247.  
  248.                                 }
  249.                                 query = query.concat(") GROUP BY Event_ID;");
  250.                         } else {
  251.                                 query = query.concat(" GROUP BY Event_ID;");
  252.                         }
  253.  
  254.                         Calendar cal = Calendar.getInstance();
  255.                         SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss");
  256.                         SimpleDateFormat date = new SimpleDateFormat("yyyy/mm/dd");
  257.  
  258.                         PreparedStatement ps = c.prepareStatement(query);
  259.  
  260.                         ps.setString(1, time.format(cal.getTime()));
  261.                         ps.setString(2, date.format(cal.getTime()));
  262.                         ps.setString(3, Double.toString(latitude - deltaLat));
  263.                         ps.setString(4, Double.toString(latitude + deltaLat));
  264.                         ps.setString(5, Double.toString(longitude - deltaLong));
  265.                         ps.setString(6, Double.toString(longitude + deltaLong));
  266.  
  267.                         ResultSet rs = ps.executeQuery();
  268.                         while (rs.next()) {
  269.                                 FILReplyElement t = new FILReplyElement();
  270.                                 t.name = rs.getString("Event_Name");
  271.                                 t.latitude = rs.getDouble("Latitude_cord");
  272.                                 t.longitude = rs.getDouble("Longitude_cord");
  273.                                 t.date = rs.getString("Date");
  274.                                 t.time = rs.getString("Time");
  275.                                 t.description = rs.getString("Description");
  276.                                 result.add(t);
  277.  
  278.                         }
  279.                         rs.close();
  280.                         ps.close();
  281.                         c.close();
  282.  
  283.                 } catch (SQLException e) {
  284.  
  285.                         log("Cannot execute this find event statment " + e.toString());
  286.                 }
  287.  
  288.                 log("findedEvent successfully");
  289.                 return result.toArray(new FILReplyElement[result.size()]);
  290.  
  291.         }
  292.         /*
  293.          * Metoda uzupelniajaca baze danych o przeslana lokacje
  294.          * @param name nazwa lokacji
  295.          * @param latitude szerokosc geograficzna lokacji
  296.          * @param longitude dlugosc geograficzna lokacji
  297.          * @param tags tablica tagow opisujaca lokacje
  298.          * @param owner wlasciciel lokacji
  299.          * @param description opis lokacji
  300.          * @return correct
  301.          * @exception SQLException e
  302.          * @see SQLException e
  303.          */
  304.         public boolean addPlace(String name, String latitude, String longitude, String tags[], String owner,
  305.                         String description) {
  306.                 System.out.println("addplane\n");
  307.                 boolean correct = false;
  308.                 try {
  309.                         String stringtags = "|";
  310.                         for (int i = 0; i < tags.length; i++) {
  311.                                 stringtags += Long.toString(checkTag(tags[i])) + "|";
  312.                         }
  313.                         PreparedStatement ps = c.prepareStatement(
  314.                                         "INSERT INTO Location (Location_Name, Latitude_cord, Longitude_cord, Tags, Owner, Description)"
  315.                                                         + " VALUES (?,?,?,?,?,?);");
  316.                         ps.setString(1, name);
  317.                         ps.setFloat(2, Float.parseFloat(latitude));
  318.                         ps.setFloat(3, Float.parseFloat(longitude));
  319.                         ps.setString(4, stringtags);
  320.                         ps.setString(5, owner);
  321.                         ps.setString(6, description);
  322.                         ps.executeUpdate();
  323.                         correct = true;
  324.                         ps.close();
  325.                         c.close();
  326.  
  327.                 } catch (SQLException e) {
  328.                         log("Cannot execute this addPlace event statment " + e.toString());
  329.                 }
  330.                 log("addedPlace succussfully");
  331.                 return correct;
  332.         }
  333.         /*
  334.          * Metoda uzupelniajaca baze danych o przeslane wydarzenie
  335.          * @param name nazwa lokacji
  336.          * @param latitude szerokosc geograficzna lokacji
  337.          * @param longitude dlugosc geograficzna lokacji
  338.          * @param tags tablica tagow opisujaca lokacje
  339.          * @param owner wlasciciel lokacji
  340.          * @param description opis lokacji
  341.          * @param date data wydarzenia
  342.          * @param time godzina wydarzenia
  343.          * @return correct
  344.          * @exception SQLException e
  345.          * @see SQLException e
  346.          */
  347.         public boolean addEvent(String name, Double latitude, Double longitude, String tags[], String owner,
  348.                         String description, String date, String time) {
  349.                 boolean correct = false;
  350.                 try {
  351.                         String stringtags = "|";
  352.                         for (int i = 0; i < tags.length; i++) {
  353.                                 stringtags += Long.toString(checkTag(tags[i])) + "|";
  354.                         }
  355.                         PreparedStatement ps = c.prepareStatement(
  356.                                         "INSERT INTO Events (Event_Name, Latitude_cord, Longitude_cord, Date, Time, Tags, Owner, Description)"
  357.                                                         + " VALUES (?,?,?,?,?,?,?,?);");
  358.  
  359.                         ps.setString(1, name);
  360.                         ps.setDouble(2, latitude);
  361.                         ps.setDouble(3, longitude);
  362.                         ps.setString(4, date);
  363.                         ps.setString(5, time);
  364.                         ps.setString(6, stringtags);
  365.                         ps.setString(7, owner);
  366.                         ps.setString(8, description);
  367.                         ps.executeUpdate();
  368.                         ps.close();
  369.                         c.close();
  370.  
  371.                         correct = true;
  372.                 } catch (SQLException e) {
  373.                         log("Cannot add event " + e.toString());
  374.                 }
  375.                 log("addedEvent succussfully");
  376.                 return correct;
  377.         }
  378.         /*
  379.          * Metoda wyszukujaca id tagow
  380.          * @param tags tablica tagow
  381.          * @return tags_ID
  382.          * @exception SQLException e
  383.          * @see SQLException e
  384.          */
  385.         protected List<Long> findTags(String[] tags) {
  386.                 List<Long> tags_ID = new ArrayList<Long>();
  387.                 try {
  388.                         String query = "SELECT * FROM Tags WHERE ";
  389.                         for (int i = 0; i < tags.length - 1; i++) {
  390.                                 query = query.concat(" Tag LIKE '" + tags[i] + "' OR");
  391.                         }
  392.                         query = query.concat(" Tag LIKE '" + tags[tags.length - 1] + "' GROUP BY ID;");
  393.  
  394.                         PreparedStatement ps = c.prepareStatement(query);
  395.                         ResultSet rs = ps.executeQuery();
  396.                         while (rs.next()) {
  397.                                 long a = rs.getLong("ID");
  398.                                 tags_ID.add(a);
  399.                         }
  400.                         rs.close();
  401.                         ps.close();
  402.  
  403.                 } catch (SQLException e) {
  404.                         log("Cannot find tags " + e.toString());
  405.                 }
  406.                 log("findedTags successfully");
  407.                 return tags_ID;
  408.  
  409.         }
  410.         /*
  411.          * Metoda wyszukujaca wszystkie lokacje danego wlasciciela
  412.          * @param login login wlasciciela
  413.          * @return result
  414.          * @exception SQLException e
  415.          * @see SQLException e
  416.          * @see FILReplyElement.java
  417.          */
  418.         public FILReplyElement[] findOwnLocation(String login) {
  419.                 // potrzebne zabezpieczenie if login = owner dopiero wykonuj.
  420.                 List<FILReplyElement> result = new ArrayList<FILReplyElement>();
  421.                 try {
  422.                         String query = "SELECT * FROM Location WHERE Owner = ? GROUP BY Location_ID";
  423.  
  424.                         PreparedStatement ps = c.prepareStatement(query);
  425.                         ps.setString(1, login);
  426.                         ResultSet rs = ps.executeQuery();
  427.  
  428.                         while (rs.next()) {
  429.                                 FILReplyElement tosend = new FILReplyElement();
  430.  
  431.                                 tosend.location_id = rs.getInt("Location_ID");
  432.                                 tosend.name = rs.getString("Location_Name");
  433.                                 tosend.latitude = rs.getDouble("Latitude_cord");
  434.                                 tosend.longitude = rs.getDouble("Longitude_cord");
  435.                                 tosend.tags = rs.getString("Tags");
  436.                                 tosend.owner = login;
  437.                                 tosend.description = rs.getString("Description");
  438.                                 result.add(tosend);
  439.                         }
  440.                         rs.close();
  441.                         ps.close();
  442.                         c.close();
  443.  
  444.                 } catch (SQLException e) {
  445.                         log("Cannot find own locations " + e.toString());
  446.                 }
  447.                 log("findedOwnLocations successfully");
  448.                 return result.toArray(new FILReplyElement[result.size()]);
  449.         }
  450.         /*
  451.          * Metoda wyszukujaca czy dany tag nie znajduje sie juz w bazie
  452.          * @param tag opis taga
  453.          * @return id
  454.          * @exception SQLException e
  455.          * @see SQLException e
  456.          */
  457.         public long checkTag(String tag) {
  458.                 String query = "SELECT * FROM Tags WHERE Tag=?";
  459.                 long id;
  460.                 try {
  461.                         PreparedStatement ps = c.prepareStatement(query);
  462.                         ps.setString(1, tag);
  463.                         System.out.println(ps);
  464.                        
  465.                         ResultSet rs = ps.executeQuery();
  466.  
  467.  
  468.                         if (rs.next()) {
  469.                                 id = rs.getLong("ID");
  470.                                 log("chceckedTag successfully");
  471.                                 ps.close();
  472.                                 rs.close();
  473.                                 return id;
  474.                         } else {
  475.  
  476.                                 ps.close();
  477.                                 rs.close();
  478.  
  479.                                 log("didnt find tag");
  480.  
  481.                                 query = "INSERT into Tags (Tag) values(?);";
  482.                                 PreparedStatement ps2 = c.prepareStatement(query);
  483.                                 ps2.setString(1, tag);
  484.                                 ps2.executeUpdate();
  485.  
  486.                                 ps2.close();
  487.  
  488.                                 query = "SELECT * FROM Tags WHERE Tag = ?;";
  489.                                 ps2 = c.prepareStatement(query);
  490.                                 ps2.setString(1, tag);
  491.                                 rs = ps2.executeQuery();
  492.  
  493.                                 if (rs.next()) {
  494.                                         id = rs.getLong("ID");
  495.                                         log("chceckedTag successfully");
  496.                                         ps2.close();
  497.                                         rs.close();
  498.                                         return id;
  499.  
  500.                                 } else {
  501.                                         ps2.close();
  502.                                         rs.close();
  503.                                         log("straszny blad check tags");
  504.                                         return 0;
  505.                                 }
  506.                         }
  507.                 } catch (SQLException e) {
  508.                         log("Cannot check Tag " + e.toString());
  509.                 }
  510.                 return 0;
  511.  
  512.         }
  513.  
  514. }
  515.