소스 검색

Added Role class and implemented adding roles for a new movie

toomanysugar 4 년 전
부모
커밋
a0423434d9

+ 1 - 0
.gitignore

@@ -2,3 +2,4 @@
 /.idea/
 /target/
 /src/test/
+/javafx-sdk-16/

+ 82 - 31
src/main/java/com/moviesdb/AddMovieController.java

@@ -2,15 +2,20 @@ package com.moviesdb;
 
 import javafx.fxml.FXML;
 import javafx.fxml.FXMLLoader;
+import javafx.scene.Node;
 import javafx.scene.control.Button;
 import javafx.scene.control.ScrollPane;
 import javafx.scene.control.TextArea;
 import javafx.scene.control.TextField;
 import javafx.scene.layout.AnchorPane;
+import javafx.scene.layout.HBox;
 import javafx.scene.layout.VBox;
 import javafx.stage.Modality;
 
+import javax.enterprise.inject.New;
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
 
 //For reading properties from .properties file
 
@@ -25,30 +30,30 @@ public class AddMovieController extends Controller {
     VBox actorsGridAncor;
 
     @FXML
-            TextField urlTextBox;
+    TextField urlTextBox;
     @FXML
-            TextField titleTextBox;
+    TextField titleTextBox;
     @FXML
-            TextField genreTextBox;
+    TextField genreTextBox;
     @FXML
     TextArea descriptionTextBox;
     @FXML
-            TextField yearTextBox;
+    TextField yearTextBox;
     @FXML
-            TextField timeTextBox;
+    TextField timeTextBox;
     @FXML
-            TextField studioTextBox;
+    TextField studioTextBox;
     @FXML
-            TextField directorTextBox;
+    TextField directorTextBox;
     @FXML
-            TextField rateTextBox;
+    TextField rateTextBox;
 
     Object caller;
 
     public void open(Object caller) {
         prepare();
         stage.initModality(Modality.WINDOW_MODAL);
-        stage.initOwner(((BrowserController)caller).stage.getScene().getWindow());
+        stage.initOwner(((BrowserController) caller).stage.getScene().getWindow());
         this.caller = caller;
         stage.setTitle("Log in");
         stage.show();
@@ -56,22 +61,20 @@ public class AddMovieController extends Controller {
         addActor();
     }
 
-    public void addActor(){
+    public void addActor() {
         try {
             AnchorPane row = FXMLLoader.load(getClass().getResource("../../ActorRoleSegment.fxml"));
             //row.setPrefWidth(actorsGrid.getWidth());
             actorsGridAncor.getChildren().add(row);
             actorsGrid.setMinHeight(actorsGrid.getMinHeight() + 35);
             //actorsGrid.getChildren().add(row);
-        }
-        catch (IOException ex)
-        {
-
+        } catch (IOException ex) {
+            ex.printStackTrace();
         }
         //System.out.println("LoginForm closed");
     }
 
-    public void addMovie(){
+    public void executeForm() {
         //read all textboxes
         Movie NewMovie = new Movie();
         NewMovie.setCover(urlTextBox.getText());
@@ -84,28 +87,76 @@ public class AddMovieController extends Controller {
         NewMovie.setRating(rateTextBox.getText());
         NewMovie.setDescription(descriptionTextBox.getText());
 
-        try{
+
+        //TODO Сheck on id (if addMovie(...) = -1 movie not added)
+        NewMovie.setId(((BrowserController)caller).my_connector.addMovie(NewMovie));
+        System.out.println("Added Movie with ID: " + NewMovie.id());
+
+        List<Role> newMovieRoles = fetchRoles(NewMovie);
+        if (newMovieRoles.size() > 0) {
+            System.out.println("had something to add");
+            int counter = 0;
+            StringBuilder addedIDs = new StringBuilder();
+            for (Role element : newMovieRoles) {
+                element.setRoleId(((BrowserController) caller).my_connector.addRole(element));
+                if (element.id() != -1) {
+                    addedIDs.append(" ").append(element.id()).append(",");
+                    counter++;
+                }
+            }
+            System.out.println("Added " + counter + " Roles with IDs:" + addedIDs);
+        } else {
+            System.out.println("nothing to add");
+        }
+    }
+
+
+    private List<Role> fetchRoles(Movie targetMovie) {
+        List<Role> roles = new ArrayList<Role>();
+        try {
             //AnchorPane row = (AnchorPane) actorsGrid.getChildren().get(1);
-            /*
-            for (Node row : actorsGrid.getChildren()) {
-                System.out.println("Id: " + row.getId());
+
+            for (Node row : actorsGridAncor.getChildren()) {
+                System.out.println("Type: " + row.toString());
                 if (row instanceof AnchorPane) {
                     // clear
-                    for (Node node : ((AnchorPane)row).getChildren()) {
-                        System.out.println("Id: " + node.getId());
-                        if (node instanceof TextField) {
-                            // clear
-                            ((TextField)node).setText("I have access");
-                        }
+                    String name = null;
+                    String role = null;
+                    for (Node hbox : ((AnchorPane) row).getChildren()) {
+                        System.out.println("\t-Type: " + hbox.toString());
+
+                        if (hbox instanceof HBox)
+                            for (Node halfrow : ((HBox) hbox).getChildren()) {
+                                System.out.println("\t\t-Type: " + halfrow.toString());
+
+                                if (halfrow instanceof AnchorPane)
+                                    for (Node part : ((AnchorPane) halfrow).getChildren()) {
+                                        System.out.println("\t\t\t-Type: " + part.toString());
+
+                                        if (part instanceof TextField) {
+                                            System.out.println("\t\t\t\t Text: " + ((TextField) part).getText());
+                                            if (part.getId().equals("actorName")) {
+                                                name = ((TextField) part).getText();
+                                            }
+                                            if (part.getId().equals("actorRole")) {
+                                                role = ((TextField) part).getText();
+                                            }
+                                        }
+                                    }
+                            }
+                    }
+                    if (name != "" || role != "") {
+                        Role rowRole = new Role(targetMovie.id(), name, role);
+                        roles.add(rowRole);
+                        System.out.println("Final: movieId=" + rowRole.movieId() + ", actorName=" + rowRole.actor() + ", actorRole=" + rowRole.role() + "\n");
+                        System.out.println(rowRole.movieId());
                     }
                 }
-            }
-            */
-        }
-        catch (Exception ex)
-        {
 
+            }
+        } catch (Exception ex) {
+            ex.printStackTrace();
         }
-        ((BrowserController)caller).my_connector.addMovie(NewMovie);
+        return roles;
     }
 }

+ 63 - 1
src/main/java/com/moviesdb/DataBaseConnector.java

@@ -3,6 +3,7 @@ package com.moviesdb;
 import java.sql.*;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Objects;
 
 public class DataBaseConnector {
 
@@ -148,7 +149,7 @@ public class DataBaseConnector {
         return  resultedMovies;
     }
 
-    public void addMovie(Movie movie) {
+    public int addMovie(Movie movie) {
         try {
             Statement statement = conn.createStatement();
             String req;
@@ -212,9 +213,67 @@ public class DataBaseConnector {
             System.out.println(req);
             //выполнение запроса
             statement.execute(req);
+
+            //For returning id of added movie
+            statement = conn.prepareStatement("SELECT * FROM Movies;", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
+            req = "SELECT * FROM Movies;";
+            ResultSet rs = statement.executeQuery(req);
+            rs.last();
+            return rs.getInt("id");
+        } catch (SQLException ex) {
+            ex.printStackTrace();
+        }
+        return -1;
+    }
+
+    public int addRole(Role role) {
+        try {
+            Statement statement = conn.createStatement();
+            String req;
+
+            String intoPart = "";
+            String valPart = "";
+
+            if (role.movieId() > 0) {
+                intoPart += " movie_id,";
+                valPart += " '" + role.movieId() + "',";
+            }
+
+            if (!role.actor().equals("Role_actor_undefined")) {
+                intoPart += " actor,";
+                valPart += " '" + role.actor() + "',";
+            }
+
+            if (!role.role().equals("Role_role_undefined")) {
+                intoPart += " role,";
+                valPart += " '" + role.role() + "',";
+            }
+            if (intoPart.endsWith(",") && valPart.endsWith(",")) {
+                intoPart = intoPart.substring(1, intoPart.length() - 1);
+                valPart = valPart.substring(1, valPart.length() - 1);
+
+                //сборка текста запроса
+                req = "INSERT INTO Roles (" + intoPart + ") ";
+                req += "values (" + valPart + ");";
+                System.out.println(req);
+                //выполнение запроса
+                if (true) {
+                    statement.execute(req);
+
+                    //For returning id of added movie
+                    statement = conn.prepareStatement("SELECT * FROM Roles;", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
+                    req = "SELECT * FROM Roles;";
+                    ResultSet rs = statement.executeQuery(req);
+                    rs.last();
+                    return rs.getInt("id");
+                }
+            } else {
+                System.out.println("[ERROR] Error in creation of addRole's query!");
+            }
         } catch (SQLException ex) {
             ex.printStackTrace();
         }
+        return -1;
     }
 
     public void updateMovie(Movie oldMovie, Movie newMovie) {
@@ -287,6 +346,9 @@ public class DataBaseConnector {
     }
 
     public boolean granted(String grantName){
+        if (connRights == null){
+            return false;
+        }
         return connRights.contains(grantName);
     }
 

+ 50 - 0
src/main/java/com/moviesdb/Role.java

@@ -0,0 +1,50 @@
+package com.moviesdb;
+
+import java.util.Objects;
+
+public class Role {
+    private int roleId;
+    private final int movieId;
+    private final String actor;
+    private final String role;
+
+    //To get a Role from DataBaseConnector getRoles
+    public Role(int movieId, String actor, String role, int roleId) {
+        this.roleId = roleId;
+        this.movieId = movieId;
+        this.actor = actor;
+        this.role = role;
+    }
+
+    //To get a Role from AddMovieConnector fetchRoles
+    public Role(int movieId, String actor, String role) {
+        this.movieId = movieId;
+        this.actor = actor;
+        this.role = role;
+    }
+
+    public int id(){
+        return roleId;
+    }
+
+    public int movieId() {
+        return movieId;
+    }
+
+    public String actor() {
+        if (actor != "")
+            return actor;
+        return "Role_actor_undefined";
+    }
+
+    public String role() {
+        if (role != "")
+            return role;
+        return "Role_role_undefined";
+    }
+
+    //For testing needs only
+    public void setRoleId(int roleId) {
+        this.roleId = roleId;
+    }
+}

+ 1 - 1
src/main/resources/AddMovieWindow.fxml

@@ -158,7 +158,7 @@
              </AnchorPane>
              <AnchorPane prefWidth="200.0" VBox.vgrow="NEVER">
                 <children>
-                   <Label onMouseClicked="#addMovie" text="Add" textFill="#9f9f9f" AnchorPane.bottomAnchor="14.0" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="14.0">
+                   <Label onMouseClicked="#executeForm" text="Add" textFill="#9f9f9f" AnchorPane.bottomAnchor="14.0" AnchorPane.rightAnchor="14.0" AnchorPane.topAnchor="14.0">
                       <font>
                          <Font name="System Bold" size="16.0" />
                       </font>