1 /*
2 * Copyright (c) 2002-2020, City of Paris
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice
10 * and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice
13 * and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *
32 * License 1.0
33 */
34 package fr.paris.lutece.plugins.participatorybudget.deamon;
35
36 import java.lang.Thread.State;
37 import java.sql.Timestamp;
38 import java.util.Calendar;
39 import java.util.List;
40
41 import fr.paris.lutece.plugins.participatorybudget.business.bizstat.BizStatFile;
42 import fr.paris.lutece.plugins.participatorybudget.business.bizstat.BizStatFileHome;
43 import fr.paris.lutece.plugins.participatorybudget.service.bizstat.BizStartExportThread;
44 import fr.paris.lutece.portal.service.daemon.Daemon;
45
46 /**
47 * Daemon to send notification to users
48 */
49 public class BizStatExportDaemon extends Daemon
50 {
51
52 private BizStartExportThread _threadExport = null;
53
54 @Override
55 public void run( )
56 {
57 purge( );
58 export( );
59
60 }
61
62 // *********************************************************************************************
63 // * PURGE PURGE PURGE PURGE PURGE PURGE PURGE PURGE PURGE PURGE PURGE PURGE PURGE PURGE PURGE *
64 // * PURGE PURGE PURGE PURGE PURGE PURGE PURGE PURGE PURGE PURGE PURGE PURGE PURGE PURGE PURGE *
65 // *********************************************************************************************
66
67 private void purge( )
68 {
69
70 // Get all exports
71 List<BizStatFile> files = BizStatFileHome.findAllWithoutBinaryContent( );
72
73 // Calculating beginning of the current day
74 Calendar calendar = Calendar.getInstance( );
75 calendar.setTimeInMillis( System.currentTimeMillis( ) );
76 calendar.set( Calendar.HOUR_OF_DAY, 0 );
77 calendar.set( Calendar.MINUTE, 0 );
78 calendar.set( Calendar.SECOND, 0 );
79 calendar.set( Calendar.MILLISECOND, 0 );
80 Timestamp today = new Timestamp( calendar.getTimeInMillis( ) );
81
82 // Purging old ones
83 for ( BizStatFile file : files )
84 {
85 if ( file.getCreationDate( ).before( today ) )
86 {
87 file.setValue( null );
88 file.setStatus( BizStatFile.STATUS_PURGED );
89 BizStatFileHome.update( file );
90 }
91 }
92
93 }
94
95 // *********************************************************************************************
96 // * EXPORT EXPORT EXPORT EXPORT EXPORT EXPORT EXPORT EXPORT EXPORT EXPORT EXPORT EXPORT EXPOR *
97 // * EXPORT EXPORT EXPORT EXPORT EXPORT EXPORT EXPORT EXPORT EXPORT EXPORT EXPORT EXPORT EXPOR *
98 // *********************************************************************************************
99
100 private void export( )
101 {
102
103 if ( _threadExport == null )
104 {
105 // Creates a thread if does not exist
106 _threadExport = new BizStartExportThread( );
107 }
108 else
109 if ( _threadExport.getState( ) == State.TERMINATED )
110 {
111 // Creates a new thread
112 _threadExport = new BizStartExportThread( );
113
114 // Files stilling in a 'being processed' status are statued as 'error'
115 List<BizStatFile> files = BizStatFileHome.findByStatusWithoutBinaryContent( BizStatFile.STATUS_UNDER_TREATMENT );
116 for ( BizStatFile file : files )
117 {
118 file.setStatus( BizStatFile.STATUT_ERROR );
119 file.setError( "Still 'being processed' while the export daemon was terminated." );
120 BizStatFileHome.update( file );
121 }
122 }
123
124 if ( _threadExport.getState( ) == State.NEW )
125 {
126 // Launches the thread if it does not run
127 try
128 {
129 _threadExport.start( );
130 }
131 catch( Exception e )
132 {
133 _threadExport.interrupt( );
134 _threadExport = null;
135 }
136 }
137 }
138
139 }