--- /dev/null
+/*****************************************************************************
+ * Copyright Igor Barma, Eric Brun, Alexandre Desoubeaux, Christian Martel,
+ * (2 décembre 2008)
+ *
+ * Ce logiciel est un programme informatique servant à l'évaluation des
+ * compétences.
+ *
+ * Ce logiciel est régi par la licence CeCILL soumise au droit français et
+ * respectant les principes de diffusion des logiciels libres. Vous pouvez
+ * utiliser, modifier et/ou redistribuer ce programme sous les conditions
+ * de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
+ * sur le site "http://www.cecill.info".
+ *
+ * En contrepartie de l'accessibilité au code source et des droits de copie,
+ * de modification et de redistribution accordés par cette licence, il n'est
+ * offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons,
+ * seule une responsabilité restreinte pèse sur l'auteur du programme, le
+ * titulaire des droits patrimoniaux et les concédants successifs.
+ *
+ * A cet égard l'attention de l'utilisateur est attirée sur les risques
+ * associés au chargement, à l'utilisation, à la modification et/ou au
+ * développement et à la reproduction du logiciel par l'utilisateur étant
+ * donné sa spécificité de logiciel libre, qui peut le rendre complexe à
+ * manipuler et qui le réserve donc à des développeurs et des professionnels
+ * avertis possédant des connaissances informatiques approfondies. Les
+ * utilisateurs sont donc invités à charger et tester l'adéquation du
+ * logiciel à leurs besoins dans des conditions permettant d'assurer la
+ * sécurité de leurs systèmes et ou de leurs données et, plus généralement,
+ * à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.
+ *
+ * Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
+ * pris connaissance de la licence CeCILL, et que vous en avez accepté les
+ * termes.
+ *******************************************************************************/
+package com.pentila.evalcomp.utilities;
+
+import groovy.lang.Binding;
+import groovy.lang.GroovyShell;
+
+import java.util.Date;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
+import org.dozer.Mapper;
+
+import com.pentila.evalcomp.domain.definition.Property;
+import com.pentila.evalcomp.domain.service.ExtendActivity;
+import com.pentila.evalcomp.domain.service.ExtendTransition;
+import com.pentila.evalcomp.domain.service.Service;
+import com.pentila.evalcomp.domain.transit.Activity;
+import com.pentila.evalcomp.domain.transit.Process;
+import com.pentila.evalcomp.domain.transit.Transition;
+import com.pentila.evalcomp.manager.Beans;
+
+
+/**
+ * Classe utilitaire d'evalaution de condition groovy dans le contexte du processus
+ */
+public class InstanceUtil {
+
+
+ /** The logger. */
+ static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(InstanceUtil.class);
+
+ Binding binding;
+
+
+ public InstanceUtil(){
+ binding = new Binding();
+ }
+
+ /**
+ * Evaluation du contexte sur un Process.
+ */
+ public Process applyContext(Process process, Map<String, Object> map, List<String> options){
+
+
+ if (logger.isDebugEnabled()){
+ logger.debug("SET FOR PROCESS : "+ process.getId() + "(" +process.getSIID()+")");
+ }
+
+ // Tout d'abort on construit le contexte : binding
+
+ // La date courante
+ binding.setVariable("now", new Date());
+
+ // Les proriété de Processus
+ for (Property prop : process.getProperties()){
+ if (logger.isDebugEnabled()){
+ logger.debug("SETTING PROCESS PROPERTY : " + prop.getName() + " = " + prop.getValue());
+ }
+ binding.setVariable(prop.getName(), prop.getValue());
+
+ }
+
+ // Les variables du process coté EMa
+ if (map != null){
+ for (Entry<String, Object> m : map.entrySet()){
+ if (logger.isDebugEnabled()){
+ logger.debug("SETTING MAP PROPERTY : " + m.getKey() + " = " + m.getValue());
+ }
+ binding.setVariable(m.getKey(), m.getValue());
+
+ }
+ }
+
+ Set<Activity> sa = new HashSet<Activity>();
+
+ // Les deadLines
+ for (Activity a : process.getActivities()){
+
+ if (options != null && options.contains("DEADLINE_" + a.getId())){
+ continue;
+ }
+
+ Activity ac = applyContext(a);
+
+ if (ac != null){
+ sa.add(ac);
+ }
+
+ }
+
+ process.setActivities(sa);
+
+ return process;
+ }
+
+
+ /**
+ * Evaluation pour les activités
+ */
+ private Activity applyContext(Activity activity){
+
+
+ // Ajout des variables d'activités
+ for (Property prop : activity.getProperties()){
+ if (logger.isDebugEnabled()){
+ logger.debug("SETTING ACTIVITY PROPERTY = " + prop.getName() + " = " + prop.getValue());
+ }
+ if (binding.getVariable(prop.getName()) != null){
+
+ if (logger.isDebugEnabled()){
+ logger.debug("NOT SET BECAUSE NOT NULL !");
+ }
+ continue;
+ }
+
+ binding.setVariable(prop.getName(), prop.getValue());
+ }
+
+ ExtendActivity ea = switchLabel(activity.getExtActivity());
+
+ if (ea == null){
+
+ ea = new ExtendActivity();
+
+ ea.setId(activity.getId());
+ ea.setNamei18n(activity.getName());
+ ea.setDescriptioni18n(activity.getDescription());
+ ea.setTransitions(new HashSet<ExtendTransition>());
+ for (Transition t : activity.getTransitions()){
+ ExtendTransition et = new ExtendTransition();
+ et.setId(t.getId());
+ et.setDescriptioni18n(t.getDescription());
+ et.setNamei18n(t.getName());
+
+ ea.getTransitions().add(et);
+
+ }
+
+ Service es = new Service();
+
+ es.setTitlei18n(ea.getNamei18n());
+ es.setDescriptioni18n(ea.getDescriptioni18n());
+ ea.setService(es);
+
+
+
+ //return null;
+ }
+
+ activity.setExtActivity(ea);
+
+ return activity;
+ }
+
+
+
+ /**
+ * Evaluations des labels et conditions sur transitions
+ */
+ public ExtendActivity switchLabel(ExtendActivity eaModel){
+ if (eaModel == null){
+ return null;
+ }
+
+ // Recuperation du Bean Dozer
+ Mapper mapper = (Mapper)Beans.getApplicationContext().getBean("dozerMapper");
+
+ // Groovy pour l'evaluationserviceTitlei18n
+ GroovyShell groovyShell = new GroovyShell(binding);
+
+ //FIXME: Dozer ne support pas IWigdet alors on fait ca pour l'instant.
+
+
+ try{
+ eaModel.getService().setWidgets(null);
+ }catch(Exception e){
+
+ }
+ // On fait une copie du model
+ ExtendActivity ea = mapper .map(eaModel, ExtendActivity.class);
+
+ if (logger.isDebugEnabled()){
+
+ for (Object o : binding.getVariables().entrySet()){
+ logger.debug("OBJECT IN BINDING : " + o.toString());
+ }
+ }
+
+
+ String condition = ea.getCondition();
+
+ // Evaluation de la condition du l'activity
+ if (condition != null && condition.startsWith("/*scriptgroovy*/")){
+ boolean cond = (Boolean) groovyShell.evaluate(condition);
+
+ if(!cond){
+ return null;
+ }
+ }
+
+ // Evaluation sur les transitions
+ Set<ExtendTransition> sTrans = new HashSet<ExtendTransition>();
+
+ for (ExtendTransition transition : ea.getTransitions() ){
+
+ String tCondition = transition.getCondition();
+ if (logger.isDebugEnabled()){
+ logger.debug("Transistion condition :" + transition.getNamei18n()+ "=" + tCondition);
+ }
+ if(tCondition != null){
+ boolean cond = true;
+ try{
+ cond = (Boolean) groovyShell.evaluate(tCondition);
+ if (logger.isDebugEnabled()){
+ logger.debug("Transistion condition result :" + cond);
+ }
+ }catch (Exception e){
+ if (logger.isDebugEnabled()){
+ e.printStackTrace();
+ }
+ }
+ if (!cond){
+ continue;
+ }
+ }else{
+
+ }
+
+ sTrans.add(transition);
+
+
+ }
+
+ ea.setTransitions(sTrans);
+
+
+ // Evaluation des Labels , description etc ...
+
+ String serviceTitlei18n = ea.getService().getTitlei18n();
+ String serviceDescriptioni18n = ea.getService().getDescriptioni18n();
+ String descriptioni18n = ea.getDescriptioni18n();
+ String result = "";
+
+ if (logger.isDebugEnabled()){
+ logger.debug("serviceTitlei18n : "+serviceTitlei18n);
+ logger.debug("serviceDescriptioni18n : "+serviceDescriptioni18n);
+ logger.debug("descriptioni18n : "+descriptioni18n);
+ }
+
+
+ if ((serviceTitlei18n != null && serviceTitlei18n.startsWith("/*scriptgroovy*/")) || (descriptioni18n != null && descriptioni18n.startsWith("/*scriptgroovy*/")) || (serviceDescriptioni18n != null && serviceDescriptioni18n.startsWith("/*scriptgroovy*/"))){
+
+ if(serviceTitlei18n != null && serviceTitlei18n.startsWith("/*scriptgroovy*/")){
+ try{
+ result = (String)groovyShell.evaluate(serviceTitlei18n);
+ }catch (Exception e){
+ result = "ERROR : " + serviceTitlei18n;
+ }
+ ea.getService().setTitlei18n(result);
+ }
+
+ if(descriptioni18n != null && descriptioni18n.startsWith("/*scriptgroovy*/")){
+ try{
+ result = (String)groovyShell.evaluate(descriptioni18n);
+ }catch (Exception e){
+ result = "ERROR : " + descriptioni18n;
+ }
+ ea.setDescriptioni18n(result);
+ }
+
+ if(serviceDescriptioni18n != null && serviceDescriptioni18n.startsWith("/*scriptgroovy*/")){
+ try{
+ result = (String)groovyShell.evaluate(serviceDescriptioni18n);
+ }catch (Exception e){
+ result = "ERROR : " + serviceDescriptioni18n;
+ }
+ ea.getService().setDescriptioni18n(result);
+ }
+
+
+ }
+
+ return ea;
+
+ }
+
+
+
+}