package org.motrice.coordinatrice

import org.activiti.engine.runtime.Execution
import org.activiti.engine.runtime.ProcessInstance
import org.motrice.coordinatrice.pxd.PxdFormdefVer
import org.motrice.coordinatrice.pxd.PxdItem

/**
 * A BPMN process instance as implemented by Activiti.
 * This class is NOT PERSISTED, constructed read-only from the BPMN engine.
 */
class Procinst extends BpmnExecution {
  /**
   * Process definition.
   */
  Procdef procdef

  /**
   * Start form definition.
   */
  PxdFormdefVer formdef

  /**
   * Start form instance.
   */
  PxdItem forminst

  /**
   * Process to form connection.
   */
  MtfStartFormDefinition msfd

  /**
   * Process instance assignee.
   */
  String assignee

  /**
   * Process variables (transient).
   */
  Map variables

  /**
   * Business key: a unique label attached to a process instance.
   * It may be generated by the application or by Activiti.
   */
  String businessKey

  /**
   * Not a database object, never to be persisted
   */
  static mapWith = 'none'

  static transients = ['variables']
  static constraints = {
    procdef nullable: false
    formdef nullable: true
  }

  def assignFromExecution(Execution ex) {
    super.assignFromExecution(ex)
    def procInst = (ProcessInstance)ex
    def varMap = procInst.processVariables
    if (varMap) {
      variables = new TreeMap(varMap)
    } else {
      variables = [:]
    }
  }

  String toString() {
    "[Procinst(${processInstanceId}/${activityId}) ${procdef}, ${formdef}, ${forminst}, ${msfd} ${toStringDetails()}]"
  }

}
