Skip to content

Commit

Permalink
Added Manual Report Generation Functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
iamKunal committed Apr 11, 2017
1 parent 78bcf5e commit e879d96
Show file tree
Hide file tree
Showing 5 changed files with 275 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/visMan/CreateConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package visMan;
import java.sql.*;
public class CreateConnection {

public static Connection conn;
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/";
static final String USER = "root";
static final String PASS = "toor";
static final String timeConvert = "?zeroDateTimeBehavior=convertToNull";
CreateConnection() {
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL+"visitormanagement" +timeConvert, USER, PASS);

} catch (Exception e) {
try{
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL+timeConvert, USER, PASS);
}
catch (Exception e1) {
// TODO: handle exception
}
}
}
}
11 changes: 10 additions & 1 deletion src/visMan/Main.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,16 @@
<RadioButton id="mainRadioButton" mnemonicParsing="false" text="Checkout" toggleGroup="$check" />
</children>
</VBox>
<Button fx:id="goButton" disable="true" mnemonicParsing="false" onAction="#submitGo" prefHeight="50.0" prefWidth="100.0" text="Go" />
<HBox alignment="CENTER" spacing="10.0">
<children>
<Button fx:id="goButton" disable="true" mnemonicParsing="false" onAction="#submitGo" prefHeight="50.0" prefWidth="100.0" text="Go" />
<Button mnemonicParsing="false" onAction="#manualReportScene" prefHeight="50.0" prefWidth="100.0" text="Manual Report" wrapText="true">
<font>
<Font name="Arial" size="12.0" />
</font>
</Button>
</children>
</HBox>
</children>
</VBox>
</children>
Expand Down
34 changes: 34 additions & 0 deletions src/visMan/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,40 @@ public void deleteTempData(){
@FXML ToggleGroup gateNo;
@FXML VBox mainRoot;
@FXML Button goButton;

@FXML
void manualReportScene() {
try{
FXMLLoader loader = new FXMLLoader(getClass().getResource("ManualReport.fxml"));
Parent manualReportRoot = (Parent) loader.load();
Scene manualReport = new Scene(manualReportRoot);
manualReport.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
// NewUserController controller = (NewUserController) loader.getController();
// control.initData(selectedSong);
Stage stager = new Stage();
stager.setScene(manualReport);
// stager.initStyle(StageStyle.UNDECORATED);
stager.setResizable(false);
stager.initModality(Modality.WINDOW_MODAL);
stager.initOwner(goButton.getScene().getWindow());
// stager.setOnCloseRequest((new EventHandler<WindowEvent>() {
// public void handle(WindowEvent we)
// {
// mainRoot.setDisable(false);
// if(NewUserController.openStage!=null){
// NewUserController.openStage.close();
// NewUserController.openStage=null;
// }
// }
// }));
stager.setTitle("Manual Report Generation");
stager.showAndWait();
}
catch (Exception e)
{
e.printStackTrace();
}
}
@FXML
void submitGo(){
if(Utils.getToggleText(check).equals("Checkin")){
Expand Down
55 changes: 55 additions & 0 deletions src/visMan/ManualReport.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="visMan.ManualReportController">
<children>
<BorderPane layoutX="-198.0" layoutY="-139.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<top>
<Label styleClass="heading" text="Manual Report Generation" underline="true" BorderPane.alignment="CENTER">
<font>
<Font name="System Italic" size="34.0" />
</font>
<padding>
<Insets bottom="20.0" top="20.0" />
</padding>
</Label>
</top>
<center>
<VBox alignment="CENTER" BorderPane.alignment="CENTER">
<children>
<Label text="Select Date Below :">
<font>
<Font name="Arial Bold" size="24.0" />
</font>
<VBox.margin>
<Insets bottom="20.0" />
</VBox.margin>
</Label>
<DatePicker fx:id="datePicker" promptText="01/01/2017" />
<Label fx:id="numberOfReports" text="Label" />
</children>
</VBox>
</center>
<bottom>
<ButtonBar prefHeight="40.0" prefWidth="200.0" BorderPane.alignment="CENTER_RIGHT">
<buttons>
<Button fx:id="gerenateButton" mnemonicParsing="false" onAction="#generateReport" text="Generate" />
<Button fx:id="cancelButton" mnemonicParsing="false" onAction="#cancelExecute" text="Cancel" />
</buttons>
<BorderPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</BorderPane.margin>
</ButtonBar>
</bottom>
</BorderPane>
</children>
</AnchorPane>
150 changes: 150 additions & 0 deletions src/visMan/ManualReportController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package visMan;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;

import java.io.File;
import java.io.PrintWriter;
import java.net.URL;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.ResourceBundle;


import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class ManualReportController extends CreateConnection implements Initializable {
private final static String FOLDERNAME="reportlogs";
private static DateTimeFormatter dtf=DateTimeFormatter.ofPattern("HH:mm:ss dd-MM-YYYY");;
private final static String header[]={"S.No.","VisitorID","Name","Category","Location","Purpose of Visit","In Time","Out Time","Pass Validity","Gate Number","Gender","Contact","Date of Birth","Address",};
@FXML
private DatePicker datePicker;
@FXML
private Button gerenateButton;
@FXML
private Button cancelButton;
private PreparedStatement prepare=null;
@FXML
private Label numberOfReports;
// Event Listener on Button[#gerenateButton].onAction
@FXML
public void generateReport() {
PreparedStatement statement = null;
String sql;
try {

sql = "SELECT report.uid,report.name,category,location,purposeOFVisit,inTimeStamp,outTimeStamp,validUpto,gateNo,gender,contact,dateOfBirth,address "+
"FROM userinfo RIGHT JOIN report ON userinfo.uid=report.uid WHERE DATE(inTimeStamp)=? OR DATE(outTimeStamp)=? ORDER BY inTimeStamp ASC;";

statement = conn.prepareStatement(sql);
statement.setDate(1, Date.valueOf(datePicker.getValue()));
statement.setDate(2, Date.valueOf(datePicker.getValue()));
ResultSet res=statement.executeQuery();
LocalDate today = datePicker.getValue();
String subFolder = String.format("%02d",today.getMonthValue()) + "-"+today.getYear();
// f.(rowString(header));
File f=new File(FOLDERNAME);
f.mkdir();
f=new File(FOLDERNAME + "/" + today.getYear() + "-" + String.format("%02d",today.getMonthValue()));
f.mkdir();
f = new File(FOLDERNAME + "/" + today.getYear() + "-" + String.format("%02d",today.getMonthValue()) + "/" + String.format("%02d",today.getDayOfMonth())+"-"+subFolder+".csv");
f.createNewFile();
PrintWriter p = new PrintWriter(f);
p.println(rowString(header));
ArrayList<String> row = new ArrayList<>();
int sNo=1;
while(res.next()){
row.add(Integer.toString(sNo));
row.add(res.getString("uid"));
row.add(res.getString("name"));
row.add(res.getString("category"));
row.add(res.getString("location"));
row.add(res.getString("purposeOfVisit"));
row.add(res.getTimestamp("inTimeStamp").toLocalDateTime().format(dtf));
try{
row.add(res.getTimestamp("outTimeStamp").toLocalDateTime().format(dtf));
}
catch (NullPointerException e) {
row.add("Not Checked Out");
}
row.add(res.getTimestamp("validUpto").toLocalDateTime().format(dtf));
row.add(res.getString("gateNo"));
row.add(res.getString("gender"));
row.add(res.getString("contact"));
row.add(res.getDate("dateOfBirth").toLocalDate().format(DateTimeFormatter.ofPattern("dd-MM-YYYY")));
row.add(res.getString("address"));
p.println(rowString(row.toArray(new String[row.size()])));
row.clear();
sNo++;
}
p.close();
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Report Generated");
alert.setHeaderText(null);
alert.setContentText("Report for " + datePicker.getValue()+ " has been successfully generated!");

alert.showAndWait();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {

}
}
}

}
// Event Listener on Button[#cancelButton].onAction
@FXML
public void cancelExecute() {
Stage currentStage = (Stage) cancelButton.getScene().getWindow();
currentStage.close();
}
private static String rowString(String [] toWrite){
ArrayList<String> mod= new ArrayList<>();
for(String s : toWrite){
s="\""+s+"\"";
mod.add(s);
}
String writeIt=String.join(",", mod);
return writeIt;
}
@Override
public void initialize(URL location, ResourceBundle resources) {
datePicker.valueProperty().addListener((o,oldValue,newValue) -> {
if(newValue!=null){
// DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate localDate = LocalDate.now().minusDays(1);
if(ChronoUnit.DAYS.between(newValue,localDate)<0 || ChronoUnit.YEARS.between(newValue,localDate)>150)
datePicker.setValue(localDate);
try{
prepare = conn.prepareStatement("SELECT count(*) FROM report WHERE DATE(inTimeStamp)=? OR DATE(outTimeStamp)=?");
prepare.setDate(1, Date.valueOf(datePicker.getValue()));
prepare.setDate(2, Date.valueOf(datePicker.getValue()));
ResultSet res = prepare.executeQuery();
while(res.next()){
numberOfReports.setText(res.getInt(1) + " entries for that day.");
}
}
catch (Exception e) {
// TODO: handle exception
}
}
});
datePicker.setValue(LocalDate.now().minusDays(1));
}
}

0 comments on commit e879d96

Please sign in to comment.