/*
* PanelUserRightsEdit.java
*
* Created on 4 Март 2008 г., 15:19
*/
package clips.administrator.right;
import beans.UserRightsSet;
import cli_fmw.delegate.DelegateLine2;
import cli_fmw.delegate.directory.complex.DirectoryLocator;
import cli_fmw.main.ClipsException;
import cli_fmw.main.ContentStateListener;
import cli_fmw.main.DirectoryItemNotFoundException;
import cli_fmw.main.PageContainer;
import cli_fmw.main.PageGeneric;
import cli_fmw.main.Persistentable;
import cli_fmw.main.Printable;
import cli_fmw.report.FormReportCreator;
import cli_fmw.report.ReporterFactory;
import cli_fmw.rights_m.TableCellRendererUserRightsM;
import cli_fmw.rights_m.TableModelUserRightsM;
import cli_fmw.utils.ErrorModeListener;
import cli_fmw.utils.MessageBox;
import cli_fmw.utils.SelectorEditable;
import cli_fmw.utils.table_config_states.StateSaver;
import cli_fmw.utils.table_config_states.TableState;
import clips.delegate.client.CollaboratorFunctionsChunk;
import clips.delegate.client.CollaboratorLocal;
import clips.delegate.directory.ro.DirectoryCollaborator;
import clips.delegate.directory.ro.DirectoryCollaboratorItem;
import framework.security.UserRight;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;
import javax.swing.DefaultListModel;
import framework.security.UserRightsGroup;
import java.util.HashSet;
/**
*
* @author petr
*/
public class PanelUserRightsEdit extends PageGeneric
implements Persistentable, Printable {
private CollaboratorLocal collabLoc;
private boolean errorMode;
private UserRightsSet rightsSet;
private DirectoryCollaborator dirCollab;
/** Creates new form PanelUserRightsEdit
* @param container
* @throws ClipsException
*/
public PanelUserRightsEdit(PageContainer container) throws ClipsException {
super(container);
dirCollab = DirectoryLocator.getDirectory(DirectoryCollaborator.class);
rightsSet = new UserRightsSet();
errorMode = false;
initComponents();
Enumeration<UserRightsGroup> rightsGroups = rightsSet.rightsGroups();
while (rightsGroups.hasMoreElements()) {
UserRightsGroup urg = rightsGroups.nextElement();
System.out.println("Group: " + urg);
Set<UserRight> rights = urg.getRights();
for (UserRight userRight : rights) {
System.out.print(" right: " + userRight + " mask: " + userRight.getRightsMask());
Set<UserRight> includes = new HashSet<UserRight>();
for (UserRight ur : rights) {
if (!ur.equals(userRight) && UserRight.include(userRight.getRightsMask(), ur.getRightsMask())) {
includes.add(ur);
}
}
if (includes.isEmpty()){
System.out.println();
}else{
System.out.println(" include:");
for (UserRight ur : includes) {
System.out.println(" - " + ur + " mask: " + ur.getRightsMask());
}
}
}
}
panelCollaboratorSelect1.setSelectedCollaborator(null);
panelCollaboratorSelect1.addContentStateListener(new ContentStateListener() {
@Override
public void contentStateChanged() {
setErrorMode(false);
DirectoryCollaboratorItem selectedCollaborator = panelCollaboratorSelect1.getSelectedItem();
if (selectedCollaborator == null) {
return;
}
if (collabLoc != null && collabLoc.isDirty() && selectedCollaborator.getID() != collabLoc.getID()) {
MessageBox.showWarning(MessageBox.W_NOT_CLOSE_EDITING);
try {
panelCollaboratorSelect1.setSelectedCollaborator(dirCollab.getItemFromID(collabLoc.getID()));
} catch (DirectoryItemNotFoundException ex) {
MessageBox.showException(ex);
}
} else {
if (collabLoc != null && selectedCollaborator.getID() == collabLoc.getID()) {
return;
}
try {
spec.setText("");
collabLoc = new CollaboratorLocal(selectedCollaborator.getID(), getAuditManager());
collabLoc.getDetails();//костыль, иначе детали не прогрузятся
collabLoc.addContentStateListener(getContainer());
DefaultListModel model = new DefaultListModel();
Enumeration<UserRightsGroup> enu = rightsSet.rightsGroups();
while(enu.hasMoreElements()) {
UserRightsGroup gr = enu.nextElement();
model.addElement(gr);
}
rightsGroup.setModel(model);
resetTable();
fireContainerEventStateChanged();
spec.setText(getExtraText(collabLoc));
} catch (ClipsException ex) {
setErrorMode(true);
MessageBox.showException(ex);
}
}
}
});
tblUserRights.setDefaultRenderer(Boolean.class, new TableCellRendererUserRightsM());
resetTable();
resizeColumn();
StateSaver.attachTo(this);
}
/**
* Обновление таблицы
* @param cl
*/
private void resetTable() throws ClipsException {
UserRightsGroup gr = (UserRightsGroup) rightsGroup.getSelectedValue();
if(gr == null) {
tblUserRights.setModel(new TableModelUserRightsM());
} else {
tblUserRights.setModel(new TableModelUserRightsM(gr, collabLoc));
}
}
private void setErrorMode(boolean mode) {
errorMode = mode;
tblUserRights.setEnabled(!mode);
if (mode) {
tblUserRights.addMouseListener(ErrorModeListener.errorModeListener);
} else {
tblUserRights.removeMouseListener(ErrorModeListener.errorModeListener);
}
}
/**
* Изменяет ширину столбцов
*/
private void resizeColumn() {
TableState state = new TableState();
state.setPreferredWidth(TableModelUserRightsM.COLUMN_RIGHTS, 19);
state.setPreferredWidth(TableModelUserRightsM.COLUMN_CHECKS, 1);
StateSaver.setDefaultState(this, tblUserRights, state);
}
/** 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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jSplitPane1 = new javax.swing.JSplitPane();
jScrollPane2 = new javax.swing.JScrollPane();
rightsGroup = new javax.swing.JList();
jScrollPane1 = new javax.swing.JScrollPane();
tblUserRights = new cli_fmw.utils.sorted_table.SortedTable();
jPanel3 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
panelCollaboratorSelect1 = new clips.accountant.PanelCollaboratorSelect();
spec = new javax.swing.JLabel();
setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
setLayout(new java.awt.BorderLayout(0, 10));
jPanel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
jPanel1.setLayout(new java.awt.BorderLayout(10, 0));
jScrollPane2.setPreferredSize(new java.awt.Dimension(250, 154));
rightsGroup.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
rightsGroupValueChanged(evt);
}
});
jScrollPane2.setViewportView(rightsGroup);
jSplitPane1.setLeftComponent(jScrollPane2);
jScrollPane1.addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentResized(java.awt.event.ComponentEvent evt) {
jScrollPane1ComponentResized(evt);
}
});
jScrollPane1.setViewportView(tblUserRights);
jSplitPane1.setRightComponent(jScrollPane1);
jPanel1.add(jSplitPane1, java.awt.BorderLayout.CENTER);
add(jPanel1, java.awt.BorderLayout.CENTER);
jPanel3.setLayout(new java.awt.BorderLayout());
jPanel2.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(panelCollaboratorSelect1, javax.swing.GroupLayout.PREFERRED_SIZE, 362, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(spec, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(panelCollaboratorSelect1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(spec, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
);
jPanel3.add(jPanel2, java.awt.BorderLayout.CENTER);
add(jPanel3, java.awt.BorderLayout.PAGE_START);
}// </editor-fold>//GEN-END:initComponents
private void rightsGroupValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_rightsGroupValueChanged
try {
resetTable();
} catch (ClipsException ex) {
setErrorMode(true);
MessageBox.showException(ex);
}
}/*
resetTable();//GEN-LAST:event_rightsGroupValueChanged
*/
private void jScrollPane1ComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jScrollPane1ComponentResized
}//GEN-LAST:event_jScrollPane1ComponentResized
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JSplitPane jSplitPane1;
private clips.accountant.PanelCollaboratorSelect panelCollaboratorSelect1;
private javax.swing.JList rightsGroup;
private javax.swing.JLabel spec;
private cli_fmw.utils.sorted_table.SortedTable tblUserRights;
// End of variables declaration//GEN-END:variables
private String getExtraText(CollaboratorLocal coll) throws ClipsException {
SelectorEditable<CollaboratorFunctionsChunk> it = coll.getFunctions();
String s = "";
for (int i = 0; i < it.size(); i++) {
CollaboratorFunctionsChunk cfc = it.get(i);
s = s + cfc.getSpeciality().toString();
if (i != (it.size() - 1)) {
s = s + ", ";
}
}
return s;
}
@Override
public String getPageTitle() {
return "Права пользователей";
}
// Persistentable
@Override
public boolean isDirty() {
if (errorMode) {
return false;
}
if (collabLoc == null) {
return false;
}
return collabLoc.isDirty();
}
@Override
public void save() throws ClipsException {
collabLoc.save1();
}
@Override
public void restore() {
try {
collabLoc.restore();
resetTable();
} catch (ClipsException ex) {
setErrorMode(true);
MessageBox.showException(ex);
}
}
@Override
public boolean readyForPrint() {
return !(isDirty() || collabLoc == null);
}
@Override
public void print() {
if (readyForPrint()) {
try {
FormReportCreator creater = ReporterFactory.createFormReporter(getClass());
String title = "Список прав, которыми обладает сотрудник: " + collabLoc.toString();
String extra = getExtraText(collabLoc);
if(extra.length() > 0) {
title += "(" + extra + ")";
}
HashMap<String, Object> data = new LinkedHashMap<String, Object>();
String rights = "";
Iterator<UserRight> rr = collabLoc.getRights().iterator();
while (rr.hasNext()) {
rights += " - " + rr.next().toString() + "\n";
}
if (rights.isEmpty()) {
rights += "Сотрудник не имеет прав";
}
data.put("rights", rights);
creater.createReport(title, null, data.entrySet(), null);
creater.finish();
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}
}
@Override
public DelegateLine2 getDelegate() {
return collabLoc;
}
}