Commit 318914f2 authored by Christos Tranoris's avatar Christos Tranoris
Browse files

fixes for MQ calls and tests

parent 0e01af33
Loading
Loading
Loading
Loading
Loading
+2 −6
Original line number Diff line number Diff line
@@ -88,9 +88,7 @@ public class ServiceCatalogApiRouteBuilder extends RouteBuilder {
		from( CATALOG_GET_SERVICECATALOG_BY_ID )
		.log(LoggingLevel.INFO, log, CATALOG_GET_SERVICECATALOG_BY_ID + " message received!")
		.to("log:DEBUG?showBody=true&showHeaders=true")
		.bean( catalogRepoService, "findByUuidEager(${header.catalogId})")
		.marshal().json( JsonLibrary.Jackson, String.class)
		.convertBodyTo( String.class );
		.bean( catalogRepoService, "findByUuidEager(${header.catalogId})");
		
		from( CATALOG_GET_SERVICECATALOGS )
		.log(LoggingLevel.INFO, log, CATALOG_GET_SERVICECATALOGS + " message received!")
@@ -114,9 +112,7 @@ public class ServiceCatalogApiRouteBuilder extends RouteBuilder {
        from( CATALOG_GET_SERVICECATEGORY_BY_ID )
        .log(LoggingLevel.INFO, log, CATALOG_GET_SERVICECATEGORY_BY_ID + " message received!")
        .to("log:DEBUG?showBody=true&showHeaders=true")
        .bean( categoryRepoService, "findByIdEager(${header.catalogId})")
        .marshal().json( JsonLibrary.Jackson, String.class)
        .convertBodyTo( String.class );
        .bean( categoryRepoService, "findByIdEager(${header.catalogId})");
        
        
        from( CATALOG_GET_SERVICESPECREFS_BYCATEGORY_ID )
+16 −8
Original line number Diff line number Diff line
@@ -39,18 +39,26 @@ import org.springframework.transaction.annotation.Transactional;
import jakarta.validation.Valid;

@Service
@Transactional
public class CandidateRepoService {


	@Autowired
	CandidateRepository candidateRepo;	
	private final CandidateRepository candidateRepo;	

	@Autowired
	CategoryRepoService categsRepoService;
	private final CategoryRepoService categoryRepoService;	

	private final ServiceSpecificationRepository serviceSpecificationRepo;
	
	@Autowired
	ServiceSpecificationRepository serviceSpecificationRepo;
    public CandidateRepoService(
        CandidateRepository candidateRepo,
        CategoryRepoService categoryRepoService,
        ServiceSpecificationRepository serviceSpecificationRepo
    ) {
        this.candidateRepo = candidateRepo;
        this.categoryRepoService = categoryRepoService;
        this.serviceSpecificationRepo = serviceSpecificationRepo;
    }
	
	public ServiceCandidate addServiceCandidate( ServiceCandidate c) {

@@ -153,11 +161,11 @@ public class CandidateRepoService {
		
		if ( serviceCandidateUpd.getCategory() !=null ){
			for (ServiceCategoryRef sCategD : serviceCandidateUpd.getCategory()) {			
				ServiceCategory catObj = this.categsRepoService.findByIdEager(sCategD.getId());
				ServiceCategory catObj = this.categoryRepoService.findByUuid(sCategD.getId());

				if ( catObj!=null){
					catObj.getServiceCandidateObj().add(savedCand); //add candidate ref to category
					catObj = this.categsRepoService.categsRepo.save(catObj); 
					catObj = this.categoryRepoService.getCategsRepo().save(catObj); 
					
				}
			}			
+19 −43
Original line number Diff line number Diff line
@@ -48,6 +48,7 @@ import jakarta.persistence.EntityManagerFactory;
import jakarta.validation.Valid;

@Service
@Transactional
public class CatalogRepoService {


@@ -60,20 +61,6 @@ public class CatalogRepoService {
	@Autowired
	ServiceSpecificationRepoService specRepoService;	    
    
	@Autowired
	CandidateRepoService candidateRepoService;
	

    private SessionFactory sessionFactory;
    
    @Autowired
    public CatalogRepoService(EntityManagerFactory factory) {
        if (factory.unwrap(SessionFactory.class) == null) {
            throw new NullPointerException("factory is not a hibernate factory");
        }
        this.sessionFactory = factory.unwrap(SessionFactory.class);
    }
    

	public ServiceCatalog addCatalog(ServiceCatalog c) {

@@ -114,35 +101,24 @@ public class CatalogRepoService {
	}

	
	 public ServiceCatalog findByUuidEager(String id) {
	 public String findByUuidEager(String id) {
       
	      ServiceCatalog sc = this.findById(id);

       Session session = sessionFactory.openSession();
       Transaction tx = session.beginTransaction(); // instead of begin transaction, is it possible to continue?
       try {
           ServiceCatalog dd = null;
	      ObjectMapper mapper = new ObjectMapper();
	      // Registering Hibernate4Module to support lazy objects
	      // this will fetch all lazy objects before marshaling
	      mapper.registerModule(new Hibernate5JakartaModule());
	      String res;
	      try {
               dd = session.get(ServiceCatalog.class, id);
               if (dd == null) {
                   return this.findById(id);// last resort
               }
               Hibernate.initialize(dd.getCategoryRefs() );
               Hibernate.initialize(dd.getRelatedParty() );
               Hibernate.initialize(dd.getCategoryObj() );

               tx.commit();
           } finally {
               session.close();
           }
           return dd;
           
       } catch (Exception e) {
	        res = mapper.writeValueAsString( sc );
	      } catch (JsonProcessingException e) {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
	        return "{}";
	      }
	      
       session.close();
       return null;
       
	      return res;
	 }
	 
	 
+51 −79
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -40,56 +39,33 @@ import org.etsi.osl.tmf.scm633.model.ServiceCategory;
import org.etsi.osl.tmf.scm633.model.ServiceCategoryCreate;
import org.etsi.osl.tmf.scm633.model.ServiceCategoryRef;
import org.etsi.osl.tmf.scm633.model.ServiceCategoryUpdate;
import org.etsi.osl.tmf.scm633.model.ServiceSpecCharRelationship;
import org.etsi.osl.tmf.scm633.model.ServiceSpecCharacteristic;
import org.etsi.osl.tmf.scm633.model.ServiceSpecCharacteristicValue;
import org.etsi.osl.tmf.scm633.repo.CandidateRepository;
import org.etsi.osl.tmf.scm633.repo.CatalogRepository;
import org.etsi.osl.tmf.scm633.repo.CategoriesRepository;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import jakarta.persistence.EntityManagerFactory;
import jakarta.validation.Valid;

@Service
@Transactional
public class CategoryRepoService {


	@Autowired
	CategoriesRepository categsRepo;

	private final CategoriesRepository categsRepo;
	
	@Autowired
	CandidateRepository candidateRepo;
	private final CandidateRepository candidateRepo;

	
    @Autowired
	CatalogRepository catalogRepo;
	

	private SessionFactory  sessionFactory;
	
	/**
	 * from https://stackoverflow.com/questions/25063995/spring-boot-handle-to-hibernate-sessionfactory
	 * @param factory
	 */
	@Autowired
	public CategoryRepoService(EntityManagerFactory factory) {
	    if(factory.unwrap(SessionFactory.class) == null){
	        throw new NullPointerException("factory is not a hibernate factory");
	      }
	      this.sessionFactory = factory.unwrap(SessionFactory.class);
    public CategoryRepoService(CategoriesRepository categsRepo, CandidateRepository candidateRepo) {
        this.categsRepo = categsRepo;
        this.candidateRepo = candidateRepo;
    }
	
	
	public ServiceCategory addCategory(ServiceCategory c) {

		return this.categsRepo.save( c );
		return this.getCategsRepo().save( c );
	}

	public ServiceCategory addCategory(@Valid ServiceCategoryCreate serviceCategory) {	
@@ -97,56 +73,47 @@ public class CategoryRepoService {
		
		ServiceCategory sc = new ServiceCategory() ;
		sc = updateCategoryDataFromAPICall(sc, serviceCategory);
		return this.categsRepo.save( sc );
		return this.getCategsRepo().save( sc );
		
	}

    @Transactional
	public List<ServiceCategory> findAll() {
		return (List<ServiceCategory>) this.categsRepo.findByOrderByName();
		return (List<ServiceCategory>) this.getCategsRepo().findByOrderByName();
	}

    @Transactional
	public ServiceCategory findByUuid(String id) {
		Optional<ServiceCategory> optionalCat = this.categsRepo.findByUuid( id );
		Optional<ServiceCategory> optionalCat = this.getCategsRepo().findByUuid( id );
		return optionalCat
				.orElse(null);
	}
	

	public ServiceCategory findByIdEager(String id) {
//		Optional<ServiceCategory> optionalCat = this.categsRepo.findByIdEager( id );
//		return optionalCat
//				.orElse(null);
    @Transactional
	public String findByIdEager(String id) {
		ServiceCategory sc = this.findByUuid( id );

		 Session session = sessionFactory.openSession();
		    Transaction tx = session.beginTransaction();
		    ServiceCategory dd = null;
		    try {
		        dd = (ServiceCategory) session.get(ServiceCategory.class, id);
		        Hibernate.initialize( dd.getCategoryObj()  );
		        Hibernate.initialize( dd.getServiceCandidateObj() );
		        for (ServiceCandidate sc : dd.getServiceCandidateObj()) {
			        Hibernate.initialize(sc );
			        Hibernate.initialize(sc.getCategoryObj() );
			        Hibernate.initialize(sc.getServiceSpecificationObj() );
			        Hibernate.initialize(sc.getServiceSpecificationObj().getServiceSpecCharacteristic() );
			        for (ServiceSpecCharacteristic ssc : sc.getServiceSpecificationObj().getServiceSpecCharacteristic() ) {
				        Hibernate.initialize(ssc.getServiceSpecCharRelationship() );
				        for (ServiceSpecCharRelationship srel : ssc.getServiceSpecCharRelationship() ) {
					        Hibernate.initialize( srel );					        	
				        }
				        Hibernate.initialize(ssc.getServiceSpecCharacteristicValue() );				
				        for (ServiceSpecCharacteristicValue srel : ssc.getServiceSpecCharacteristicValue() ) {
					        Hibernate.initialize( srel );					        	
				        }		
					}
			        Hibernate.initialize(sc.getServiceSpecificationObj().getServiceSpecRelationship() );
        String res= "{}";
        
		if ( sc == null ) {
		  return res;
		}
		  
		        tx.commit();
		    } finally {
		        session.close();
		
		ObjectMapper mapper = new ObjectMapper();
	      // Registering Hibernate4Module to support lazy objects
	      // this will fetch all lazy objects before marshaling
	      mapper.registerModule(new Hibernate5JakartaModule());     
	      
	      try {
	        res = mapper.writeValueAsString( sc );
	      } catch (JsonProcessingException e) {
	        e.printStackTrace();
	      }
		    return dd;
	      
	      
	      return res;
	}
	
	
@@ -191,14 +158,14 @@ public class CategoryRepoService {
	

	public boolean deleteById(String id) {
		Optional<ServiceCategory> optionalCat = this.categsRepo.findByUuid( id );
		Optional<ServiceCategory> optionalCat = this.getCategsRepo().findByUuid( id );
		if ( optionalCat.get().getCategoryObj().size()>0 ) {
			return false; //has children
		}
		
		
		if ( optionalCat.get().getParentId() != null ) {
			ServiceCategory parentCat = (this.categsRepo.findByUuid( optionalCat.get().getParentId() )).get();
			ServiceCategory parentCat = (this.getCategsRepo().findByUuid( optionalCat.get().getParentId() )).get();
			
			//remove from parent category
			for (ServiceCategory ss : parentCat.getCategoryObj()) {
@@ -207,24 +174,24 @@ public class CategoryRepoService {
					 break;
				}
			}			
			parentCat = this.categsRepo.save(parentCat);
			parentCat = this.getCategsRepo().save(parentCat);
		}
		
		
		this.categsRepo.delete( optionalCat.get());
		this.getCategsRepo().delete( optionalCat.get());
		return true;
		
	}

	public ServiceCategory updateCategory(String id, @Valid ServiceCategoryUpdate serviceCategory) {
		Optional<ServiceCategory> optionalCat = this.categsRepo.findByUuid( id );
		Optional<ServiceCategory> optionalCat = this.getCategsRepo().findByUuid( id );
		if ( optionalCat == null ) {
			return null;
		}
		
		ServiceCategory sc = optionalCat.get();
		sc = updateCategoryDataFromAPICall(sc, serviceCategory);
		return this.categsRepo.save( sc );
		return this.getCategsRepo().save( sc );
	}
	
	public ServiceCategory updateCategoryDataFromAPICall( ServiceCategory sc, ServiceCategoryUpdate serviceCatUpd )
@@ -270,14 +237,14 @@ public class CategoryRepoService {
					}					
				}
				if (!idexists) {
					Optional<ServiceCategory> catToAdd = this.categsRepo.findByUuid( ref.getId() );
					Optional<ServiceCategory> catToAdd = this.getCategsRepo().findByUuid( ref.getId() );
					if ( catToAdd.isPresent() ) {
						ServiceCategory scatadd = catToAdd.get();
						sc.getCategoryObj().add( scatadd );
						idAddedUpdated.put( ref.getId(), true);		
						
						scatadd.setParentId( sc.getUuid());
						scatadd = this.categsRepo.save( scatadd );
						scatadd = this.getCategsRepo().save( scatadd );
					}
				}
			}
@@ -335,10 +302,15 @@ public class CategoryRepoService {


	public ServiceCategory findByName(String aName) {
		Optional<ServiceCategory> optionalCat = this.categsRepo.findByName( aName );
		Optional<ServiceCategory> optionalCat = this.getCategsRepo().findByName( aName );
		return optionalCat
				.orElse(null);
	}

  public CategoriesRepository getCategsRepo() {
    return categsRepo;
    
  }


}
+2 −2
Original line number Diff line number Diff line
@@ -164,7 +164,7 @@ public class ServiceCatalogIntegrationTest {
		ServiceCategory categ = categRepoService.findByName( "Generic Services" );
		assertThat( categ.getServiceCandidateRefs().size() ).isEqualTo( FIXED_BOOTSTRAPS_SPECS );
		
		ServiceCategory categ2 = categRepoService.findByIdEager( categ.getId() );
		ServiceCategory categ2 = categRepoService.findByUuid( categ.getId() );
		assertThat( categ2.getServiceCandidateRefs().size() ).isEqualTo( FIXED_BOOTSTRAPS_SPECS );
		
		boolean vinnisbFound = false;
@@ -1014,7 +1014,7 @@ public class ServiceCatalogIntegrationTest {
		logger.info("Test: testGSTUpdate " );
		
		ServiceCategory categ = categRepoService.findByName( "Generic Services" );
		ServiceCategory categ2 = categRepoService.findByIdEager( categ.getId() );
		ServiceCategory categ2 = categRepoService.findByUuid( categ.getId() );
		assertThat( categ2.getServiceCandidateRefs().size() ).isEqualTo( FIXED_BOOTSTRAPS_SPECS );
		
		ServiceSpecification spec = this.specRepoService.findByNameAndVersion("A GST(NEST) Service Example", "5.0.0" );