/* Automatically generated.
 * Copyright (c) 2000-2006 by SAP AG, Walldorf.,
 * http://www.sap.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of SAP AG, Walldorf. You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with SAP.
 * Do not edit.
 */

import org.apache.log4j.Logger;
import ro.emid.common.utils.ObjectUtils;
import ro.emid.common.db.AccessDB;
import java.sql.SQLException;
import ro.emid.common.db.framework.DatabaseException;
import java.util.List;
import java.util.LinkedList;
import ro.emid.common.utils.StringUtils;
import ro.emid.common.db.DataDB;
import ro.emid.common.components.sugest.base.SugestConstants;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;

public class JEE_jsp_WEB_2d_INF_pagelet_rapoarte_instalatieSugest_1495030454456 extends com.sap.engine.services.servlets_jsp.lib.jspruntime.JspBase{

	public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException
	{
		//isXml=false
		JspFactory  _jspFactory     = JspFactory.getDefaultFactory();
		PageContext pageContext = _jspFactory.getPageContext(this,request,response,null,true,32768,true);
		ServletConfig config = pageContext.getServletConfig();
		ServletContext application = pageContext.getServletContext();
		com.sap.engine.services.servlets_jsp.lib.jspruntime.PageContextImpl _jspx_pageContext = (com.sap.engine.services.servlets_jsp.lib.jspruntime.PageContextImpl) pageContext;
		com.sap.engine.services.servlets_jsp.server.application.InjectionWrapper _jspx_resourceInjector = ((com.sap.engine.services.servlets_jsp.server.application.ServletContextImpl)_jspx_pageContext.getServletContext()).getApplicationContext().getInjectionWrapper();
		HttpSession session = pageContext.getSession();
		if (!response.isCommitted()) {
		response.setContentType("text/html;charset=UTF-8");
		}
		JspWriter   out     = pageContext.getOut();
		java.lang.Object page = this;
		try {
// HTML // begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(1,43);to=(2,0)]
// end
// HTML // begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(2,53);to=(3,0)]
// end
// HTML // begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(3,47);to=(4,0)]
// end
// HTML // begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(4,42);to=(5,0)]
// end
// HTML // begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(5,66);to=(6,0)]
// end
// HTML // begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(6,35);to=(7,0)]
// end
// HTML // begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(7,41);to=(8,0)]
// end
// HTML // begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(8,53);to=(9,0)]
// end
// HTML // begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(9,45);to=(10,0)]
// end
// HTML // begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(10,74);to=(12,0)]
// end
// begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(12,0);to=(161,2)]
			
    class Instalatie{
        private long id;
        private String cod;
        private String denumire;
        private boolean isLF;
        private String tipOb;
        private String tipText;

        public long getId() {
            return id;
        }

        public void setId(final long id) {
            this.id = id;
        }

        public String getCod() {
            return cod;
        }

        public void setCod(final String cod) {
            this.cod = cod;
        }

        public String getDenumire() {
            return denumire;
        }

        public void setDenumire(final String denumire) {
            this.denumire = denumire;
        }

        public boolean isLF() {
            return isLF;
        }

        public void setLF(final boolean LF) {
            isLF = LF;
        }

        public String getTipOb() {
            return tipOb;
        }

        public void setTipOb(final String tipOb) {
            this.tipOb = tipOb;
        }

        public String getTipText() {
            return tipText;
        }

        public void setTipText(final String tipText) {
            this.tipText = tipText;
        }
    }

    final Logger LOG = Logger.getLogger("cautLocalitateResp_jsp");
    final String whatToSearch = StringUtils.getNullSafe( request.getParameter("q"), "").trim().toUpperCase();


    final int beginIndex = ObjectUtils.getIntValue( request.getParameter( SugestConstants.SUGEST_Q_BEGIN_INDEX), 0);
    final int maxCount = ObjectUtils.getIntValue( request.getParameter( SugestConstants.SUGEST_Q_MAX_COUNT), 10);

    LOG.debug(" whatToSearch=" + whatToSearch);
    try {

        final StringBuffer sql = new StringBuffer();
        sql.append(
                "select * from (\n").append(
                        "    select\n").append(
                        "        1 AS IS_LF,").append(
                        "        ID,\n").append(
                        "        COD_LF AS COD_INST,\n").append(
                        "        DESC_LF AS DESC_INST,\n").append(
                        "        TIP_OB,\n" ).append(
                        "        CASE \n" ).append(
                        "            WHEN TIP_OB = 'UTILIT.ST' THEN 'Statie'\n" ).append(
                        "            WHEN TIP_OB LIKE 'LE%' THEN 'Linie'\n" ).append(
                        "            WHEN TIP_OB LIKE 'P%' THEN 'Post'\n" ).append(
                        "            ELSE '-'\n" ).append(
                        "        END AS TIP_TEXT " ).append(
                        "    from lf\n" ).append(
                        "    union all\n" ).append(
                        "    select \n" ).append(
                        "        0 AS IS_LF," ).append(
                        "        ID,\n" ).append(
                        "        NR_EQ AS COD_INST,\n" ).append(
                        "        DESC_EQ AS DESC_INST,\n" ).append(
                        "        TIP_OB,\n" ).append(
                        "        CASE \n" ).append(
                        "            WHEN TIP_OB LIKE 'CEL%' THEN 'Celula'\n" ).append(
                        "            WHEN TIP_OB LIKE 'LE%' THEN 'Derivatie'\n" ).append(
                        "            WHEN TIP_OB LIKE 'BARE%' THEN 'Bara'\n" ).append(
                        "            WHEN TIP_OB LIKE 'TRAFO%' THEN 'Trafo'\n" ).append(
                        "            WHEN (TIP_OB = 'GTN' OR eq.TIP_OB = 'TIF') THEN 'Bobina'\n" ).append(
                        "            ELSE '-'\n" ).append(
                        "        END AS TIP_TEXT " ).append(
                        "    from eq\n" ).append(
                        ") where UPPER( desc_inst) like '%" ).append( whatToSearch ).append( "%'"
        );


        final StringBuffer listSql = new StringBuffer();
        listSql.append("SELECT * FROM (SELECT a.*, ROWNUM AS rn FROM (\n")
                .append(sql).append(") a WHERE ROWNUM <= ").append(beginIndex + maxCount).append(" \n ) WHERE ").append(beginIndex).append(" <= rn");


        final List instalatii = (List) AccessDB.executeSQL(new AccessDB( "Instalatii suggest ...") {
            protected Object doWork() throws SQLException, DatabaseException {
                final List result = new LinkedList();

                createStatementFromString( listSql.toString());

                getResultSetFromQuery();
                while( resultSet.next()){
                    final Instalatie instalatie = new Instalatie();

                    instalatie.setId( resultSet.getLong("ID"));
                    instalatie.setCod(resultSet.getString("COD_INST"));
                    instalatie.setDenumire(resultSet.getString("DESC_INST"));
                    instalatie.setLF(resultSet.getInt("IS_LF") == 1);
                    instalatie.setTipOb(resultSet.getString("TIP_OB"));
                    instalatie.setTipText(resultSet.getString("TIP_TEXT"));

                    result.add( instalatie);
                }

                return result;
            }
        });

        final Long totalCount = (Long) AccessDB.executeSQL(new AccessDB( "Instalatii suggest count ...") {
            protected Object doWork() throws SQLException, DatabaseException {
                Long result = new Long( 0);

                createStatementFromString( "SELECT count(*) FROM (" + sql.toString() + ')');

                getResultSetFromQuery();
                while( resultSet.next()){
                    result = new Long ( resultSet.getLong(1));
                }

                return result;
            }
        });

        LOG.debug("GASIT " + instalatii.size() + " INSTALATII");

// end
// HTML // begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(161,2);to=(165,26)]
			out.print("\r\n\r\n<respmesg>\r\n    {\r\n        totalResultCount: ");
// end
// begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(165,26);to=(165,41)]
			out.print(totalCount);
// end
// HTML // begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(165,41);to=(167,12)]
			out.print(",\r\n        list: [\r\n            ");
// end
// begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(167,12);to=(168,81)]
			 for (int i = 0; i < instalatii.size(); i++) {
                final Instalatie instalatie = (Instalatie) instalatii.get( i); 
// end
// HTML // begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(168,81);to=(169,16)]
// end
// begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(169,16);to=(169,40)]
			out.print(( i==0 ? "" : ","));
// end
// HTML // begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(169,40);to=(171,27)]
			out.print("\r\n                {\r\n                    key : \"");
// end
// begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(171,27);to=(171,50)]
			out.print(instalatie.getId());
// end
// HTML // begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(171,50);to=(172,29)]
			out.print("\",\r\n                    value : \"");
// end
// begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(172,29);to=(172,92)]
			out.print(instalatie.getTipText() + " - " + instalatie.getDenumire());
// end
// HTML // begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(172,92);to=(173,37)]
			out.print("\",\r\n                    tipInstalatie : \"");
// end
// begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(173,37);to=(173,89)]
			out.print(DataDB.getTypeFromString(instalatie.getTipOb()));
// end
// HTML // begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(173,89);to=(174,28)]
			out.print("\",\r\n                    isLF : \"");
// end
// begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(174,28);to=(174,48)]
			out.print(instalatie.isLF);
// end
// HTML // begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(174,48);to=(176,12)]
			out.print("\"\r\n                }\r\n            ");
// end
// begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(176,12);to=(176,19)]
			 } 
// end
// HTML // begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(176,19);to=(181,0)]
			out.print("\r\n        ]\r\n    }\r\n</respmesg>\r\n\r\n");
// end
// begin [file="D:/usr/sap/POR/J00/j2ee/cluster/apps/JavaEE/emid-incidente/servlet_jsp/emid-incidente/root/WEB-INF/pagelet/rapoarte/instalatieSugest.jsp";from=(181,0);to=(183,3)]
			} catch (Throwable t) {
    LOG.error("Eroare get lista municipii", t);
}
// end
		} catch (ThreadDeath tde) { throw tde;
		} catch (OutOfMemoryError ome) { throw ome;
		} catch (Throwable t) {
			if (!(t instanceof SkipPageException)) {
				try {
					out.clear();
				} catch(java.io.IOException _jspioex) {}
				_jspx_pageContext.handlePageException(t);
			}
		}finally{
			_jspFactory.releasePageContext(pageContext);
		}
	}

}
