package org.camunda.bpm.pa;

import java.util.HashMap;
import java.util.Map;

import org.camunda.bpm.admin.impl.web.SetupResource;
import org.camunda.bpm.application.PostDeploy;
import org.camunda.bpm.application.ProcessApplication;
import org.camunda.bpm.application.impl.ServletProcessApplication;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.impl.ProcessEngineImpl;
import org.camunda.bpm.engine.rest.dto.identity.UserCredentialsDto;
import org.camunda.bpm.engine.rest.dto.identity.UserDto;
import org.camunda.bpm.engine.rest.dto.identity.UserProfileDto;
import org.camunda.bpm.pa.demo.InvoiceDemoDataGenerator;

/**
 *
 * @author nico.rehwaldt
 */
@ProcessApplication("camunda-test-processes")
public class DevProcessApplication extends ServletProcessApplication {

  @PostDeploy
  public void startProcesses(ProcessEngine engine) throws Exception {    
    createAdminDemoData(engine);    
    createTasklistDemoData(engine);
    createCockpitDemoData(engine);
  }

  private void createCockpitDemoData(ProcessEngine engine) throws Exception {
    RuntimeService runtimeService = engine.getRuntimeService();

    Map<String, Object> vars1 = new HashMap<String, Object>();
    vars1.put("booleanVar", true);
    runtimeService.startProcessInstanceByKey("ProcessWithExclusiveGateway", "secondUserTask", vars1);
    
    Map<String, Object> vars2 = new HashMap<String, Object>();
    vars2.put("booleanVar", false);
    runtimeService.startProcessInstanceByKey("ProcessWithExclusiveGateway", "firstUserTask", vars2);
    
    runtimeService.startProcessInstanceByKey("multipleFailingServiceTasks", "aBusinessKey");
    
    runtimeService.startProcessInstanceByKey("TwoParallelCallActivitiesCallingDifferentProcess");
    runtimeService.startProcessInstanceByKey("TwoParallelCallActivitiesCallingDifferentProcess");
    runtimeService.startProcessInstanceByKey("TwoParallelCallActivitiesCallingDifferentProcess");

    runtimeService.startProcessInstanceByKey("TwoParallelCallActivitiesCallingSameProcess");
    runtimeService.startProcessInstanceByKey("TwoParallelCallActivitiesCallingSameProcess");
    runtimeService.startProcessInstanceByKey("TwoParallelCallActivitiesCallingSameProcess");
    runtimeService.startProcessInstanceByKey("TwoParallelCallActivitiesCallingSameProcess");
    
    runtimeService.startProcessInstanceByKey("CallingCallActivity");
    runtimeService.startProcessInstanceByKey("CallingCallActivity");
    runtimeService.startProcessInstanceByKey("CallingCallActivity");
    runtimeService.startProcessInstanceByKey("CallingCallActivity");
    runtimeService.startProcessInstanceByKey("CallingCallActivity");
    runtimeService.startProcessInstanceByKey("CallingCallActivity");
    runtimeService.startProcessInstanceByKey("CallingCallActivity");
    
    runtimeService.startProcessInstanceByKey("OrderProcess");
    runtimeService.startProcessInstanceByKey("FailingProcess");
    runtimeService.startProcessInstanceByKey("FailingProcess");
    runtimeService.startProcessInstanceByKey("CallActivity");

    runtimeService.startProcessInstanceByKey("OrderProcess");
    runtimeService.startProcessInstanceByKey("OrderProcess");
    runtimeService.startProcessInstanceByKey("OrderProcess");
    runtimeService.startProcessInstanceByKey("OrderProcess");

    runtimeService.startProcessInstanceByKey("FailingProcess");
    runtimeService.startProcessInstanceByKey("FailingProcess");
    runtimeService.startProcessInstanceByKey("FailingProcess");
    runtimeService.startProcessInstanceByKey("FailingProcess");
    runtimeService.startProcessInstanceByKey("FailingProcess");
    runtimeService.startProcessInstanceByKey("FailingProcess");

    runtimeService.startProcessInstanceByKey("AnotherFailingProcess");
    runtimeService.startProcessInstanceByKey("AnotherFailingProcess");
    runtimeService.startProcessInstanceByKey("AnotherFailingProcess");
    runtimeService.startProcessInstanceByKey("AnotherFailingProcess");
    runtimeService.startProcessInstanceByKey("AnotherFailingProcess");
    runtimeService.startProcessInstanceByKey("AnotherFailingProcess");
    
    runtimeService.startProcessInstanceByKey("CallActivity");
    runtimeService.startProcessInstanceByKey("CallActivity");
    runtimeService.startProcessInstanceByKey("CallActivity");
    runtimeService.startProcessInstanceByKey("CallActivity");
    runtimeService.startProcessInstanceByKey("CallActivity");

    Map<String, Object> params = new HashMap<String, Object>();
    params.put("value1", "a");
    params.put("value2", "b");
    params.put("value3", "c");

    runtimeService.startProcessInstanceByKey("cornercasesProcess", params);
    runtimeService.startProcessInstanceByKey("cornercasesProcess", params);
    runtimeService.startProcessInstanceByKey("cornercasesProcess", params);
    runtimeService.startProcessInstanceByKey("cornercasesProcess", params);

    runtimeService.startProcessInstanceByKey("processWithSubProcess");
    runtimeService.startProcessInstanceByKey("processWithSubProcess");
    runtimeService.startProcessInstanceByKey("processWithSubProcess");
    runtimeService.startProcessInstanceByKey("processWithSubProcess");
    runtimeService.startProcessInstanceByKey("processWithSubProcess");
    runtimeService.startProcessInstanceByKey("processWithSubProcess");

    runtimeService.startProcessInstanceByKey("executionProcess");
    runtimeService.startProcessInstanceByKey("executionProcess");
    runtimeService.startProcessInstanceByKey("executionProcess");
    runtimeService.startProcessInstanceByKey("executionProcess");
    runtimeService.startProcessInstanceByKey("executionProcess");
    runtimeService.startProcessInstanceByKey("executionProcess");


    ((ProcessEngineImpl) engine).getProcessEngineConfiguration().getJobExecutor().start();
  }

