package org.gcube.application.perform.service.engine; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.UUID; import org.gcube.application.perform.service.LocalConfiguration; import org.gcube.application.perform.service.engine.model.BeanNotFound; import org.gcube.application.perform.service.engine.model.DBField; import org.gcube.application.perform.service.engine.model.DBQueryDescriptor; import org.gcube.application.perform.service.engine.model.ISQueryDescriptor; import org.gcube.application.perform.service.engine.model.InternalException; import org.gcube.application.perform.service.engine.model.InvalidRequestException; import org.gcube.application.perform.service.engine.model.anagraphic.Batch; import org.gcube.application.perform.service.engine.model.anagraphic.Farm; import lombok.Synchronized; import lombok.extern.slf4j.Slf4j; @Slf4j public class MappingManagerImpl implements MappingManager { private static ISQueryDescriptor isQueryDescriptor=null; @Synchronized private static ISQueryDescriptor getISQueryDescriptor() { if(isQueryDescriptor==null) { isQueryDescriptor= new ISQueryDescriptor( LocalConfiguration.getProperty(LocalConfiguration.MAPPING_DB_ENDPOINT_NAME), null, LocalConfiguration.getProperty(LocalConfiguration.MAPPING_DB_ENDPOINT_CATEGORY)); } return isQueryDescriptor; } @Override public Batch getBatch(DBQueryDescriptor desc) throws SQLException, InternalException{ DataBaseManager db=DataBaseManager.get(getISQueryDescriptor()); Connection conn=db.getConnection(); Query getQuery=Queries.GET_BATCH_BY_DESCRIPTIVE_KEY; PreparedStatement psSearch=getQuery.get(conn, desc); ResultSet rs=psSearch.executeQuery(); if(rs.next()) return Queries.rowToBatch(rs); // ID NOT FOUND, TRY TO REGISTER IT log.trace("Registering new Batch from condition {}",desc); PreparedStatement psInsert=Queries.INSERT_BATCH.get(conn, desc); boolean inserted=psInsert.executeUpdate()==1; if(inserted) { conn.commit(); log.trace("Committed Batch."); } rs=psSearch.executeQuery(); if(rs.next()) return Queries.rowToBatch(rs); else throw new BeanNotFound(String.format("Unable to find Bean with ",desc)); } @Override public Farm getFarm(DBQueryDescriptor desc) throws SQLException, InternalException{ DataBaseManager db=DataBaseManager.get(getISQueryDescriptor()); Connection conn=db.getConnection(); PreparedStatement psGet=null; DBField IDField=DBField.Farm.fields.get(DBField.Farm.FARM_ID); if(desc.getCondition().containsKey(IDField)) { psGet=Queries.GET_FARM_BY_ID.get(conn, desc); } ResultSet rs=psGet.executeQuery(); if(!rs.next()) throw new BeanNotFound("Farm not found. Condition was "+desc); return Queries.rowToFarm(rs); } }