// Chromis POS - The New Face of Open Source POS
// Copyright (c) (c) 2015-2016
// http://www.chromis.co.uk
//
// This file is part of Chromis POS
//
// Chromis POS is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Chromis POS is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Chromis POS. If not, see <http://www.gnu.org/licenses/>.
package uk.chromis.pos.repair;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import uk.chromis.pos.forms.AppConfig;
import uk.chromis.pos.forms.DriverWrapper;
import uk.chromis.pos.util.AltEncrypter;
public class ResetDialog extends javax.swing.JPanel {
private String db_user;
private String db_url;
private String db_password;
private ResultSet rs;
private PreparedStatement stmt2;
private String SQL;
private String SQL2;
private final Connection connection;
public ResetDialog() {
initComponents();
connection = getConnection();
if (connection == null) {
System.out.println("no connection");
System.exit(0);
}
jResource.addItem(null);
populate();
}
private void populate() {
try {
// get list of resources in database that match resource in templates
Statement stmt = connection.createStatement();
rs = stmt.executeQuery("SELECT * FROM RESOURCES ORDER BY NAME");
while (rs.next()) {
if (checkResource(rs.getString("NAME"))) {
jResource.addItem(rs.getString("NAME"));
}
}
} catch (SQLException ex) {
Logger.getLogger(ResetDialog.class.getName()).log(Level.SEVERE, null, ex);
}
}
private String getFullResourceName(String resource) {
try {
InputStream i = getClass().getClassLoader().getResourceAsStream("uk/chromis/pos/templates/" + resource + ".txt");
BufferedReader r = new BufferedReader(new InputStreamReader(i));
r.close();
i.close();
return "uk/chromis/pos/templates/" + resource + ".txt";
} catch (Exception ex) {
try {
InputStream i = getClass().getClassLoader().getResourceAsStream("uk/chromis/pos/templates/" + resource + ".xml");
BufferedReader r = new BufferedReader(new InputStreamReader(i));
r.close();
i.close();
return "uk/chromis/pos/templates/" + resource + ".xml";
} catch (Exception e) {
return "";
}
}
}
private Boolean checkResource(String resource) {
try {
InputStream i = getClass().getClassLoader().getResourceAsStream("uk/chromis/pos/templates/" + resource + ".txt");
BufferedReader r = new BufferedReader(new InputStreamReader(i));
r.close();
i.close();
return true;
} catch (Exception ex) {
try {
InputStream i = getClass().getClassLoader().getResourceAsStream("uk/chromis/pos/templates/" + resource + ".xml");
BufferedReader r = new BufferedReader(new InputStreamReader(i));
r.close();
i.close();
return true;
} catch (Exception e) {
return false;
}
}
}
/**
* 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">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jPanel1 = new javax.swing.JPanel();
jEmployeeRole = new javax.swing.JCheckBox();
jAdminRole = new javax.swing.JCheckBox();
jManagerRole = new javax.swing.JCheckBox();
jGuestRole = new javax.swing.JCheckBox();
jPanel3 = new javax.swing.JPanel();
jPanel4 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jResource = new javax.swing.JComboBox<>();
jButton1 = new javax.swing.JButton();
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/uk/chromis/fixedimages/chromislogomedi.png"))); // NOI18N
jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
jEmployeeRole.setText("Employee role");
jAdminRole.setText("Administrator role");
jManagerRole.setText("Manager role");
jGuestRole.setText("Guest role");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jAdminRole, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jManagerRole, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jEmployeeRole, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jGuestRole, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jAdminRole)
.addComponent(jEmployeeRole))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jManagerRole)
.addComponent(jGuestRole))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 206, Short.MAX_VALUE)
);
jLabel2.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("pos_messages"); // NOI18N
jLabel2.setText(bundle.getString("label.selectresource")); // NOI18N
jResource.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
jButton1.setText(bundle.getString("button.restoreresource")); // NOI18N
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
jPanel4.setLayout(jPanel4Layout);
jPanel4Layout.setHorizontalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1)
.addContainerGap())
.addComponent(jResource, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel4Layout.createSequentialGroup()
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
jPanel4Layout.setVerticalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addGap(6, 6, 6)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jResource, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(1, 1, 1)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(7, 7, 7)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(1, 1, 1))
.addGroup(layout.createSequentialGroup()
.addGap(34, 34, 34)
.addComponent(jLabel1)
.addContainerGap(62, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(1, 1, 1))
);
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
Boolean processed = false;
if (jAdminRole.isSelected()) {
createResourceString("uk/chromis/pos/templates/Role.Administrator.xml", "UPDATE ROLES SET PERMISSIONS = ? WHERE ID = '0'", "UPDATE ROLES SET RIGHTSLEVEL = 9 WHERE ID = '0'");
jAdminRole.setSelected(false);
processed = true;
}
if (jManagerRole.isSelected()) {
createResourceString("uk/chromis/pos/templates/Role.Manager.xml", "UPDATE ROLES SET PERMISSIONS = ? WHERE ID = '1'", "UPDATE ROLES SET RIGHTSLEVEL = 6 WHERE ID = '1'");
jManagerRole.setSelected(false);
processed = true;
}
if (jEmployeeRole.isSelected()) {
createResourceString("uk/chromis/pos/templates/Role.Employee.xml", "UPDATE ROLES SET PERMISSIONS = ? WHERE ID = '2'", "UPDATE ROLES SET RIGHTSLEVEL = 5 WHERE ID = '2'");
jEmployeeRole.setSelected(false);
processed = true;
}
if (jGuestRole.isSelected()) {
createResourceString("uk/chromis/pos/templates/Role.Guest.xml", "UPDATE ROLES SET PERMISSIONS = ? WHERE ID = '3'", "UPDATE ROLES SET RIGHTSLEVEL = 5 WHERE ID = '3'");
jGuestRole.setSelected(false);
processed = true;
}
if (jResource.getSelectedItem()
!= null) {
createResourceString(getFullResourceName(jResource.getSelectedItem().toString()), "UPDATE RESOURCES SET CONTENT = ? WHERE NAME = '" + jResource.getSelectedItem().toString() + "'", null);
processed = true;
}
if (processed) {
JOptionPane.showMessageDialog(this, "Restore complete.");
}
processed = false;
}//GEN-LAST:event_jButton1ActionPerformed
private void createResourceString(String resource, String sql1, String sql2) {
StringBuilder val = new StringBuilder();
try {
InputStream i = getClass().getClassLoader().getResourceAsStream(resource);
BufferedReader r = new BufferedReader(new InputStreamReader(i));
String l;
while ((l = r.readLine()) != null) {
val.append(l).append("\n");
}
r.close();
i.close();
stmt2 = connection.prepareStatement(sql1);
stmt2.setBytes(1, val.toString().getBytes());
stmt2.executeUpdate();
stmt2.close();
if (sql2 != null) {
stmt2 = connection.prepareStatement(sql2);
stmt2.executeUpdate();
stmt2.close();
}
} catch (IOException | SQLException ex) {
Logger.getLogger(ResetDialog.class
.getName()).log(Level.SEVERE, null, ex);
}
}
private Connection getConnection() {
try {
db_user = (AppConfig.getInstance().getProperty("db.user"));
db_url = (AppConfig.getInstance().getProperty("db.URL"));
db_password = (AppConfig.getInstance().getProperty("db.password"));
if (db_user != null && db_password != null && db_password.startsWith("crypt:")) {
AltEncrypter cypher = new AltEncrypter("cypherkey" + db_user);
db_password = cypher.decrypt(db_password.substring(6));
}
ClassLoader cloader = new URLClassLoader(new URL[]{new File(AppConfig.getInstance().getProperty("db.driverlib")).toURI().toURL()});
DriverManager.registerDriver(new DriverWrapper((Driver) Class.forName(AppConfig.getInstance().getProperty("db.driver"), true, cloader).newInstance()));
Class.forName(AppConfig.getInstance().getProperty("db.driver"));
Connection con = DriverManager.getConnection(db_url, db_user, db_password);
con.setAutoCommit(true);
return con;
} catch (MalformedURLException | SQLException | ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
Logger.getLogger(ResetDialog.class
.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JCheckBox jAdminRole;
private javax.swing.JButton jButton1;
private javax.swing.JCheckBox jEmployeeRole;
private javax.swing.JCheckBox jGuestRole;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JCheckBox jManagerRole;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JComboBox<String> jResource;
// End of variables declaration//GEN-END:variables
}