From e2d69b23de157ab9a105a3b20f98c441106d204a Mon Sep 17 00:00:00 2001 From: Bogumil Zebek Date: Tue, 14 Jan 2020 12:00:51 +0100 Subject: Extract query Issue-ID: DCAEGEN2-2019 Signed-off-by: Zebek Bogumil Change-Id: I5f22589af67e11bf5bf7292f95df9f416007a15f --- .../api/impl/DcaeServiceTypesApiServiceImpl.java | 97 ++++------------------ 1 file changed, 17 insertions(+), 80 deletions(-) (limited to 'src/main/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImpl.java') diff --git a/src/main/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImpl.java b/src/main/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImpl.java index 13f1540..3a08daa 100644 --- a/src/main/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImpl.java +++ b/src/main/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImpl.java @@ -3,6 +3,7 @@ * dcae-inventory * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2020 Nokia Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -88,86 +89,15 @@ public class DcaeServiceTypesApiServiceImpl extends DcaeServiceTypesApiService { DateTime createdCutoff = DateTime.now(DateTimeZone.UTC); try (Handle jdbiHandle = InventoryDAOManager.getInstance().getHandle()) { - StringBuilder sb = new StringBuilder("select * from "); + final String queryStatement = DcaeServiceTypesQueryStatement.create(typeName, onlyLatest, onlyActive, + vnfType, serviceId, serviceLocation, asdcServiceId, asdcResourceId, owner, application, component); - if (onlyLatest) { - // Use the view which filters types that are of only the latest versions - sb.append("dcae_service_types_latest"); - } else { - sb.append("dcae_service_types"); - } - - List whereClauses = new ArrayList<>(); - - if (typeName != null) { - if (!typeName.contains("*")) { - whereClauses.add(":typeName = type_name"); - } - else { - typeName = typeName.replaceAll("\\*", "%"); - whereClauses.add("type_name LIKE :typeName"); - } - } - - if (vnfType != null) { - whereClauses.add("lower(:vnfType) = any(lower(vnf_types\\:\\:text)\\:\\:text[])"); - } - - if (serviceId != null) { - whereClauses.add("(:serviceId = any(service_ids) or service_ids = \'{}\' or service_ids is null)"); - } - - if (serviceLocation != null) { - whereClauses.add("(:serviceLocation = any(service_locations) or service_locations = \'{}\' or service_locations is null)"); - } - - if (asdcServiceId != null) { - if (asdcServiceId.equalsIgnoreCase("NONE")) { - whereClauses.add("asdc_service_id is null"); - } else { - whereClauses.add(":asdcServiceId = asdc_service_id"); - } - } - - if (asdcResourceId != null) { - if (asdcResourceId.equalsIgnoreCase("NONE")) { - whereClauses.add("asdc_resource_id is null"); - } else { - whereClauses.add(":asdcResourceId = asdc_resource_id"); - } - } - - if (owner != null) { - whereClauses.add(":owner = owner"); - } - - if (application != null) { - whereClauses.add(":application = application"); - } - - if (component != null) { - whereClauses.add(":component = component"); - } - - whereClauses.add("created < :createdCutoff"); - - if (onlyActive) { - whereClauses.add("deactivated is null"); - } - - if (!whereClauses.isEmpty()) { - sb.append(" where "); - sb.append(String.join(" and ", whereClauses)); - } + metricsLogger.info("Query created as: {}." + queryStatement); - // Sort by created timestamp - always descending. - sb.append(" order by created desc"); - - metricsLogger.info("Query created as: {}." + sb.toString()); - - Query query = jdbiHandle.createQuery(sb.toString()).map(new DCAEServiceTypeObjectMapper()); + Query query = jdbiHandle.createQuery(queryStatement).map(new DCAEServiceTypeObjectMapper()); if (typeName != null) { + typeName = resolveTypeName(typeName); query.bind("typeName", typeName); } @@ -190,15 +120,15 @@ public class DcaeServiceTypesApiServiceImpl extends DcaeServiceTypesApiService { if (asdcResourceId != null && !"NONE".equalsIgnoreCase(asdcResourceId)) { query.bind("asdcResourceId", asdcResourceId); } - + if (application != null) { query.bind("application", application); } - + if (component != null) { query.bind("component", component); } - + if (owner != null) { query.bind("owner", owner); } @@ -246,7 +176,7 @@ public class DcaeServiceTypesApiServiceImpl extends DcaeServiceTypesApiService { response.setLinks(navigationLinks); debugLogger.debug("Response: {}." + response.toString()); - + return Response.ok().entity(response).build(); } @@ -263,6 +193,13 @@ public class DcaeServiceTypesApiServiceImpl extends DcaeServiceTypesApiService { return Response.ok().entity(createDCAEServiceType(serviceTypeObject, uriInfo)).build(); } + static String resolveTypeName(String typeName){ + if (typeName.contains("*")) { + return typeName.replaceAll("\\*", "%"); + } + return typeName; + } + /** * Create a DCAE service type database object * -- cgit 1.2.3-korg