  private void createAdminDemoData(ProcessEngine engine) throws Exception {
    UserDto user = new UserDto();
    UserProfileDto profile = new UserProfileDto();
    profile.setId("jonny1");
    profile.setFirstName("Jonny");
    profile.setLastName("Prosciutto");
    UserCredentialsDto credentials = new UserCredentialsDto();
    credentials.setPassword("jonny1");
    user.setProfile(profile);
    user.setCredentials(credentials);
        
    // manually perform setup
    new SetupResource().createInitialUser(engine.getName(), user);
  }

  private void createTasklistDemoData(ProcessEngine engine) {

    // create invoice demo data
    new InvoiceDemoDataGenerator().createDemoData(engine);
  }
}

@@ -922,20 +922,16 @@
 eption %7B
-    
 %0A    cre
@@ -955,20 +955,16 @@
 engine);
-    
 %0A    cre
@@ -1375,21 +1375,17 @@
 vars1);%0A
-    
 %0A
+
     Map%3C
@@ -1576,21 +1576,17 @@
 vars2);%0A
-    
 %0A
+
     runt
@@ -1666,29 +1666,25 @@
 inessKey%22);%0A
-    
 %0A
+
     runtimeS
@@ -2330,37 +2330,33 @@
 gSameProcess%22);%0A
-    
 %0A
+
     runtimeServi
@@ -2818,29 +2818,25 @@
 Activity%22);%0A
-    
 %0A
+
     runtimeS
@@ -4136,21 +4136,17 @@
 cess%22);%0A
-    
 %0A
+
     runt
@@ -5736,16 +5736,90 @@
 ss%22);%0A%0A%0A
+    runtimeService.startProcessInstanceByKey(%22exclusiveGatewayProcess%22);%0A%0A
     ((Pr
@@ -6344,16 +6344,8 @@
 s);%0A
-        
 %0A   

Fix a typo in a method comment