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

org.ow2.bonita.util.FullCopyTool Maven / Gradle / Ivy

/**
 * Copyright (C) 2007  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.
 **/
package org.ow2.bonita.util;

import java.util.HashMap;
import java.util.Map;

import org.ow2.bonita.pvm.PvmException;
import org.ow2.bonita.pvm.env.Environment;
import org.ow2.bonita.pvm.internal.type.Converter;
import org.ow2.bonita.pvm.internal.type.Type;
import org.ow2.bonita.pvm.internal.type.Variable;
import org.ow2.bonita.pvm.internal.type.VariableTypeResolver;
import org.ow2.bonita.pvm.internal.type.variable.NullVariable;
import org.ow2.bonita.pvm.internal.type.variable.UnpersistableVariable;
import org.ow2.bonita.facade.runtime.var.Enumeration;


/**
 * @author Guillaume Porcher
 *
 */
public abstract class FullCopyTool extends CopyTool {

  protected FullCopyTool() {
  }


  public static Map getVariableValues(final Map variableMap) {
    final Map res = new HashMap();
    if (variableMap != null) {
      for (final Variable var : variableMap.values()) {
        res.put(var.getKey(), var.getValue());
      }
    }
    return res;
  }

  public static Map createVariableMap(final Map variables) {
    if (variables == null || variables.isEmpty()) {
      return null;
    }
    final Map variableMap = new HashMap();
    for (final Map.Entry e : variables.entrySet()) {
      final Object value = e.getValue();
      variableMap.put(e.getKey(), createVariable(e.getKey(), value));
    }
    return variableMap;
  }

  /**
   * Copied from the pvm (in VariableMap)
   * TODO: factorize code
   */
  public static Variable createVariable(final String key, final Object value) {
    Type type = null;
    final Environment environment = Environment.getCurrent();
    if (environment != null) {
      final VariableTypeResolver variableTypeResolver = environment.get(VariableTypeResolver.class);
      if (variableTypeResolver != null) {
        type = variableTypeResolver.findTypeByMatch(key, value);
      }
    }
    Variable variable = null;
    if (type != null) {
      final Class< ? > variableClass = type.getVariableClass();
      try {
        variable = (Variable) variableClass.newInstance();
      } catch (final Exception e) {
        throw new PvmException("couldn't instantiate variable instance class '" + variableClass.getName() + "'");
      }
      final Converter converter = type.getConverter();
      variable.setConverter(converter);

    } else {
      if (value == null) {
        variable = new NullVariable();
      } else {
        variable = new UnpersistableVariable();
      }
    }

    variable.setKey(key);
    variable.setValue(getVariableValueCopy(value));
    return variable;
  }

  public static Object getVariableValueCopy(final Object value) {
    if (value instanceof Enumeration) {
      final Enumeration enumeration = (Enumeration) value;
      return new Enumeration(enumeration.getPossibleValues(), enumeration.getSelectedValue());
    } else if (value instanceof String) {
      return value;
    } else {
      // TODO: throw exception ?
      return value;
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy