/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package edu.bellevue.its.migration.gui.frames.admin;
import edu.bellevue.its.migration.entities.Role;
import edu.bellevue.its.migration.entities.User;
import edu.bellevue.its.migration.global.AppGlobals;
import edu.bellevue.its.migration.gui.MainApp;
import edu.bellevue.its.migration.gui.frames.UpdateableFrame;
import java.util.Iterator;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
/**
*
* @author tj
*/
public class RoleManagementFrame extends javax.swing.JInternalFrame implements UpdateableFrame {
/**
* Creates new form RoleManagementFrame
*/
public RoleManagementFrame() {
initComponents();
initRoleList();
}
@Override
public void updateView()
{
initRoleList();
}
/**
* 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() {
jScrollPane1 = new javax.swing.JScrollPane();
listRoles = new javax.swing.JList();
btnCreateRole = new edu.bellevue.its.migration.gui.controls.RoleSecuredButton();
btnEditRole = new edu.bellevue.its.migration.gui.controls.RoleSecuredButton();
btnDeleteRole = new edu.bellevue.its.migration.gui.controls.RoleSecuredButton();
setClosable(true);
setIconifiable(true);
setResizable(true);
setTitle("Role Management Console");
addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
}
public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
formInternalFrameClosed(evt);
}
public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
}
public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
}
public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
}
public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
}
public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
}
});
listRoles.setModel(new DefaultListModel());
jScrollPane1.setViewportView(listRoles);
btnCreateRole.setText("Create Role");
btnCreateRole.setToolTipText("");
btnCreateRole.setRoleName("Admin");
btnCreateRole.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCreateRoleActionPerformed(evt);
}
});
btnEditRole.setText("Edit Role");
btnEditRole.setRoleName("Admin");
btnEditRole.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnEditRoleActionPerformed(evt);
}
});
btnDeleteRole.setText("Delete Role");
btnDeleteRole.setRoleName("Admin");
btnDeleteRole.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDeleteRoleActionPerformed(evt);
}
});
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 345, Short.MAX_VALUE)
.add(layout.createSequentialGroup()
.add(btnCreateRole, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 107, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(btnEditRole, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 110, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(btnDeleteRole, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 99, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(btnCreateRole, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(btnEditRole, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(btnDeleteRole, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void btnCreateRoleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateRoleActionPerformed
// TODO add your handling code here:
EditRoleFrame erf = new EditRoleFrame(this);
MainApp.getDesktop().add(erf);
int compCount = MainApp.getDesktop().getComponentCount();
erf.setLocation(30 * compCount, 30 * compCount);
erf.setVisible(true);
}//GEN-LAST:event_btnCreateRoleActionPerformed
private void btnEditRoleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditRoleActionPerformed
// TODO add your handling code here:
Role selectedRole = (Role)listRoles.getSelectedValue();
String roleName = selectedRole.getRoleName();
EditRoleFrame erf = new EditRoleFrame(roleName);
MainApp.getDesktop().add(erf);
int compCount = MainApp.getDesktop().getComponentCount();
erf.setLocation(30 * compCount, 30 * compCount);
erf.setVisible(true);
//eud.setVisible(true);
}//GEN-LAST:event_btnEditRoleActionPerformed
private void btnDeleteRoleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteRoleActionPerformed
// TODO add your handling code here:
try
{
Session s = AppGlobals.getInstance().hibernateSession;
s.beginTransaction();
Object o = listRoles.getSelectedValue();
s.delete(listRoles.getSelectedValue());
s.getTransaction().commit();
JOptionPane.showMessageDialog(this,"Role Deleted");
initRoleList();
} catch (Exception e)
{
StringBuilder users = new StringBuilder();
User[] userList = ((Role)listRoles.getSelectedValue()).getUsers().toArray(new User[0]);
for (User u : userList)
{
users.append(u.getUserName()).append("\n");
}
JOptionPane.showMessageDialog(this,"Unable to delete role, some users still have it assigned: \n" + userList.toString());
}
}//GEN-LAST:event_btnDeleteRoleActionPerformed
private void formInternalFrameClosed(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosed
// TODO add your handling code here:
MainApp.getDesktop().remove(evt.getInternalFrame());
}//GEN-LAST:event_formInternalFrameClosed
private void initRoleList()
{
DefaultListModel m = (DefaultListModel)listRoles.getModel();
m.clear();
Session s = AppGlobals.getInstance().hibernateSession;
List l = s.createCriteria(Role.class).addOrder(Order.asc("roleName")).list();
Iterator i = l.iterator();
while (i.hasNext())
{
Role u = (Role)i.next();
Hibernate.initialize(u.getUsers());
m.addElement(u);
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private edu.bellevue.its.migration.gui.controls.RoleSecuredButton btnCreateRole;
private edu.bellevue.its.migration.gui.controls.RoleSecuredButton btnDeleteRole;
private edu.bellevue.its.migration.gui.controls.RoleSecuredButton btnEditRole;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JList listRoles;
// End of variables declaration//GEN-END:variables
}