View Javadoc
1   package fr.paris.lutece.plugins.aot.modules.declarationimmat.utils;
2   
3   import java.util.List;
4   import java.util.Map;
5   import java.util.Map.Entry;
6   import java.util.Properties;
7   
8   import javax.el.ExpressionFactory;
9   import javax.el.ValueExpression;
10  
11  import org.apache.commons.lang3.StringUtils;
12  import org.springframework.expression.ExpressionParser;
13  import org.springframework.expression.spel.standard.SpelExpressionParser;
14  
15  import de.odysseus.el.ExpressionFactoryImpl;
16  import de.odysseus.el.util.SimpleContext;
17  
18  public class AOTStringUtils
19  {
20      public static String formatWithMap( String message, Map<String, Object> data )
21      {
22  
23          ExpressionFactory factory = new ExpressionFactoryImpl( new Properties( ), new AOTTypeConverter( ) );
24          SimpleContext context = new SimpleContext( );
25          context.putContext( javax.el.ExpressionFactory.class, factory );
26  
27          for ( Entry<String, Object> entry : data.entrySet( ) )
28          {
29              context.setVariable( entry.getKey( ), factory.createValueExpression( entry.getValue( ), entry.getValue( ).getClass( ) ) );
30          }
31          try
32          {
33              context.setFunction( "date", "nbJoursOuvres", CalculUtils.class.getMethod( "nbBusinessDaysBetweenTwoDate", String.class, String.class, String.class, String.class ) );
34              context.setFunction( "date", "nbJours", CalculUtils.class.getMethod( "nbDaysBetweenTwoDate", String.class, String.class, String.class, String.class ) );
35              context.setFunction( "date", "nbDemiJours", CalculUtils.class.getMethod( "nbHalfDaysBetweenTwoDate", String.class, String.class, String.class, String.class ) );
36              context.setFunction( "date", "nbDemiJoursOuvres", CalculUtils.class.getMethod( "nbHalfBusinessDaysBetweenTwoDate", String.class, String.class, String.class, String.class ) );
37              context.setFunction( "somme", "sommeLesIterations", CalculUtils.class.getMethod( "sumEachIteration", String.class, List.class ) );
38              context.setFunction( "date", "obtenirDate", CalculUtils.class.getMethod( "obtainDate", String.class, String.class, String.class, List.class ) );
39          }
40          catch ( NoSuchMethodException | SecurityException e )
41          {
42              // TODO Auto-generated catch block
43              e.printStackTrace( );
44          }
45  
46          ValueExpression expression = factory.createValueExpression( context, message, String.class );
47  
48          return ( String ) expression.getValue( context );
49      }
50  
51      public static String computeFormula( String formula )
52      {
53          String retour = null;
54  
55          if ( StringUtils.isNotBlank( formula ) )
56          {
57              ExpressionParser parser = new SpelExpressionParser( );
58              Double result = parser.parseExpression( formula ).getValue( Double.class );
59              retour = CalculUtils.getStringWithFormat( result );
60          }
61          return retour;
62      }
63  
64  }