/* 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.camunda.bpm.extension.elasticsearch.cockpit.demodata;

import static org.camunda.bpm.engine.authorization.Authorization.ANY;
import static org.camunda.bpm.engine.authorization.Authorization.AUTH_TYPE_GRANT;
import static org.camunda.bpm.engine.authorization.Permissions.ALL;

import org.camunda.bpm.engine.AuthorizationService;
import org.camunda.bpm.engine.IdentityService;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.authorization.Groups;
import org.camunda.bpm.engine.authorization.Resource;
import org.camunda.bpm.engine.authorization.Resources;
import org.camunda.bpm.engine.identity.Group;
import org.camunda.bpm.engine.identity.User;
import org.camunda.bpm.engine.impl.persistence.entity.AuthorizationEntity;
import org.springframework.beans.factory.InitializingBean;

/**
 * @author Daniel Meyer
 *
 */
public class DemoDataGenerator implements InitializingBean {

  protected ProcessEngine processEngine;

  public void afterPropertiesSet() throws Exception {

    System.out.println("Generating demo data");

    // start 10 process instances
    for (int i = 0; i < 10; i++) {
      processEngine.getRuntimeService()
        .startProcessInstanceByKey("generatedFormsQuickstart");
    }

    // ensure admin user exists
    IdentityService identityService = processEngine.getIdentityService();
    User user = identityService.createUserQuery().userId("demo").singleResult();
    if(user == null) {
      User newUser = identityService.newUser("demo");
      newUser.setPassword("demo");
      identityService.saveUser(newUser);
      System.out.println("Created used 'demo', password 'demo'");
      AuthorizationService authorizationService = processEngine.getAuthorizationService();

      // create group
      if(identityService.createGroupQuery().groupId(Groups.CAMUNDA_ADMIN).count() == 0) {
        Group camundaAdminGroup = identityService.newGroup(Groups.CAMUNDA_ADMIN);
        camundaAdminGroup.setName("camunda BPM Administrators");
        camundaAdminGroup.setType(Groups.GROUP_TYPE_SYSTEM);
        identityService.saveGroup(camundaAdminGroup);
      }

      // create ADMIN authorizations on all built-in resources
      for (Resource resource : Resources.values()) {
        if(authorizationService.createAuthorizationQuery().groupIdIn(Groups.CAMUNDA_ADMIN).resourceType(resource).resourceId(ANY).count() == 0) {
          AuthorizationEntity userAdminAuth = new AuthorizationEntity(AUTH_TYPE_GRANT);
          userAdminAuth.setGroupId(Groups.CAMUNDA_ADMIN);
          userAdminAuth.setResource(resource);
          userAdminAuth.setResourceId(ANY);
          userAdminAuth.addPermission(ALL);
          authorizationService.saveAuthorization(userAdminAuth);
        }
      }

      processEngine.getIdentityService()
      .createMembership("demo", Groups.CAMUNDA_ADMIN);
    }
  }



  public void setProcessEngine(ProcessEngine processEngine) {
    this.processEngine = processEngine;
  }

  public ProcessEngine getProcessEngine() {
    return processEngine;
  }

}

@@ -841,16 +841,117 @@
 s.ALL;%0A%0A
+import java.util.List;%0Aimport java.util.Random;%0Aimport java.util.Timer;%0Aimport java.util.TimerTask;%0A%0A
 import o
@@ -986,32 +986,32 @@
 izationService;%0A
-
 import org.camun
@@ -1413,16 +1413,57 @@
 Entity;%0A
+import org.camunda.bpm.engine.task.Task;%0A
 import o
@@ -1611,16 +1611,64 @@
 Bean %7B%0A%0A
+  private static final int maxInstances = 100;%0A%0A
   protec
@@ -1813,182 +1813,32 @@
     
-// start 10 process instances%0A    for (int i = 0; i %3C 10; i++) %7B%0A      processEngine.getRuntimeService()%0A        .startProcessInstanceByKey(%22generatedFormsQuicks
+scheduleInstanceS
 tart
-%22
+(
 );
-%0A    %7D
 %0A%0A  
@@ -3435,24 +3435,24 @@
 NDA_ADMIN);%0A
-
     %7D%0A  %7D%0A%0A%0A
@@ -3452,16 +3452,773 @@
 %0A  %7D%0A%0A%0A%0A
+  protected void scheduleInstanceStart() %7B%0A    new Timer(true).schedule(new TimerTask() %7B%0A      public void run() %7B%0A%0A        // start some process instances%0A        int rand = new Random().nextInt(maxInstances);%0A        for (int i = 0; i %3C rand; i++) %7B%0A          processEngine.getRuntimeService().startProcessInstanceByKey(%22generatedFormsQuickstart%22);%0A        %7D%0A%0A        // complete some process instances%0A        rand = new Random().nextInt(maxInstances);%0A        List%3CTask%3E tasks = processEngine.getTaskService().createTaskQuery()%0A          .listPage(0, rand);%0A        for (Task task : tasks) %7B%0A          processEngine.getTaskService().complete(task.getId());%0A        %7D%0A%0A        // reschedule%0A        scheduleInstanceStart();%0A      %7D%0A    %7D, 5000);%0A  %7D%0A%0A%0A%0A
   public

add timer