1
package org.hisp.dhis.reporting.tablecreator.action;
4
* Copyright (c) 2004-2007, University of Oslo
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions are met:
9
* * Redistributions of source code must retain the above copyright notice, this
10
* list of conditions and the following disclaimer.
11
* * Redistributions in binary form must reproduce the above copyright notice,
12
* this list of conditions and the following disclaimer in the documentation
13
* and/or other materials provided with the distribution.
14
* * Neither the name of the HISP project nor the names of its contributors may
15
* be used to endorse or promote products derived from this software without
16
* specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
import static org.hisp.dhis.reporttable.ReportTableInternalProcess.PROCESS_TYPE;
31
import static org.hisp.dhis.util.InternalProcessUtil.PROCESS_KEY_REPORT;
32
import static org.hisp.dhis.util.InternalProcessUtil.setCurrentRunningProcess;
34
import java.util.ArrayList;
35
import java.util.Collection;
36
import java.util.Date;
37
import java.util.List;
39
import org.amplecode.cave.process.ProcessCoordinator;
40
import org.amplecode.cave.process.ProcessExecutor;
41
import org.apache.commons.logging.Log;
42
import org.apache.commons.logging.LogFactory;
43
import org.hisp.dhis.i18n.I18nFormat;
44
import org.hisp.dhis.organisationunit.OrganisationUnit;
45
import org.hisp.dhis.organisationunit.OrganisationUnitService;
46
import org.hisp.dhis.period.MonthlyPeriodType;
47
import org.hisp.dhis.report.ReportStore;
48
import org.hisp.dhis.reporttable.ReportTable;
49
import org.hisp.dhis.reporttable.ReportTableInternalProcess;
50
import org.hisp.dhis.reporttable.ReportTableService;
51
import org.hisp.dhis.user.CurrentUserService;
53
import com.opensymphony.xwork.Action;
56
* @author Lars Helge Overland
59
public class ReCreateTableAction
62
private static final Log log = LogFactory.getLog( ReCreateTableAction.class );
64
private static final String MODE_REPORT = "report";
66
private static final String MODE_REPORT_TABLE = "table";
68
// -------------------------------------------------------------------------
70
// -------------------------------------------------------------------------
72
private ProcessCoordinator processCoordinator;
74
public void setProcessCoordinator( ProcessCoordinator processCoordinator )
76
this.processCoordinator = processCoordinator;
79
private CurrentUserService currentUserService;
81
public void setCurrentUserService( CurrentUserService currentUserService )
83
this.currentUserService = currentUserService;
86
private ReportTableService reportTableService;
88
public void setReportTableService( ReportTableService reportTableService )
90
this.reportTableService = reportTableService;
93
private ReportStore reportStore;
95
public void setReportStore( ReportStore reportStore )
97
this.reportStore = reportStore;
100
private OrganisationUnitService organisationUnitService;
102
public void setOrganisationUnitService( OrganisationUnitService organisationUnitService )
104
this.organisationUnitService = organisationUnitService;
107
private I18nFormat format;
109
public void setFormat( I18nFormat format )
111
this.format = format;
114
// -------------------------------------------------------------------------
116
// -------------------------------------------------------------------------
120
public void setId( Integer id )
127
public void setMode( String mode )
132
private Integer reportingPeriod;
134
public void setReportingPeriod( Integer reportingPeriod )
136
this.reportingPeriod = reportingPeriod;
139
private Integer parentOrganisationUnitId;
141
public void setParentOrganisationUnitId( Integer parentOrganisationUnitId )
143
this.parentOrganisationUnitId = parentOrganisationUnitId;
146
private Integer organisationUnitId;
148
public void setOrganisationUnitId( Integer organisationUnitId )
150
this.organisationUnitId = organisationUnitId;
153
// -------------------------------------------------------------------------
154
// Action implementation
155
// -------------------------------------------------------------------------
157
public String execute()
160
String owner = currentUserService.getCurrentUsername();
162
ProcessExecutor executor = processCoordinator.newProcess( PROCESS_TYPE, owner );
164
ReportTableInternalProcess process = (ReportTableInternalProcess) executor.getProcess();
166
process.setDoDataMart( true );
168
// ---------------------------------------------------------------------
170
// ---------------------------------------------------------------------
172
for ( ReportTable reportTable : getReportTables( id, mode ) )
174
// -----------------------------------------------------------------
175
// Reporting period report parameter / current reporting period
176
// -----------------------------------------------------------------
180
if ( reportTable.getReportParams() != null && reportTable.getReportParams().isParamReportingMonth() )
182
reportTable.setRelativePeriods( reportTableService.getRelativePeriods( reportTable.getRelatives(), reportingPeriod ) );
184
date = reportTableService.getDateFromPreviousMonth( reportingPeriod );
186
log.info( "Reporting period: " + reportingPeriod );
190
reportTable.setRelativePeriods( reportTableService.getRelativePeriods( reportTable.getRelatives(), -1 ) );
192
date = reportTableService.getDateFromPreviousMonth( -1 );
195
String reportingMonthName = format.formatPeriod( new MonthlyPeriodType().createPeriod( date ) );
197
reportTable.setReportingMonthName( reportingMonthName );
199
// -----------------------------------------------------------------
200
// Parent organisation unit report parameter
201
// -----------------------------------------------------------------
203
if ( reportTable.getReportParams() != null && reportTable.getReportParams().isParamParentOrganisationUnit() )
205
OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( parentOrganisationUnitId );
207
reportTable.setUnits( new ArrayList<OrganisationUnit>( organisationUnit.getChildren() ) );
209
log.info( "Parent organisation unit: " + organisationUnit.getName() );
212
// -----------------------------------------------------------------
213
// Organisation unit report parameter
214
// -----------------------------------------------------------------
216
if ( reportTable.getReportParams() != null && reportTable.getReportParams().isParamOrganisationUnit() )
218
OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( organisationUnitId );
220
List<OrganisationUnit> organisationUnits = new ArrayList<OrganisationUnit>();
221
organisationUnits.add( organisationUnit );
222
reportTable.setUnits( organisationUnits );
224
log.info( "Organisation unit: " + organisationUnit.getName() );
227
reportTable.setI18nFormat( format );
230
process.addReportTable( reportTable );
233
// ---------------------------------------------------------------------
234
// Internal process execution
235
// ---------------------------------------------------------------------
237
processCoordinator.requestProcessExecution( executor );
239
setCurrentRunningProcess( PROCESS_KEY_REPORT, executor.getId() );
244
// -------------------------------------------------------------------------
245
// Action implementation
246
// -------------------------------------------------------------------------
249
* If report table mode, this method will return the report table with the
250
* given identifier. If report mode, this method will return the report
251
* tables associated with the report.
253
* @param id the identifier.
254
* @param mode the mode.
256
private Collection<ReportTable> getReportTables( Integer id, String mode )
258
Collection<ReportTable> reportTables = new ArrayList<ReportTable>();
260
if ( mode.equals( MODE_REPORT_TABLE ) )
262
reportTables.add( reportTableService.getReportTable( id ) );
264
else if ( mode.equals( MODE_REPORT ) )
266
reportTables = reportStore.getReport( id ).getReportTables();