~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/tablecreator/action/ReCreateTableAction.java

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hisp.dhis.reporting.tablecreator.action;
 
2
 
 
3
/*
 
4
 * Copyright (c) 2004-2007, University of Oslo
 
5
 * All rights reserved.
 
6
 *
 
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.
 
17
 *
 
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.
 
28
 */
 
29
 
 
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;
 
33
 
 
34
import java.util.ArrayList;
 
35
import java.util.Collection;
 
36
import java.util.Date;
 
37
import java.util.List;
 
38
 
 
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;
 
52
 
 
53
import com.opensymphony.xwork.Action;
 
54
 
 
55
/**
 
56
 * @author Lars Helge Overland
 
57
 * @version $Id$
 
58
 */
 
59
public class ReCreateTableAction
 
60
    implements Action
 
61
{
 
62
    private static final Log log = LogFactory.getLog( ReCreateTableAction.class );
 
63
    
 
64
    private static final String MODE_REPORT = "report";
 
65
 
 
66
    private static final String MODE_REPORT_TABLE = "table";
 
67
 
 
68
    // -------------------------------------------------------------------------
 
69
    // Dependencies
 
70
    // -------------------------------------------------------------------------
 
71
    
 
72
    private ProcessCoordinator processCoordinator;
 
73
 
 
74
    public void setProcessCoordinator( ProcessCoordinator processCoordinator )
 
75
    {
 
76
        this.processCoordinator = processCoordinator;
 
77
    }
 
78
    
 
79
    private CurrentUserService currentUserService;
 
80
 
 
81
    public void setCurrentUserService( CurrentUserService currentUserService )
 
82
    {
 
83
        this.currentUserService = currentUserService;
 
84
    }
 
85
 
 
86
    private ReportTableService reportTableService;
 
87
 
 
88
    public void setReportTableService( ReportTableService reportTableService )
 
89
    {
 
90
        this.reportTableService = reportTableService;
 
91
    }
 
92
 
 
93
    private ReportStore reportStore;
 
94
 
 
95
    public void setReportStore( ReportStore reportStore )
 
96
    {
 
97
        this.reportStore = reportStore;
 
98
    }
 
99
    
 
100
    private OrganisationUnitService organisationUnitService;
 
101
 
 
102
    public void setOrganisationUnitService( OrganisationUnitService organisationUnitService )
 
103
    {
 
104
        this.organisationUnitService = organisationUnitService;
 
105
    }
 
106
 
 
107
    private I18nFormat format;
 
108
 
 
109
    public void setFormat( I18nFormat format )
 
110
    {
 
111
        this.format = format;
 
112
    }
 
113
 
 
114
    // -------------------------------------------------------------------------
 
115
    // Input
 
116
    // -------------------------------------------------------------------------
 
117
 
 
118
    private Integer id;
 
119
 
 
120
    public void setId( Integer id )
 
121
    {
 
122
        this.id = id;
 
123
    }
 
124
 
 
125
    private String mode;
 
126
 
 
127
    public void setMode( String mode )
 
128
    {
 
129
        this.mode = mode;
 
130
    }
 
131
 
 
132
    private Integer reportingPeriod;
 
133
 
 
134
    public void setReportingPeriod( Integer reportingPeriod )
 
135
    {
 
136
        this.reportingPeriod = reportingPeriod;
 
137
    }
 
138
 
 
139
    private Integer parentOrganisationUnitId;
 
140
 
 
141
    public void setParentOrganisationUnitId( Integer parentOrganisationUnitId )
 
142
    {
 
143
        this.parentOrganisationUnitId = parentOrganisationUnitId;
 
144
    }
 
145
 
 
146
    private Integer organisationUnitId;
 
147
 
 
148
    public void setOrganisationUnitId( Integer organisationUnitId )
 
149
    {
 
150
        this.organisationUnitId = organisationUnitId;
 
151
    }
 
152
    
 
153
    // -------------------------------------------------------------------------
 
154
    // Action implementation
 
155
    // -------------------------------------------------------------------------
 
156
 
 
157
    public String execute()
 
158
        throws Exception
 
159
    {
 
160
        String owner = currentUserService.getCurrentUsername();
 
161
 
 
162
        ProcessExecutor executor = processCoordinator.newProcess( PROCESS_TYPE, owner );
 
163
        
 
164
        ReportTableInternalProcess process = (ReportTableInternalProcess) executor.getProcess();
 
165
 
 
166
        process.setDoDataMart( true );
 
167
 
 
168
        // ---------------------------------------------------------------------
 
169
        // Report parameters
 
170
        // ---------------------------------------------------------------------
 
171
 
 
172
        for ( ReportTable reportTable : getReportTables( id, mode ) )
 
173
        {
 
174
            // -----------------------------------------------------------------
 
175
            // Reporting period report parameter / current reporting period
 
176
            // -----------------------------------------------------------------
 
177
 
 
178
            Date date = null;
 
179
 
 
180
            if ( reportTable.getReportParams() != null && reportTable.getReportParams().isParamReportingMonth() )
 
181
            {
 
182
                reportTable.setRelativePeriods( reportTableService.getRelativePeriods( reportTable.getRelatives(), reportingPeriod ) );
 
183
                
 
184
                date = reportTableService.getDateFromPreviousMonth( reportingPeriod );
 
185
                
 
186
                log.info( "Reporting period: " + reportingPeriod );
 
187
            }
 
188
            else
 
189
            {
 
190
                reportTable.setRelativePeriods( reportTableService.getRelativePeriods( reportTable.getRelatives(), -1 ) );
 
191
                
 
192
                date = reportTableService.getDateFromPreviousMonth( -1 );
 
193
            }
 
194
 
 
195
            String reportingMonthName = format.formatPeriod( new MonthlyPeriodType().createPeriod( date ) );
 
196
            
 
197
            reportTable.setReportingMonthName( reportingMonthName );
 
198
 
 
199
            // -----------------------------------------------------------------
 
200
            // Parent organisation unit report parameter
 
201
            // -----------------------------------------------------------------
 
202
 
 
203
            if ( reportTable.getReportParams() != null && reportTable.getReportParams().isParamParentOrganisationUnit() )
 
204
            {
 
205
                OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( parentOrganisationUnitId );
 
206
 
 
207
                reportTable.setUnits( new ArrayList<OrganisationUnit>( organisationUnit.getChildren() ) );
 
208
                
 
209
                log.info( "Parent organisation unit: " + organisationUnit.getName() );
 
210
            }
 
211
 
 
212
            // -----------------------------------------------------------------
 
213
            // Organisation unit report parameter
 
214
            // -----------------------------------------------------------------
 
215
 
 
216
            if ( reportTable.getReportParams() != null && reportTable.getReportParams().isParamOrganisationUnit() )
 
217
            {
 
218
                OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( organisationUnitId );
 
219
                
 
220
                List<OrganisationUnit> organisationUnits = new ArrayList<OrganisationUnit>();
 
221
                organisationUnits.add( organisationUnit );
 
222
                reportTable.setUnits( organisationUnits );
 
223
                
 
224
                log.info( "Organisation unit: " + organisationUnit.getName() );
 
225
            }
 
226
 
 
227
            reportTable.setI18nFormat( format );
 
228
            reportTable.init();
 
229
 
 
230
            process.addReportTable( reportTable );
 
231
        }
 
232
 
 
233
        // ---------------------------------------------------------------------
 
234
        // Internal process execution
 
235
        // ---------------------------------------------------------------------
 
236
 
 
237
        processCoordinator.requestProcessExecution( executor );
 
238
 
 
239
        setCurrentRunningProcess( PROCESS_KEY_REPORT, executor.getId() );
 
240
 
 
241
        return SUCCESS;
 
242
    }
 
243
 
 
244
    // -------------------------------------------------------------------------
 
245
    // Action implementation
 
246
    // -------------------------------------------------------------------------
 
247
 
 
248
    /**
 
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.
 
252
     * 
 
253
     * @param id the identifier.
 
254
     * @param mode the mode.
 
255
     */
 
256
    private Collection<ReportTable> getReportTables( Integer id, String mode )
 
257
    {
 
258
        Collection<ReportTable> reportTables = new ArrayList<ReportTable>();
 
259
 
 
260
        if ( mode.equals( MODE_REPORT_TABLE ) )
 
261
        {
 
262
            reportTables.add( reportTableService.getReportTable( id ) );
 
263
        }
 
264
        else if ( mode.equals( MODE_REPORT ) )
 
265
        {
 
266
            reportTables = reportStore.getReport( id ).getReportTables();
 
267
        }
 
268
 
 
269
        return reportTables;
 
270
    }
 
271
}