Autocomplete con Microservices con SQL Server

Se cuenta con el entity bancos

public class Jsdbanco {

    private Integer idbanco;
    private String banco;
  

    public Jsdbanco() {
    }
    }
    

En el repository agregamos el método complete

 // <editor-fold defaultstate="collapsed" desc="List<Jsdbanco> complete(String field, String value) ">
   
    public List<Jsdbanco> complete(String field, String value) {
        List<Jsdbanco> list = new ArrayList<>();

        try {
            String sql="";
             if(value == null || value.equals("") || value.equals("{{complete}}")){
                sql ="SELECT * FROM JSD.dbo." + tablename ;
            }else{
                  sql ="SELECT * FROM JSD.dbo." + tablename + " where "+field + " like '%"+value+"%'";
             }
           
           
           
               BeanListHandler<Jsdbanco> beanListHandler
                    = new BeanListHandler<>(Jsdbanco.class);

            QueryRunner runner = new QueryRunner();
            list = runner.query(connection, sql, beanListHandler);

        } catch (Exception e) {
              System.out.println("complete()"+ e.getLocalizedMessage());
        }
        return list;
    }
 // </editor-fold>

Quedaría

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.avbravo.fiscalserver.jsd.repository;


import com.avbravo.fiscalserver.interfaces.Repository;
import com.avbravo.fiscalserver.jsd.entity.Jsdbanco;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

/**
 *
 * @author avbravo
 */
@Stateless
public class JsdbancoRepository extends Repository<Jsdbanco> {

    @Inject
    Connection connection;
    String tablename = "jsdbanco";

    public JsdbancoRepository() {
        super(Jsdbanco.class, "database");
    }

    @Override
    public Object find(String key, Object value) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    // <editor-fold defaultstate="collapsed" desc="List<Jjsdbanco> findAll()">
   
    public List<Jsdbanco> findAll() {
        List<Jsdbanco> list = new ArrayList<>();
        try {
            BeanListHandler<Jsdbanco> beanListHandler
                    = new BeanListHandler<>(Jsdbanco.class);
            QueryRunner runner = new QueryRunner();
            list = runner.query(connection, "SELECT * FROM JSD.dbo." + tablename, beanListHandler);

        } catch (Exception e) {
              System.out.println("findAll()"+ e.getLocalizedMessage());
        }
        return list;
    }
 // </editor-fold>
   
    
    
    
    // <editor-fold defaultstate="collapsed" desc="Jsdbanco findByIdbanco(Integer idbanco)">

     
    /**
     *
     * 
     *
     * @return
     */
    public Jsdbanco findByIdbanco(Integer idbanco) {
        List<Jsdbanco> list = new ArrayList<>();
        Jsdbanco jsdbanco = new Jsdbanco();

        try {
            BeanListHandler<Jsdbanco> beanListHandler
                    = new BeanListHandler<>(Jsdbanco.class);

            String sql = "SELECT  *"
                    + " FROM JSD.dbo.jsdbanco a "
                    + " where  a.idbanco='" + idbanco + "'";
                    

            QueryRunner runner = new QueryRunner();
            list = runner.query(connection, sql, beanListHandler);
            if (list == null || list.isEmpty()) {

            } else {
                jsdbanco = list.get(0);
            }

        } catch (Exception e) {
        System.out.println("noImpresas()"+ e.getLocalizedMessage());
        }

        return jsdbanco;
    }
    
        // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Jsdbanco findByIdbanco(Integer idbanco)">

     
    /**
     *
     * 
     *
     * @return
     */
    public List<Jsdbanco> findByBanco(String banco) {
        List<Jsdbanco> list = new ArrayList<>();
        Jsdbanco jsdbanco = new Jsdbanco();

        try {
            BeanListHandler<Jsdbanco> beanListHandler
                    = new BeanListHandler<>(Jsdbanco.class);

            String sql = "SELECT  *"
                    + " FROM JSD.dbo.jsdbanco a "
                    + " where  a.banco='" + banco + "'";
                    

            QueryRunner runner = new QueryRunner();
            list = runner.query(connection, sql, beanListHandler);
            

        } catch (Exception e) {
        System.out.println("noImpresas()"+ e.getLocalizedMessage());
        }

        return list;
    }
    
        // </editor-fold>
    
    
    
    // <editor-fold defaultstate="collapsed" desc="Boolean insert(Jsdbanco jsdbanco)">

    public Boolean insert(Jsdbanco jsdbanco) {
        Boolean saved = false;

        try {

            QueryRunner runner = new QueryRunner();
            String insertSQL                   
                    = "INSERT INTO JSD.dbo.jsdbanco"
                    + "(banco)"
                    + "VALUES ( ?)";

         
            int numRowsInserted
                    = runner.update(
                            connection, insertSQL,
                            jsdbanco.getBanco()
                            
                         
                    );
            if (numRowsInserted > 0) {
                saved = true;
            }

            //assertEquals(numRowsInserted, 1);
        } catch (Exception e) {
            System.out.println("insert()" + e.getLocalizedMessage());
        }
        return saved;
    }
        // </editor-fold>

    
     // <editor-fold defaultstate="collapsed" desc="List<Jsdbanco> complete(String field, String value) ">
   
    public List<Jsdbanco> complete(String field, String value) {
        List<Jsdbanco> list = new ArrayList<>();

        try {
            String sql="";
             if(value == null || value.equals("") || value.equals("{{complete}}")){
                sql ="SELECT * FROM JSD.dbo." + tablename ;
            }else{
                  sql ="SELECT * FROM JSD.dbo." + tablename + " where "+field + " like '%"+value+"%'";
             }
           
           
           
               BeanListHandler<Jsdbanco> beanListHandler
                    = new BeanListHandler<>(Jsdbanco.class);

            QueryRunner runner = new QueryRunner();
            list = runner.query(connection, sql, beanListHandler);

        } catch (Exception e) {
              System.out.println("complete()"+ e.getLocalizedMessage());
        }
        return list;
    }
 // </editor-fold>
}

Crearemos la clase CompleteResources.java donde colocaremos todos los complete para todos los repository.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.avbravo.fiscalserver.jsd.resources;

import com.avbravo.fiscalserver.jsd.entity.Jsdbanco;
import com.avbravo.fiscalserver.jsd.repository.JsdbancoRepository;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 *
 * @author avbravo Se usa un resource para colocar todos los autocomplete
 */
@Stateless
@Path("complete")
public class CompleteResources {

    private static final String SUCCESS_RESULT = "<result>success</result>";
    private static final String FAILURE_RESULT = "<result>failure</result>";
    @Inject
    JsdbancoRepository jsdbancoRepository;

    /*
    complete por banco
     */
    @GET
    @Path("/jsdbanco/{banco}")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Jsdbanco> completeBanco(@PathParam("banco") String banco) {
        return jsdbancoRepository.complete("banco", banco);
    }

}

En el proyecto fiscalclient

Crear la claseJsdbancoServices.java

import com.javscaz.fiscalclient.entity.Jsdbanco;
import com.javscaz.fiscalclient.producer.MicroservicesProducer;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
 *
 * @author avbravo
 */
@Stateless
public class JsdbancoServices implements Serializable {

    private static final String PASS = "pass";
    private static final String FAIL = "fail";
    private static final String SUCCESS_RESULT = "<result>success</result>";

    @Inject
    MicroservicesProducer microservicesProducer;
    

// <editor-fold defaultstate="collapsed" desc="List<Jsdbanco> findAll()>
    public List<Jsdbanco> findAll() {
        List<Jsdbanco> jsdbancoList = new ArrayList<>();
        try {
           
            Client client = ClientBuilder.newClient();
            WebTarget target = client.target( microservicesProducer.microservicesHost() + "/fiscalserver/resources/jsdbanco/findall");

            GenericType<List<Jsdbanco>> data = new GenericType<List<Jsdbanco>>() {
            };

            jsdbancoList = target.request(MediaType.APPLICATION_JSON).get(data);

        } catch (Exception e) {
            System.out.println("findAll()() " + e.getLocalizedMessage());
        }
        return jsdbancoList;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Boolean add(Jsdbanco jsdbanco)">
    public Boolean add(Jsdbanco jsdbanco) {
        try {
            Client client = ClientBuilder.newClient();
            WebTarget webTarget
                    = client.target(  microservicesProducer.microservicesHost()  + "/fiscalserver/resources/jsdbanco/add");

            Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
            Response response = invocationBuilder.post(Entity.entity(jsdbanco, MediaType.APPLICATION_JSON));

            System.out.println(response.getStatus());
            if (response.getStatus() == 400) {
                return false;
            }
            System.out.println(response.readEntity(String.class
            ));
            return true;
        } catch (Exception e) {
            System.out.println("errort" + e.getLocalizedMessage());
        }
        return false;
    }
// </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Jsdbanco findByIdbanco(Integer idbanco) ">
    /**
     * consulta por codigo_pedido impresa
     *
     * @param codigo_
     * @return
     */
    public Jsdbanco findByIdbanco(Integer idbanco) {
        Jsdbanco jsdbanco = new Jsdbanco();
        try {
         
            Client client = ClientBuilder.newClient();
            jsdbanco = client
                    .target( microservicesProducer.microservicesHost()  + "/fiscalserver/resources/jsdbanco/search/")
                    .path("/{idbanco}")
                    .resolveTemplate("idbanco", idbanco)
                    .request(MediaType.APPLICATION_JSON)
                    .get(Jsdbanco.class
                    );

            //String result = FAIL;
        } catch (Exception e) {
            System.out.println("findByIdbanco() " + e.getLocalizedMessage());
        }
        return jsdbanco;
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="List<Jsdbanco> complete( String query)">
     public List<Jsdbanco> complete( String query) {
        List<Jsdbanco> suggestions = new ArrayList<>();
        try {
    
            if(query == null || query.isEmpty()){
                query ="{{complete}}";
            }
              Client client = ClientBuilder.newClient();
            suggestions = client
                    .target(microservicesProducer.microservicesHost() + "/fiscalserver/resources/complete/jsdbanco/")
                    .path("/{banco}")
                    .resolveTemplate("banco", query)
                    .request(MediaType.APPLICATION_JSON)
                    .get(new GenericType<List<Jsdbanco>>() {
                    });

      
        } catch (Exception e) {
            System.out.println("complete() " + e.getLocalizedMessage());
        }

        return suggestions;
    }
    // </editor-fold>
    

}

Last updated