From 0f128341aad4bdd866f1496adb8d30d3ba54767c Mon Sep 17 00:00:00 2001 From: jez04 <david.jezek@post.cz> Date: Thu, 14 Mar 2024 23:19:35 +0100 Subject: [PATCH] feat: init sources for lab start --- .gitignore | 18 ++ pom.xml | 168 ++++++++++++++++++ src/main/java/cz/vsb/fei/java2/lab05/App.java | 100 +++++++++++ .../cz/vsb/fei/java2/lab05/AppController.java | 20 +++ .../vsb/fei/java2/lab05/entities/Course.java | 21 +++ .../vsb/fei/java2/lab05/entities/Student.java | 16 ++ src/main/java/module-info.java | 15 ++ src/main/resources/META-INF/persistence.xml | 58 ++++++ .../cz/vsb/fei/java2/lab05/AppWindow.fxml | 93 ++++++++++ .../cz/vsb/fei/java2/lab05/application.css | 1 + src/main/resources/log4j2.xml | 13 ++ .../java/cz/vsb/fei/java2/lab05/AppTest.java | 19 ++ 12 files changed, 542 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/cz/vsb/fei/java2/lab05/App.java create mode 100644 src/main/java/cz/vsb/fei/java2/lab05/AppController.java create mode 100644 src/main/java/cz/vsb/fei/java2/lab05/entities/Course.java create mode 100644 src/main/java/cz/vsb/fei/java2/lab05/entities/Student.java create mode 100644 src/main/java/module-info.java create mode 100644 src/main/resources/META-INF/persistence.xml create mode 100644 src/main/resources/cz/vsb/fei/java2/lab05/AppWindow.fxml create mode 100644 src/main/resources/cz/vsb/fei/java2/lab05/application.css create mode 100644 src/main/resources/log4j2.xml create mode 100644 src/test/java/cz/vsb/fei/java2/lab05/AppTest.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..06fab23 --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ +# Eclipse +.classpath +.project +.settings/ + +# Intellij +.idea/ +*.iml +*.iws + +# Mac +.DS_Store + +# Maven +log/ +target/ + +db/ \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..732df19 --- /dev/null +++ b/pom.xml @@ -0,0 +1,168 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>cz.vsb.fei.java2</groupId> + <artifactId>lab05</artifactId> + <version>0.0.1-SNAPSHOT</version> + + <name>lab05</name> + + <packaging>jar</packaging> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <maven.compiler.release>21</maven.compiler.release> + <JavaFX.version>22-ea+16</JavaFX.version> + <JUnit.version>5.10.1</JUnit.version> + <log4j.version>2.22.1</log4j.version> + <lombok.version>1.18.30</lombok.version> + </properties> + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.junit</groupId> + <artifactId>junit-bom</artifactId> + <version>${JUnit.version}</version> + <type>pom</type> + <scope>import</scope> + </dependency> + </dependencies> + </dependencyManagement> + + <dependencies> + <dependency> + <groupId>org.openjfx</groupId> + <artifactId>javafx-controls</artifactId> + <version>${JavaFX.version}</version> + </dependency> + <dependency> + <groupId>org.openjfx</groupId> + <artifactId>javafx-fxml</artifactId> + <version>${JavaFX.version}</version> + </dependency> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-core</artifactId> + <version>${log4j.version}</version> + </dependency> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-api</artifactId> + <version>${log4j.version}</version> + </dependency> + <dependency> + <groupId>org.projectlombok</groupId> + <artifactId>lombok</artifactId> + <version>${lombok.version}</version> + <scope>provided</scope> + </dependency> + + <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-core</artifactId> + <version>6.4.4.Final</version> + </dependency> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-jpamodelgen</artifactId> + <scope>provided</scope> + <version>6.4.4.Final</version> + </dependency> + + <!-- https://mvnrepository.com/artifact/org.apache.derby/derby --> + <dependency> + <groupId>org.apache.derby</groupId> + <artifactId>derby</artifactId> + <version>10.17.1.0</version> + </dependency> + <!-- https://mvnrepository.com/artifact/org.apache.derby/derbyclient --> + <dependency> + <groupId>org.apache.derby</groupId> + <artifactId>derbyclient</artifactId> + <version>10.17.1.0</version> + </dependency> + <!-- https://mvnrepository.com/artifact/org.apache.derby/derbytools --> + <dependency> + <groupId>org.apache.derby</groupId> + <artifactId>derbytools</artifactId> + <version>10.17.1.0</version> + </dependency> + <!-- https://mvnrepository.com/artifact/com.h2database/h2 --> + <dependency> + <groupId>com.h2database</groupId> + <artifactId>h2</artifactId> + <version>2.2.224</version> + </dependency> + + <!-- + https://mvnrepository.com/artifact/jakarta.persistence/jakarta.persistence-api --> + <dependency> + <groupId>jakarta.persistence</groupId> + <artifactId>jakarta.persistence-api</artifactId> + <version>3.1.0</version> + </dependency> + + + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.12.1</version> + <configuration> + <annotationProcessorPaths> + <path> + <groupId>org.projectlombok</groupId> + <artifactId>lombok</artifactId> + <version>${lombok.version}</version> + </path> + <path> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-jpamodelgen</artifactId> + <version>6.4.4.Final</version> + </path> + </annotationProcessorPaths> + </configuration> + <executions> + <execution> + <id>process</id> + <phase>generate-sources</phase> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>build-helper-maven-plugin</artifactId> + <version>3.5.0</version> + <configuration> + <sources> + <source>target/generated-sources/annotations</source> + </sources> + </configuration> + <executions> + <execution> + <id>add-source</id> + <phase>process-resources</phase> + <goals> + <goal>add-source</goal> + </goals> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-surefire-plugin</artifactId> + <version>3.2.1</version> + </plugin> + </plugins> + </build> + +</project> diff --git a/src/main/java/cz/vsb/fei/java2/lab05/App.java b/src/main/java/cz/vsb/fei/java2/lab05/App.java new file mode 100644 index 0000000..3d248c8 --- /dev/null +++ b/src/main/java/cz/vsb/fei/java2/lab05/App.java @@ -0,0 +1,100 @@ +package cz.vsb.fei.java2.lab05; + +import java.sql.SQLException; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Random; + +import org.h2.tools.Server; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityManagerFactory; +import jakarta.persistence.Persistence; +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.scene.layout.BorderPane; +import javafx.stage.Stage; +import javafx.stage.WindowEvent; +import lombok.extern.log4j.Log4j2; + +/** + * Class <b>App</b> - extends class Application and it is an entry point of the + * program + * + * @author Java I + */ +@Log4j2 +public class App extends Application { + + private static final Random RANDOM = new Random(); + private static final List<String> COURSE_NAMES = Collections + .unmodifiableList(Arrays.asList("Java 1", "Java 2", "C# 1", "C# 2", "Python", "Databáze I", "Databáze II", + "FunkcionálnĂ programovánĂ", "OOP", "PS", "APPS", "SWI")); + private static final List<String> FIRST_NAMES = Collections + .unmodifiableList(Arrays.asList("Petr", "Marie", "Jan", "Jana", "Tomáš", "KateĹ™ina", "Lukáš", "Tereza", + "Martin", "Veronika", "David", "Eva", "Jakub", "Lucie", "Michal", "Anna", "Adam", "Monika", "Tom", + "Klára", "Robert", "KristĂ˝na", "Marek", "Simona", "Filip", "Petra", "OndĹ™ej", "Lenka", "MatÄ›j", + "MarkĂ©ta", "Pavel", "Hana", "Jakub", "AdĂ©la", "Daniel", "Barbora", "Lukáš", "Eliška", "Josef")); + private static final List<String> LAST_NAMES = Collections.unmodifiableList(Arrays.asList("Novák", "Svoboda", + "NovotnĂ˝", "Dvořák", "ÄŚernĂ˝", "Procházková", "KuÄŤera", "VeselĂ˝", "Horák", "NÄ›mec", "PokornĂ˝", "Mareš", + "PospĂšilová", "Hájek", "JelĂnek", "Král", "RĹŻĹľiÄŤka", "Beneš", "Fiala", "Sedláček", "KĹ™ĂĹľ", "NÄ›mcová", + "VlÄŤek", "Kolář", "Bartoš", "BĂlĂ˝", "Veselá", "KovaĹ™Ăk", "Havelka", "MalĂ˝", "Urban", "KopeckĂ˝", "Vlach", + "Ĺ imek", "KoneÄŤnĂ˝", "DoleĹľal", "Ĺ ĹĄastnĂ˝", "KopeÄŤná", "Holub", "PospĂchal")); + + public static <T> T randomElement(List<T> list) { + return list.get(RANDOM.nextInt(list.size())); + } + + public static String randomCode() { + return String.format("%d-%d/%02d", 400+RANDOM.nextInt(80), 2015+RANDOM.nextInt(10), 1+RANDOM.nextInt(5)); + } + public static void main(String[] args) { + log.info("Launching JavaFX application."); + + EntityManagerFactory emf = Persistence.createEntityManagerFactory("java2"); + EntityManager em = emf.createEntityManager(); + +// launch(args); + + //Start HTTP server for access H2 DB for look inside + try { + Server server = Server.createWebServer(); + log.info("To inspect DB go to URL: " + server.getURL()); + server.start(); + } catch (SQLException e) { + e.printStackTrace(); + } + + } + + private AppController controller; + + @Override + public void start(Stage primaryStage) { + try { + // Construct a main window with a canvas. + + FXMLLoader loader = new FXMLLoader(this.getClass().getResource("AppWindow.fxml")); + + BorderPane root = loader.load(); + + Scene scene = new Scene(root); + + primaryStage.setScene(scene); + primaryStage.setTitle("Hello word app"); + primaryStage.show(); + controller = loader.getController(); + + // Exit program when main window is closed + primaryStage.setOnCloseRequest(this::exitProgram); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void exitProgram(WindowEvent evt) { + System.exit(0); + } +} \ No newline at end of file diff --git a/src/main/java/cz/vsb/fei/java2/lab05/AppController.java b/src/main/java/cz/vsb/fei/java2/lab05/AppController.java new file mode 100644 index 0000000..0357df1 --- /dev/null +++ b/src/main/java/cz/vsb/fei/java2/lab05/AppController.java @@ -0,0 +1,20 @@ +package cz.vsb.fei.java2.lab05; + +import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.scene.control.Label; +import javafx.scene.control.MenuItem; + +public class AppController { + + @FXML + private Label infoLabel; + + @FXML + private void menuPressed(ActionEvent event) { + if(event.getSource() instanceof MenuItem menuItem) { + infoLabel.setText("Selected menu: " + menuItem.getText()); + } + } + +} diff --git a/src/main/java/cz/vsb/fei/java2/lab05/entities/Course.java b/src/main/java/cz/vsb/fei/java2/lab05/entities/Course.java new file mode 100644 index 0000000..318d24a --- /dev/null +++ b/src/main/java/cz/vsb/fei/java2/lab05/entities/Course.java @@ -0,0 +1,21 @@ +package cz.vsb.fei.java2.lab05.entities; + +import java.util.List; + +import lombok.ToString; + +public class Course { + + private Long id; + + @ToString.Exclude + private List<Student> students; + + private String code; + + private String name; + + public String toStringWithStudents() { + return "Course [id=" + id + ", code=" + code + ", name=" + name + ", students=" + students + "]"; + } +} diff --git a/src/main/java/cz/vsb/fei/java2/lab05/entities/Student.java b/src/main/java/cz/vsb/fei/java2/lab05/entities/Student.java new file mode 100644 index 0000000..2e1ad4d --- /dev/null +++ b/src/main/java/cz/vsb/fei/java2/lab05/entities/Student.java @@ -0,0 +1,16 @@ +package cz.vsb.fei.java2.lab05.entities; + +public class Student { + + private Long id; + + private String firstName; + + private String lastName; + + private Course course; + + public String toStringNoCourse() { + return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]"; + } +} diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java new file mode 100644 index 0000000..1afe166 --- /dev/null +++ b/src/main/java/module-info.java @@ -0,0 +1,15 @@ +module cz.vsb.fei.java2.lab05 { + requires transitive javafx.controls; + requires javafx.fxml; + opens cz.vsb.fei.java2.lab05 to javafx.fxml; + exports cz.vsb.fei.java2.lab05; + + requires static lombok; + requires org.apache.logging.log4j; + + requires jakarta.persistence; + requires jakarta.annotation; + requires com.h2database; + opens cz.vsb.fei.java2.lab05.entities; + requires org.hibernate.orm.core; +} \ No newline at end of file diff --git a/src/main/resources/META-INF/persistence.xml b/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000..73176e7 --- /dev/null +++ b/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,58 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- JBoss, Home of Professional Open Source Copyright 2013, Red Hat, Inc. + and/or its affiliates, and individual contributors by the @authors tag. See + the copyright.txt in the distribution for a full listing of individual contributors. + Licensed under the Apache License, Version 2.0 (the "License"); you may not + use this file except in compliance with the License. You may obtain a copy + of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required + by applicable law or agreed to in writing, software distributed under the + License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS + OF ANY KIND, either express or implied. See the License for the specific + language governing permissions and limitations under the License. --> +<persistence version="2.0" + xmlns="http://java.sun.com/xml/ns/persistence" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://java.sun.com/xml/ns/persistence + http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> + <persistence-unit name="java2" + transaction-type="RESOURCE_LOCAL"> + <!-- + <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> + --> + + <!-- If you are running in a production environment, add a managed data + source, this example data source is just for development and testing! --> + <properties> + <!-- Properties for JPA (for any provider) --> + <!-- DB Apache Derby + <property name="jakarta.persistence.jdbc.url" + value="jdbc:derby:db/lab05;create=true" /> + <property name="jakarta.persistence.jdbc.driver" + value="org.apache.derby.jdbc.EmbeddedDriver" /> + --> + + <property name="jakarta.persistence.jdbc.url" + value="jdbc:h2:file:./db/java2" /> + <!-- In memory DB no store to disk + <property name="jakarta.persistence.jdbc.url" + value="jdbc:h2:mem:java2" /> + --> + <property name="jakarta.persistence.jdbc.driver" + value="org.h2.Driver" /> + + <property name="jakarta.persistence.jdbc.user" value="app"/> + <property name="jakarta.persistence.jdbc.password" value="app"/> + <property name="jakarta.persistence.schema-generation.database.action" value="create"></property> + + <!-- Properties for Hibernate --> + <property name="hibernate.show_sql" value="false" /> + <property name="hibernate.format_sql" value="true" /> + <!-- + <property name="hibernate.hbm2ddl.auto" value="update" /> + --> + + </properties> + </persistence-unit> + +</persistence> diff --git a/src/main/resources/cz/vsb/fei/java2/lab05/AppWindow.fxml b/src/main/resources/cz/vsb/fei/java2/lab05/AppWindow.fxml new file mode 100644 index 0000000..ccb2a0c --- /dev/null +++ b/src/main/resources/cz/vsb/fei/java2/lab05/AppWindow.fxml @@ -0,0 +1,93 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- + Copyright (c) 2015, 2019, Gluon and/or its affiliates. + All rights reserved. Use is subject to license terms. + + This file is available and licensed under the following license: + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + - Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the distribution. + - Neither the name of Oracle Corporation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +--> + +<?import javafx.scene.control.Label?> +<?import javafx.scene.control.Menu?> +<?import javafx.scene.control.MenuBar?> +<?import javafx.scene.control.MenuItem?> +<?import javafx.scene.control.SeparatorMenuItem?> +<?import javafx.scene.layout.BorderPane?> +<?import javafx.scene.text.Font?> + + +<BorderPane minHeight="400.0" minWidth="640.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cz.vsb.fei.java2.lab05.AppController"> + <top> + <MenuBar BorderPane.alignment="CENTER"> + <menus> + <Menu mnemonicParsing="false" text="File"> + <items> + <MenuItem mnemonicParsing="false" onAction="#menuPressed" text="New" /> + <MenuItem mnemonicParsing="false" onAction="#menuPressed" text="Open…" /> + <Menu mnemonicParsing="false" text="Open Recent" /> + <SeparatorMenuItem mnemonicParsing="false" /> + <MenuItem mnemonicParsing="false" onAction="#menuPressed" text="Close" /> + <MenuItem mnemonicParsing="false" onAction="#menuPressed" text="Save" /> + <MenuItem mnemonicParsing="false" onAction="#menuPressed" text="Save As…" /> + <MenuItem mnemonicParsing="false" onAction="#menuPressed" text="Revert" /> + <SeparatorMenuItem mnemonicParsing="false" onAction="#menuPressed" /> + <MenuItem mnemonicParsing="false" onAction="#menuPressed" text="Preferences…" /> + <SeparatorMenuItem mnemonicParsing="false" /> + <MenuItem mnemonicParsing="false" onAction="#menuPressed" text="Quit" /> + </items> + </Menu> + <Menu mnemonicParsing="false" text="Edit"> + <items> + <MenuItem mnemonicParsing="false" onAction="#menuPressed" text="Undo" /> + <MenuItem mnemonicParsing="false" onAction="#menuPressed" text="Redo" /> + <SeparatorMenuItem mnemonicParsing="false" /> + <MenuItem mnemonicParsing="false" onAction="#menuPressed" text="Cut" /> + <MenuItem mnemonicParsing="false" onAction="#menuPressed" text="Copy" /> + <MenuItem mnemonicParsing="false" onAction="#menuPressed" text="Paste" /> + <MenuItem mnemonicParsing="false" onAction="#menuPressed" text="Delete" /> + <SeparatorMenuItem mnemonicParsing="false" /> + <MenuItem mnemonicParsing="false" onAction="#menuPressed" text="Select All" /> + <MenuItem mnemonicParsing="false" onAction="#menuPressed" text="Unselect All" /> + </items> + </Menu> + <Menu mnemonicParsing="false" text="Help"> + <items> + <MenuItem mnemonicParsing="false" onAction="#menuPressed" text="About MyHelloApp" /> + </items> + </Menu> + </menus> + </MenuBar> + </top> + <center> + <Label fx:id="infoLabel" alignment="CENTER" style=" " text="Drag components from Library here…" textAlignment="CENTER" textFill="#9f9f9f" wrapText="false" BorderPane.alignment="CENTER"> + <font> + <Font size="18.0" /> + </font> + </Label> + </center> +</BorderPane> diff --git a/src/main/resources/cz/vsb/fei/java2/lab05/application.css b/src/main/resources/cz/vsb/fei/java2/lab05/application.css new file mode 100644 index 0000000..83d6f33 --- /dev/null +++ b/src/main/resources/cz/vsb/fei/java2/lab05/application.css @@ -0,0 +1 @@ +/* JavaFX CSS - Leave this comment until you have at least create one rule which uses -fx-Property */ \ No newline at end of file diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml new file mode 100644 index 0000000..acb3514 --- /dev/null +++ b/src/main/resources/log4j2.xml @@ -0,0 +1,13 @@ +<Configuration> + <Appenders> + <Console name="Console"> + <PatternLayout + pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" /> + </Console> + </Appenders> + <Loggers> + <Root level="info"> + <AppenderRef ref="Console"></AppenderRef> + </Root> + </Loggers> +</Configuration> diff --git a/src/test/java/cz/vsb/fei/java2/lab05/AppTest.java b/src/test/java/cz/vsb/fei/java2/lab05/AppTest.java new file mode 100644 index 0000000..86b4c21 --- /dev/null +++ b/src/test/java/cz/vsb/fei/java2/lab05/AppTest.java @@ -0,0 +1,19 @@ +package cz.vsb.fei.java2.lab05; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +/** + * Unit test for simple App. + */ +class AppTest { + + /** + * Rigorous Test :-) + */ + @Test + void shouldAnswerWithTrue() { + assertTrue(true); + } +} -- GitLab