Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
package com.carvalhotechsolutions.mundoanimal.controllers;

import com.carvalhotechsolutions.mundoanimal.model.Usuario;
import com.carvalhotechsolutions.mundoanimal.model.enums.TipoUsuario;
import com.carvalhotechsolutions.mundoanimal.utils.SessionManager;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;

public class MenuController implements Initializable {
Expand Down Expand Up @@ -36,10 +48,34 @@ public class MenuController implements Initializable {
@FXML
private Button servicos_btn;

@FXML
private Button sair_btn;

@FXML
private Label userNameLabel;

@FXML
private Label userTypeLabel;


@Override
public void initialize(URL url, ResourceBundle resourceBundle) {

private Button activeButton; // Botão atualmente ativo

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
Usuario usuarioLogado = SessionManager.getCurrentUser();
if (usuarioLogado.getTipoUsuario() == TipoUsuario.SECRETARIO) {
servicos_btn.setVisible(false);
secretarios_btn.setVisible(false);
userTypeLabel.setText("Secretário");
}
userNameLabel.setText(usuarioLogado.getNomeUsuario());
// Set up button actions
servicos_btn.setOnAction(event -> loadPage("servicos.fxml"));
secretarios_btn.setOnAction(event -> loadPage("secretarios.fxml"));
sair_btn.setOnAction(event -> logout());
// Configure ações para cada botão
configureButton(inicio_btn, "inicio.fxml");
configureButton(clientes_btn, "clientes.fxml");
Expand Down Expand Up @@ -83,4 +119,41 @@ private void loadPage(String fxmlFile) {
e.printStackTrace();
}
}

private void logout() {
// Cria um alerta de confirmação
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Confirmação de Saída");
alert.setHeaderText("Você deseja realmente sair?");
alert.setContentText("Ao sair, você será desconectado do sistema.");

// Adiciona um ícone personalizado ao alerta
ImageView imageView = new ImageView(new Image(getClass().getResourceAsStream("/assets/images/sad-dog.png"))); // Adicione o caminho para o seu ícone
imageView.setFitHeight(50);
imageView.setFitWidth(50);

alert.setGraphic(imageView);

// Captura a escolha do usuário
Optional<ButtonType> result = alert.showAndWait();

// Verifica a escolha
if (result.isPresent() && result.get() == ButtonType.OK) {
try {
Stage stage = (Stage) contentArea.getScene().getWindow(); // Obtém o estágio atual a partir do contentArea
Parent loginPage = FXMLLoader.load(getClass().getResource("/fxml/autenticacao/login.fxml")); // Carrega a página de login
Scene newScene = new Scene(loginPage); // Cria uma nova cena com a página de login carregada
stage.setScene(newScene); // Define a nova cena no estágio
stage.show(); // Exibe o estágio atualizado

SessionManager.setCurrentUser(null); // Limpa o usuário logado
} catch (IOException e) {
e.printStackTrace();
}
} else {
// Usuário cancelou a ação
alert.close();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import javafx.stage.Stage;

import java.io.IOException;
import java.math.BigDecimal;
import java.net.URL;
import java.util.ResourceBundle;

Expand All @@ -35,7 +36,7 @@ public class ServicoController implements Initializable {
private TableColumn<Servico, String> descricaoColumn;

@FXML
private TableColumn<Servico, Double> valorColumn;
private TableColumn<Servico, BigDecimal> valorColumn;

@FXML
private TableColumn<Servico, Void> acaoColumn;
Expand All @@ -50,6 +51,7 @@ public void initialize(URL url, ResourceBundle resourceBundle) {
descricaoColumn.setCellValueFactory(new PropertyValueFactory<>("descricao"));
valorColumn.setCellValueFactory(new PropertyValueFactory<>("valorServico"));

configurarColunaValor();
configurarColunaAcao();
atualizarTableView();
}
Expand Down Expand Up @@ -93,6 +95,23 @@ protected void updateItem(Void item, boolean empty) {
});
}

private void configurarColunaValor() {
valorColumn.setCellFactory(column -> new TableCell<Servico, BigDecimal>() {
@Override
protected void updateItem(BigDecimal item, boolean empty) {
super.updateItem(item, empty);

if (empty || item == null) {
setText(null);
} else {
// Formatar valor para exibição com "R$"
setText("R$ " + item.setScale(2, BigDecimal.ROUND_HALF_UP).toString().replace(".", ","));
}
}
});
}


@FXML
public void abrirModalCadastrarServico() {
try {
Expand Down Expand Up @@ -178,3 +197,4 @@ public void atualizarTableView() {
}

}

Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.carvalhotechsolutions.mundoanimal.controllers;
package com.carvalhotechsolutions.mundoanimal.controllers.login;

import com.carvalhotechsolutions.mundoanimal.JPAutil;
import com.carvalhotechsolutions.mundoanimal.model.Usuario;
import com.carvalhotechsolutions.mundoanimal.security.PasswordUtils;
import com.carvalhotechsolutions.mundoanimal.utils.NavigationManager;
import com.carvalhotechsolutions.mundoanimal.utils.SessionManager;
import jakarta.persistence.EntityManager;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
Expand All @@ -29,30 +31,41 @@ private void handleLogin(ActionEvent event) {
return;
}

try(EntityManager em = JPAutil.getEntityManager()) {
try (EntityManager em = JPAutil.getEntityManager()) {

// Verifica o usuário e senha no banco de dados
Long count = em.createQuery("SELECT COUNT(a) FROM Administrador a WHERE a.nomeUsuario = :username", Long.class)
// Verifica se o usuário é Administrador
Usuario usuario = em.createQuery(
"SELECT u FROM Administrador u WHERE u.nomeUsuario = :username", Usuario.class)
.setParameter("username", username)
.getSingleResult();
.getResultStream()
.findFirst()
.orElse(null);

if (count == 0) {
// Se não for Administrador, verifica se é Secretário
if (usuario == null) {
usuario = em.createQuery(
"SELECT u FROM Secretario u WHERE u.nomeUsuario = :username", Usuario.class)
.setParameter("username", username)
.getResultStream()
.findFirst()
.orElse(null);
}

if (usuario == null) {
showAlert("Erro", "Usuário não encontrado.");
return;
}

// Recupera o administrador para verificar a senha
String storedPasswordHash = em.createQuery("SELECT a.senha FROM Administrador a WHERE a.nomeUsuario = :username", String.class)
.setParameter("username", username)
.getSingleResult();

if (!PasswordUtils.checkPassword(password, storedPasswordHash)) {
// Verifica a senha
if (!PasswordUtils.checkPassword(password, usuario.getSenha())) {
showAlert("Erro", "Senha incorreta.");
return;
}

// Login bem-sucedido, troca para a tela do menu
// Login bem-sucedido - Direciona para o menu principal
SessionManager.setCurrentUser(usuario);
NavigationManager.switchScene(event, "/fxml/gerenciamento/menu.fxml", "Pet Shop Mundo Animal");

} catch (Exception e) {
showAlert("Erro", "Ocorreu um erro ao verificar as credenciais: " + e.getMessage());
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.carvalhotechsolutions.mundoanimal.controllers;
package com.carvalhotechsolutions.mundoanimal.controllers.login;

import com.carvalhotechsolutions.mundoanimal.utils.NavigationManager;
import javafx.event.ActionEvent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.carvalhotechsolutions.mundoanimal.controllers;
package com.carvalhotechsolutions.mundoanimal.controllers.login;

import com.carvalhotechsolutions.mundoanimal.utils.NavigationManager;
import javafx.event.ActionEvent;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.carvalhotechsolutions.mundoanimal.utils;

import com.carvalhotechsolutions.mundoanimal.model.Usuario;

public class SessionManager {

private static Usuario currentUser;

public static Usuario getCurrentUser() {
return currentUser;
}

public static void setCurrentUser(Usuario user) {
currentUser = user;
}

}
2 changes: 2 additions & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@
org.hibernate.orm.core,
jakarta.persistence;
exports com.carvalhotechsolutions.mundoanimal.model;
exports com.carvalhotechsolutions.mundoanimal.controllers.login;
opens com.carvalhotechsolutions.mundoanimal.controllers.login to javafx.fxml;
}
4 changes: 2 additions & 2 deletions src/main/resources/META-INF/persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
<class>com.carvalhotechsolutions.mundoanimal.model.Servico</class>
<properties>
<!-- Altere os valores de acordo com as configurações do seu postgres -->
<property name="jakarta.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/mundoanimal" />
<property name="jakarta.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/mundoanimalteste" />
<property name="jakarta.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="jakarta.persistence.jdbc.user" value="postgres" />
<property name="jakarta.persistence.jdbc.password" value="password" />
<property name="jakarta.persistence.jdbc.password" value="12345678" />

<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
Expand Down
Binary file added src/main/resources/assets/images/sad-dog.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/main/resources/fxml/autenticacao/login.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>

<AnchorPane styleClass="default_bg_color" prefHeight="1080.0" prefWidth="1920.0" stylesheets="@../../css/style.css" xmlns="http://javafx.com/javafx/21.0.0" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.carvalhotechsolutions.mundoanimal.controllers.LoginController">
<AnchorPane styleClass="default_bg_color" prefHeight="1080.0" prefWidth="1920.0" stylesheets="@../../css/style.css" xmlns="http://javafx.com/javafx/21.0.0" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.carvalhotechsolutions.mundoanimal.controllers.login.LoginController">
<children>
<VBox styleClass="auth_container, white_bg_color" alignment="CENTER" focusTraversable="true" layoutX="749.0" layoutY="259.0" nodeOrientation="LEFT_TO_RIGHT" prefHeight="563.0" prefWidth="422.0" scaleShape="false" spacing="29.0">
<children>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/fxml/autenticacao/recuperar-senha.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>

<AnchorPane styleClass="default_bg_color" prefHeight="1080.0" prefWidth="1920.0" stylesheets="@../../css/style.css" xmlns="http://javafx.com/javafx/21.0.0" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.carvalhotechsolutions.mundoanimal.controllers.RecuperarSenhaController">
<AnchorPane styleClass="default_bg_color" prefHeight="1080.0" prefWidth="1920.0" stylesheets="@../../css/style.css" xmlns="http://javafx.com/javafx/21.0.0" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.carvalhotechsolutions.mundoanimal.controllers.login.RecuperarSenhaController">
<children>
<VBox styleClass="auth_container, white_bg_color" alignment="CENTER" focusTraversable="true" layoutX="749.0" layoutY="259.0" nodeOrientation="LEFT_TO_RIGHT" prefHeight="563.0" prefWidth="422.0" scaleShape="false" spacing="29.0">
<children>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/fxml/autenticacao/redefinir-senha.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>

<AnchorPane styleClass="default_bg_color" prefHeight="1080.0" prefWidth="1920.0" stylesheets="@../../css/style.css" xmlns="http://javafx.com/javafx/23.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.carvalhotechsolutions.mundoanimal.controllers.RedefinirSenhaController">
<AnchorPane styleClass="default_bg_color" prefHeight="1080.0" prefWidth="1920.0" stylesheets="@../../css/style.css" xmlns="http://javafx.com/javafx/23.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.carvalhotechsolutions.mundoanimal.controllers.login.RedefinirSenhaController">
<children>
<VBox styleClass="auth_container, white_bg_color" alignment="CENTER" focusTraversable="true" layoutX="749.0" layoutY="259.0" nodeOrientation="LEFT_TO_RIGHT" prefHeight="563.0" prefWidth="422.0" scaleShape="false" spacing="29.0">
<children>
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/fxml/gerenciamento/menu.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
</ImageView>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="51.0" prefWidth="116.0" spacing="4.0">
<children>
<Label id="top_pane_user_name" text="Fulano">
<Label id="top_pane_user_name" fx:id="userNameLabel" text="Fulano">
<font>
<Font name="System Bold" size="24.0" />
</font>
</Label>
<Label id="top_pane_user_role" text="Administrador">
<Label id="top_pane_user_role" fx:id="userTypeLabel" text="Administrador">
<font>
<Font size="16.0" />
</font>
Expand Down Expand Up @@ -186,7 +186,7 @@
<children>
<Button fx:id="sair_btn" mnemonicParsing="false" prefHeight="70.0" prefWidth="344.0" styleClass="menu_btn_red">
<graphic>
<HBox prefHeight="100.0" prefWidth="200.0" spacing="25.0">
<HBox alignment="BOTTOM_LEFT" prefHeight="100.0" prefWidth="200.0" spacing="25.0">
<padding>
<Insets bottom="19.0" left="37.0" right="37.0" top="19.0" />
</padding>
Expand Down