|
|
@@ -0,0 +1,389 @@
|
|
|
+package nordh.xyz.Database;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.sql.Array;
|
|
|
+import java.sql.Blob;
|
|
|
+import java.sql.Connection;
|
|
|
+import java.sql.DriverManager;
|
|
|
+import java.sql.PreparedStatement;
|
|
|
+import java.sql.ResultSet;
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.sql.Statement;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+
|
|
|
+import com.mysql.cj.x.protobuf.MysqlxPrepare.Prepare;
|
|
|
+
|
|
|
+import nordh.xyz.objects.Ingredient;
|
|
|
+import nordh.xyz.objects.Recepie;
|
|
|
+
|
|
|
+public class DatabaseConnection {
|
|
|
+ private static final String USERNAME = "recepieUser";
|
|
|
+ private static final String PASSWORD = "v.FV-ssZPzVyev28";
|
|
|
+ private static final String DATABASE = "recept";
|
|
|
+ private static final String URL = "jdbc:mysql://nordh.xyz:3306/";
|
|
|
+
|
|
|
+ Connection conn;
|
|
|
+ private static DatabaseConnection instance = new DatabaseConnection();
|
|
|
+
|
|
|
+ private DatabaseConnection() {
|
|
|
+ }
|
|
|
+
|
|
|
+ public static DatabaseConnection getInstance() {
|
|
|
+ return instance;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Connection getConnection() {
|
|
|
+ if (conn == null) {
|
|
|
+ try {
|
|
|
+ conn = DriverManager.getConnection(URL + DATABASE, USERNAME, PASSWORD);
|
|
|
+ } catch (SQLException e) {
|
|
|
+ System.out.println("EXCEPTION: getConnection " + e.getSQLState() + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return conn;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Set<Recepie> getRecepiesByDescription(String searchString) {
|
|
|
+ Set<Recepie> result = new HashSet<Recepie>();
|
|
|
+ String[] split = searchString.split(" ");
|
|
|
+
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (String word : split) {
|
|
|
+ sb.append("description LIKE '%" + word + "%' AND ");
|
|
|
+ }
|
|
|
+
|
|
|
+ String whereString = sb.substring(0, sb.length() - 4);
|
|
|
+
|
|
|
+ String sql = "SELECT * FROM recepie WHERE " + whereString;
|
|
|
+
|
|
|
+ addRecepie(result, sql);
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addRecepie(Set<Recepie> result, String sql) {
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(sql)) {
|
|
|
+ ResultSet rs = stat.executeQuery();
|
|
|
+
|
|
|
+ while (rs.next()) {
|
|
|
+ Recepie r = getRecepieFromResultSet(rs);
|
|
|
+ result.add(r);
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ System.out.println("Something wrong with SQL " + sql);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Recepie getRecepieFromResultSet(ResultSet rs) throws SQLException {
|
|
|
+ Recepie r = new Recepie();
|
|
|
+ r.setId(rs.getInt("id"));
|
|
|
+ r.setIngredients(getRecepieIngredients(r.getId()));
|
|
|
+ r.setName(rs.getString("name"));
|
|
|
+ r.setDescription(rs.getString("description"));
|
|
|
+ r.setUrl(rs.getString("url"));
|
|
|
+
|
|
|
+ String tagSql = "SELECT group_concat(rt.name) as tags FROM recepieTags rt JOIN recepieTagLinkTable rtlt ON rt.id = rtlt.tagId WHERE rtlt.recepieId = ? GROUP BY recepieId";
|
|
|
+
|
|
|
+ try (PreparedStatement stat2 = getConnection().prepareStatement(tagSql)) {
|
|
|
+ stat2.setInt(1, rs.getInt("id"));
|
|
|
+ ResultSet rs2 = stat2.executeQuery();
|
|
|
+
|
|
|
+ while (rs2.next()) {
|
|
|
+ r.setTags(Arrays.asList(rs2.getString("tags").split(",")));
|
|
|
+ }
|
|
|
+ } catch (SQLException e1) {
|
|
|
+ System.out.println("Something wrong with SQL " + tagSql);
|
|
|
+ e1.printStackTrace();
|
|
|
+ }
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+
|
|
|
+ public File getRecepieImage(int recepieId) {
|
|
|
+ String sql = "SELECT image FROM recepieImage WHERE recepieId = ?";
|
|
|
+ File result = null;
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(sql)) {
|
|
|
+ stat.setInt(1, recepieId);
|
|
|
+ ResultSet rs = stat.executeQuery();
|
|
|
+ while (rs.next()) {
|
|
|
+ Blob b = rs.getBlob("image");
|
|
|
+ InputStream is = b.getBinaryStream(1, b.length());
|
|
|
+ BufferedImage image = ImageIO.read(is);
|
|
|
+
|
|
|
+ if (image != null) {
|
|
|
+ File outputFile = new File("output.png");
|
|
|
+ ImageIO.write(image, "png", outputFile);
|
|
|
+
|
|
|
+ result = outputFile;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ System.out.println("Something wrong with SQL " + sql);
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // public File getRecepieImage(int recepieId) {
|
|
|
+ // File result = new File("tempname.png");
|
|
|
+ // String sql = "SELECT image FROM recepieImage WHERE recepieId = ?";
|
|
|
+ // try (PreparedStatement stat = getConnection().prepareStatement(sql);
|
|
|
+ // FileOutputStream fos = new FileOutputStream(result);) {
|
|
|
+
|
|
|
+ // stat.setInt(1, recepieId);
|
|
|
+ // ResultSet rs = stat.executeQuery();
|
|
|
+
|
|
|
+ // byte[] buffer = new byte[1];
|
|
|
+ // while (rs.next()) {
|
|
|
+ // InputStream is = rs.getBinaryStream("image");
|
|
|
+ // while (is.read(buffer) > 0) {
|
|
|
+ // fos.write(buffer);
|
|
|
+ // }
|
|
|
+ // is.close();
|
|
|
+ // }
|
|
|
+
|
|
|
+ // } catch (SQLException | IOException e) {
|
|
|
+ // System.out.println("EXCEPTION getRecepieImage Something wrong with SQL " +
|
|
|
+ // sql);
|
|
|
+ // e.printStackTrace();
|
|
|
+ // }
|
|
|
+
|
|
|
+ // return result;
|
|
|
+ // }
|
|
|
+
|
|
|
+ public List<Ingredient> getRecepieIngredients(int recepieId) {
|
|
|
+ List<Ingredient> result = new ArrayList<Ingredient>();
|
|
|
+ String sql = "SELECT * FROM ingredient i JOIN recepieIngredientLink ril ON ril.ingredientId = i.id WHERE ril.recepieId = ?";
|
|
|
+
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(sql)) {
|
|
|
+ stat.setInt(1, recepieId);
|
|
|
+
|
|
|
+ ResultSet rs = stat.executeQuery();
|
|
|
+
|
|
|
+ while (rs.next()) {
|
|
|
+ Ingredient i = new Ingredient();
|
|
|
+ i.setIngredientId(rs.getInt("id"));
|
|
|
+ i.setName(rs.getString("name"));
|
|
|
+ i.setAmount(rs.getString("amount"));
|
|
|
+ i.setMeasure(rs.getString("measurement"));
|
|
|
+
|
|
|
+ result.add(i);
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ System.out.println("EXCEPTION IN getRecepieIngredients sql " + sql);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Set<Recepie> getRecepiesByName(String searchString) {
|
|
|
+ Set<Recepie> result = new HashSet<Recepie>();
|
|
|
+ String[] split = searchString.split(" ");
|
|
|
+
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (String word : split) {
|
|
|
+ sb.append("name like '%" + word + "%' AND ");
|
|
|
+ }
|
|
|
+ String whereString = sb.substring(0, sb.length() - 4);
|
|
|
+ String sql = "SELECT * FROM recepie WHERE " + whereString;
|
|
|
+
|
|
|
+ addRecepie(result, sql);
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Set<Recepie> getRecepiesByIngredients(String searchString) {
|
|
|
+ Set<Recepie> result = new HashSet<Recepie>();
|
|
|
+
|
|
|
+ String[] split = searchString.split(" ");
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (String word : split) {
|
|
|
+ sb.append("i.name LIKE '%" + word + "%' AND ");
|
|
|
+ }
|
|
|
+ String whereString = sb.substring(0, sb.length() - 4);
|
|
|
+
|
|
|
+ String sql = "SELECT DISTINCT r.id FROM recepie r JOIN recepieIngredientLink ril ON ril.recepieId = r.id JOIN ingredient i ON i.id = ril.ingredientId WHERE "
|
|
|
+ + whereString;
|
|
|
+
|
|
|
+ StringBuilder sb2 = new StringBuilder();
|
|
|
+
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(sql)) {
|
|
|
+ ResultSet rs = stat.executeQuery();
|
|
|
+ while (rs.next()) {
|
|
|
+ sb2.append(rs.getInt("id") + ",");
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ System.out.println("EXCEPTION getRecepiesByIngredients sql: " + sql);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ if (sb2.length() > 0) {
|
|
|
+ sb2.substring(0, sb2.length() - 1);
|
|
|
+ String recepieSql = "SELECT * FROM recepie where id IN (" + sb2.substring(0, sb2.length() - 1) + ")";
|
|
|
+
|
|
|
+ addRecepie(result, recepieSql);
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Map<Integer, String> getRecepieSteps(int id) {
|
|
|
+ String sql = "SELECT stepNumber, text FROM recepieSteps rs WHERE rs.recepieId = ? ORDER BY stepNumber ASC";
|
|
|
+ Map<Integer, String> steps = new HashMap<>();
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(sql)) {
|
|
|
+ stat.setInt(1, id);
|
|
|
+ ResultSet rs = stat.executeQuery();
|
|
|
+ while (rs.next()) {
|
|
|
+ int step = rs.getInt("stepNumber");
|
|
|
+ String text = rs.getString("text");
|
|
|
+
|
|
|
+ steps.put(step, text);
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ System.out.println("EXCEPTION getRecepiesByIngredients sql: " + sql);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return steps;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<String> getTags() {
|
|
|
+ String sql = "SELECT DISTINCT(name) as name FROM recepieTags";
|
|
|
+ List<String> result = new ArrayList<>();
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(sql)) {
|
|
|
+ ResultSet rs = stat.executeQuery();
|
|
|
+ while (rs.next()) {
|
|
|
+ result.add(rs.getString("name"));
|
|
|
+
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ System.out.println("EXCEPTION getTags sql: " + sql);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void updateRecepieTag(Recepie recepie, String newTag) {
|
|
|
+ String selectSql = "SELECT id FROM recepieTags WHERE name = ?";
|
|
|
+ int tagId = -1;
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(selectSql)) {
|
|
|
+ stat.setString(1, newTag);
|
|
|
+ ResultSet rs = stat.executeQuery();
|
|
|
+ while (rs.next()) {
|
|
|
+ tagId = rs.getInt("id");
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ System.out.println("EXCEPTION updateRecepieTag sql: " + selectSql);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (tagId == -1) {
|
|
|
+ String sql = "INSERT INTO recepieTags (name) VALUES (?)";
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);) {
|
|
|
+ stat.setString(1, newTag);
|
|
|
+ stat.executeUpdate();
|
|
|
+ ResultSet rs = stat.getGeneratedKeys();
|
|
|
+ while (rs.next()) {
|
|
|
+ tagId = rs.getInt(1);
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ System.out.println("EXCEPTION updateRecepieTag sql: " + sql);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (tagId > 0) {
|
|
|
+ String linkTableInsertSql = "INSERT INTO recepieTagLinkTable (recepieId, tagId) VALUES (?, ?)";
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(linkTableInsertSql)) {
|
|
|
+ stat.setInt(1, recepie.getId());
|
|
|
+ stat.setInt(2, tagId);
|
|
|
+
|
|
|
+ stat.executeUpdate();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ System.out.println("EXCEPTION updateRecepieTag sql: " + linkTableInsertSql);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public Recepie getRandomRecepie() {
|
|
|
+ String sql = "SELECT * FROM recepie ORDER BY RAND() LIMIT 1";
|
|
|
+ Recepie result = null;
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(sql)) {
|
|
|
+ ResultSet rs = stat.executeQuery();
|
|
|
+ while (rs.next()) {
|
|
|
+ result = getRecepieFromResultSet(rs);
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ System.out.println("EXCEPTION getRandomRecepie sql: " + sql);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Recepie getRandomRecepie(List<String> selectedTags, boolean andSelected) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+
|
|
|
+ String sql;
|
|
|
+ if (!andSelected) {
|
|
|
+ sb.append(String.join("','", selectedTags));
|
|
|
+ sb.insert(0, '\'');
|
|
|
+ sb.append("'");
|
|
|
+ sql = "SELECT r.* FROM recepie r JOIN recepieTagLinkTable rtlt ON rtlt.recepieId = r.id JOIN recepieTags rt ON rt.id = rtlt.tagId WHERE rt.name IN ("
|
|
|
+ + sb.toString() + ") ORDER BY RAND() LIMIT 1";
|
|
|
+ } else {
|
|
|
+ sb.append(
|
|
|
+ "SELECT r.* FROM recepie r JOIN recepieTagLinkTable rtlt ON rtlt.recepieId = r.id JOIN recepieTags rt ON rt.id = rtlt.tagId WHERE rt.name = ?");
|
|
|
+ for (int i = 1; i < selectedTags.size(); i++) {
|
|
|
+ sb.append(" AND rt.name = ?");
|
|
|
+ }
|
|
|
+ sb.append(" ORDER BY RAND() LIMIT 1");
|
|
|
+ sql = sb.toString();
|
|
|
+ }
|
|
|
+ Recepie result = null;
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(sql)) {
|
|
|
+ if (andSelected) {
|
|
|
+ for (int i = 0; i < selectedTags.size(); i++) {
|
|
|
+ stat.setString(i + 1, selectedTags.get(i));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ResultSet rs = stat.executeQuery();
|
|
|
+ while (rs.next()) {
|
|
|
+ result = getRecepieFromResultSet(rs);
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ System.out.println("EXCEPTION getRandomRecepie sql: " + sql);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void removeTag(Recepie recepie, String text) {
|
|
|
+ String sql = "DELETE FROM recepieTagLinkTable rtlt WHERE recepieId = ? AND tagId = (SELECT id FROM recepieTags WHERE name = ?)";
|
|
|
+
|
|
|
+ try (PreparedStatement stat = getConnection().prepareStatement(sql)) {
|
|
|
+ stat.setInt(1, recepie.getId());
|
|
|
+ stat.setString(2, text);
|
|
|
+
|
|
|
+ stat.execute();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ System.out.println("EXCEPTION removeTag sql: " + sql);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|