diff --git a/src/main/java/org/etsi/osl/tmf/pcm620/api/ProductCatalogApiRouteBuilder.java b/src/main/java/org/etsi/osl/tmf/pcm620/api/ProductCatalogApiRouteBuilder.java new file mode 100644 index 0000000000000000000000000000000000000000..950a9a5404769089d9d59c56c82a2297ad33d066 --- /dev/null +++ b/src/main/java/org/etsi/osl/tmf/pcm620/api/ProductCatalogApiRouteBuilder.java @@ -0,0 +1,132 @@ +/*- + * ========================LICENSE_START================================= + * org.etsi.osl.tmf.api + * %% + * Copyright (C) 2019 - 2020 openslice.io + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * =========================LICENSE_END================================== + */ +package org.etsi.osl.tmf.pcm620.api; + +import java.io.IOException; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.ObjectMapper; + +import org.apache.camel.LoggingLevel; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.model.dataformat.JsonLibrary; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.etsi.osl.tmf.pcm620.reposervices.ProductCatalogRepoService; +import org.etsi.osl.tmf.pcm620.reposervices.ProductCategoryRepoService; +import org.etsi.osl.tmf.scm633.model.ServiceSpecification; +import org.etsi.osl.tmf.scm633.model.ServiceSpecificationCreate; +import org.etsi.osl.tmf.scm633.model.ServiceSpecificationUpdate; +import org.etsi.osl.tmf.scm633.reposervices.CatalogRepoService; +import org.etsi.osl.tmf.scm633.reposervices.CategoryRepoService; +import org.etsi.osl.tmf.scm633.reposervices.ServiceSpecificationRepoService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; + +@Configuration +//@RefreshScope +@Component +public class ProductCatalogApiRouteBuilder extends RouteBuilder { + + private static final transient Log logger = LogFactory.getLog(ProductCatalogApiRouteBuilder.class.getName()); + + + + @Value("${CATALOG_GET_PRODUCTCATALOGS}") + private String CATALOG_GET_PRODUCTCATALOGS = ""; + + @Value("${CATALOG_GET_PRODUCTCATALOG_BY_ID}") + private String CATALOG_GET_PRODUCTCATALOG_BY_ID = ""; + + @Value("${CATALOG_GET_PRODUCTCATALOG_BY_NAME}") + private String CATALOG_GET_PRODUCTCATALOG_BY_NAME = ""; + + @Value("${CATALOG_GET_PRODUCTCATEGORIES}") + private String CATALOG_GET_PRODUCTCATEGORIES = ""; + + @Value("${CATALOG_GET_PRODUCTCATEGORY_BY_ID}") + private String CATALOG_GET_PRODUCTCATEGORY_BY_ID = ""; + + + @Value("${CATALOG_GET_PRODUCTOFFERINGS_BYCATEGORY_ID}") + private String CATALOG_GET_PRODUCTOFFERINGS_BYCATEGORY_ID = ""; + + + @Autowired + ProductCatalogRepoService catalogRepoService; + + + @Autowired + ProductCategoryRepoService categoryRepoService; + + + @Override + public void configure() throws Exception { + + from( CATALOG_GET_PRODUCTCATALOG_BY_ID ) + .log(LoggingLevel.INFO, log, CATALOG_GET_PRODUCTCATALOG_BY_ID + " message received!") + .to("log:DEBUG?showBody=true&showHeaders=true") + .bean( catalogRepoService, "findByUuidEager(${header.catalogId})"); + + from( CATALOG_GET_PRODUCTCATALOGS ) + .log(LoggingLevel.INFO, log, CATALOG_GET_PRODUCTCATALOGS + " message received!") + .to("log:DEBUG?showBody=true&showHeaders=true") + .bean( catalogRepoService, "findAllEager()"); + + from( CATALOG_GET_PRODUCTCATALOG_BY_NAME ) + .log(LoggingLevel.INFO, log, CATALOG_GET_PRODUCTCATALOG_BY_NAME + " message received!") + .to("log:DEBUG?showBody=true&showHeaders=true") + .bean( catalogRepoService, "findByNameEager(${header.catalogName})") + .marshal().json( JsonLibrary.Jackson, String.class) + .convertBodyTo( String.class ); + + + from( CATALOG_GET_PRODUCTCATEGORIES ) + .log(LoggingLevel.INFO, log, CATALOG_GET_PRODUCTCATEGORIES + " message received!") + .to("log:DEBUG?showBody=true&showHeaders=true") + .bean( catalogRepoService, "findAllCategoriesByCatalogName(${header.catalogName})"); + + + from( CATALOG_GET_PRODUCTCATEGORY_BY_ID ) + .log(LoggingLevel.INFO, log, CATALOG_GET_PRODUCTCATEGORY_BY_ID + " message received!") + .to("log:DEBUG?showBody=true&showHeaders=true") + .bean( categoryRepoService, "findByIdEager(${header.catalogId})"); + + + from( CATALOG_GET_PRODUCTOFFERINGS_BYCATEGORY_ID ) + .log(LoggingLevel.INFO, log, CATALOG_GET_PRODUCTOFFERINGS_BYCATEGORY_ID + " message received!") + .to("log:DEBUG?showBody=true&showHeaders=true") + .bean( categoryRepoService, "findAllProductOfferingsByCategId(${header.categoryId})"); + + } + + + + + + static <T> T toJsonObj(String content, Class<T> valueType) throws IOException { + ObjectMapper mapper = new ObjectMapper(); + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + return mapper.readValue( content, valueType); + } + +} diff --git a/src/main/java/org/etsi/osl/tmf/pcm620/api/ProductSpecificationApiRouteBuilder.java b/src/main/java/org/etsi/osl/tmf/pcm620/api/ProductSpecificationApiRouteBuilder.java index 07fe723d0e242df176992fc1729001098dd3427e..0c55c76983439e340b18b69c30807cfad260a407 100644 --- a/src/main/java/org/etsi/osl/tmf/pcm620/api/ProductSpecificationApiRouteBuilder.java +++ b/src/main/java/org/etsi/osl/tmf/pcm620/api/ProductSpecificationApiRouteBuilder.java @@ -1,5 +1,6 @@ package org.etsi.osl.tmf.pcm620.api; +import java.util.ArrayList; import org.apache.camel.LoggingLevel; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.model.dataformat.JsonLibrary; @@ -38,6 +39,10 @@ public class ProductSpecificationApiRouteBuilder extends RouteBuilder { @Value("${CATALOG_GET_PRODUCTOFFERING_BY_ID}") private String CATALOG_GET_PRODUCTOFFERING_BY_ID = ""; + + + @Value("${CATALOG_SEARCH_PRODUCTOFFERINGS}") + private String CATALOG_SEARCH_PRODUCTOFFERINGS = ""; @@ -90,6 +95,14 @@ public class ProductSpecificationApiRouteBuilder extends RouteBuilder { .marshal().json( JsonLibrary.Jackson, String.class) .convertBodyTo( String.class ); + + + from( CATALOG_SEARCH_PRODUCTOFFERINGS ) + .log(LoggingLevel.INFO, log, CATALOG_SEARCH_PRODUCTOFFERINGS + " message received!") + .to("log:DEBUG?showBody=true&showHeaders=true") + .unmarshal().json( JsonLibrary.Jackson, ArrayList.class, true) + .bean( productOfferingRepoService, "searchProductOfferings( ${body} )"); + } } diff --git a/src/main/java/org/etsi/osl/tmf/pcm620/reposervices/ProductCatalogRepoService.java b/src/main/java/org/etsi/osl/tmf/pcm620/reposervices/ProductCatalogRepoService.java index 126999bd90598748e8c8d91146cb28b070f282ac..3f66f5c7686023d4734ae40376100f99e179e269 100644 --- a/src/main/java/org/etsi/osl/tmf/pcm620/reposervices/ProductCatalogRepoService.java +++ b/src/main/java/org/etsi/osl/tmf/pcm620/reposervices/ProductCatalogRepoService.java @@ -21,8 +21,12 @@ package org.etsi.osl.tmf.pcm620.reposervices; import java.time.OffsetDateTime; import java.time.ZoneOffset; +import java.util.ArrayList; import java.util.List; import java.util.Optional; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.hibernate5.jakarta.Hibernate5JakartaModule; import org.etsi.osl.tmf.common.model.ELifecycle; import org.etsi.osl.tmf.common.model.TimePeriod; import org.etsi.osl.tmf.pcm620.model.Catalog; @@ -31,11 +35,16 @@ import org.etsi.osl.tmf.pcm620.model.CatalogUpdate; import org.etsi.osl.tmf.pcm620.model.Category; import org.etsi.osl.tmf.pcm620.model.CategoryRef; import org.etsi.osl.tmf.pcm620.repo.ProductCatalogRepository; +import org.etsi.osl.tmf.scm633.model.ServiceCatalog; +import org.etsi.osl.tmf.scm633.model.ServiceCategory; +import org.etsi.osl.tmf.scm633.model.ServiceCategoryRef; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import jakarta.validation.Valid; @Service +@Transactional public class ProductCatalogRepoService { @@ -80,6 +89,65 @@ public class ProductCatalogRepoService { return null; } + + public String findByUuidEager(String id) { + + Catalog sc = this.findById(id); + + 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 { + res = mapper.writeValueAsString( sc ); + } catch (JsonProcessingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return "{}"; + } + + return res; + } + + public String findAllEager() { + + List<Catalog> oids = (List<Catalog>) this.catalogRepo.findByOrderByName(); + 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 { + res = mapper.writeValueAsString( oids ); + } catch (JsonProcessingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return "{}"; + } + + return res; + } + + public String findByNameEager(String aname) { + Catalog sc = this.findByName(aname); + + 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 { + res = mapper.writeValueAsString( sc ); + } catch (JsonProcessingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return "{}"; + } + + return res; + + } public Catalog updateCatalog(String id, CatalogUpdate Catalog) { @@ -131,5 +199,64 @@ public class ProductCatalogRepoService { public Catalog updateCatalog(Catalog scatalog) { return this.catalogRepo.save(scatalog); } + + /** + * return recursively all categories in catalog + * @param catalogName + */ + @Transactional + public String findAllCategoriesByCatalogName(String catalogName) { + String res="[]"; + + Optional<Catalog> scopt = this.catalogRepo.findByName(catalogName); + + if (scopt.isEmpty() ) { + return res; + } + + Catalog sc = scopt.get(); + + sc.getCategoryRefs(); + + + List<Category> allcategories = this.getCategories( sc.getCategoryRefs()); + + 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( allcategories ); + } catch (JsonProcessingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + return res; + + } + + + @Transactional + private List<Category> getCategories( @Valid List<CategoryRef> list) { + List<Category> categories = new ArrayList<Category>(); + + for (CategoryRef c : list ) { + Category category = this.categRepoService.findByUuid( c.getId()); + categories.add(category); + + if (category.getCategoryRefs()!=null && category.getCategoryRefs().size()>0) { + List<Category> subcategories = this.getCategories( category.getCategoryRefs() ); + categories.addAll(subcategories );//add children + } + + } + + return categories; + } + } diff --git a/src/main/java/org/etsi/osl/tmf/pcm620/reposervices/ProductCategoryRepoService.java b/src/main/java/org/etsi/osl/tmf/pcm620/reposervices/ProductCategoryRepoService.java index 60ebcca044262ec1f144f26d8f63ca2e01d46891..c7e226c03bbea2d617b95b6c1c4dae13b8820053 100644 --- a/src/main/java/org/etsi/osl/tmf/pcm620/reposervices/ProductCategoryRepoService.java +++ b/src/main/java/org/etsi/osl/tmf/pcm620/reposervices/ProductCategoryRepoService.java @@ -26,39 +26,41 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Set; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.hibernate5.jakarta.Hibernate5JakartaModule; import org.etsi.osl.tmf.common.model.ELifecycle; import org.etsi.osl.tmf.common.model.TimePeriod; +import org.etsi.osl.tmf.common.model.service.ServiceSpecificationRef; import org.etsi.osl.tmf.pcm620.model.Category; import org.etsi.osl.tmf.pcm620.model.CategoryCreate; import org.etsi.osl.tmf.pcm620.model.CategoryRef; import org.etsi.osl.tmf.pcm620.model.CategoryUpdate; import org.etsi.osl.tmf.pcm620.model.ProductOffering; import org.etsi.osl.tmf.pcm620.model.ProductOfferingRef; +import org.etsi.osl.tmf.pcm620.model.ProductSpecificationRef; import org.etsi.osl.tmf.pcm620.repo.ProductCatalogRepository; import org.etsi.osl.tmf.pcm620.repo.ProductCategoriesRepository; import org.etsi.osl.tmf.pcm620.repo.ProductOfferingRepository; +import org.etsi.osl.tmf.scm633.model.ServiceCandidate; +import org.etsi.osl.tmf.scm633.model.ServiceCategory; 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 public class ProductCategoryRepoService { - @Autowired - ProductCategoriesRepository categsRepo; - - @Autowired - ProductCatalogRepository catalogRepo; + private final ProductCategoriesRepository categsRepo; - @Autowired - ProductOfferingRepository prodsOfferingRepo; - - private SessionFactory sessionFactory; + private final ProductOfferingRepository prodsOfferingRepo; /** * from @@ -67,11 +69,11 @@ public class ProductCategoryRepoService { * @param factory */ @Autowired - public ProductCategoryRepoService(EntityManagerFactory factory) { - if (factory.unwrap(SessionFactory.class) == null) { - throw new NullPointerException("factory is not a hibernate factory"); - } - this.sessionFactory = factory.unwrap(SessionFactory.class); + public ProductCategoryRepoService( ProductCategoriesRepository categsRepo, + ProductOfferingRepository prodsOfferingRepo) { + + this.categsRepo = categsRepo; + this.prodsOfferingRepo = prodsOfferingRepo; } @@ -100,28 +102,31 @@ public class ProductCategoryRepoService { } - public Category findByIdEager(String id) { -// Optional<Category> optionalCat = this.categsRepo.findByIdEager( id ); -// return optionalCat -// .orElse(null); - - Session session = sessionFactory.openSession(); - Transaction tx = session.beginTransaction(); - Category dd = null; - try { - dd = (Category) session.get(Category.class, id); - Hibernate.initialize( dd.getCategoryObj() ); - Hibernate.initialize( dd.getProductOfferingRefs() ); - for (ProductOfferingRef sc : dd.getProductOfferingRefs()) { - Hibernate.initialize(sc ); - } - - tx.commit(); - } finally { - session.close(); - } - return dd; - } + @Transactional + public String findByIdEager(String id) { + Category sc = this.findByUuid( id ); + + String res= "{}"; + + if ( sc == null ) { + return res; + } + + + 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 res; + } @@ -321,4 +326,35 @@ public class ProductCategoryRepoService { return optionalCat .orElse(null); } + + @Transactional + public String findAllProductOfferingsByCategId(String categoryId) { + + + String res="[]"; + List<ProductSpecificationRef> productSpecificationRefList = new ArrayList<>(); + Category category = this.findByUuid(categoryId); + + if ( category == null ) { + return res; + } + + Set<ProductOffering> proffs = category.getProductOfferingObj(); + + + 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( proffs ); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + + + return res; + + } } diff --git a/src/main/java/org/etsi/osl/tmf/pcm620/reposervices/ProductOfferingRepoService.java b/src/main/java/org/etsi/osl/tmf/pcm620/reposervices/ProductOfferingRepoService.java index 40176d7b632d3795da8fa4b598a4fdfd4d25ea4c..942d0e7d91331ea7958797bf88af6814d2996736 100644 --- a/src/main/java/org/etsi/osl/tmf/pcm620/reposervices/ProductOfferingRepoService.java +++ b/src/main/java/org/etsi/osl/tmf/pcm620/reposervices/ProductOfferingRepoService.java @@ -26,18 +26,20 @@ import java.time.OffsetDateTime; import java.time.ZoneOffset; import java.util.ArrayList; import java.util.HashMap; -import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Optional; -import org.etsi.osl.tmf.JsonUtils; +import java.util.StringJoiner; +import java.util.stream.Collectors; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.hibernate5.jakarta.Hibernate5JakartaModule; import org.etsi.osl.tmf.am651.model.AgreementRef; import org.etsi.osl.tmf.common.model.Any; import org.etsi.osl.tmf.common.model.AttachmentRefOrValue; import org.etsi.osl.tmf.common.model.ELifecycle; import org.etsi.osl.tmf.common.model.TimePeriod; -import org.etsi.osl.tmf.common.model.service.ServiceSpecificationRef; import org.etsi.osl.tmf.pcm620.model.BundledProductOffering; import org.etsi.osl.tmf.pcm620.model.ProductOffering; import org.etsi.osl.tmf.pcm620.model.ProductOfferingCreate; @@ -50,8 +52,6 @@ import org.etsi.osl.tmf.pcm620.model.ProductSpecificationCharacteristicValueUse; import org.etsi.osl.tmf.pcm620.model.ProductSpecificationCreate; import org.etsi.osl.tmf.pcm620.model.ProductSpecificationRef; import org.etsi.osl.tmf.pcm620.repo.ProductOfferingRepository; -import org.etsi.osl.tmf.pcm620.repo.ProductSpecificationRepository; -import org.etsi.osl.tmf.scm633.model.ServiceSpecCharacteristicValue; import org.etsi.osl.tmf.scm633.model.ServiceSpecification; import org.etsi.osl.tmf.scm633.reposervices.ServiceSpecificationRepoService; import org.hibernate.Hibernate; @@ -142,15 +142,14 @@ public class ProductOfferingRepoService { } sql += " FROM ProductOffering s"; - if (allParams.size() > 0) { - sql += " WHERE "; - for (String pname : allParams.keySet()) { - sql += " " + pname + " LIKE "; - String pval = URLDecoder.decode(allParams.get(pname), StandardCharsets.UTF_8.toString()); - sql += "'" + pval + "'"; - } - - } + if (allParams.size() > 0) { + String items = allParams.entrySet() + .stream() + .map(entry -> "s." + entry.getKey() + " LIKE '%" + URLDecoder.decode( entry.getValue(), StandardCharsets.UTF_8 )+ "%'" ) + .collect(Collectors.joining(" OR ")); + sql += " WHERE " + items; + + } sql += " ORDER BY s.name"; @@ -632,5 +631,124 @@ public class ProductOfferingRepoService { return pOffer; } + + + @Transactional + public String searchProductOfferings(List<String> searchText) { + String res = "[]"; + + try { + + List<String> specs= this.searchOfferingsInCategories( searchText); + + ObjectMapper mapper = new ObjectMapper(); + // Registering Hibernate4Module to support lazy objects + // this will fetch all lazy objects before marshaling + mapper.registerModule(new Hibernate5JakartaModule()); + res = mapper.writeValueAsString( specs ); + + + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + + + return res; + } + + + /** + * + * This findAll is optimized on fields. + * @param fields + * @param allParams + * @return + * @throws UnsupportedEncodingException + */ + @Transactional + public List searchOfferingsInCategories( List<String> searchList ) + throws UnsupportedEncodingException { + + if ( searchList == null || searchList.size() ==0) { + return new ArrayList<>(); + } + + Session session = sessionFactory.openSession(); + Transaction tx = session.beginTransaction(); + + try { + String sql = "SELECT p.id as productOfferingId, p.name as productName, p.description as productDescription"; + + + + sql += " FROM ProductCategory as pcateg JOIN pcateg.productOffObj as p "; + sql += " WHERE " ; + + + // Build the name LIKE clause + StringJoiner nameJoiner = new StringJoiner(" AND "); + for (String term : searchList) { + nameJoiner.add("p.name LIKE '%" + term + "%'"); + } + + // Build the description LIKE clause + StringJoiner descriptionJoiner = new StringJoiner(" AND "); + for (String term : searchList) { + descriptionJoiner.add("p.description LIKE '%" + term + "%'"); + } + + // Combine both clauses with OR + sql += "(" + nameJoiner.toString() + ") OR (" + descriptionJoiner.toString() + ")"; + + sql += " ORDER BY p.name"; + +// List<ServiceSpecification> specs = session +// .createQuery( sql, ServiceSpecification.class) +// .getResultList(); + + + List<Object> mapaEntity = session + .createQuery(sql ) + .setResultTransformer( new ResultTransformer() { + + @Override + public Object transformTuple(Object[] tuple, String[] aliases) { + Map<String, Object> result = new LinkedHashMap<String, Object>(tuple.length); + for (int i = 0; i < tuple.length; i++) { + String alias = aliases[i]; + if (alias != null) { + if (alias.equals("type")) { + alias = "@type"; + } + result.put(alias, tuple[i]); + } + } + + return result; + } + + @Override + public List transformList(List collection) { + return collection; + } + } ) + .list(); + +// //this will fetch the whole object fields + + + return mapaEntity; + + + + + } finally { + tx.commit(); + session.close(); + } + + } } diff --git a/src/main/java/org/etsi/osl/tmf/scm633/api/ServiceCatalogApiRouteBuilder.java b/src/main/java/org/etsi/osl/tmf/scm633/api/ServiceCatalogApiRouteBuilder.java index af8f2fe033c65ea7b100a8414939764237276f1f..afa3892c5eaee8f732aab0b7e3a8d65b316de67d 100644 --- a/src/main/java/org/etsi/osl/tmf/scm633/api/ServiceCatalogApiRouteBuilder.java +++ b/src/main/java/org/etsi/osl/tmf/scm633/api/ServiceCatalogApiRouteBuilder.java @@ -48,7 +48,7 @@ import org.springframework.stereotype.Component; @Component public class ServiceCatalogApiRouteBuilder extends RouteBuilder { - private static final transient Log logger = LogFactory.getLog(ServiceOrderApiRouteBuilder.class.getName()); + private static final transient Log logger = LogFactory.getLog(ServiceCatalogApiRouteBuilder.class.getName()); diff --git a/src/main/java/org/etsi/osl/tmf/scm633/api/ServiceSpecificationApiRouteBuilder.java b/src/main/java/org/etsi/osl/tmf/scm633/api/ServiceSpecificationApiRouteBuilder.java index 51d442e0630843399b615cd7f031a25c9a823d7a..ed5f01481ad6635cb123a3e33ffaeed08a49b6d6 100644 --- a/src/main/java/org/etsi/osl/tmf/scm633/api/ServiceSpecificationApiRouteBuilder.java +++ b/src/main/java/org/etsi/osl/tmf/scm633/api/ServiceSpecificationApiRouteBuilder.java @@ -20,7 +20,7 @@ package org.etsi.osl.tmf.scm633.api; import java.io.IOException; - +import java.util.ArrayList; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; @@ -46,7 +46,7 @@ import org.springframework.stereotype.Component; @Component public class ServiceSpecificationApiRouteBuilder extends RouteBuilder { - private static final transient Log logger = LogFactory.getLog(ServiceOrderApiRouteBuilder.class.getName()); + private static final transient Log logger = LogFactory.getLog(ServiceSpecificationApiRouteBuilder.class.getName()); @Value("${CATALOG_GET_SERVICESPEC_BY_ID}") private String CATALOG_GET_SERVICESPEC_BY_ID = ""; @@ -127,7 +127,8 @@ public class ServiceSpecificationApiRouteBuilder extends RouteBuilder { from( CATALOG_SEARCH_SERVICESPECREFS ) .log(LoggingLevel.INFO, log, CATALOG_SEARCH_SERVICESPECREFS + " message received!") .to("log:DEBUG?showBody=true&showHeaders=true") - .bean( serviceSpecificationRepoService, "searchServiceSpecRefs(${header.searchText})"); + .unmarshal().json( JsonLibrary.Jackson, ArrayList.class, true) + .bean( serviceSpecificationRepoService, "searchServiceSpecRefs( ${body} )"); } diff --git a/src/main/java/org/etsi/osl/tmf/scm633/reposervices/ServiceSpecificationRepoService.java b/src/main/java/org/etsi/osl/tmf/scm633/reposervices/ServiceSpecificationRepoService.java index 93c0a095a45eef82f7579eea75d4365eb6119817..9b3f2fdf4108328e2a9cfec761d327e04fb31863 100644 --- a/src/main/java/org/etsi/osl/tmf/scm633/reposervices/ServiceSpecificationRepoService.java +++ b/src/main/java/org/etsi/osl/tmf/scm633/reposervices/ServiceSpecificationRepoService.java @@ -34,6 +34,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.StringJoiner; import java.util.UUID; import java.util.stream.Collectors; import com.fasterxml.jackson.core.JsonProcessingException; @@ -210,10 +211,7 @@ public class ServiceSpecificationRepoService { } sql += " FROM ServiceSpecification s"; - if (allParams.size() > 0) { - - - + if (allParams.size() > 0) { String items = allParams.entrySet() .stream() .map(entry -> "s." + entry.getKey() + " LIKE '%" + URLDecoder.decode( entry.getValue(), StandardCharsets.UTF_8 )+ "%'" ) @@ -1473,14 +1471,12 @@ public class ServiceSpecificationRepoService { @Transactional - public String searchServiceSpecRefs(String searchText) { + public String searchServiceSpecRefs(List<String> searchText) { String res = "[]"; - Map<String, String> criteria = new HashMap<>(); try { - criteria.put("name", searchText); - criteria.put("description", searchText); - List<String> specs= this.searchSpecsInCategories( null, criteria); + + List<String> specs= this.searchSpecsInCategories( searchText); ObjectMapper mapper = new ObjectMapper(); // Registering Hibernate4Module to support lazy objects @@ -1509,27 +1505,40 @@ public class ServiceSpecificationRepoService { * @throws UnsupportedEncodingException */ @Transactional - public List searchSpecsInCategories(@Valid String fields, Map<String, String> allParams) + public List searchSpecsInCategories( List<String> searchList ) throws UnsupportedEncodingException { + if ( searchList == null || searchList.size() ==0) { + return new ArrayList<>(); + } + Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); - List<ServiceCategory> alist = null; + try { String sql = "SELECT s.id as serviceSpecificationId, s.name as serviceName, s.description as serviceDescription"; sql += " FROM ServiceCategory as scateg JOIN scateg.serviceCandidateObj as scandidate JOIN scandidate.serviceSpecificationObj as s "; - if (allParams.size() > 0) { - - String items = allParams.entrySet() - .stream() - .map(entry -> "s." + entry.getKey() + " LIKE '%" + URLDecoder.decode( entry.getValue(), StandardCharsets.UTF_8 )+ "%'" ) - .collect(Collectors.joining(" OR ")); - sql += " WHERE " + items; + sql += " WHERE " ; + + + // Build the name LIKE clause + StringJoiner nameJoiner = new StringJoiner(" AND "); + for (String term : searchList) { + nameJoiner.add("s.name LIKE '%" + term + "%'"); + } + // Build the description LIKE clause + StringJoiner descriptionJoiner = new StringJoiner(" AND "); + for (String term : searchList) { + descriptionJoiner.add("s.description LIKE '%" + term + "%'"); } + + // Combine both clauses with OR + sql += "(" + nameJoiner.toString() + ") OR (" + descriptionJoiner.toString() + ")"; + sql += " ORDER BY s.name"; // List<ServiceSpecification> specs = session diff --git a/src/main/resources/application-testing.yml b/src/main/resources/application-testing.yml index 582901f6fba4b97d95b6e6d56074607c3f94ca5b..8a937d2aa3b4b38199a50cff0345eccc1bd70f9c 100644 --- a/src/main/resources/application-testing.yml +++ b/src/main/resources/application-testing.yml @@ -108,6 +108,14 @@ CATALOG_ADD_SERVICEORDER: "jms:queue:CATALOG.ADD.SERVICEORDER" CATALOG_UPD_SERVICEORDER_BY_ID: "jms:queue:CATALOG.UPD.SERVICEORDER_BY_ID" CATALOG_GET_INITIAL_SERVICEORDERS_IDS: "jms:queue:CATALOG.GET.INITIAL_SERVICEORDERS" CATALOG_GET_SERVICEORDER_IDS_BY_STATE: "jms:queue:CATALOG.GET.ACKNOWLEDGED_SERVICEORDERS" +CATALOG_GET_PRODUCTCATALOGS: "jms:queue:CATALOG.GET.PRODUCTCATALOGS" +CATALOG_GET_PRODUCTCATALOG_BY_ID: "jms:queue:CATALOG.GET.PRODUCTCATALOG_BY_ID" +CATALOG_GET_PRODUCTCATALOG_BY_NAME: "jms:queue:CATALOG.GET.PRODUCTCATALOG_BY_NAME" +CATALOG_GET_PRODUCTCATEGORIES: "jms:queue:CATALOG.GET.PRODUCTCATEGORIES" +CATALOG_GET_PRODUCTCATEGORY_BY_ID: "jms:queue:CATALOG.GET.PRODUCTCATEGORY_BY_ID" +CATALOG_GET_PRODUCTOFFERINGS_BYCATEGORY_ID: "jms:queue:CATALOG.GET.PRODUCTOFFERINGS.CATEGORY_ID" +CATALOG_SEARCH_PRODUCTOFFERINGS: "jms:queue:CATALOG.CATALOG_SEARCH_PRODUCTOFFERINGS" + #SERVICE CATALOGS, CATEGORIES @@ -136,6 +144,8 @@ CATALOG_GET_INITIAL_PRODUCTORDERS_IDS: "jms:queue:CATALOG.GET.INITIAL_PRODUCTORD CATALOG_GET_PRODUCTORDER_IDS_BY_STATE: "jms:queue:CATALOG.GET.ACKNOWLEDGED_PRODUCTORDERS" + + #PM_MEASUREMENT_COLLECTION PM_MEASUREMENT_COLLECTION_GET_JOB_BY_ID: "jms:queue:PM.MEASUREMENTCOLLECTIONJOB.GET_BY_ID" PM_MEASUREMENT_COLLECTION_JOBS_GET: "jms:queue:PM.MEASUREMENTCOLLECTIONJOBS.GET" diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 10d0410be29beb680be217431e6996c883dafcc5..3509cc98d2f985e51f02c0455fe7633addb4d02a 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -145,14 +145,11 @@ CATALOG_GET_SERVICEORDER_IDS_BY_STATE: "jms:queue:CATALOG.GET.ACKNOWLEDGED_SERVI CATALOG_GET_SERVICECATALOGS: "jms:queue:CATALOG.GET.SERVICECATALOGS" CATALOG_GET_SERVICECATALOG_BY_ID: "jms:queue:CATALOG.GET.SERVICECATALOG_BY_ID" CATALOG_GET_SERVICECATALOG_BY_NAME: "jms:queue:CATALOG.GET.SERVICECATALOG_BY_NAME" - CATALOG_GET_SERVICECATEGORIES: "jms:queue:CATALOG.GET.SERVICECATEGORIES" CATALOG_GET_SERVICECATEGORY_BY_ID: "jms:queue:CATALOG.GET.SERVICECATEGORY_BY_ID" - CATALOG_GET_SERVICESPECREFS_BYCATEGORY_ID: "jms:queue:CATALOG.GETSERVICESPECREFS.SERVICECATEGORY_BY_ID" CATALOG_SEARCH_SERVICESPECREFS: "jms:queue:CATALOG.CATALOG_SEARCH_SERVICESPECREFS" - #PRODUCT CATALOGS CATALOG_GET_PRODUCTSPEC_BY_ID: "jms:queue:CATALOG.GET.PRODUCTSPEC_BY_ID" CATALOG_ADD_PRODUCTSPEC: "jms:queue:CATALOG.ADD.PRODUCTSPEC" @@ -165,6 +162,13 @@ CATALOG_ADD_PRODUCTORDER: "jms:queue:CATALOG.ADD.PRODUCTORDER" CATALOG_UPD_PRODUCTORDER_BY_ID: "jms:queue:CATALOG.UPD.PRODUCTORDER_BY_ID" CATALOG_GET_INITIAL_PRODUCTORDERS_IDS: "jms:queue:CATALOG.GET.INITIAL_PRODUCTORDERS" CATALOG_GET_PRODUCTORDER_IDS_BY_STATE: "jms:queue:CATALOG.GET.ACKNOWLEDGED_PRODUCTORDERS" +CATALOG_GET_PRODUCTCATALOGS: "jms:queue:CATALOG.GET.PRODUCTCATALOGS" +CATALOG_GET_PRODUCTCATALOG_BY_ID: "jms:queue:CATALOG.GET.PRODUCTCATALOG_BY_ID" +CATALOG_GET_PRODUCTCATALOG_BY_NAME: "jms:queue:CATALOG.GET.PRODUCTCATALOG_BY_NAME" +CATALOG_GET_PRODUCTCATEGORIES: "jms:queue:CATALOG.GET.PRODUCTCATEGORIES" +CATALOG_GET_PRODUCTCATEGORY_BY_ID: "jms:queue:CATALOG.GET.PRODUCTCATEGORY_BY_ID" +CATALOG_GET_PRODUCTOFFERINGS_BYCATEGORY_ID: "jms:queue:CATALOG.GET.PRODUCTOFFERINGS.CATEGORY_ID" +CATALOG_SEARCH_PRODUCTOFFERINGS: "jms:queue:CATALOG.CATALOG_SEARCH_PRODUCTOFFERINGS" #PM_MEASUREMENT_COLLECTION