/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package de.zell.texpad.camunda.resources;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import org.camunda.bpm.BpmPlatform;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.repository.ProcessDefinition;


/**
 *
 * @author Christopher Zell <christopher.zell@camunda.com>
 */
@Path("/Texpad")
public class TexpadServlet {

  private final UriBuilder builder = UriBuilder.fromResource(TexpadServlet.class);
  private static final Logger LOGGER = Logger.getLogger(TexpadServlet.class.getName());

  @POST
  public Response buildTexFile() {
    ProcessEngine engine = BpmPlatform.getProcessEngineService().getDefaultProcessEngine();
    LOGGER.log(Level.INFO, "Get ENGINE");

    ProcessDefinition processDefinition = engine.getRepositoryService().createProcessDefinitionQuery().processDefinitionKey("Texpad").singleResult();
    engine.getRuntimeService().startProcessInstanceById(processDefinition.getId());

    return Response.created(builder.clone().path("").build()).build();
  }

}

@@ -232,31 +232,49 @@
 ort 
-java.util.logging.Level
+de.zell.texpad.camunda.entities.TexEntity
 ;%0Aim
@@ -327,37 +327,179 @@
 .rs.
-POST;%0Aimport javax.ws.rs.Path
+Consumes;%0Aimport javax.ws.rs.InternalServerErrorException;%0Aimport javax.ws.rs.POST;%0Aimport javax.ws.rs.Path;%0Aimport javax.ws.rs.Produces;%0Aimport javax.ws.rs.core.MediaType
 ;%0Aim
@@ -682,35 +682,30 @@
 ne.r
-epository.ProcessDefinition
+untime.ProcessInstance
 ;%0A%0A%0A
@@ -784,17 +784,34 @@
 ath(
-%22/
 Texpad
-%22
+Servlet.PROC_DEF_KEY
 )%0Apu
@@ -838,16 +838,70 @@
 vlet %7B%0A%0A
+  public static final String PROC_DEF_KEY = %22Texpad%22;%0A
   privat
@@ -1078,37 +1078,302 @@
 T%0A  
-public Response buildTexFile(
+@Consumes(MediaType.APPLICATION_JSON)%0A  @Produces(MediaType.APPLICATION_JSON)%0A  public Response buildTexFile(TexEntity entity) %7B%0A%0A%0A    startProcessInstance(entity);%0A    return Response.created(builder.clone().path(%22%22).build()).build();%0A  %7D%0A%0A%0A  private void startProcessInstance(TexEntity entity
 ) %7B%0A
@@ -1472,47 +1472,109 @@
     
-LOGGER.log(Level.INFO, %22Get ENGINE
+if (engine == null)%0A      throw new InternalServerErrorException(%22No process engine available!
 %22);%0A%0A
+//
     
@@ -1693,24 +1693,28 @@
 tionKey(
-%22Texpad%22
+PROC_DEF_KEY
 ).single
@@ -1726,16 +1726,43 @@
 t();%0A   
+ ProcessInstance instance =
  engine.
@@ -1807,108 +1807,48 @@
 ceBy
-Id(processDefinition.getId());%0A%0A    return Response.created(builder.clone().path(%22%22).build()).build(
+Key(PROC_DEF_KEY, entity.getAttributes()
 );%0A 

improve texpad resource