/*
 * Copyright 2017 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package org.jboss.forge.addon.springboot.commands.jpa;

import org.jboss.forge.addon.javaee.jpa.JPADataSource;
import org.jboss.forge.addon.javaee.jpa.JPAFacet;
import org.jboss.forge.addon.projects.Project;
import org.jboss.forge.addon.projects.ProjectFactory;
import org.jboss.forge.addon.projects.Projects;
import org.jboss.forge.addon.springboot.utils.DependencyHelper;
import org.jboss.forge.addon.ui.command.UICommand;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.context.UIExecutionContext;
import org.jboss.forge.addon.ui.result.Result;
import org.jboss.forge.addon.ui.wizard.UIWizardStep;

import javax.inject.Inject;
import java.util.Map;

/**
 * @author <a href="claprun@redhat.com">Christophe Laprun</a>
 */
public class AddDBJarDependencyCommand implements UICommand, UIWizardStep {
   @Inject
   private ProjectFactory factory;

   @Override
   public Result execute(UIExecutionContext context) throws Exception {
      final UIContext uiContext = context.getUIContext();
      final Project project = Projects.getSelectedProject(factory, uiContext);
      if (project == null) {
         throw new IllegalStateException("A project is required in the current context");
      }

      final Map<Object, Object> attributeMap = uiContext.getAttributeMap();
      final JPADataSource dataSource = (JPADataSource) attributeMap.get(JPADataSource.class);
      switch (dataSource.getDatabase()) {
         case H2:
            DependencyHelper.addDependency(project, "com.h2database", "h2").setScopeType("runtime");
            break;
         case DERBY:
            DependencyHelper.addDependency(project, "org.apache.derby", "derby").setScopeType("runtime");
            break;
         case HSQLDB:
            DependencyHelper.addDependency(project, "org.hsqldb", "hsqldb").setScopeType("runtime");
            break;
      }

      // erase persistence.xml
      project.getFacet(JPAFacet.class).getConfigFile().delete();

      return null;
   }
}

@@ -233,16 +233,124 @@
 s.jpa;%0A%0A
+import org.jboss.forge.addon.dependencies.Coordinate;%0Aimport org.jboss.forge.addon.javaee.jpa.DatabaseType;%0A
 import o
@@ -641,26 +641,96 @@
 t.utils.
-Dependency
+CollectionStringBuffer;%0Aimport org.jboss.forge.addon.springboot.utils.SpringBoot
 Helper;%0A
@@ -995,57 +995,174 @@
 ep;%0A
-%0Aimport javax.inject.Inject;%0Aimport java.util.Map
+import org.springframework.boot.jdbc.DatabaseDriver;%0A%0Aimport javax.inject.Inject;%0Aimport java.util.Map;%0A%0Aimport static org.jboss.forge.addon.javaee.jpa.DatabaseType.*
 ;%0A%0A/
@@ -1883,16 +1883,38 @@
     
-switch (
+final DatabaseType database = 
 data
@@ -1937,436 +1937,1452 @@
 se()
-) %7B
+;
 %0A      
-   case H2:%0A            DependencyHelper.addDependency(project, %22com.h2database%22, %22h2%22).setScopeType(%22runtime%22);%0A            break;%0A         case DERBY:%0A            DependencyHelper.addDependency(project, %22org.apache.derby%22, %22derby%22).setScopeType(%22runtime%22);%0A            break;%0A         case HSQLDB:%0A            DependencyHelper.addDependency(project, %22org.hsqldb%22, %22hsqldb%22).setScopeType(%22runtime%22);%0A            break
+final Coordinate driverCoordinate = database.getDriverCoordinate();%0A      // add driver dependency%0A      SpringBootHelper.addDependency(project, driverCoordinate.getGroupId(), driverCoordinate.getArtifactId())%0A            .setScopeType(%22runtime%22);%0A      // if we're not using H2, Derby or HSQL embedded databases, we also need to add DB info in application.properties%0A      if (!database.equals(H2) && !database.equals(DERBY) && !database.equals(HSQLDB)) %7B%0A%0A         final DatabaseDriver driver = DatabaseDriver.fromProductName(database.name());%0A         if (driver.equals(DatabaseDriver.UNKNOWN)) %7B%0A            // Spring Boot doesn't know about this DB%0A            throw new IllegalStateException(%22Unknown DB: %22 + dataSource);%0A         %7D%0A%0A         final CollectionStringBuffer buffer = new CollectionStringBuffer();%0A         final String jndiDataSource = dataSource.getJndiDataSource();%0A         if (jndiDataSource != null) %7B%0A            buffer.append(%22spring.datasource.jndi-name=%22 + jndiDataSource);%0A         %7D else %7B%0A            final String databaseURL = dataSource.getDatabaseURL();%0A            if (databaseURL != null) %7B%0A               buffer.append(%22spring.datasource.url=%22 + databaseURL);%0A            %7D else %7B%0A               throw new IllegalStateException(%22Must provide either a JNDI data source or a database connection URL!%22);%0A            %7D%0A         %7D%0A%0A         SpringBootHelper.writeToApplicationProperties(project, buffer)
 ;%0A  

Add DB information to application.properties when needed. Needs to be hardened and improved (can lead to duplicate entries in file)