package org.uberfire.backend.server.impl;

import java.util.ArrayList;
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;

//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(StartupType.BOOTSTRAP)
@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";

    private static final String PLAYGROUND_GROUP = "demo";
    private static final String PLAYGROUND_GROUP_OWNER = "demo@uberfire.com";

    @Inject
    private RepositoryService repositoryService;

    @Inject
    private GroupService groupService;

    @PostConstruct
    public void assertPlayground() {
        final List<Repository> repositories = new ArrayList<Repository>();

        final Repository repository = repositoryService.getRepository( PLAYGROUND_ALIAS );
        if ( repository == null ) {
            repositoryService.createRepository( PLAYGROUND_SCHEME, PLAYGROUND_ALIAS,
                                                new HashMap<String, Object>() {{
                                                    put( "origin", PLAYGROUND_ORIGIN );
                                                    put( "username", PLAYGROUND_UID );
                                                    put( "crypt:password", PLAYGROUND_PWD );
                                                }} );
        }

        repositories.add( repository );
        final Group group = groupService.getGroup( PLAYGROUND_GROUP );
        if ( group == null ) {
            groupService.createGroup( PLAYGROUND_GROUP,
                                      PLAYGROUND_GROUP_OWNER,
                                      repositories );
        }
    }

}

@@ -57,66 +57,15 @@
 til.
-ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.List
+HashMap
 ;%0Aim
@@ -279,97 +279,8 @@
 pe;%0A
-import org.uberfire.backend.group.Group;%0Aimport org.uberfire.backend.group.GroupService;%0A
 impo
@@ -1110,146 +1110,8 @@
 %22;%0A%0A
-    private static final String PLAYGROUND_GROUP = %22demo%22;%0A    private static final String PLAYGROUND_GROUP_OWNER = %22demo@uberfire.com%22;%0A%0A
     
@@ -1172,60 +1172,8 @@
 e;%0A%0A
-    @Inject%0A    private GroupService groupService;%0A%0A
     
@@ -1228,84 +1228,8 @@
 ) %7B%0A
-        final List%3CRepository%3E repositories = new ArrayList%3CRepository%3E();%0A%0A
     
@@ -1781,24 +1781,24 @@
 OUND_PWD );%0A
+
             
@@ -1852,333 +1852,8 @@
    %7D
-%0A%0A        repositories.add( repository );%0A        final Group group = groupService.getGroup( PLAYGROUND_GROUP );%0A        if ( group == null ) %7B%0A            groupService.createGroup( PLAYGROUND_GROUP,%0A                                      PLAYGROUND_GROUP_OWNER,%0A                                      repositories );%0A        %7D
 %0A   

Undo change to AppSetup, following explanation from maciejs (cherry picked from commit 0e7d9044b8c1fb4e448fd9a25a8ca7cf13ad8cd5)