blob: d478712c5e3bf2bf0c7e8fd7546798fe85b7fc35 [file] [log] [blame]
/***********************************************************************************************************************
* Copyright (c) 2010 Attensity Europe GmbH.
* All rights reserved.
* This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html
**********************************************************************************************************************/
package org.eclipse.smila.processing.designer.model.processor.util;
import javax.xml.namespace.QName;
import org.eclipse.bpel.model.Activity;
import org.eclipse.bpel.model.Process;
import org.eclipse.bpel.model.extensions.BPELActivitySerializer;
import org.eclipse.bpel.model.resource.BPELWriter;
import org.eclipse.emf.common.util.EList;
import org.eclipse.smila.processing.designer.model.processor.*;
import org.eclipse.smila.processing.designer.model.record.Annotation;
import org.eclipse.smila.processing.designer.model.record.AnnotationValue;
import org.eclipse.smila.processing.designer.model.record.util.RecordConstants;
import org.eclipse.smila.processing.designer.model.record.util.RecordUtils;
import org.w3c.dom.*;
/**
* Creates XML for the {@link InvokeService}s object representation.
*
* Copyright (c) 2010 Attensity Europe GmbH
*
* @author Lars Kunze
*/
public class InvokeServiceSerializer implements BPELActivitySerializer
{
@Override
public void marshall(QName elementType, Activity activity, Node parentNode, Process process, BPELWriter bpelWriter)
{
Document document = parentNode.getOwnerDocument();
/*
* InvokeService
*/
if (activity instanceof InvokeService)
{
InvokeService invokeService = (InvokeService) activity;
// (1) Create a new DOM element for our Activity
Element elementInvokeService =
document.createElementNS(elementType.getNamespaceURI(), ProcessorConstants.NODE_INVOKE_SERVICE);
elementInvokeService.setPrefix(ProcessorUtils.addNamespace(process));
invokeService.setElement(elementInvokeService);
// (2) Create child nodes
// (a) Node 'service'
Service service = invokeService.getService();
Element elementService = document.createElementNS(elementType.getNamespaceURI(), ProcessorConstants.NODE_SERVICE);
elementService.setPrefix(ProcessorUtils.addNamespace(process));
String name = service.getName();
elementService.setAttribute(ProcessorConstants.ATT_NAME, name == null ? "" : name);
elementInvokeService.appendChild(elementService);
service.setElement(elementService);
// (b) Node 'variables'
Variables variables = invokeService.getVariables();
Element elementVariables =
document.createElementNS(elementType.getNamespaceURI(), ProcessorConstants.NODE_VARIABLES);
elementVariables.setPrefix(ProcessorUtils.addNamespace(process));
elementVariables.setAttribute(ProcessorConstants.ATT_INPUT, variables.getInput());
elementVariables.setAttribute(ProcessorConstants.ATT_OUTPUT, variables.getOutput());
elementInvokeService.appendChild(elementVariables);
variables.setElement(elementVariables);
// (c) Node 'setAnnotations'
SetAnnotations setAnnotations = invokeService.getSetAnnotations();
if (setAnnotations != null)
{
Element elementSetAnnotations =
document.createElementNS(elementType.getNamespaceURI(), ProcessorConstants.NODE_SET_ANNOTATIONS);
elementSetAnnotations.setPrefix(ProcessorUtils.addNamespace(process));
elementInvokeService.appendChild(elementSetAnnotations);
createSetAnnotationsNode(document, elementType.getNamespaceURI(), elementSetAnnotations,
setAnnotations.getAnnotations(), process);
setAnnotations.setElement(elementSetAnnotations);
}
// (d) Add activity 'invokePipelet' to the parentNode
parentNode.appendChild(elementInvokeService);
// Print XML
// printXML(document, parentNode);
}
}
/**
* Creates the child elements ('An' and 'V') for the 'SetAnnotation' property (recursive method).
*
* @param document Document instance for creating new elements
* @param namespaceURI The namespaceURI for the new created elements
* @param elementParent The parent element to which the new created elements have to be appended
* @param annotations The list of annotations of the 'SetAnnotation' property
* @param process The model object Process. (Needed for node prefix determination)
*/
private void createSetAnnotationsNode(Document document, String namespaceURI, Element elementParent,
EList<Annotation> annotations, Process process)
{
for (Annotation annotation : annotations)
{
if (annotation != null && annotation.getName() != null)
{
// Node 'An'
Element elementAnnotation = document.createElementNS(namespaceURI, RecordConstants.NODE_AN);
elementAnnotation.setPrefix(RecordUtils.addNamespace(process));
elementAnnotation.setAttribute(RecordConstants.ATT_N, annotation.getName());
elementParent.appendChild(elementAnnotation);
annotation.setElement(elementAnnotation);
// Children 'V' of 'An'
if (annotation.getAnnotationValues() != null && annotation.getAnnotationValues().size() > 0)
{
// Nodes 'V'
for (AnnotationValue annotationValue : annotation.getAnnotationValues())
{
if (annotationValue != null && annotationValue.getName() != null && annotationValue.getValue() != null)
{
Element elementAnnotationValue = document.createElementNS(namespaceURI, RecordConstants.NODE_V);
elementAnnotationValue.setPrefix(RecordUtils.addNamespace(process));
elementAnnotationValue.setAttribute(RecordConstants.ATT_N, annotationValue.getName());
elementAnnotationValue.appendChild(document.createTextNode(annotationValue.getValue()));
elementAnnotation.appendChild(elementAnnotationValue);
annotationValue.setElement(elementAnnotationValue);
}
}
}
// Children 'An' of 'An'
if (annotation.getAnnotations() != null && annotation.getAnnotations().size() > 0)
{
createSetAnnotationsNode(document, namespaceURI, elementAnnotation, annotation.getAnnotations(), process);
}
}
}
}
}