Advertisement
Guest User

Untitled

a guest
Jul 13th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 24.96 KB | None | 0 0
  1. public class Playlist extends BaseEntity {
  2.     private String name;
  3.     private String description;
  4.     private boolean isPublic;
  5.     private int ownerId;
  6.  
  7.     public Playlist() {
  8.     }
  9.  
  10.     public Playlist(String name, String description, boolean isPublic) {
  11.         this.name = name;
  12.         this.description = description;
  13.         this.isPublic = isPublic;
  14.     }
  15.  
  16.  
  17.     public String getName() {
  18.         return name;
  19.     }
  20.  
  21.     public void setName(String name) {
  22.         this.name = name;
  23.     }
  24.  
  25.     public String getDescription() {
  26.         return description;
  27.     }
  28.  
  29.     public void setDescription(String description) {
  30.         this.description = description;
  31.     }
  32.  
  33.     public boolean isPublic() {
  34.         return isPublic;
  35.     }
  36.  
  37.     public void setPublic(boolean aPublic) {
  38.         isPublic = aPublic;
  39.     }
  40.  
  41.     public int getOwnerId() {
  42.         return ownerId;
  43.     }
  44.  
  45.     public void setOwnerId(int ownerId) {
  46.         this.ownerId = ownerId;
  47.     }
  48.  
  49.     @Override
  50.     public String toString() {
  51.         return "Playlist[" + getId() + "] - " + name + " Description - " + description;
  52.     }
  53.  
  54.     public static  enum COLUMNS {
  55.         NAME("name"),
  56.         DESCRIPTION("description"),
  57.         IS_PUBLIC("is_public"),
  58.         OWNER_ID("owner_id");
  59.  
  60.         private String value;
  61.  
  62.         COLUMNS(String value) {
  63.             this.value = value;
  64.         }
  65.  
  66.         public String getValue() {
  67.             return value;
  68.         }
  69.     }
  70. }
  71.  
  72.  
  73. public class Song extends BaseEntity{
  74.     private String name;
  75.     private String artistName;
  76.     private String year;
  77.  
  78.     public Song() {
  79.     }
  80.  
  81.     public Song(String name, String artistName, String year) {
  82.         this.name = name;
  83.         this.artistName = artistName;
  84.         this.year = year;
  85.     }
  86.  
  87.     public String getName() {
  88.         return name;
  89.     }
  90.  
  91.     public void setName(String name) {
  92.         this.name = name;
  93.     }
  94.  
  95.     public String getArtistName() {
  96.         return artistName;
  97.     }
  98.  
  99.     public void setArtistName(String artistName) {
  100.         this.artistName = artistName;
  101.     }
  102.  
  103.     public String getYear() {
  104.         return year;
  105.     }
  106.  
  107.     public void setYear(String year) {
  108.         this.year = year;
  109.     }
  110.  
  111.     @Override
  112.     public String toString() {
  113.         return "Song[" + getId() + "] - " + name + " produced by " + artistName + " in " + year;
  114.     }
  115.  
  116.     public static  enum COLUMNS {
  117.         NAME("name"),
  118.         AUTHOR("author"),
  119.         YEAR("year");
  120.  
  121.         private String value;
  122.  
  123.         COLUMNS(String value) {
  124.             this.value = value;
  125.         }
  126.  
  127.         public String getValue() {
  128.             return value;
  129.         }
  130.     }
  131. }
  132.  
  133. public class User  extends BaseEntity{
  134.     private String email;
  135.     private String password;
  136.     private String displayName;
  137.     private boolean isAdministrator;
  138.  
  139.     public User() {
  140.     }
  141.  
  142.     public User(String email, String password, String displayName, boolean isAdministrator) {
  143.         this.email = email;
  144.         this.password = password;
  145.         this.displayName = displayName;
  146.         this.isAdministrator = isAdministrator;
  147.     }
  148.  
  149.     public String getEmail() {
  150.         return email;
  151.     }
  152.  
  153.     public void setEmail(String email) {
  154.         this.email = email;
  155.     }
  156.  
  157.     public String getPassword() {
  158.         return password;
  159.     }
  160.  
  161.     public void setPassword(String password) {
  162.         this.password = password;
  163.     }
  164.  
  165.     public String getDisplayName() {
  166.         return displayName;
  167.     }
  168.  
  169.     public void setDisplayName(String displayName) {
  170.         this.displayName = displayName;
  171.     }
  172.  
  173.     public boolean isAdministrator() {
  174.         return isAdministrator;
  175.     }
  176.  
  177.     public void setAdministrator(boolean administrator) {
  178.         isAdministrator = administrator;
  179.     }
  180.  
  181.     public static  enum COLUMNS {
  182.         EMAIL("email"),
  183.         PASSWORD("password"),
  184.         DISPLAY_NAME("display_name"),
  185.         IS_ADMINISTRATOR("is_administrator");
  186.  
  187.         private String value;
  188.  
  189.         COLUMNS(String value) {
  190.             this.value = value;
  191.         }
  192.  
  193.         public String getValue() {
  194.             return value;
  195.         }
  196.     }
  197. }
  198.  
  199. // До тук са ентититата
  200.  
  201.  
  202. import com.emiltsonev.dao.BaseDao;
  203. import com.emiltsonev.entities.BaseEntity;
  204. import com.emiltsonev.entities.Playlist;
  205. import com.emiltsonev.entities.User;
  206.  
  207. import java.io.IOException;
  208. import java.lang.reflect.Field;
  209. import java.lang.reflect.InvocationTargetException;
  210. import java.lang.reflect.Method;
  211. import java.sql.*;
  212. import java.util.*;
  213.  
  214. public class BaseDaoDbImpl<T extends BaseEntity> implements BaseDao<T> {
  215.     private final static String CONNECTION_STRING = "jdbc:mysql://localhost/black_sound?useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
  216.     private final Connection conn;
  217.     private Class<T> entityClass;
  218.     private String tableName;
  219.  
  220.     public BaseDaoDbImpl(Class<T> entityClass, String tableName) throws ClassNotFoundException, SQLException {
  221.         this.entityClass = entityClass;
  222.         this.tableName = tableName;
  223.         Class.forName("com.mysql.cj.jdbc.Driver");
  224.         conn = DriverManager.getConnection(CONNECTION_STRING, "emil", "emil");
  225.     }
  226.  
  227.     public Connection getConn() {
  228.         return conn;
  229.     }
  230.  
  231.     public Class<T> getEntityClass() {
  232.         return entityClass;
  233.     }
  234.  
  235.     public String getTableName() {
  236.         return tableName;
  237.     }
  238.  
  239.     public static void main(String[] args) throws SQLException, ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException, IOException, NoSuchMethodException, InvocationTargetException {
  240.         BaseDaoDbImpl<Playlist> songBaseDao = new BaseDaoDbImpl<>(Playlist.class, "playlists");
  241.         BaseDaoDbImpl<User> userBaseDao = new BaseDaoDbImpl<>(User.class, "users");
  242.         BaseDaoDbImpl<Playlist> playlistBaseDao = new BaseDaoDbImpl<>(Playlist.class, "playlists");
  243.  
  244.     }
  245.  
  246.     @Override
  247.     public void update(int id) throws IOException, IllegalAccessException, NoSuchFieldException, InstantiationException, SQLException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException {
  248.  
  249.         T entity = get(id);
  250.         if(entity != null) {
  251.             StringBuilder updateStatementBuilder = new StringBuilder("update " + tableName + " set ");
  252.             Field[] fields = entityClass.getDeclaredFields();
  253.             Scanner scanner = new Scanner(System.in);
  254.             Class<?> enumClass = Class.forName(entityClass.getName() + "$COLUMNS");
  255.             Method getValue = enumClass.getDeclaredMethod("getValue");
  256.             Map<String, String> fieldsToColumnNames = new LinkedHashMap<>();
  257.             Object[] columnNames = enumClass.getEnumConstants();
  258.  
  259.             for(int i = 0; i < fields.length; i++) {
  260.                 fields[i].setAccessible(true);
  261.                 fieldsToColumnNames.put(fields[i].getName(), (String) getValue.invoke(columnNames[i]));
  262.             }
  263.  
  264.             String choice;
  265.             for(Field field : fields) {
  266.                 field.setAccessible(true);
  267.                 System.out.println("Would you like to change the value of " + field.getName() + " [y/Y] to change it.");
  268.                 System.out.print("Please enter your choice: ");
  269.                 choice = scanner.nextLine();
  270.                 String newValue = null;
  271.                 if(choice.equals("Y") || choice.equals("y")) {
  272.                     if(field.getType() == boolean.class) {
  273.                         System.out.print("Enter [y/Y] for true and anything else for false: ");
  274.                         newValue = scanner.nextLine();
  275.                         if(newValue.equals("Y") || newValue.equals("y")) {
  276.                             field.set(entity, 1);
  277.                         } else {
  278.                             field.set(entity, 0);
  279.                         }
  280.                     } else {
  281.                         System.out.print("Enter value for: " + field.getName() + ": ");
  282.                         newValue = scanner.nextLine();
  283.                         field.set(entity, newValue);
  284.                     }
  285.  
  286.                     if(field.getType() == String.class) {
  287.                         updateStatementBuilder.append(fieldsToColumnNames.get(field.getName()) + " = " + "'" + newValue + "'" + ", ");
  288.                     } else {
  289.                         updateStatementBuilder.append(fieldsToColumnNames.get(field.getName()) + " = " + newValue + ", ");
  290.                     }
  291.  
  292.                 }
  293.             }
  294.  
  295.             updateStatementBuilder.replace(updateStatementBuilder.length() - 2, updateStatementBuilder.length(), " where id=" + id);
  296.             System.out.println(updateStatementBuilder.toString());
  297.             Statement updateStatement = conn.createStatement();
  298.             updateStatement.executeUpdate(updateStatementBuilder.toString());
  299.         } else {
  300.             System.out.println("No " + entityClass.getSimpleName() + " to be updated");
  301.         }
  302.  
  303.     }
  304.  
  305.     @Override
  306.     public void save(T entity) throws IOException, IllegalAccessException, NoSuchFieldException, InstantiationException, SQLException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException {
  307.         StringBuilder statementBuilder = new StringBuilder("insert into " + tableName + " (");
  308.         Field[] fields = entityClass.getDeclaredFields();
  309.         Class<?> enumClass = Class.forName(entityClass.getName() + "$COLUMNS");
  310.         Method getValue = enumClass.getDeclaredMethod("getValue");
  311.  
  312.         for(Object object : enumClass.getEnumConstants()) {
  313.             String val = (String) getValue.invoke(object);
  314.             System.out.println(val);
  315.             statementBuilder.append(val + ", ");
  316.         }
  317.  
  318.         statementBuilder.replace(statementBuilder.length() - 2, statementBuilder.length(), "");
  319.  
  320.         statementBuilder.append(") values(");
  321.  
  322.         for(Field field : fields) {
  323.             field.setAccessible(true);
  324.             if(field.getType() == String.class) {
  325.                 statementBuilder.append("'" + field.get(entity) + "'"  + ", ");
  326.                 continue;
  327.             }
  328.  
  329.             if(field.getType() == boolean.class) {
  330.                 statementBuilder.append((boolean)field.get(entity)? 1 + ", " : 0 + ", ");
  331.                 continue;
  332.             }
  333.             statementBuilder.append(field.get(entity) + ", ");
  334.         }
  335.  
  336.         statementBuilder.replace(statementBuilder.length() - 2, statementBuilder.length(), ")");
  337.  
  338.         System.out.println(statementBuilder.toString());
  339.         Statement statement = null;
  340.  
  341.         try {
  342.             statement = conn.createStatement();
  343.             statement.executeUpdate(statementBuilder.toString(), Statement.RETURN_GENERATED_KEYS);
  344.             ResultSet result = statement.getGeneratedKeys();
  345.             result.next();
  346.             System.out.println(result.getInt(1));
  347.             entity.setId(result.getInt(1));
  348.  
  349.         } finally {
  350.  
  351.             if(statement != null) {
  352.                 statement.close();
  353.             }
  354.  
  355.         }
  356.     }
  357.  
  358.     @Override
  359.     public void delete(int id) throws IOException, InstantiationException, IllegalAccessException, NoSuchFieldException, SQLException {
  360.         T entity = get(id);
  361.         if(entity != null) {
  362.             StringBuilder statementBuilder = new StringBuilder("delete from " + tableName + " where id=" + entity.getId());
  363.  
  364.             try (Statement statement = conn.createStatement()) {
  365.                 statement.executeUpdate(statementBuilder.toString());
  366.                 System.out.println("Deletion from " + tableName + " successful");
  367.             }
  368.         } else {
  369.             System.out.println("No " + entityClass.getSimpleName() + " to be deleted with id: " + id);
  370.         }
  371.  
  372.     }
  373.  
  374.     @Override
  375.     public T get(int id) throws NoSuchFieldException, InstantiationException, IllegalAccessException, IOException, SQLException {
  376.         StringBuilder statementBuilder = new StringBuilder("select * from " + tableName + " where id=" + id);
  377.  
  378.         Field[] fields = entityClass.getDeclaredFields();
  379.         int i = 2;
  380.         try (Statement statement = conn.createStatement();
  381.              ResultSet results = statement.executeQuery(statementBuilder.toString())) {
  382.             if(!results.next()) {
  383.                 return null;
  384.             }
  385.             T entity = entityClass.newInstance();
  386.             entity.setId(results.getInt("id"));
  387.             while (results.next()) {
  388.                 for(Field field : fields) {
  389.                     field.setAccessible(true);
  390.                     if(field.getType() == boolean.class) {
  391.                         field.set(entity, results.getByte(i) == 1);
  392.                         i++;
  393.                         continue;
  394.                     }
  395.  
  396.                     if(field.getType() == int.class) {
  397.                         field.set(entity, results.getInt(i));
  398.                         i++;
  399.                         continue;
  400.                     }
  401.                     field.set(entity, results.getString(i));
  402.                     i++;
  403.                 }
  404.                 i = 2;
  405.             }
  406.             return entity;
  407.         }
  408.     }
  409.  
  410.     @Override
  411.     public List<T> getAll() throws IOException, IllegalAccessException, InstantiationException, NoSuchFieldException, SQLException {
  412.         List<T> entities = new LinkedList<>();
  413.         StringBuilder statementBuilder = new StringBuilder("select * from " + tableName);
  414.         Field[] fields = entityClass.getDeclaredFields();
  415.  
  416.  
  417.  
  418.         try (Statement statement = conn.createStatement();
  419.              ResultSet results = statement.executeQuery(statementBuilder.toString())) {
  420.             int i = 2;
  421.             while(results.next()) {
  422.                 T entity = entityClass.newInstance();
  423.                 entity.setId(results.getInt("id"));
  424.                 for(Field field : fields) {
  425.                     field.setAccessible(true);
  426.                     if(field.getType() == boolean.class) {
  427.                         field.set(entity, results.getByte(i) == 1);
  428.                         i++;
  429.                         continue;
  430.                     }
  431.  
  432.                     if(field.getType() == int.class) {
  433.                         field.set(entity, results.getInt(i));
  434.                         i++;
  435.                         continue;
  436.                     }
  437.                     field.set(entity, results.getString(i));
  438.                     i++;
  439.                 }
  440.                 entities.add(entity);
  441.                 i = 2;
  442.             }
  443.         }
  444.         return entities;
  445.     }
  446. }
  447.  
  448. import com.emiltsonev.entities.Playlist;
  449. import com.emiltsonev.entities.Song;
  450. import com.emiltsonev.util.services.AuthenticationService;
  451.  
  452. import java.io.IOException;
  453. import java.lang.reflect.Field;
  454. import java.lang.reflect.InvocationTargetException;
  455. import java.lang.reflect.Method;
  456. import java.sql.ResultSet;
  457. import java.sql.SQLException;
  458. import java.sql.Statement;
  459. import java.util.*;
  460.  
  461. public class PlaylistDaoDbImpl extends BaseDaoDbImpl<Playlist> {
  462.     private PlaylistSongDaoDbImpl playlistSongDao = new PlaylistSongDaoDbImpl();
  463.  
  464.     public PlaylistDaoDbImpl(Class<Playlist> entityClass, String tableName) throws ClassNotFoundException, SQLException {
  465.         super(entityClass, tableName);
  466.     }
  467.  
  468.     @Override
  469.     public void save(Playlist entity) throws IOException, IllegalAccessException, NoSuchFieldException, InstantiationException, SQLException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException {
  470.         entity.setOwnerId(AuthenticationService.instance().getUser().getId());
  471.         super.save(entity);
  472.     }
  473.  
  474.     public void addSongToPlaylist(int playlistId, int songId) throws SQLException {
  475.         playlistSongDao.insert(playlistId, songId);
  476.     }
  477.  
  478.     public List<Playlist> getAll(int id) throws SQLException {
  479.         List<Playlist> playlists = new LinkedList<>();
  480.  
  481.         try(Statement statement = getConn().createStatement();
  482.             ResultSet results = statement.executeQuery("select playlists.id, playlists.name, playlists.description, playlists.is_public, playlists.owner_id from playlists where playlists.owner_id = " + id)) {
  483.             while(results.next()) {
  484.                 Playlist playlist = new Playlist();
  485.                 playlist.setId(results.getInt(1));
  486.                 playlist.setName(results.getString(2));
  487.                 playlist.setDescription(results.getString(3));
  488.                 playlist.setPublic(results.getInt(3) == 1);
  489.                 playlist.setOwnerId(results.getInt(4));
  490.                 playlists.add(playlist);
  491.             }
  492.         }
  493.         return playlists;
  494.     }
  495.  
  496.     public List<Song> getSongsByPlaylistId(int playlistId) throws SQLException {
  497.         return playlistSongDao.getSongsByPlaylistId(playlistId);
  498.     }
  499.  
  500.     @Override
  501.     public void update(int id) throws IOException, IllegalAccessException, NoSuchFieldException, InstantiationException, SQLException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException {
  502.         Playlist entity = get(id);
  503.         if(entity != null) {
  504.             StringBuilder updateStatementBuilder = new StringBuilder("update " + getTableName() + " set ");
  505.             Field[] fields = getEntityClass().getDeclaredFields();
  506.             Scanner scanner = new Scanner(System.in);
  507.             Class<?> enumClass = Class.forName(getEntityClass().getName() + "$COLUMNS");
  508.             Method getValue = enumClass.getDeclaredMethod("getValue");
  509.             Map<String, String> fieldsToColumnNames = new LinkedHashMap<>();
  510.             Object[] columnNames = enumClass.getEnumConstants();
  511.  
  512.             for(int i = 0; i < fields.length; i++) {
  513.                 fields[i].setAccessible(true);
  514.                 fieldsToColumnNames.put(fields[i].getName(), (String) getValue.invoke(columnNames[i]));
  515.             }
  516.  
  517.             String choice;
  518.             for(Field field : fields) {
  519.                 if(field.getName().equals("ownerId")) {
  520.                     continue;
  521.                 }
  522.  
  523.                 field.setAccessible(true);
  524.                 System.out.println("Would you like to change the value of " + field.getName() + " [y/Y] to change it.");
  525.                 System.out.print("Please enter your choice: ");
  526.                 choice = scanner.nextLine();
  527.                 String newValue = null;
  528.                 if(choice.equals("Y") || choice.equals("y")) {
  529.                     if(field.getType() == boolean.class) {
  530.                         System.out.print("Enter [y/Y] for true and anything else for false: ");
  531.                         newValue = scanner.nextLine();
  532.                         if(newValue.equals("Y") || newValue.equals("y")) {
  533.                             field.set(entity, 1);
  534.                         } else {
  535.                             field.set(entity, 0);
  536.                         }
  537.                     } else {
  538.                         System.out.print("Enter value for: " + field.getName() + ": ");
  539.                         newValue = scanner.nextLine();
  540.                         field.set(entity, newValue);
  541.                     }
  542.  
  543.                     if(field.getType() == String.class) {
  544.                         updateStatementBuilder.append(fieldsToColumnNames.get(field.getName()) + " = " + "'" + newValue + "'" + ", ");
  545.                     } else {
  546.                         updateStatementBuilder.append(fieldsToColumnNames.get(field.getName()) + " = " + newValue + ", ");
  547.                     }
  548.  
  549.                 }
  550.             }
  551.  
  552.             updateStatementBuilder.replace(updateStatementBuilder.length() - 2, updateStatementBuilder.length(), " where id=" + id);
  553.             System.out.println(updateStatementBuilder.toString());
  554.             Statement updateStatement = getConn().createStatement();
  555.             updateStatement.executeUpdate(updateStatementBuilder.toString());
  556.         } else {
  557.             System.out.println("No " + getEntityClass().getSimpleName() + " to be updated");
  558.         }
  559.     }
  560. }
  561.  
  562. import com.emiltsonev.entities.PlaylistSong;
  563. import com.emiltsonev.entities.Song;
  564.  
  565. import java.sql.*;
  566. import java.util.LinkedList;
  567. import java.util.List;
  568.  
  569. public class PlaylistSongDaoDbImpl {
  570.     private final static String CONNECTION_STRING = "jdbc:mysql://localhost/black_sound?useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
  571.     private final Connection conn;
  572.  
  573.     public PlaylistSongDaoDbImpl() throws ClassNotFoundException, SQLException {
  574.         Class.forName("com.mysql.cj.jdbc.Driver");
  575.         conn = DriverManager.getConnection(CONNECTION_STRING, "emil", "emil");
  576.     }
  577.  
  578.     public void insert(int playlistId, int songId) throws SQLException {
  579.         Statement statement = conn.createStatement();
  580.         statement.executeUpdate("insert into playlists_songs values(" + playlistId + ", " + songId + ")");
  581.     }
  582.  
  583.     public List<Song> getSongsByPlaylistId(int playlistId) throws SQLException {
  584.         List<Song> songs = new LinkedList<>();
  585.  
  586.         try(Statement statement = conn.createStatement();
  587.             ResultSet results = statement.executeQuery("select songs.id, songs.name, songs.author, songs.year from playlists_songs join songs on playlists_songs.song_id = songs.id where playlists_songs.playlist_id = " + playlistId)) {
  588.  
  589.             while(results.next()) {
  590.                 Song song = new Song();
  591.                 song.setId(results.getInt(1));
  592.                 song.setName(results.getString(2));
  593.                 song.setArtistName(results.getString(3));
  594.                 song.setYear(results.getString(4));
  595.                 songs.add(song);
  596.             }
  597.         }
  598.         return songs;
  599.     }
  600.  
  601.     public List<PlaylistSong> getAll() throws SQLException {
  602.         List<PlaylistSong> playlistSongs = new LinkedList<>();
  603.  
  604.         try(Statement statement = conn.createStatement();
  605.             ResultSet results = statement.executeQuery("select * from playlists_songs")) {
  606.             while(results.next()) {
  607.                 playlistSongs.add(new PlaylistSong(results.getInt(1), results.getInt(2)));
  608.             }
  609.         }
  610.         return playlistSongs;
  611.     }
  612. }
  613.  
  614. import com.emiltsonev.entities.Song;
  615.  
  616. import java.io.IOException;
  617. import java.lang.reflect.InvocationTargetException;
  618. import java.sql.SQLException;
  619.  
  620. public class SongDaoDbImpl extends BaseDaoDbImpl<Song>{
  621.     private PlaylistSongDaoDbImpl playlistSongDao = new PlaylistSongDaoDbImpl();
  622.  
  623.     public SongDaoDbImpl(Class<Song> entityClass, String tableName) throws ClassNotFoundException, SQLException {
  624.         super(entityClass, tableName);
  625.     }
  626.  
  627.     @Override
  628.     public void save(Song entity) throws IOException, IllegalAccessException, NoSuchFieldException, InstantiationException, SQLException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException {
  629.         super.save(entity);
  630.         playlistSongDao.insert(1, entity.getId());
  631.     }
  632. }
  633.  
  634. import com.emiltsonev.dao.UserDao;
  635. import com.emiltsonev.entities.User;
  636.  
  637. import java.io.IOException;
  638. import java.sql.ResultSet;
  639. import java.sql.SQLException;
  640. import java.sql.Statement;
  641.  
  642. public class UserDaoDbImpl extends BaseDaoDbImpl<User> implements UserDao {
  643.  
  644.     public UserDaoDbImpl(Class<User> entityClass, String tableName) throws ClassNotFoundException, SQLException {
  645.         super(entityClass, tableName);
  646.     }
  647.  
  648.     @Override
  649.     public User login(String email, String password) throws NoSuchFieldException, InstantiationException, IllegalAccessException, IOException, SQLException {
  650.         try(Statement statement = getConn().createStatement();
  651.             ResultSet results = statement.executeQuery("select users.id,  users.email, users.password, users.display_name, users.is_administrator from users where users.email = '" + email + "' and users.password = '" + password + "'")) {
  652.  
  653.             if(results.next()) {
  654.                 User user = new User();
  655.                 user.setId(results.getInt(1));
  656.                 user.setEmail(results.getString(2));
  657.                 user.setPassword(results.getString(3));
  658.                 user.setDisplayName(results.getString(4));
  659.                 user.setAdministrator(results.getInt(5) == 1);
  660.                 return user;
  661.             }
  662.         }
  663.         return null;
  664.     }
  665. }
  666.  
  667. // Ето ги и репо-тата
  668.  
  669. public class AuthenticationService {
  670.     private User user;
  671.     private UserDaoDbImpl userDao;
  672.     private static AuthenticationService authenticationService = null;
  673.  
  674.     private AuthenticationService() throws SQLException, ClassNotFoundException {
  675.         userDao = new UserDaoDbImpl(User.class, "users");
  676.     }
  677.  
  678.     public static AuthenticationService instance() throws SQLException, ClassNotFoundException {
  679.         if(authenticationService == null) {
  680.             authenticationService = new AuthenticationService();
  681.         }
  682.  
  683.         return authenticationService;
  684.     }
  685.     public User authenticateUser(String email, String pasword) throws IOException, InstantiationException, IllegalAccessException, NoSuchFieldException, SQLException {
  686.         user = userDao.login(email, pasword);
  687.         return user;
  688.     }
  689.  
  690.     public User getUser() {
  691.         return user;
  692.     }
  693. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement