forked from Coded/SIPRP
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
207 lines
5.2 KiB
207 lines
5.2 KiB
package siprp.higiene.gestao.legislacao;
|
|
|
|
import static com.evolute.utils.strings.UnicodeLatin1Map.atilde;
|
|
import static com.evolute.utils.strings.UnicodeLatin1Map.ccedil;
|
|
|
|
import javax.swing.tree.DefaultMutableTreeNode;
|
|
import javax.swing.tree.TreePath;
|
|
|
|
import leaf.ui.LeafError;
|
|
import leaf.ui.TreeInserterDialog;
|
|
import leaf.ui.TreeTools;
|
|
import siprp.database.cayenne.objects.Empresas;
|
|
import siprp.database.cayenne.objects.Estabelecimentos;
|
|
import siprp.database.cayenne.objects.HsLegislacao;
|
|
import siprp.database.cayenne.objects.HsLegislacaoEmpresa;
|
|
import siprp.database.cayenne.objects.HsLegislacaoEstabelecimento;
|
|
import siprp.higiene.gestao.AdicionarPanel;
|
|
import siprp.logic.HigieneSegurancaLogic;
|
|
|
|
public class AdicionarLegislacaoPanel extends AdicionarPanel
|
|
{
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
private Empresas empresa = null;
|
|
|
|
private Estabelecimentos estabelecimento = null;
|
|
|
|
public void setEmpresa( Empresas empresa )
|
|
{
|
|
this.empresa = empresa;
|
|
this.estabelecimento = null;
|
|
refresh();
|
|
}
|
|
|
|
public void setEstabelecimento( Estabelecimentos estabelecimento )
|
|
{
|
|
this.empresa = null;
|
|
this.estabelecimento = estabelecimento;
|
|
refresh();
|
|
}
|
|
|
|
@Override
|
|
protected void add()
|
|
{
|
|
DefaultMutableTreeNode allLegislacao = getAllLegislacao();
|
|
removeCurrent( allLegislacao );
|
|
TreeInserterDialog dialog = new TreeInserterDialog( "Adicionar Legisla" + ccedil + atilde + "o", allLegislacao );
|
|
DefaultMutableTreeNode result = dialog.getResult();
|
|
if( result != null )
|
|
{
|
|
addResult( result );
|
|
refresh();
|
|
}
|
|
setEnabled();
|
|
}
|
|
|
|
private DefaultMutableTreeNode getAllLegislacao()
|
|
{
|
|
DefaultMutableTreeNode result = new DefaultMutableTreeNode();
|
|
if( empresa != null )
|
|
{
|
|
result = HigieneSegurancaLogic.getLegislacaoTreeForEmpresa( null );
|
|
}
|
|
else if( estabelecimento != null )
|
|
{
|
|
result = HigieneSegurancaLogic.getLegislacaoTreeForEmpresa( estabelecimento.getToEmpresas() );
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private void removeCurrent( DefaultMutableTreeNode all )
|
|
{
|
|
if( all != null && root.getChildCount() == 2 && all.getChildCount() == 2 )
|
|
{
|
|
DefaultMutableTreeNode geralAll = (DefaultMutableTreeNode) all.getChildAt( 0 );
|
|
DefaultMutableTreeNode especificaAll = (DefaultMutableTreeNode) all.getChildAt( 1 );
|
|
DefaultMutableTreeNode geralCurrent = (DefaultMutableTreeNode) root.getChildAt( 0 );
|
|
DefaultMutableTreeNode especificaCurrent = (DefaultMutableTreeNode) root.getChildAt( 1 );
|
|
TreeTools.remove( geralAll, geralCurrent );
|
|
TreeTools.removeAllLeafs( especificaAll, especificaCurrent );
|
|
}
|
|
}
|
|
|
|
private void addResult( DefaultMutableTreeNode root )
|
|
{
|
|
try
|
|
{
|
|
if( root != null )
|
|
{
|
|
Object userObject = root.getUserObject();
|
|
if( userObject instanceof HsLegislacao )
|
|
{
|
|
if( empresa != null )
|
|
{
|
|
HsLegislacaoEmpresa rel = new HsLegislacaoEmpresa();
|
|
rel.setToHsEmpresa( empresa );
|
|
rel.setToHsLegislacao( ((HsLegislacao) userObject ) );
|
|
rel.save();
|
|
}
|
|
else if( estabelecimento != null )
|
|
{
|
|
HsLegislacaoEstabelecimento rel = new HsLegislacaoEstabelecimento();
|
|
rel.setToHsEstabelecimento( estabelecimento );
|
|
rel.setToHsLegislacao( ((HsLegislacao) userObject ) );
|
|
rel.save();
|
|
}
|
|
}
|
|
}
|
|
for( int i = 0; i < root.getChildCount(); ++i )
|
|
{
|
|
DefaultMutableTreeNode child = (DefaultMutableTreeNode) root.getChildAt( i );
|
|
addResult( child );
|
|
}
|
|
}
|
|
catch( Exception e )
|
|
{
|
|
LeafError.error( e );
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void refresh()
|
|
{
|
|
root.removeAllChildren();
|
|
if( empresa != null )
|
|
{
|
|
TreeTools.merge( root, HigieneSegurancaLogic.getLegislacaoTreeForEmpresa( empresa ) );
|
|
}
|
|
else if( estabelecimento != null )
|
|
{
|
|
TreeTools.merge( root, HigieneSegurancaLogic.getLegislacaoTreeForEstabelecimento( estabelecimento ) );
|
|
}
|
|
setEnabled();
|
|
TreeTools.refreshTree( tree, root );
|
|
}
|
|
|
|
@Override
|
|
protected void setEnabled()
|
|
{
|
|
buttonAdicionar.setEnabled( empresa != null || estabelecimento != null );
|
|
buttonRemover.setEnabled( tree.getSelectionCount() > 0 );
|
|
}
|
|
|
|
@Override
|
|
protected void rem()
|
|
{
|
|
try
|
|
{
|
|
TreePath path = tree.getSelectionPath();
|
|
if( path != null )
|
|
{
|
|
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
|
|
Object obj = node.getUserObject();
|
|
if( obj != null && (obj instanceof HsLegislacao) )
|
|
{
|
|
if( empresa != null )
|
|
{
|
|
removeFromEmpresa( ((HsLegislacao) obj ) );
|
|
}
|
|
else if( estabelecimento != null )
|
|
{
|
|
removeFromEstabelecimento( ((HsLegislacao) obj ));
|
|
}
|
|
refresh();
|
|
}
|
|
}
|
|
}
|
|
catch( Exception e )
|
|
{
|
|
LeafError.error( e );
|
|
}
|
|
}
|
|
|
|
private void removeFromEmpresa( HsLegislacao legislacao ) throws Exception
|
|
{
|
|
if( legislacao != null && empresa != null )
|
|
{
|
|
for( HsLegislacaoEmpresa rel : empresa.getHsLegislacaoEmpresaArray() )
|
|
{
|
|
if( legislacao.equals( rel.getToHsLegislacao() ) )
|
|
{
|
|
rel.delete();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void removeFromEstabelecimento( HsLegislacao legislacao ) throws Exception
|
|
{
|
|
if( legislacao != null && estabelecimento != null )
|
|
{
|
|
for( HsLegislacaoEstabelecimento rel : estabelecimento.getHsLegislacaoEstabelecimentoArray() )
|
|
{
|
|
if( legislacao.equals( rel.getToHsLegislacao() ) )
|
|
{
|
|
rel.delete();
|
|
break;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
}
|