1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.temporarycode.utils;
35
36 import java.io.IOException;
37 import java.sql.Timestamp;
38 import java.util.Calendar;
39
40 import org.apache.commons.lang3.RandomStringUtils;
41
42 import com.fasterxml.jackson.core.JsonGenerationException;
43 import com.fasterxml.jackson.databind.JsonMappingException;
44 import com.fasterxml.jackson.databind.ObjectMapper;
45
46 import fr.paris.lutece.plugins.temporarycode.business.EnumCharacterType;
47 import fr.paris.lutece.plugins.temporarycode.business.TemporaryCodeConfig;
48 import fr.paris.lutece.portal.service.util.AppLogService;
49 import fr.paris.lutece.util.json.AbstractJsonResponse;
50
51
52
53
54
55
56 public class TemporaryCodeUtils
57 {
58
59
60
61
62 private TemporaryCodeUtils ( )
63 {
64
65 }
66
67
68 private static ObjectMapper _mapper;
69
70
71
72
73
74
75 public static String generateCode( TemporaryCodeConfig config )
76 {
77 int length = config.getCodeLength( );
78 boolean useLetters = EnumCharacterType.ALPHANUMERIC.name( ).equals( config.getCharacterType( ) );
79 boolean useNumbers = EnumCharacterType.NUMERIC.name( ).equals( config.getCharacterType( ) );
80
81 if( useLetters )
82 {
83 useNumbers = true;
84 }
85
86 return RandomStringUtils.random( length, useLetters, useNumbers ).toLowerCase( );
87 }
88
89
90
91
92
93
94 public static Timestamp addMinuteToDate ( TemporaryCodeConfig config )
95 {
96 Calendar c = Calendar.getInstance( );
97 c.add( Calendar.MINUTE, + config.getValidityTime( ) );
98
99 return new Timestamp( c.getTime( ).getTime( ) );
100 }
101
102
103
104
105
106
107 public static String buildJsonResponse( AbstractJsonResponse jsonResponse )
108 {
109 String strJsonResponse = null;
110
111 if ( _mapper == null )
112 {
113 initMapper( );
114 }
115
116 try
117 {
118 strJsonResponse = _mapper.writeValueAsString( jsonResponse );
119 }
120 catch ( JsonGenerationException e )
121 {
122 AppLogService.error( e );
123 }
124 catch ( JsonMappingException e )
125 {
126 AppLogService.error( e );
127 }
128 catch ( IOException e )
129 {
130 AppLogService.error( e );
131 }
132
133 return strJsonResponse;
134 }
135
136 private static void initMapper( )
137 {
138 _mapper = new ObjectMapper( );
139
140 }
141 }