diff options
Diffstat (limited to 'src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAOManager.java')
-rw-r--r-- | src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAOManager.java | 101 |
1 files changed, 80 insertions, 21 deletions
diff --git a/src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAOManager.java b/src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAOManager.java index fd4d2eb..77611b5 100644 --- a/src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAOManager.java +++ b/src/main/java/org/openecomp/dcae/inventory/daos/InventoryDAOManager.java @@ -1,30 +1,32 @@ -package org.openecomp.dcae.inventory.daos; - -/* - * ============LICENSE_START========================================== - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. - * =================================================================== +/*- + * ============LICENSE_START======================================================= + * dcae-inventory + * ================================================================================ + * Copyright (C) 2017 AT&T 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. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * + * 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============================================ - * - * ECOMP and OpenECOMP are trademarks - * and service marks of AT&T Intellectual Property. - * + * ============LICENSE_END========================================================= */ +package org.openecomp.dcae.inventory.daos; + +import org.openecomp.dcae.inventory.InventoryConfiguration; +import org.openecomp.dcae.inventory.dbthings.StringListArgument; +import io.dropwizard.jdbi.DBIFactory; +import io.dropwizard.setup.Environment; import org.skife.jdbi.v2.DBI; import org.skife.jdbi.v2.Handle; +import org.skife.jdbi.v2.util.BooleanMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -50,18 +52,54 @@ public final class InventoryDAOManager { return instance; } + public static class InventoryDAOManagerSetupException extends RuntimeException { + + public InventoryDAOManagerSetupException(String message) { + super(message); + } + + } + private final static Logger LOG = LoggerFactory.getLogger(InventoryDAOManager.class); // WATCH! Table creation order matters where mapping tables refer to other tables for foreign keys. private final static List<Class> DAO_CLASSES = Arrays.asList(DCAEServiceTypesDAO.class, DCAEServicesDAO.class, DCAEServiceComponentsDAO.class, DCAEServicesComponentsMapsDAO.class); private DBI jdbi; + private Environment environment; + private InventoryConfiguration configuration; private InventoryDAOManager() { } - public void init(DBI jdbi) { - this.jdbi = jdbi; + /** + * Setup the manager + * + * Saving the Dropwizard environment and configuration which are used to construct the DBI object in a later + * initialize call. This method can only be called once to be safe and to avoid runtime problems that could be + * caused if the global instance of this class gets into a weird state (Couldn't use Java's `final` qualifier). + * + * @param environment + * @param inventoryConfiguration + */ + public void setup(Environment environment, InventoryConfiguration inventoryConfiguration) { + if (this.environment == null && this.configuration == null) { + this.environment = environment; + this.configuration = inventoryConfiguration; + } else { + throw new InventoryDAOManagerSetupException("InventoryDAOManager setup can only be called once."); + } + } + + /** + * Initialize the manager + * + * Create the underlying validated DBI object that is used to manage database connections + */ + public void initialize() { + final DBIFactory factory = new DBIFactory(); + final DBI jdbi = factory.build(this.environment, this.configuration.getDataSourceFactory(), "dcae-database"); + jdbi.registerArgumentFactory(new StringListArgument()); for (Class<? extends InventoryDAO> daoClass : DAO_CLASSES) { final InventoryDAO dao = jdbi.onDemand(daoClass); @@ -73,6 +111,31 @@ public final class InventoryDAOManager { LOG.info(String.format("Sql table created: %s", daoClass.getSimpleName())); } } + + // CREATE VIEWS + // TODO: This doesn't belong here and is not consistent with the above approach. Make it better. + try (Handle jdbiHandle = jdbi.open()) { + String viewName = "dcae_service_types_latest"; + String checkQuery = String.format("select exists (select * from information_schema.tables where table_name = '%s')", + viewName); + + if (jdbiHandle.createQuery(checkQuery).map(BooleanMapper.FIRST).first()) { + LOG.info(String.format("Sql view exists: %s", viewName)); + } else { + StringBuilder sb = new StringBuilder(String.format("create view %s as ", viewName)); + sb.append("select s.* from dcae_service_types s "); + sb.append("join (select type_name, max(type_version) as max_version from dcae_service_types group by type_name) as f "); + sb.append("on s.type_name = f.type_name and s.type_version = f.max_version"); + + jdbiHandle.execute(sb.toString()); + LOG.info(String.format("Sql view created: %s", viewName)); + } + } catch (Exception e) { + throw new RuntimeException("", e); + } + + // Do this assignment at the end after performing table checks to ensure that connection is good + this.jdbi = jdbi; } private InventoryDAO getDAO(Class<? extends InventoryDAO> klass) { @@ -102,10 +165,6 @@ public final class InventoryDAOManager { return jdbi.onDemand(DCAEServiceTransactionDAO.class); } - public DCAEServiceTypeTransactionDAO getDCAEServiceTypeTransactionDAO() { - return jdbi.onDemand(DCAEServiceTypeTransactionDAO.class); - } - public DCAEServiceTypesDAO getDCAEServiceTypesDAO() { return (DCAEServiceTypesDAO) this.getDAO(DCAEServiceTypesDAO.class); } |