All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.bonita.parsing.binding.ParticipantBinding Maven / Gradle / Ivy

/**
 * Copyright (C) 2006  Bull S. A. S.
 * Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation
 * version 2.1 of the License.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA  02110-1301, USA.
 * 
 * Modified by Matthieu Chaffotte - BonitaSoft S.A.
 **/
package org.ow2.bonita.parsing.binding;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.ow2.bonita.definition.InstanceInitiator;
import org.ow2.bonita.util.ProcessBuilder;
import org.ow2.bonita.util.xml.Parse;
import org.ow2.bonita.util.xml.Parser;
import org.ow2.bonita.util.xml.XmlUtil;
import org.w3c.dom.Element;

/**
 * @author Marc Blachon, Guillaume Porcher, Charles Souillard, Miguel Valdes, Pierre Vigneras
 */
public class ParticipantBinding extends MajorElementBinding {

  private static final Logger LOG = Logger.getLogger(ParticipantBinding.class.getName());

  public ParticipantBinding() {
    super("Participant");
  }

  public Object parse(final Element participantElement, final Parse parse, final Parser parser) {
    if (LOG.isLoggable(Level.FINE)) {
      LOG.fine("parsing element = " + participantElement);
    }

    final String id = getId(participantElement);
    final String description = getChildTextContent(participantElement, "Description");
    
    ProcessBuilder processBuilder = parse.findObject(ProcessBuilder.class);
    
    String participantType = null;
    final Element participantTypeElement = XmlUtil.element(participantElement, "ParticipantType");
    if (participantElement != null) {
      participantType = XmlUtil.attribute(participantTypeElement, "Type");
    }
    if (participantType == null) {
      parse.addProblem("Participant: " + id
          + " does not define the mandatory Type attribute on ParticipantType element.");
    }

    if (participantType.equals("HUMAN")) {
      processBuilder.addHuman(id);  
    } else if (participantType.equals("ROLE")) {
      processBuilder.addGroup(id);
    }
    processBuilder.addDescription(description);
    processBuilder.addLabel(XmlUtil.attribute(participantElement, "Name"));
    if (participantType.equals("ROLE")) {
      parseRoleMapper(participantElement, parse, processBuilder, id);
    }
    return null;
  }

  protected void parseRoleMapper(final Element participantElement, final Parse parse, final ProcessBuilder processBuilder, final String roleId) {
    final Element mapperElement = getExtendedAttribute(participantElement, "Mapper");
    if (mapperElement != null) {
      final String value = XmlUtil.attribute(mapperElement, "Value");
      String className = null;
      if ("Custom".equals(value)) {
        final Element mapperClassNameElement = getExtendedAttribute(participantElement, "MapperClassName");
        className = XmlUtil.attribute(mapperClassNameElement, "Value");
      } else if ("Instance Initiator".equals(value)) {
        className = InstanceInitiator.class.getName();
      } else if ("Ldap".equals(value)) {
        className = "org.ow2.bonita.rolemapper.LdapRoleMapper";
      } else {
        parse.addProblem("Unsupported value on extendedAttribute Mapper: " + value);
      }
      processBuilder.addGroupResolver(className);
      Map parameters = getRoleMapperParameters(mapperElement, parse);
      if (parameters != null) {
        for (Entry parameter : parameters.entrySet()) {
          processBuilder.addParameter(parameter.getKey(), parameter.getValue());
        }
      }
      
    }
  }

  private Map getRoleMapperParameters(final Element participantElement, final Parse parse) {
    Map roleMapperParameters = null;
    final Element roleMapperParameterElement =
      XmlUtil.element(participantElement, "Parameters");
    if (roleMapperParameterElement != null) {
      roleMapperParameters = new HashMap();
      List parameters = XmlUtil.elements(roleMapperParameterElement);
      for (Element param : parameters) {
        String paramType = param.getLocalName();
        String variable = param.getAttribute("Var");
        String method;
        if (paramType.equals("InParameter")) {
          if (param.hasAttribute("Setter")) {
            method = param.getAttribute("Setter");
            if (method.length() != 0) {
              roleMapperParameters.put(method, new Object[]{variable});
            } else {
              parse.addProblem("The Setter attribute should not be empty!");
            }
          } else {
            parse.addProblem("InParameter need the Setter attribute");
          }
        } else if (paramType.equals("OutParameter")) {
          parse.addProblem("A RoleMapper cannot set process variables.");
        } else if (paramType.equals("Properties")) {
          if (variable.startsWith("file:")) {
            roleMapperParameters.put("file:",new Object[]{variable.substring(5)});
          } else if (variable.startsWith("bar:")) {
            roleMapperParameters.put("bar:",new Object[]{variable.substring(4)});
          } else if (variable.startsWith("resource:")) {
            roleMapperParameters.put("resource:",new Object[]{variable.substring(9)});
          } else {
            parse.addProblem("The value of Var attribute can only be either "
                + "file: or bar: or"
                + "resource:");
          }
        } // ignores all other tags
      }
    }
    return roleMapperParameters;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy