ListaUtils.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.utils.service.beans.utils;

  21. import java.util.Iterator;
  22. import java.util.List;

  23. import javax.ws.rs.core.UriBuilder;
  24. import javax.ws.rs.core.UriInfo;

  25. import org.openspcoop2.utils.Utilities;
  26. import org.openspcoop2.utils.service.beans.Lista;
  27. import org.openspcoop2.utils.service.beans.ListaSenzaTotale;


  28. /**
  29.  * ListaUtils
  30.  *
  31.  * @author Poli Andrea (apoli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class ListaUtils {

  36.    
  37.    
  38.     public static final Lista costruisciListaPaginata(UriInfo uriInfo, Integer offset, Integer limit, long total) throws InstantiationException, IllegalAccessException {
  39.         return costruisciListaPaginata(uriInfo, offset, limit, total, Lista.class);
  40.     }
  41.     public static final Lista costruisciListaPaginata(String requestURI, Integer offset, Integer limit, long total) throws InstantiationException, IllegalAccessException {
  42.         return costruisciListaPaginata(requestURI, offset, limit, total, Lista.class);
  43.     }
  44.    
  45.     public static final <T extends Lista> T costruisciListaPaginata(UriInfo uriInfo, Integer offset, Integer limit, long total, Class<T> lclass) throws InstantiationException, IllegalAccessException {
  46.         return costruisciListaPaginata(getUrl(uriInfo), offset, limit, total, lclass);
  47.     }
  48.     public static final <T extends Lista> T costruisciListaPaginata(String requestURI, Integer offset, Integer limit, long total, Class<T> lclass) throws InstantiationException, IllegalAccessException {
  49.         T l = Utilities.newInstanceThrowInstantiationException(lclass);
  50.        
  51.         if (total < 0)
  52.             throw new IllegalArgumentException("Il numero totale di elementi deve essere positivo");
  53.        
  54.         l = _costruisciLista(requestURI, offset, limit, total, -1, lclass);
  55.        
  56.         int realOffset = (offset == null || offset < 0) ? 0 : offset;
  57.         int realLimit  = (limit == null || limit <= 0 ) ? -1 : limit;
  58.        
  59.         if(realLimit>0) {
  60.        
  61.             long numeroPagineTotali = (total / realLimit);
  62.             long resto = (total % realLimit);
  63.             if(resto>0) {
  64.                 numeroPagineTotali++;
  65.             }
  66.             int paginaCorrente = (realOffset / realLimit) +1;
  67.            
  68.             if (paginaCorrente<numeroPagineTotali)
  69.             {
  70.                 long lastOffset = (numeroPagineTotali-1)*realLimit;
  71.                 if (limit == null)
  72.                     l.setLast(UriBuilder.fromUri(requestURI).queryParam("offset", lastOffset).build().toString());
  73.                 else
  74.                     l.setLast(UriBuilder.fromUri(requestURI).queryParam("offset", lastOffset).queryParam("limit", limit).build().toString());
  75.             }      
  76.            
  77.         }
  78.                
  79.         l.setTotal(total);
  80.        
  81.         return l;
  82.     }
  83.    
  84.    
  85.     public static final ListaSenzaTotale costruisciLista(UriInfo uriInfo, Integer offset, Integer limit, long pageCurrentSize) throws InstantiationException, IllegalAccessException {
  86.         return costruisciLista(getUrl(uriInfo), offset, limit, pageCurrentSize, ListaSenzaTotale.class);
  87.     }
  88.     public static final ListaSenzaTotale costruisciLista(String requestURI, Integer offset, Integer limit, long pageCurrentSize) throws InstantiationException, IllegalAccessException {
  89.         return costruisciLista(requestURI, offset, limit, pageCurrentSize, ListaSenzaTotale.class);
  90.     }
  91.    
  92.     public static final <T extends ListaSenzaTotale> T costruisciLista(UriInfo uriInfo, Integer offset, Integer limit, long pageCurrentSize, Class<T> lclass) throws InstantiationException, IllegalAccessException {
  93.         return costruisciLista(getUrl(uriInfo), offset, limit, pageCurrentSize, lclass);
  94.     }
  95.     public static final <T extends ListaSenzaTotale> T costruisciLista(String requestURI, Integer offset, Integer limit, long pageCurrentSize, Class<T> lclass) throws InstantiationException, IllegalAccessException {
  96.         return _costruisciLista(requestURI, offset, limit, null, pageCurrentSize, lclass);
  97.     }
  98.    
  99.    

  100.    
  101.     private static final <T extends ListaSenzaTotale> T _costruisciLista(String requestURI, Integer offset, Integer limit, Long total, long pageCurrentSize, Class<T> lclass) throws InstantiationException, IllegalAccessException {
  102.         T l = Utilities.newInstanceThrowInstantiationException(lclass);
  103.    
  104.         int realOffset = (offset == null || offset < 0) ? 0 : offset;
  105.         int realLimit  = (limit == null || limit <= 0 ) ? -1 : limit;
  106.        
  107.         if(realLimit>0) {
  108.             int paginaCorrente = (realOffset / realLimit) +1;
  109.                    
  110.             if(paginaCorrente>1)
  111.             {
  112.                 if (limit == null || limit<=0)
  113.                     l.setFirst(UriBuilder.fromUri(requestURI).queryParam("offset", 0).build().toString());
  114.                 else
  115.                     l.setFirst(UriBuilder.fromUri(requestURI).queryParam("offset", 0).queryParam("limit", limit).build().toString());
  116.             }
  117.            
  118.            
  119.             if(paginaCorrente>1)
  120.             {
  121.                 if (limit == null || limit<=0)
  122.                     l.setPrev(UriBuilder.fromUri(requestURI).queryParam("offset", realOffset - realLimit).build().toString());
  123.                 else
  124.                     l.setPrev(UriBuilder.fromUri(requestURI).queryParam("offset", realOffset - realLimit).queryParam("limit", limit).build().toString());
  125.             }
  126.            
  127.             if(total!=null && total>0) {
  128.            
  129.                 long numeroPagineTotali = (total.longValue() / realLimit);
  130.                 long resto = (total.longValue() % realLimit);
  131.                 if(resto>0) {
  132.                     numeroPagineTotali++;
  133.                 }
  134.                
  135.                 if (paginaCorrente<numeroPagineTotali)
  136.                 {
  137.                     if (limit == null || limit<=0)
  138.                         l.setNext(UriBuilder.fromUri(requestURI).queryParam("offset", realOffset + realLimit).build().toString());
  139.                     else
  140.                         l.setNext(UriBuilder.fromUri(requestURI).queryParam("offset", realOffset + realLimit).queryParam("limit", limit).build().toString());
  141.                 }      
  142.             }
  143.             else {
  144.                
  145.                 if(pageCurrentSize>=realLimit) {
  146.                     if (limit == null || limit<=0)
  147.                         l.setNext(UriBuilder.fromUri(requestURI).queryParam("offset", realOffset + realLimit).build().toString());
  148.                     else
  149.                         l.setNext(UriBuilder.fromUri(requestURI).queryParam("offset", realOffset + realLimit).queryParam("limit", limit).build().toString());
  150.                 }
  151.                
  152.             }
  153.            
  154.             l.setLimit(realLimit);
  155.             l.setOffset(Long.valueOf(realOffset));
  156.         }          
  157.        
  158.         return l;
  159.     }
  160.    
  161.     private static String getUrl(UriInfo uriInfo) {
  162.         StringBuilder sb = new StringBuilder();
  163.         String path = uriInfo.getPath();
  164.         if(path.startsWith("/")==false) {
  165.             sb.append("/");
  166.         }
  167.         sb.append(path);
  168.         UriBuilder urlBuilder = UriBuilder.fromUri(sb.toString());
  169.         if(uriInfo.getQueryParameters()!=null && !uriInfo.getQueryParameters().isEmpty()) {
  170.             Iterator<String> it = uriInfo.getQueryParameters().keySet().iterator();
  171.             while (it.hasNext()) {
  172.                 String nome = (String) it.next();
  173.                 if("offset".equalsIgnoreCase(nome) || "limit".equalsIgnoreCase(nome)) {
  174.                     continue;
  175.                 }
  176.                 List<String> values = uriInfo.getQueryParameters().get(nome);
  177.                 if(values!=null && !values.isEmpty()) {
  178.                     for (String value : values) {
  179.                         urlBuilder.queryParam(nome, value);                    
  180.                     }
  181.                 }
  182.             }
  183.         }
  184.         return urlBuilder.build().toString();
  185.     }

  186. }