package org.uberfire.backend.server.impl;

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

    @Inject
    private RepositoryService repositoryService;

    @PostConstruct
    public void assertPlayground() {
        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 );
                                                }} );
        }
    }

}

@@ -53,23 +53,74 @@
 va.util.
-HashMap
+ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.List
 ;%0Aimport
@@ -326,16 +326,105 @@
 upType;%0A
+import org.uberfire.backend.group.Group;%0Aimport org.uberfire.backend.group.GroupService;%0A
 import o
@@ -1246,16 +1246,154 @@
 1234%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
     @Inj
@@ -1446,16 +1446,68 @@
 rvice;%0A%0A
+    @Inject%0A    private GroupService groupService;%0A%0A
     @Pos
@@ -1550,24 +1550,100 @@
 yground() %7B%0A
+        final List%3CRepository%3E repositories = new ArrayList%3CRepository%3E();%0A%0A
         fina
@@ -2187,24 +2187,24 @@
 OUND_PWD );%0A
-
             
@@ -2254,16 +2254,341 @@
        %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    %7D%0A%0A

Add demo Group for use in revised Repository screens (cherry picked from commit bf24c043bbfe3f3bb9f3dc3918a4d10f07ea68a9)