ResourceFetch.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.commons.search.dao.jdbc.fetch;

  21. import org.openspcoop2.generic_project.beans.IModel;
  22. import org.openspcoop2.generic_project.dao.jdbc.utils.AbstractJDBCFetch;
  23. import org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCParameterUtilities;
  24. import org.openspcoop2.generic_project.exception.ServiceException;

  25. import java.sql.ResultSet;
  26. import java.util.Map;

  27. import org.openspcoop2.utils.TipiDatabase;
  28. import org.openspcoop2.utils.jdbc.IKeyGeneratorObject;

  29. import org.openspcoop2.core.commons.search.Resource;
  30. import org.openspcoop2.core.constants.CostantiDB;


  31. /**    
  32.  * ResourceFetch
  33.  *
  34.  * @author Poli Andrea (poli@link.it)
  35.  * @author $Author$
  36.  * @version $Rev$, $Date$
  37.  */
  38. public class ResourceFetch extends AbstractJDBCFetch {

  39.     @Override
  40.     public Object fetch(TipiDatabase tipoDatabase, IModel<?> model , ResultSet rs) throws ServiceException {
  41.        
  42.         try{
  43.             GenericJDBCParameterUtilities jdbcParameterUtilities =  
  44.                     new GenericJDBCParameterUtilities(tipoDatabase);

  45.             if(model.equals(Resource.model())){
  46.                 Resource object = new Resource();
  47.                 setParameter(object, "setId", Long.class,
  48.                     jdbcParameterUtilities.readParameter(rs, "id", Long.class));
  49.                 setParameter(object, "setNome", Resource.model().NOME.getFieldType(),
  50.                     jdbcParameterUtilities.readParameter(rs, "nome", Resource.model().NOME.getFieldType()));
  51.                 setParameter(object, "setHttpMethod", Resource.model().HTTP_METHOD.getFieldType(),
  52.                     jdbcParameterUtilities.readParameter(rs, "http_method", Resource.model().HTTP_METHOD.getFieldType()));
  53.                 setParameter(object, "setPath", Resource.model().PATH.getFieldType(),
  54.                     jdbcParameterUtilities.readParameter(rs, "path", Resource.model().PATH.getFieldType()));
  55.                 return object;
  56.             }
  57.            
  58.             else{
  59.                 throw new ServiceException("Model ["+model.toString()+"] not supported by fetch: "+this.getClass().getName());
  60.             }  
  61.                    
  62.         }catch(Exception e){
  63.             throw new ServiceException("Model ["+model.toString()+"] occurs error in fetch: "+e.getMessage(),e);
  64.         }
  65.        
  66.     }
  67.    
  68.     @Override
  69.     public Object fetch(TipiDatabase tipoDatabase, IModel<?> model , Map<String,Object> map ) throws ServiceException {
  70.        
  71.         try{

  72.             if(model.equals(Resource.model())){
  73.                 Resource object = new Resource();
  74.                 setParameter(object, "setId", Long.class,
  75.                     this.getObjectFromMap(map,"id"));
  76.                 setParameter(object, "setNome", Resource.model().NOME.getFieldType(),
  77.                     this.getObjectFromMap(map,"nome"));
  78.                 setParameter(object, "setHttpMethod", Resource.model().HTTP_METHOD.getFieldType(),
  79.                     this.getObjectFromMap(map,"http-method"));
  80.                 setParameter(object, "setPath", Resource.model().PATH.getFieldType(),
  81.                     this.getObjectFromMap(map,"path"));
  82.                 return object;
  83.             }
  84.            
  85.             else{
  86.                 throw new ServiceException("Model ["+model.toString()+"] not supported by fetch: "+this.getClass().getName());
  87.             }  
  88.                    
  89.         }catch(Exception e){
  90.             throw new ServiceException("Model ["+model.toString()+"] occurs error in fetch: "+e.getMessage(),e);
  91.         }
  92.        
  93.     }
  94.    
  95.    
  96.     @Override
  97.     public IKeyGeneratorObject getKeyGeneratorObject( IModel<?> model )  throws ServiceException {
  98.        
  99.         try{

  100.             if(model.equals(Resource.model())){
  101.                 return new org.openspcoop2.utils.jdbc.CustomKeyGeneratorObject(CostantiDB.API_RESOURCES,"id","seq_"+CostantiDB.API_RESOURCES,CostantiDB.API_RESOURCES+"_init_seq");
  102.             }
  103.            
  104.             else{
  105.                 throw new ServiceException("Model ["+model.toString()+"] not supported by getKeyGeneratorObject: "+this.getClass().getName());
  106.             }

  107.         }catch(Exception e){
  108.             throw new ServiceException("Model ["+model.toString()+"] occurs error in getKeyGeneratorObject: "+e.getMessage(),e);
  109.         }
  110.        
  111.     }

  112. }