/*
 * Copyright 2012 JBoss Inc
 *
 * 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.
 */

package org.kie.guvnor.backend.server;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import org.kie.commons.services.cdi.Startup;
import org.uberfire.backend.repositories.Repository;
import org.uberfire.backend.repositories.RepositoryService;
import org.uberfire.backend.server.config.ConfigGroup;
import org.uberfire.backend.server.config.ConfigType;
import org.uberfire.backend.server.config.ConfigurationFactory;
import org.uberfire.backend.server.config.ConfigurationService;

//This is a temporary solution when running in PROD-MODE as /webapp/.niogit/system.git folder
//is not deployed to the Application Servers /bin folder. This will be remedied when an
//installer is written to create the system.git repository in the correct location.
@Startup
@ApplicationScoped
public class AppSetup {

    private static final String PLAYGROUND_SCHEME = "git";
    private static final String PLAYGROUND_ALIAS = "uf-playground";
    private static final String PLAYGROUND_ORIGIN = "https://github.com/guvnorngtestuser1/guvnorng-playground.git";
    private static final String PLAYGROUND_UID = "guvnorngtestuser1";
    private static final String PLAYGROUND_PWD = "test1234";

    @Inject
    private RepositoryService repositoryService;

    @Inject
    private ConfigurationService configurationService;

    @Inject
    private ConfigurationFactory configurationFactory;

    @PostConstruct
    public void assertPlayground() {
        final Repository repository = repositoryService.getRepository( PLAYGROUND_ALIAS );
        if ( repository == null ) {
            repositoryService.cloneRepository( PLAYGROUND_SCHEME,
                                               PLAYGROUND_ALIAS,
                                               PLAYGROUND_ORIGIN,
                                               PLAYGROUND_UID,
                                               PLAYGROUND_PWD );
            configurationService.addConfiguration( getConfiguration() );
        }
    }

    private ConfigGroup getConfiguration() {
        final ConfigGroup group = configurationFactory.newConfigGroup( ConfigType.GLOBAL,
                                                                       "settings",
                                                                       "" );
        group.addConfigItem( configurationFactory.newConfigItem( "drools.dateformat",
                                                                 "dd-MMM-yyyy" ) );
        group.addConfigItem( configurationFactory.newConfigItem( "drools.datetimeformat",
                                                                 "dd-MMM-yyyy hh:mm:ss" ) );
        group.addConfigItem( configurationFactory.newConfigItem( "drools.defaultlanguage",
                                                                 "en" ) );
        group.addConfigItem( configurationFactory.newConfigItem( "drools.defaultcountry",
                                                                 "US" ) );
        group.addConfigItem( configurationFactory.newConfigItem( "build.enable-incremental",
                                                                 "true" ) );
        group.addConfigItem( configurationFactory.newConfigItem( "rule-modeller-onlyShowDSLStatements",
                                                                 "false" ) );
        return group;
    }

}

@@ -629,16 +629,39 @@
 erver;%0A%0A
+import java.util.List;%0A
 import j
@@ -1486,16 +1486,58 @@
 etup %7B%0A%0A
+    // default repository section - start%0A
     priv
@@ -1552,32 +1552,42 @@
 ic final String 
+DROOLS_WB_
 PLAYGROUND_SCHEM
@@ -1621,32 +1621,42 @@
 ic final String 
+DROOLS_WB_
 PLAYGROUND_ALIAS
@@ -1699,32 +1699,42 @@
 ic final String 
+DROOLS_WB_
 PLAYGROUND_ORIGI
@@ -1825,32 +1825,42 @@
 ic final String 
+DROOLS_WB_
 PLAYGROUND_UID =
@@ -1913,16 +1913,26 @@
  String 
+DROOLS_WB_
 PLAYGROU
@@ -1953,16 +1953,119 @@
 1234%22;%0A%0A
+    private static final String GLOBAL_SETTINGS = %22settings%22;%0A    // default repository section - end%0A%0A
     @Inj
@@ -2306,24 +2306,107 @@
 yground() %7B%0A
+        // TODO in case repo is not defined in system repository so we add default%0A
         fina
@@ -2456,32 +2456,42 @@
 .getRepository( 
+DROOLS_WB_
 PLAYGROUND_ALIAS
@@ -2577,16 +2577,26 @@
 sitory( 
+DROOLS_WB_
 PLAYGROU
@@ -2645,32 +2645,42 @@
                 
+DROOLS_WB_
 PLAYGROUND_ALIAS
@@ -2720,32 +2720,42 @@
                 
+DROOLS_WB_
 PLAYGROUND_ORIGI
@@ -2796,32 +2796,42 @@
                 
+DROOLS_WB_
 PLAYGROUND_UID,%0A
@@ -2877,16 +2877,26 @@
         
+DROOLS_WB_
 PLAYGROU
@@ -2901,24 +2901,542 @@
 OUND_PWD );%0A
+        %7D%0A%0A        // TODO in case mandatory properties are not defined in system repository so we add default%0A        List%3CConfigGroup%3E configGroups = configurationService.getConfiguration( ConfigType.GLOBAL );%0A        boolean globalSettingsDefined = false;%0A        for ( ConfigGroup configGroup : configGroups ) %7B%0A            if ( GLOBAL_SETTINGS.equals( configGroup.getName() ) ) %7B%0A                globalSettingsDefined = true;%0A                break;%0A            %7D%0A        %7D%0A        if ( !globalSettingsDefined ) %7B%0A
             
@@ -3473,24 +3473,30 @@
 uration( get
+Global
 Configuratio
@@ -3546,16 +3546,22 @@
 roup get
+Global
 Configur

Make programmartic bootstrap consistent with kie-ide, to ease identifying necessary steps