package siprp.pagina;

import com.evolute.utils.arrays.*;
import com.evolute.utils.strings.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import java.lang.reflect.Array;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.velocity.*;
import org.apache.velocity.app.*;

public class doGetListaEmpresas extends siprpServlet{
    
    /** Creates a new instance of doGetListaEmpresas */
    public doGetListaEmpresas(HttpServletRequest req, HttpServletResponse res) throws IOException
    {
        ServletOutputStream out = res.getOutputStream();
        Connection con = null ;
        Statement stmt = null ;
        ResultSet2DArray rs;
        StringBuffer dbQuery;
        String userRole="", temp="";
        HttpSession session = req.getSession(false);
        Vector links = new Vector();
        Vector desc = new Vector();
String empresa_nome;
        try
        {    
                userRole = (String)session.getAttribute(super.sessionUserRole);
empresa_nome = ( String ) session.getAttribute( sessionCompanyName );

		if (userRole == null)
               {
                throw new IllegalStateException();
               }
               
               if ( userRole.equals ( super.superUserRole ) )
               {
empresa_nome = null;
                    Class.forName(super.bdDriver);
                    con = DriverManager.getConnection( bdLocalUrl, bdLocalUsername, bdLocalPassword );
                    dbQuery = new StringBuffer();
                    dbQuery.append( "SELECT id, designacao_social, designacao_social_plain FROM empresas WHERE inactivo <> 'y' ORDER BY designacao_social_plain" );
                    stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                    rs = new ResultSet2DArray( stmt.executeQuery( dbQuery.toString()) );
                    
                    int index=0; 
                    int max = rs.columnLength();

                    while ( index < max )
                    {
						String str = (String)rs.get(index,1);
						if( str != null && str.trim().length() > 0 )
						{
							temp=""+rs.get(index,0); // converter de int para String
							dbQuery = new StringBuffer();
							dbQuery.append("/"+super.servletName+"/?"+temp); // contruir url
							links.add(dbQuery.toString());
							desc.add((String)rs.get(index,1));
						}
						++index;
                    }
                    stmt.close();
                    con.close();


session.setAttribute( sessionEstabelecimentoId, null );

HashMap hmValues = new HashMap();
//hmValues.put( "empresa_nome", session.getAttribute( sessionCompanyName ) );
hmValues.put( "empresa_nome", empresa_nome );
hmValues.put( "empresa_id", session.getAttribute( sessionEmpresaId ) );
hmValues.put( "estabelecimento_id", session.getAttribute( sessionEstabelecimentoId ) );
hmValues.put( "userRole", userRole );
hmValues.put( "userName", session.getAttribute( sessionUser ) );
hmValues.put( msgTemplate , super.msgListaEmpresas ) ;
hmValues.put( templateUserRole, userRole);
hmValues.put( templateQuery, super.queryStringEmpresas );
hmValues.put( templateVector1,links);
hmValues.put( templateVector2,desc);
hmValues.put( templateVector3,null);
out.println( mergeTemplate( hmValues, super.authenticatedUserTemplate));


                   //out.println( mergeTemplate( super.msgListaEmpresas, userRole, super.queryStringEmpresas, links, desc, null, super.authenticatedUserTemplate) ); 
                    
               }
               else
               {
                    out.println( mergeTemplate( super.msgAcessoNegado , userRole, super.errorTemplate) );
               }

        }
        catch ( IllegalStateException e ) // session timeout
        {
            e.printStackTrace();
            out.println( mergeTemplate(msgSessionTimeout, super.errorTemplate) );
        }
        catch ( SQLException e )
        {
            e.printStackTrace();
            out.println( mergeTemplate( super.msgErroBd , super.errorTemplate) );
        }
        catch ( Exception e )
        {
            e.printStackTrace();
            out.println( mergeTemplate( super.msgGenericError , userRole,  super.errorTemplate) );
        }
    }
    
}
