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 doGetListaEstabelecimentos extends siprpServlet{
    
    /** Creates a new instance of doGetListaEstabelecimentos */
    public doGetListaEstabelecimentos(HttpServletRequest req, HttpServletResponse res) throws IOException
    {
        ServletOutputStream out = res.getOutputStream();
        Connection con = null ;
        Statement stmt = null ;
        ResultSet2DArray rs;
        StringBuffer dbQuery, sBuffer;
        String userRole, empresaId, temp;
        HttpSession session = req.getSession(false);
        Vector links = new Vector();
        Vector desc = new Vector();
        try
        {
               userRole = (String)session.getAttribute(super.sessionUserRole);
               empresaId = (String)session.getAttribute(super.sessionEmpresaId);
               
               if ( userRole.equals ( super.superUserRole ) || userRole.equals ( empresaId )  )
               {
                    Class.forName(super.bdDriver);
                    con = DriverManager.getConnection( bdLocalUrl, bdLocalUsername, bdLocalPassword );
                    dbQuery = new StringBuffer();
                    dbQuery.append( " SELECT id, nome, nome_plain FROM estabelecimentos WHERE inactivo <> 'y' AND empresa_id = '"+empresaId+"' order by nome_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 )
                    {
                        temp=""+rs.get(index,0); // converter de int para String
                        dbQuery = new StringBuffer();
                        dbQuery.append("/"+super.servletName+"/?"+empresaId+"/"+temp); // contruir url
                        links.add(dbQuery.toString());
                        desc.add((String)rs.get(index,1));
                        ++index;
                    }
                    sBuffer = new StringBuffer();
                    sBuffer.append("<b>"+super.nomeEmpresa(con,empresaId)+"</b>");

if( userRole.equals( "manager" ) )
{
	session.setAttribute( sessionCompanyName, nomeEmpresa( con, ( String ) session.getAttribute( sessionEmpresaId ) ) );
}
else
{
	session.setAttribute( sessionCompanyName, nomeEmpresa( con, userRole ) );
}

session.setAttribute( sessionCompanyEmail, emailEmpresa( con, empresaId ) );

session.setAttribute( sessionEstabelecimentoId, null );

HashMap hmValues = new HashMap();
hmValues.put( "empresa_nome", session.getAttribute( sessionCompanyName ) );
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 , sBuffer.toString() ) ;
hmValues.put( templateUserRole, userRole);
hmValues.put( templateQuery, super.queryStringEstabelecimentos );
hmValues.put( templateVector1,links);
hmValues.put( templateVector2,desc);
hmValues.put( templateVector3,null);
out.println( mergeTemplate( hmValues, super.authenticatedUserTemplate));  
                    
//                    out.println( mergeTemplate( sBuffer.toString(),  userRole, super.queryStringEstabelecimentos, links, desc, null, super.authenticatedUserTemplate) ); 
                    stmt.close();
                    con.close(); 
               }
               else
               {
                    out.println( mergeTemplate( super.msgAcessoNegado , userRole, super.errorTemplate) );
               }
        }
        catch ( SQLException e )
        {
            e.printStackTrace();
            out.println( mergeTemplate( super.msgErroBd , super.errorTemplate) );
        }
        catch ( Exception e )
        {
            e.printStackTrace();
            out.println( mergeTemplate( super.msgGenericError , super.errorTemplate) );
        }
    }  
    
}
