diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c367d38 --- /dev/null +++ b/.gitignore @@ -0,0 +1,33 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store +/src/jtds-1.3.1.jar +/src/mssql-jdbc-12.6.1.jre8.jar +/src/mssql-jdbc-12.6.1.jre11.jar +/BD.iml diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..2c7bcad --- /dev/null +++ b/src/Main.java @@ -0,0 +1,262 @@ +import java.io.File; +import java.io.FileInputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.sql.*; +import java.util.*; +import java.util.stream.Collectors; + +public class Main { + + static boolean tableExistsSQL(Connection connection, String tableName) throws SQLException { + PreparedStatement preparedStatement = connection.prepareStatement("SELECT TOP 1 count(*) " + + "FROM information_schema.tables " + + "WHERE table_name = ?" + + ";"); + preparedStatement.setString(1, tableName); + + ResultSet resultSet = preparedStatement.executeQuery(); + resultSet.next(); + return resultSet.getInt(1) != 0; + } + public static void addTask(Connection conn, Task task) { + if (conn != null) { + try { + String sql = "INSERT INTO TODO (task, deadline, waga) VALUES (?, ?, ?)"; + PreparedStatement insert = conn.prepareStatement(sql); + + insert.setString(1, task.getTask()); + insert.setString(2, task.getDeadline()); + insert.setInt(3, task.getWaga()); + + insert.execute(); + } catch (SQLException e) { + throw new RuntimeException("Error adding task: " + e.getMessage(), e); + } + } + } + + public static void show(List tasks) { + tasks.forEach(System.out::println); + } + + public static void deleteTask(Connection conn, int id) { + try { + String sql = "DELETE FROM TODO WHERE id = ?"; + PreparedStatement psDel = conn.prepareStatement(sql); + psDel.setInt(1, id); + psDel.execute(); + } catch (SQLException e) { + throw new RuntimeException("Error deleting task: " + e.getMessage(), e); + } + } + + public static List filter(List tasks, String like) { + return tasks.stream() + .filter(task -> task.getTask().contains(like)) + .collect(Collectors.toList()); + } + + public static List getTasks(Connection conn) throws SQLException { + if (conn != null) { + if (tableExistsSQL(conn,"TODO")) { + try { + String sql = "SELECT * FROM TODO;"; + Statement st = conn.createStatement(); + ResultSet rs = st.executeQuery(sql); + + List tasks = new ArrayList<>(); + while (rs.next()) { + Task task = new Task(); + task.setId(rs.getInt("id")); + task.setTask(rs.getString("task")); + task.setDeadline(rs.getString("deadline")); + task.setWaga(rs.getInt("waga")); + tasks.add(task); + } + return tasks; + } catch (SQLException e) { + throw new RuntimeException("Error retrieving tasks: " + e.getMessage(), e); + } + } + else{ + try { + String sql = "CREATE TABLE TODO " + + " ( " + + " id int IDENTITY(1,1) NOT NULL CONSTRAINT TODO_PK PRIMARY KEY," + + " task VARCHAR (40) NOT NULL, " + + " deadline DATETIME," + + " waga INT" + + " );"; + PreparedStatement psDel = conn.prepareStatement(sql); + psDel.executeUpdate(); + } catch (SQLException e) { + throw new RuntimeException("Error creating table: " + e.getMessage(), e); + } + } + } + return Collections.emptyList(); + } + + public static Connection connect() { + Connection conn = null; + String configPath = "src/config.properties"; + try (FileInputStream propInput = new FileInputStream(configPath)) { + Properties properties = new Properties(); + properties.load(propInput); + + String url = "jdbc:sqlserver://" + properties.getProperty("DB_URL") + ";database=" + properties.getProperty("DB_DB"); + String user = properties.getProperty("DB_USER"); + String password = properties.getProperty("DB_PASSWORD"); + + conn = DriverManager.getConnection(url, user, password); + } catch (SQLException | IOException e) { + throw new RuntimeException("Error connecting to database: " + e.getMessage(), e); + } + return conn; + } + + public static void layout() { + List labels = Arrays.asList( + "[1] Dodaj task", + "[2] Usuń task", + "[3] Sortuj (data)", + "[4] Sortuj (waga)", + "[5] Filtruj", + "[6] Generuj HTML", + "[7] Generuj Prolog", + "[0] Wyjdź" + ); + labels.forEach(System.out::println); + } + + public static void genHTML(List tasks) { + try { + File template = new File("template.html"); + Scanner reader = new Scanner(template); + File outFile = new File("todo.html"); + + if (outFile.createNewFile()) { + System.out.println("Nowy plik: " + outFile.getName()); + } + + FileWriter writer = new FileWriter(outFile); + String line; + + while (reader.hasNextLine() && !(line = reader.nextLine()).trim().equals("")) { + writer.write(line + System.lineSeparator()); + } + + for (Task task : tasks) { + writer.write("" + System.lineSeparator()); + writer.write("" + task.getId() + "" + System.lineSeparator()); + writer.write("" + task.getTask() + "" + System.lineSeparator()); + writer.write("" + task.getDeadline() + "" + System.lineSeparator()); + writer.write("" + task.getWaga() + "" + System.lineSeparator()); + writer.write("" + System.lineSeparator()); + } + + while (reader.hasNextLine()) { + writer.write(reader.nextLine() + System.lineSeparator()); + } + + reader.close(); + writer.close(); + System.out.println("Wygenerowano."); + } catch (IOException e) { + System.out.println("An error occurred while generating HTML."); + e.printStackTrace(); + } + } + + public static void genProlog(List tasks){ + try{ + File outFile = new File("todo.pl"); + + if (outFile.createNewFile()) { + System.out.println("Nowy plik: " + outFile.getName()); + } + + FileWriter writer = new FileWriter(outFile); + + tasks.forEach(task -> { + try { + writer.write( + "task(\'" + task.getTask() + "\', date(" + task.getDeadline().substring(0, 4) + "," + task.getDeadline().substring(5, 7) + "," + task.getDeadline().substring(8, 10) + ")" + "," + task.getWaga() + ")." + System.lineSeparator()); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + writer.write("spoznione(X) :- task(X,Y,_),date_time_stamp(Y,S),get_time(T),S tasks = getTasks(conn); + + while (true) { + show(tasks); + layout(); + option = input.nextInt(); + + switch (option) { + case 1: + System.out.println("Task: "); + String taskName = input.next(); + System.out.println("Deadline (yyyy-mm-dd): "); + String deadline = input.next(); + System.out.println("Waga: "); + int waga = input.nextInt(); + addTask(conn, new Task(taskName, deadline, waga)); + tasks = getTasks(conn); + break; + case 2: + System.out.println("ID: "); + int id = input.nextInt(); + deleteTask(conn, id); + tasks = getTasks(conn); + break; + case 3: + tasks.sort(Comparator.comparing(Task::getDeadline)); + break; + case 4: + tasks.sort(Comparator.comparing(Task::getWaga)); + break; + case 5: + System.out.println("Filtruj: "); + String like = input.next(); + tasks = filter(getTasks(conn), like); + break; + case 6: + genHTML(tasks); + break; + case 7: + genProlog(tasks); + break; + case 0: + conn.close(); + return; + default: + System.out.println("Invalid option."); + break; + } + } + } + } + + public static void main(String[] args) throws SQLException { + menu(); + } +} diff --git a/src/Task.java b/src/Task.java new file mode 100644 index 0000000..dc14a88 --- /dev/null +++ b/src/Task.java @@ -0,0 +1,57 @@ +public class Task{ + int id; + private String task; + private String deadline; + private int waga; + + public Task() { + } + + public Task( String task, String deadline, int waga) { + this.task = task; + this.deadline = deadline; + this.waga = waga; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getTask() { + return task; + } + + @Override + public String toString() { + return "[\t" + + "id=" + id + + ",\t" + task + + ",\t" + deadline + + ",\t" + waga + + "\t]"; + } + + public void setTask(String task) { + this.task = task; + } + + public String getDeadline() { + return deadline; + } + + public void setDeadline(String deadline) { + this.deadline = deadline; + } + + public int getWaga() { + return waga; + } + + public void setWaga(int waga) { + this.waga = waga; + } +} \ No newline at end of file diff --git a/src/config.properties b/src/config.properties new file mode 100644 index 0000000..40b6b39 --- /dev/null +++ b/src/config.properties @@ -0,0 +1,4 @@ +DB_URL=mssql-2017.labs.wmi.amu.edu.pl +DB_DB= +DB_USER= +DB_PASSWORD= \ No newline at end of file diff --git a/src/style.css b/src/style.css new file mode 100644 index 0000000..fdf0799 --- /dev/null +++ b/src/style.css @@ -0,0 +1,102 @@ +body { + padding: 0; + background-color: white; + margin: 0; +} + +.container { + font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; + margin: 0; +} + +.topbar { + padding: 5px; + background-color: rgb(0, 183, 255); + border-bottom-right-radius: 20px; + border-bottom-left-radius: 20px; + text-align: center; + color: darkblue; +} + + +.btn { + height: 20px; + width: 170px; + text-align: center; + padding: 10px 20px; + background-color: rgb(0, 183, 255); + border-radius: 20px; + color: white; + font-weight: bolder; + border: 2px white solid; +} + +a { + text-decoration: none; +} + +.content { + margin: 0 auto; + padding: 0% 1vw; +} + +.wrapper { + text-align: justify; + width: 60vw; + margin: 0 auto; + padding: 1vh 18vw; + align-items: center; +} + +p { + vertical-align: top; + height: 100%; + padding: 1vw; + background-color: #eee; + border-radius: 30px; +} + +img { + vertical-align: top; + padding-top: 1vh; + margin: 1vh 1vw; + border-radius: 30px; +} + +table { + border-collapse: collapse; + margin: 0 auto; + padding-top: 2vh; +} + +hr { + width: 55vw; + height: 1px; + background-color: rgb(0, 183, 255); +} + +td, +th { + border: 1px solid #ddd; + padding: 8px; +} + +tr:nth-child(even) { + background-color: #f2f2f2; +} + +tr:hover { + background-color: #ddd; +} + +th { + padding-top: 10px; + padding-bottom: 10px; + text-align: left; + background-color: darkblue; + color: white; +} + +#gallery>img { + border-radius: 0; +} \ No newline at end of file diff --git a/template.html b/template.html new file mode 100644 index 0000000..b0c1a98 --- /dev/null +++ b/template.html @@ -0,0 +1,35 @@ + + + + + + + + Trasy + + + +
+
+

TODO

+
+
+ + + + + + + + + + + + + +
IDTaskDeadlineWaga
+
+
+ + + \ No newline at end of file diff --git a/todo.html b/todo.html new file mode 100644 index 0000000..17b8324 --- /dev/null +++ b/todo.html @@ -0,0 +1,52 @@ + + + + + + + + Trasy + + + +
+
+

TODO

+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
IDTaskDeadlineWaga
1kolokwium2024-06-17 00:00:00.01
2projekt_paradygmaty2024-06-16 00:00:00.00
3projekt_BD2024-06-20 00:00:00.02
+
+
+ + + diff --git a/todo.pl b/todo.pl new file mode 100644 index 0000000..bc5c88d --- /dev/null +++ b/todo.pl @@ -0,0 +1,5 @@ +task('kolokwium', date(2024,06,17),1). +task('projekt_paradygmaty', date(2024,06,16),0). +task('projekt_BD', date(2024,06,20),2). +spoznione(X) :- task(X,Y,_),date_time_stamp(Y,S),get_time(T),S