package org.jbpm.designer.uberfire.backend.server.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import org.kie.commons.services.cdi.Startup;
import org.kie.commons.services.cdi.StartupType;
import org.uberfire.backend.group.Group;
import org.uberfire.backend.group.GroupService;
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;
import org.uberfire.security.impl.authz.RuntimeAuthorizationManager;
import org.uberfire.security.server.cdi.SecurityFactory;

@ApplicationScoped
@Startup(StartupType.BOOTSTRAP)
public class AppSetup {

    private static final String JBPM_REPO_PLAYGROUND = "jbpm-playground";
    private static final String GUVNOR_REPO_PLAYGROUND = "uf-playground";
    // default repository section - start
    private static final String JBPM_URL = "https://github.com/guvnorngtestuser1/jbpm-console-ng-playground-kjar.git";
    private static final String GUVNOR_URL = "https://github.com/guvnorngtestuser1/guvnorng-playground.git";

    private final String userName = "guvnorngtestuser1";
    private final String password = "test1234";

    private static final String GLOBAL_SETTINGS = "settings";

    @Inject
    private RepositoryService repositoryService;

    @Inject
    private GroupService groupService;

    @Inject
    private ConfigurationService configurationService;

    @Inject
    private ConfigurationFactory configurationFactory;

    @PostConstruct
    public void onStartup() {
        try {
            SecurityFactory.setAuthzManager( new RuntimeAuthorizationManager() );

            Repository jbpmRepo = repositoryService.getRepository( JBPM_REPO_PLAYGROUND );
            if ( jbpmRepo == null ) {
                jbpmRepo = repositoryService.createRepository( "git",
                                                               JBPM_REPO_PLAYGROUND,
                                                               new HashMap<String, Object>() {{
                                                                   put( "origin", JBPM_URL );
                                                                   put( "username", userName );
                                                                   put( "crypt:password", password );
                                                               }} );
            }

            // TODO in case repo is not defined in system repository so we add default
            Repository guvnorRepo = repositoryService.getRepository( GUVNOR_REPO_PLAYGROUND );
            if ( guvnorRepo == null ) {
                guvnorRepo = repositoryService.createRepository( "git",
                                                                 GUVNOR_REPO_PLAYGROUND,
                                                                 new HashMap<String, Object>() {{
                                                                     put( "origin", GUVNOR_URL );
                                                                     put( "username", userName );
                                                                     put( "crypt:password", password );
                                                                 }} );
            }

            // TODO in case groups are not defined
            Collection<Group> groups = groupService.getGroups();
            if ( groups == null || groups.isEmpty() ) {
                final List<Repository> repositories = new ArrayList<Repository>();
                repositories.add( jbpmRepo );
                repositories.add( guvnorRepo );
                Group g = groupService.createGroup( "demo",
                                                    "demo@jbpm.org",
                                                    repositories );
            }

            //Define mandatory properties
            List<ConfigGroup> globalConfigGroups = configurationService.getConfiguration( ConfigType.GLOBAL );
            boolean globalSettingsDefined = false;
            for ( ConfigGroup globalConfigGroup : globalConfigGroups ) {
                if ( GLOBAL_SETTINGS.equals( globalConfigGroup.getName() ) ) {
                    globalSettingsDefined = true;
                    break;
                }
            }
            if ( !globalSettingsDefined ) {
                configurationService.addConfiguration( getGlobalConfiguration() );
            }
        } catch ( Exception e ) {
            throw new RuntimeException( "Error when starting designer " + e.getMessage(), e );
        }
    }

    private ConfigGroup getGlobalConfiguration() {
        //Global Configurations used by many of Drools Workbench editors
        final ConfigGroup group = configurationFactory.newConfigGroup( ConfigType.GLOBAL,
                                                                       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;
    }
}


@@ -401,60 +401,112 @@
 end.
-group.Group;%0Aimport org.uberfire.backend.group.Group
+organizationalunit.OrganizationalUnit;%0Aimport org.uberfire.backend.organizationalunit.OrganizationalUnit
 Serv
@@ -1741,26 +1741,52 @@
 ate 
-GroupService group
+OrganizationalUnitService organizationalUnit
 Serv
@@ -3724,21 +3724,35 @@
 in case 
-group
+Organizational Unit
 s are no
@@ -3788,90 +3788,168 @@
 ion%3C
-Group%3E groups = groupService.getGroups();%0A            if ( groups == null %7C%7C group
+OrganizationalUnit%3E organizationalUnits = organizationalUnitService.getOrganizationalUnits();%0A            if ( organizationalUnits == null %7C%7C organizationalUnit
 s.is
@@ -4157,121 +4157,241 @@
     
-Group g = groupService.createGroup( %22demo%22,%0A                                                    %22demo@jbpm.org%22,%0A
+OrganizationalUnit ou = organizationalUnitService.createOrganizationalUnit( %22demo%22,%0A                                                                                            %22demo@jbpm.org%22,%0A                                        
     

Rename Group to OrganizationalUnit