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
35
36
37
38
39 package fr.paris.lutece.portal.service.progressmanager;
40
41 import fr.paris.lutece.portal.business.progressmanager.ProgressFeed;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.HashMap;
45 import java.util.List;
46 import java.util.Map;
47 import java.util.UUID;
48
49
50
51
52
53 public final class ProgressManagerService
54 {
55
56 private static ProgressManagerService _singleton;
57 private static Map<String, ProgressFeed> _progressFeeds;
58
59
60
61
62 private ProgressManagerService( )
63 {
64 }
65
66
67
68
69
70
71 public static synchronized ProgressManagerService getInstance( )
72 {
73 if ( _singleton == null )
74 {
75 _singleton = new ProgressManagerService( );
76 _progressFeeds = new HashMap<>( );
77 }
78 return _singleton;
79 }
80
81
82
83
84
85
86 public Map<String, ProgressFeed> getProgressFeeds( )
87 {
88 Map<String, ProgressFeed> feedMap = new HashMap<>( );
89 feedMap.putAll( _progressFeeds );
90
91 return feedMap;
92 }
93
94
95
96
97
98
99
100
101 public String registerFeed( String strProgressFeedName, int nTotalItems )
102 {
103 ProgressFeediness/progressmanager/ProgressFeed.html#ProgressFeed">ProgressFeed feed = new ProgressFeed( );
104 feed.setId( strProgressFeedName );
105 feed.setNbItemTotal( nTotalItems );
106
107 String strToken = UUID.randomUUID( ).toString( );
108 feed.setToken( strToken );
109
110 _progressFeeds.put( strToken, feed );
111
112 return feed.getToken( );
113 }
114
115
116
117
118
119
120 public void unRegisterFeed( String strFeedToken )
121 {
122 _progressFeeds.remove( strFeedToken );
123 }
124
125
126
127
128
129
130
131 public boolean isRegistred( String strFeedToken )
132 {
133 return ( _progressFeeds.get( strFeedToken ) != null );
134 }
135
136
137
138
139
140
141
142 public synchronized void incrementSuccess( String strFeedToken, int nSuccessItems )
143 {
144 if ( _progressFeeds.get( strFeedToken ) != null )
145 {
146 _progressFeeds.get( strFeedToken ).addSuccessItems( nSuccessItems );
147 }
148 }
149
150
151
152
153
154
155
156 public synchronized void incrementFailure( String strFeedToken, int nFailureItems )
157 {
158 if ( _progressFeeds.get( strFeedToken ) != null )
159 {
160 _progressFeeds.get( strFeedToken ).addFailureItems( nFailureItems );
161 }
162 }
163
164
165
166
167
168
169
170 public void addReport( String strFeedToken, String strReportLine )
171 {
172 if ( _progressFeeds.get( strFeedToken ) != null )
173 {
174 _progressFeeds.get( strFeedToken ).addReport( strReportLine );
175 }
176 }
177
178
179
180
181
182
183
184 public int getProgressStatus( String strFeedToken )
185 {
186 if ( _progressFeeds.get( strFeedToken ) != null )
187 {
188 ProgressFeed feed = _progressFeeds.get( strFeedToken );
189 if ( feed.getNbItemTotal( ) > 0 )
190 {
191 if ( feed.getNbItemTotal( ) <= feed.getNbItemSuccess( ) + feed.getNbItemFailure( ) )
192 {
193 return 100;
194 }
195 else
196 {
197 return (int) ( ( feed.getNbItemSuccess( ) + feed.getNbItemFailure( ) ) * 100.0 / feed.getNbItemTotal( ) + 0.5 );
198 }
199 }
200 }
201
202 return -1;
203 }
204
205
206
207
208
209
210
211 public int getSuccessNb( String strFeedToken )
212 {
213 if ( _progressFeeds.get( strFeedToken ) != null )
214 {
215 return _progressFeeds.get( strFeedToken ).getNbItemSuccess( );
216 }
217
218 return -1;
219 }
220
221
222
223
224
225
226
227 public int getFailureNb( String strFeedToken )
228 {
229 if ( _progressFeeds.get( strFeedToken ) != null )
230 {
231 return _progressFeeds.get( strFeedToken ).getNbItemFailure( );
232 }
233
234 return -1;
235 }
236
237
238
239
240
241
242
243 public List<String> getReport( String strFeedToken )
244 {
245 return getReport( strFeedToken, -1 );
246 }
247
248
249
250
251
252
253
254
255 public List<String> getReport( String strFeedToken, int iFromLine )
256 {
257 if ( _progressFeeds.get( strFeedToken ) != null )
258 {
259 List<String> reportList = _progressFeeds.get( strFeedToken ).getReportList( );
260
261 if ( iFromLine > 0 && iFromLine < reportList.size( ) )
262 {
263
264 ArrayList<String> clonedReport = (ArrayList<String>) ( (ArrayList<String>) reportList ).clone( );
265 return clonedReport.subList( iFromLine - 1, clonedReport.size( ) - 1 );
266 }
267 else
268 if ( iFromLine >= reportList.size( ) )
269 {
270 return new ArrayList<String>( );
271 }
272 else
273 {
274 return reportList;
275 }
276 }
277
278 return null;
279 }
280
281
282
283
284
285
286
287
288 public boolean initFeed( String strFeedToken, int nTotalItems )
289 {
290 if ( _progressFeeds.get( strFeedToken ) != null )
291 {
292 _progressFeeds.get( strFeedToken ).setNbItemTotal( nTotalItems );
293 return true;
294 }
295 else
296 {
297 return false;
298 }
299 }
300 }