/*
 * Copyright 2014 Effektif GmbH.
 *
 * 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 com.effektif.workflow.api.workflow;

import com.effektif.workflow.api.bpmn.BpmnReader;
import com.effektif.workflow.api.bpmn.BpmnWriter;
import com.effektif.workflow.api.model.WorkflowId;
import com.effektif.workflow.api.workflow.diagram.Diagram;
import com.effektif.workflow.api.workflow.diagram.Node;

/**
 * @author Tom Baeyens
 */
public abstract class AbstractWorkflow extends Scope {

  protected WorkflowId id;
  protected Trigger trigger;
  protected Boolean enableCases;
  protected Diagram diagram;

  public abstract String getSourceWorkflowId();
  
  @Override
  public void readBpmn(BpmnReader r) {
    id = r.readIdAttributeBpmn("id", WorkflowId.class);
    super.readBpmn(r);
  }

  @Override
  public void writeBpmn(BpmnWriter w) {
    w.writeIdAttributeBpmn("id", id);
    w.writeStringAttributeBpmn("name", name);
  }

  public WorkflowId getId() {
    return this.id;
  }
  public void setId(WorkflowId id) {
    this.id = id;
  }
  public AbstractWorkflow id(WorkflowId id) {
    this.id = id;
    return this;
  }

  public Trigger getTrigger() {
    return this.trigger;
  }
  public void setTrigger(Trigger trigger) {
    this.trigger = trigger;
  }
  public AbstractWorkflow trigger(Trigger trigger) {
    this.trigger = trigger;
    return this;
  }
  
  public boolean isEnableCases() {
    return Boolean.TRUE.equals(this.enableCases);
  }
  public void setEnableCases(Boolean enableCases) {
    this.enableCases = enableCases;
  }
  /** enables cases, which means that each workflow instance will also create a corresponding case, 
   * which is a collaboration space around the tasks for a single workflow instance.
   * If enabled, cases will have the same id internal string value as the workflow instances ids. */
  public AbstractWorkflow enableCases() {
    this.enableCases = true;
    return this;
  }

  public Diagram getDiagram() {
    return diagram;
  }

  public void setDiagram(Diagram diagram) {
    this.diagram = diagram;
  }
}

@@ -632,16 +632,39 @@
 kflow;%0A%0A
+import java.util.Map;%0A%0A
 import c
@@ -701,24 +701,24 @@
 BpmnReader;%0A
-
 import com.e
@@ -868,64 +868,8 @@
 ram;
-%0Aimport com.effektif.workflow.api.workflow.diagram.Node;
 %0A%0A/*
@@ -1348,16 +1348,16 @@
 %22, id);%0A
-
     w.wr
@@ -1394,16 +1394,644 @@
  name);%0A
+    writeSimpleProperties(w);%0A  %7D%0A%0A  /**%0A   * Serialises properties with simple Java types as String values.%0A   */%0A  private void writeSimpleProperties(BpmnWriter w) %7B%0A    if (properties != null) %7B%0A      w.startExtensionElements();%0A      for (Map.Entry%3CString, Object%3E property : properties.entrySet()) %7B%0A        Class%3C?%3E type = property.getValue().getClass();%0A        boolean simpleType = type.isPrimitive() %7C%7C type.getName().startsWith(%22java.lang.%22);%0A        if (simpleType) %7B%0A          w.writeStringValue(%22property%22, property.getKey(), property.getValue().toString());%0A        %7D%0A      %7D%0A      w.endExtensionElements();%0A    %7D%0A
   %7D%0A%0A  p

Add simple workflow property values to BPMN export