/* 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 org.activiti.bpmn.converter; import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamWriter; import org.activiti.bpmn.model.BaseElement; import org.activiti.bpmn.model.FieldExtension; import org.activiti.bpmn.model.ImplementationType; import org.activiti.bpmn.model.ServiceTask; import org.apache.commons.lang.StringUtils; /** * @author Tijs Rademakers */ public class ServiceTaskXMLConverter extends BaseBpmnXMLConverter { public static String getXMLType() { return ELEMENT_TASK_SERVICE; } public static Class<? extends BaseElement> getBpmnElementType() { return ServiceTask.class; } @Override protected String getXMLElementName() { return ELEMENT_TASK_SERVICE; } @Override protected BaseElement convertXMLToElement(XMLStreamReader xtr) { ServiceTask serviceTask = new ServiceTask(); if (StringUtils.isNotEmpty(xtr.getAttributeValue(ACTIVITI_EXTENSIONS_NAMESPACE, ATTRIBUTE_TASK_SERVICE_CLASS))) { serviceTask.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_CLASS); serviceTask.setImplementation(xtr.getAttributeValue(ACTIVITI_EXTENSIONS_NAMESPACE, ATTRIBUTE_TASK_SERVICE_CLASS)); } else if (StringUtils.isNotEmpty(xtr.getAttributeValue(ACTIVITI_EXTENSIONS_NAMESPACE, ATTRIBUTE_TASK_SERVICE_EXPRESSION))) { serviceTask.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_EXPRESSION); serviceTask.setImplementation(xtr.getAttributeValue(ACTIVITI_EXTENSIONS_NAMESPACE, ATTRIBUTE_TASK_SERVICE_EXPRESSION)); } else if (StringUtils.isNotEmpty(xtr.getAttributeValue(ACTIVITI_EXTENSIONS_NAMESPACE, ATTRIBUTE_TASK_SERVICE_DELEGATEEXPRESSION))) { serviceTask.setImplementationType(ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION); serviceTask.setImplementation(xtr.getAttributeValue(ACTIVITI_EXTENSIONS_NAMESPACE, ATTRIBUTE_TASK_SERVICE_DELEGATEEXPRESSION)); } serviceTask.setResultVariableName(xtr.getAttributeValue(ACTIVITI_EXTENSIONS_NAMESPACE, ATTRIBUTE_TASK_SERVICE_RESULTVARIABLE)); serviceTask.setType(xtr.getAttributeValue(ACTIVITI_EXTENSIONS_NAMESPACE, ATTRIBUTE_TYPE)); parseChildElements(getXMLElementName(), serviceTask, xtr); return serviceTask; } @Override protected void writeAdditionalAttributes(BaseElement element, XMLStreamWriter xtw) throws Exception { ServiceTask serviceTask = (ServiceTask) element; if (ImplementationType.IMPLEMENTATION_TYPE_CLASS.equals(serviceTask.getImplementationType())) { writeQualifiedAttribute(ATTRIBUTE_TASK_SERVICE_CLASS, serviceTask.getImplementation(), xtw); } else if (ImplementationType.IMPLEMENTATION_TYPE_EXPRESSION.equals(serviceTask.getImplementationType())) { writeQualifiedAttribute(ATTRIBUTE_TASK_SERVICE_EXPRESSION, serviceTask.getImplementation(), xtw); } else if (ImplementationType.IMPLEMENTATION_TYPE_DELEGATEEXPRESSION.equals(serviceTask.getImplementationType())) { writeQualifiedAttribute(ATTRIBUTE_TASK_SERVICE_DELEGATEEXPRESSION, serviceTask.getImplementation(), xtw); } if (StringUtils.isNotEmpty(serviceTask.getResultVariableName())) { writeQualifiedAttribute(ATTRIBUTE_TASK_SERVICE_RESULTVARIABLE, serviceTask.getResultVariableName(), xtw); } if (StringUtils.isNotEmpty(serviceTask.getType())) { writeQualifiedAttribute(ATTRIBUTE_TYPE, serviceTask.getType(), xtw); } } @Override protected void writeAdditionalChildElements(BaseElement element, XMLStreamWriter xtw) throws Exception { ServiceTask serviceTask = (ServiceTask) element; for (FieldExtension fieldExtension : serviceTask.getFieldExtensions()) { if (StringUtils.isNotEmpty(fieldExtension.getFieldName())) { if (didWriteExtensionStartElement == false) { xtw.writeStartElement(ELEMENT_EXTENSIONS); didWriteExtensionStartElement = true; } xtw.writeStartElement(ACTIVITI_EXTENSIONS_PREFIX, ELEMENT_FIELD, ACTIVITI_EXTENSIONS_NAMESPACE); writeDefaultAttribute(ATTRIBUTE_FIELD_NAME, fieldExtension.getFieldName(), xtw); writeDefaultAttribute(ATTRIBUTE_FIELD_STRING, fieldExtension.getStringValue(), xtw); writeDefaultAttribute(ATTRIBUTE_FIELD_EXPRESSION, fieldExtension.getExpression(), xtw); xtw.writeEndElement(); } } } }