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

org.ow2.bonita.util.CopyTool 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.
 * 
 * MOdified by Matthieu Chaffotte - BonitaSoft S.A.
 **/
package org.ow2.bonita.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.ow2.bonita.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;

/**
 * @author Pierre Vigneras
 */
public abstract class CopyTool {

  public static final String DEPLOYMENT_IDPREFIX = "deployment$";

  protected CopyTool() { }

  public static Set copy(final Set src) {
    if (src == null) {
      return new HashSet();
    }
    return new HashSet(src);
  }

  public static List copy(final List src) {
    if (src == null) {
      return new ArrayList();
    }
    return new ArrayList(src);
  }

  public static Map copy(final Map src) {
    if (src == null) {
      return new HashMap();
    }
    return new HashMap(src);
  }
  
  public static Map copyMap(final Map src) {
    if (src == null) {
      return new HashMap();
    }
    Map result = new HashMap();
    for (Map.Entry entry : src.entrySet()) {
      String key = entry.getKey();
      Object[] value = entry.getValue();
      Collection newValue = null;
      if (value != null) {
        newValue = new ArrayList();
        for (Object o : value) {
          newValue.add(o);
        }
      }
      result.put(key, newValue.toArray());
    }
    return result;
  }

  public static Date copy(final Date src) {
    if (src == null) {
      return null;
    }
    return (Date) src.clone();
  }

  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) {
      	String message = ExceptionManager.getInstance().getFullMessage("bafi_FCT_1", variableClass.getName());
        throw new BonitaRuntimeException(message);
      }
      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 String) {
      return value;
    } else {
      // TODO: throw exception ?
      return value;
    }
  }

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