Java tutorial
package tarefas.controller; import Hibernate_Files.Fillingposts; import Hibernate_Files.Fillings; import java.util.List; import Hibernate_Files.NewHibernateUtil; import java.math.BigDecimal; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.servlet.http.HttpSession; import org.hibernate.Query; import org.hibernate.Session; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author caoc1 */ @Controller public class DAOController { Session session; public DAOController() { session = NewHibernateUtil.getSessionFactory().openSession(); } /* *Name: check_login *Parameters: username -> username inserted in login page * password -> password inserted in login page *Variables: query -> saves the query code to get data from DB. * list -> save the query result *Description: Method responsible for checking if login data is corect. * It returns true if it's correct and false if isn't. */ @RequestMapping("/checkLogin.htm") public String check_login(String username, String password, HttpSession httpsession) { Query query = session.createQuery("from Fillingposts"); List<Fillingposts> list = query.list(); if (username.equals("admin") && password.equals("admin")) { httpsession.setAttribute("loggedUser", "admin"); return "personalPage_admin"; } for (Fillingposts aux : list) { if (aux.getName().equals(username) && aux.getPassword().equals(password)) { httpsession.setAttribute("loggedUser", aux); if (aux.getType().equals("post")) { return "personalPage_post"; } else { return "personalPage_user"; } } } return "loginPage_Error"; } /* *Name: getGasolineiras *Parameters: name -> name of the wanted filling station. *Variables: query -> saves the query code to get data from DB. * list -> save the query result *Description: Method responsible to return the filling station with * the given name from DB. */ public Fillingposts getGasolineiras(String name) { Query query = session.createQuery("from Fillingposts"); List<Fillingposts> list = query.list(); for (Fillingposts aux : list) { if (aux.getName().equals(name)) { return aux; } } return null; } /* *Name: create_filling *Parameters: quant -> quantity of fuel needed gas -> Filling station to serve. *Variables: abast -> filling data * tx -> DB transaction system *Description: Method responsible to insert a new filling * in DB */ @RequestMapping("/createFill_post.htm") public void create_filling_post(HttpSession httpsession) { Fillingposts fill = (Fillingposts) httpsession.getAttribute("loggedUser"); int id = fill.getId(); session.beginTransaction(); this.inverseFillingPost_STATE(fill); session.createSQLQuery( "INSERT INTO fillings(data, quantity, filling_post, state) VALUES (null, 500, " + id + ", false);") .executeUpdate(); session.getTransaction().commit(); } @RequestMapping("/createFill_user.htm") public String create_filling_user(HttpSession httpsession, String quantity, boolean urgence, String type) { Fillingposts fill = (Fillingposts) httpsession.getAttribute("loggedUser"); int id = fill.getId(); int quantidade = Integer.parseInt(quantity); session.beginTransaction(); this.inverseFillingPost_STATE(fill); session.createSQLQuery( "INSERT INTO fillings(data, quantity, filling_post, state, urgent, type) VALUES (null, " + quantidade + ", " + id + ", false, " + urgence + ", '" + type + "');") .executeUpdate(); session.getTransaction().commit(); return "personalPage_user"; } public void inverseFillingPost_STATE(Fillingposts fill) { fill.setState(!fill.getState()); session.update(fill); } @RequestMapping("/createPost_admin.htm") public String create_post(HttpSession httpsession, String street, String country, String city, String number, String capacity, String lat, String lon) { String name = "gasoabast_" + country.toLowerCase(); int num = Integer.parseInt(number); float capac = Float.parseFloat(capacity); float latitude = Float.parseFloat(lat); float longitude = Float.parseFloat(lon); session.beginTransaction(); session.createSQLQuery( "INSERT INTO fillingposts(name, street, number, country, city, capacity, ocupation, password, geom, state, type) VALUES " + "('" + name + "', '" + street + "', " + num + ", '" + country + "', '" + city + "', " + capac + ", null, '" + country.toLowerCase() + "',ST_GeomFromText('POINT(" + latitude + " " + longitude + ")', 4326), false, 'post');") .executeUpdate(); session.getTransaction().commit(); return "personalPage_admin"; } @RequestMapping("/createHome_all.htm") public String create_home(HttpSession httpsession, String login, String password, String street, String country, String city, String number, String lat, String lon) { int num = Integer.parseInt(number); float latitude = Float.parseFloat(lat); float longitude = Float.parseFloat(lon); session.beginTransaction(); session.createSQLQuery( "INSERT INTO fillingposts(name, street, number, country, city, capacity, ocupation, password, geom, state, type) VALUES " + "('" + login + "', '" + street + "', " + num + ", '" + country + "', '" + city + "', " + "0" + ", null, '" + password + "',ST_GeomFromText('POINT(" + latitude + " " + longitude + ")', 4326), false, 'home');") .executeUpdate(); session.getTransaction().commit(); return "index"; } public String getFastestWay() throws ClassNotFoundException, SQLException { String way = ""; Class.forName("org.postgresql.Driver"); Connection connection = null; connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/GasoAbast", "postgres", "caoc10"); Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery("select * from all_fillposts_way('central_trofa')"); while (rs.next()) { String aux = rs.getString("txt"); aux = aux.replace("LINESTRING(", ""); aux = aux.replace(")", ""); aux = aux.concat(", "); way = way.concat(aux); } way = way.substring(0, way.length() - 2); System.out.println(way); return way; } @RequestMapping("/endConection.htm") public void endConection() { session.close(); } }