/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package gridmanualprocessor;
import javax.swing.*;
import java.io.*;
import java.util.HashSet;
import java.security.MessageDigest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* @author admin
*/
public class GridManualProcess extends javax.swing.JFrame {
/**
* Creates new form GridManualProcess
*/
public GridManualProcess() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
TxtFilePath = new javax.swing.JTextField();
BSelectFolder = new javax.swing.JButton();
BRunProgram = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
TAUniqueCount = new javax.swing.JTextArea();
jScrollPane2 = new javax.swing.JScrollPane();
TAShaReport = new javax.swing.JTextArea();
jLabel3 = new javax.swing.JLabel();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenu2 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
setTitle("Grid Manual Processor");
setBackground(java.awt.Color.red);
setResizable(false);
jLabel1.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
jLabel1.setText("File Path:");
TxtFilePath.setEditable(false);
BSelectFolder.setText("Select Folder");
BSelectFolder.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BSelectFolderActionPerformed(evt);
}
});
BRunProgram.setText("Run Program");
BRunProgram.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BRunProgramActionPerformed(evt);
}
});
jLabel2.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
jLabel2.setText("Download and Unique Count:");
TAUniqueCount.setEditable(false);
TAUniqueCount.setColumns(20);
TAUniqueCount.setRows(5);
jScrollPane1.setViewportView(TAUniqueCount);
TAShaReport.setEditable(false);
TAShaReport.setColumns(20);
TAShaReport.setRows(5);
jScrollPane2.setViewportView(TAShaReport);
jLabel3.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
jLabel3.setText("Sha1 Report:");
jMenu1.setText("File");
jMenuBar1.add(jMenu1);
jMenu2.setText("Edit");
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(20, 20, 20)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addComponent(TxtFilePath, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(BSelectFolder)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addComponent(BRunProgram))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jScrollPane2))))
.addContainerGap(23, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(TxtFilePath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(BSelectFolder)
.addComponent(BRunProgram))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
.addComponent(jScrollPane1))
.addContainerGap())
);
pack();
}// </editor-fold>
private void BSelectFolderActionPerformed(java.awt.event.Action Event evt) {
// TODO add your handling code here:
FCFolderPath = new JFileChooser();
FCFolderPath.setCurrentDirectory(new java.io.File("C:\\Harvest\\"));
FCFolderPath.setDialogTitle("Folder Path");
FCFolderPath.setFileSelectionMode(JFileChooser.DIR ECTORIES_ONLY);
int result = FCFolderPath.showDialog(this,"Select Folder");
if (result == JFileChooser.APPROVE_OPTION)
{
File file = FCFolderPath.getSelectedFile();
TAShaReport.setText(file.getAbsolutePath());
}
}
private void BRunProgramActionPerformed(java.awt.event.ActionEv ent evt) {
// TODO add your handling code here:
String filepaths = TxtFilePath.getText();
String PWord = "PWord";
String DBUrl = "DBUrl";
String UName = "UName";
String SiteInitial = "SiteInitial";
String OJTNumber = "OJTNumber";
if ("".equals(TxtFilePath.getText()))
{
JOptionPane.showMessageDialog(null, "PLEASE CHOOSE A FOLDER PATH","MESSAGE ALERT",JOptionPane.ERROR_MESSAGE);
}
else
{
try
{
DeduplicateFiles(filepaths);
//MyUniqueReporting();
System.out.println("\tProcess done.\n\n");
}
catch(Exception e)
{
}
}
}
public static byte[] createChecksum(String filename) throws Exception
{
MessageDigest complete;
try (InputStream fis = new FileInputStream(filename))
{
byte[] buffer = new byte[1024];
complete = MessageDigest.getInstance("MD5");
int numRead;
do
{
numRead = fis.read(buffer);
if (numRead > 0)
{
complete.update(buffer, 0, numRead);
}
}
while (numRead != -1);
}
return complete.digest();
}
public static String DeduplicateFiles(String myFolderLocation)
{
try
{
HashSet<String> newset = new HashSet<>();
File folder = new File(myFolderLocation); //Directory where the files are located
File[] listOfFiles = folder.listFiles();
for (File listOfFile : listOfFiles)
{
//Enumerate the files inside the folder
if (listOfFile.isFile())
{
String myMD5 = getMD5Checksum(listOfFile.getPath());
if (newset.contains(myMD5))
{
DuplicateCount++;
System.out.println(listOfFile.getName() + "," + myMD5 + ", DUPLICATE");
File myFilePath = new File(listOfFile.getPath());
myFilePath.delete();
}
else
{
UniqueCount++;
System.out.println(listOfFile.isDirectory() + "," + myMD5 + ", UNIQUE");
newset.add(myMD5);
}
}
else if (listOfFile.isDirectory())
{
System.out.println("Directory " + listOfFile.getName());
}
}
}
catch (Exception e)
{
}
return null;
}
public static String getMD5Checksum(String filename) throws Exception
{
byte[] b = createChecksum(filename);
String result = "";
for (int i = 0; i < b.length; i++)
{
result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
}
return result;
}
public static String MyUniqueReporting()
{
DateFormat NgayonAraw = new SimpleDateFormat("yyyy-MM-dd");
Date Araw = new Date();
PrintWriter MyUniqueReport;
try
{
MyUniqueReport = new PrintWriter(new FileOutputStream(NgayonAraw.format(Araw)+"-UniqueReport.txt",true));
int downloadCount = UniqueCount + DuplicateCount;
int yieldCount = downloadCount - (DuplicateCount + KnownInACL + totalProvenanceBreakdown);
MyUniqueReport.println("Folder Name: , Total Download Files: , Total Unique Files:");
MyUniqueReport.println(downloadCount+yieldCount);
MyUniqueReport.close();
}
catch (FileNotFoundException exc)
{
JOptionPane.showMessageDialog(null,"File not Found.");
}
return null;
}
private void fillGuiWithSha1Results(File f)
{
if (f.isDirectory())
{
//recurse?
}
else
{
try
{
// TAShaReport.setText(DeduplicateFiles(f)));
}
catch (Exception e)
{
}
}
}
private void TxtFilePathActionPerformed(java.awt.event.ActionEv ent evt)
{
//TxtFilePath.getText(Chooses); // TODO add your handling code here:
System.out.printf("TxtFilePathAcc");
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see
How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClass Name());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GridManualProce ss.class.getName()).log(java.util.logging.Level.SE VERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(() -> {
new GridManualProcess().setVisible(true);
});
}
public static Date date = new Date();
public static SimpleDateFormat TodayDate = new SimpleDateFormat("yyyy-MM-dd");
public static Integer UniqueCount = 0, DuplicateCount = 0, KnownInACL = 0, UnknownInACL = 0;
public static Integer totalProvenanceBreakdown = 0, total_provenance = 0;
public static String siteInitial = "";
private javax.swing.JFileChooser FCFolderPath;
// Variables declaration - do not modify
private javax.swing.JButton BRunProgram;
private javax.swing.JButton BSelectFolder;
private javax.swing.JTextArea TAShaReport;
private javax.swing.JTextArea TAUniqueCount;
private javax.swing.JTextField TxtFilePath;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
// End of variables declaration
}