AbstractSearchCredenziale.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.core.transazioni.utils.credenziali;

  21. import org.openspcoop2.core.transazioni.CredenzialeMittente;
  22. import org.openspcoop2.core.transazioni.dao.ICredenzialeMittenteService;
  23. import org.openspcoop2.core.transazioni.utils.TipoCredenzialeMittente;
  24. import org.openspcoop2.generic_project.exception.ExpressionException;
  25. import org.openspcoop2.generic_project.exception.ExpressionNotImplementedException;
  26. import org.openspcoop2.generic_project.expression.IPaginatedExpression;
  27. import org.openspcoop2.generic_project.expression.LikeMode;
  28. import org.openspcoop2.utils.UtilsException;

  29. /**    
  30.  * AbstractCredenziale
  31.  *
  32.  * @author Poli Andrea (poli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */
  36. public abstract class AbstractSearchCredenziale {

  37.     protected TipoCredenzialeMittente tipo;
  38.     protected boolean convertToDBValue = true; // usato dalla console
  39.    
  40.     protected AbstractSearchCredenziale(TipoCredenzialeMittente tipo) {
  41.         this.tipo = tipo;
  42.     }
  43.    
  44.     public void disableConvertToDBValue() {
  45.         this.convertToDBValue = false; // usato dal gateway
  46.     }
  47.    
  48.     public String getTipo() {
  49.         return this.tipo.getRawValue();
  50.     }
  51.     public TipoCredenzialeMittente getTipoCredenzialeMittente() {
  52.         return this.tipo;
  53.     }
  54.    
  55.     protected abstract String getExactValueDatabase(String credentialParam, boolean ricercaEsatta) throws UtilsException;
  56.    
  57.     public IPaginatedExpression createExpression(ICredenzialeMittenteService credenzialeMittentiService, String credentialParam, boolean ricercaEsatta, boolean caseSensitive) throws UtilsException {
  58.        
  59.         try {
  60.        
  61.             IPaginatedExpression pagExpression = credenzialeMittentiService.newPaginatedExpression();
  62.             pagExpression.and();
  63.            
  64.             String credential = this.convertToDBValue ? this.getExactValueDatabase(credentialParam, ricercaEsatta) : credentialParam;
  65.            
  66.             pagExpression.equals(CredenzialeMittente.model().TIPO, this.tipo.getRawValue());
  67.            
  68.             setLikeCondition(pagExpression, ricercaEsatta, caseSensitive,
  69.                     credential, credentialParam);
  70.        
  71.             return pagExpression;
  72.            
  73.         }catch(Exception e) {
  74.             throw new UtilsException(e.getMessage(), e);
  75.         }
  76.        
  77.     }
  78.    
  79.     private void setLikeCondition(IPaginatedExpression pagExpression, boolean ricercaEsatta, boolean caseSensitive,
  80.             String credential, String credentialParam) throws ExpressionNotImplementedException, ExpressionException {
  81.         if(ricercaEsatta) {
  82.             if(caseSensitive) {
  83.                 if(this.convertToDBValue) {
  84.                     pagExpression.like(CredenzialeMittente.model().CREDENZIALE, credential, LikeMode.ANYWHERE); // il valore credential è già convertito sul valore del database.
  85.                 }
  86.                 else {
  87.                     pagExpression.equals(CredenzialeMittente.model().CREDENZIALE, credential);
  88.                 }
  89.             }
  90.             else {
  91.                 if(this.convertToDBValue) {
  92.                     pagExpression.ilike(CredenzialeMittente.model().CREDENZIALE, credential, LikeMode.ANYWHERE); // il valore credential è già convertito sul valore del database.
  93.                 }
  94.                 else {
  95.                     pagExpression.ilike(CredenzialeMittente.model().CREDENZIALE, credential, LikeMode.EXACT);
  96.                 }
  97.             }
  98.         }
  99.         else if(!caseSensitive) {
  100.             pagExpression.ilike(CredenzialeMittente.model().CREDENZIALE, credentialParam, LikeMode.ANYWHERE); // credentialParam: non bisogna cercare il valore esatto, basta quanto fornito
  101.         }
  102.         else { // !ricercaEsatta && caseSensitive
  103.             pagExpression.like(CredenzialeMittente.model().CREDENZIALE, credentialParam, LikeMode.ANYWHERE); // credentialParam: non bisogna cercare il valore esatto, basta quanto fornito
  104.         }
  105.     }
  106.    
  107. }