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

« back to all changes in this revision

Viewing changes to dhis-2/dhis-services/dhis-service-importexport/src/main/java/org/hisp/dhis/importexport/dxf/converter/ReportTableConverter.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.importexport.dxf.converter;
 
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 java.util.Collection;
 
31
import java.util.Map;
 
32
 
 
33
import org.amplecode.staxwax.reader.XMLReader;
 
34
import org.amplecode.staxwax.writer.XMLWriter;
 
35
import org.hisp.dhis.jdbc.BatchHandler;
 
36
import org.hisp.dhis.importexport.ExportParams;
 
37
import org.hisp.dhis.importexport.GroupMemberType;
 
38
import org.hisp.dhis.importexport.ImportObjectService;
 
39
import org.hisp.dhis.importexport.ImportParams;
 
40
import org.hisp.dhis.importexport.XMLConverter;
 
41
import org.hisp.dhis.importexport.converter.AbstractReportTableConverter;
 
42
import org.hisp.dhis.importexport.mapping.NameMappingUtil;
 
43
import org.hisp.dhis.reporttable.RelativePeriods;
 
44
import org.hisp.dhis.reporttable.ReportParams;
 
45
import org.hisp.dhis.reporttable.ReportTable;
 
46
import org.hisp.dhis.reporttable.ReportTableStore;
 
47
 
 
48
/**
 
49
 * @author Lars Helge Overland
 
50
 * @version $Id$
 
51
 */
 
52
public class ReportTableConverter
 
53
    extends AbstractReportTableConverter implements XMLConverter
 
54
{
 
55
    public static final String COLLECTION_NAME = "reportTables";
 
56
    public static final String ELEMENT_NAME = "reportTable";
 
57
 
 
58
    private static final String FIELD_ID = "id";
 
59
    private static final String FIELD_NAME = "name";
 
60
    private static final String FIELD_TABLE_NAME = "tableName";
 
61
    private static final String FIELD_EXISTING_TABLE_NAME = "existingTableName";
 
62
    private static final String FIELD_MODE = "mode";
 
63
    private static final String FIELD_REGRESSION = "regression";
 
64
            
 
65
    private static final String FIELD_DO_INDICATORS = "doIndicators";
 
66
    private static final String FIELD_DO_CATEGORY_OPTION_COMBOS = "doCategoryOptionCombos";
 
67
    private static final String FIELD_DO_PERIODS = "doPeriods";
 
68
    private static final String FIELD_DO_ORGANISATION_UNITS = "doOrganisationUnits";
 
69
    
 
70
    private static final String FIELD_REPORTING_MONTH = "reportingMonth";
 
71
    private static final String FIELD_LAST_3_MONTHS = "last3Months";
 
72
    private static final String FIELD_LAST_6_MONTHS = "last6Months";
 
73
    private static final String FIELD_LAST_9_MONTHS = "last9Months";
 
74
    private static final String FIELD_LAST_12_MONTHS = "last12Months";
 
75
    private static final String FIELD_SO_FAR_THIS_YEAR = "soFarThisYear";
 
76
    private static final String FIELD_SO_FAR_THIS_FINANCIAL_YEAR = "soFarThisFinancialYear";
 
77
    private static final String FIELD_LAST_3_TO_6_MONTHS = "last3To6Months";
 
78
    private static final String FIELD_LAST_6_TO_9_MONTHS = "last6To9Months";
 
79
    private static final String FIELD_LAST_9_TO_12_MONTHS = "last9To12Months";
 
80
    private static final String FIELD_LAST_12_INDIVIDUAL_MONHTS = "last12IndividualMonths";
 
81
    private static final String FIELD_INDIVIDUAL_MONTHS_THIS_YEAR = "individualMonthsThisYear";
 
82
    private static final String FIELD_INDIVIDUAL_QUARTERS_THIS_YEAR = "individualQuartersThisYear";
 
83
    
 
84
    private static final String FIELD_PARAM_REPORTING_MONTH = "paramReportingMonth";
 
85
    private static final String FIELD_PARAM_PARENT_ORG_UNIT = "paramParentOrganisationUnit";
 
86
    private static final String FIELD_PARAM_ORG_UNIT = "paramOrganisationUnit";
 
87
        
 
88
    // -------------------------------------------------------------------------
 
89
    // Constructor
 
90
    // -------------------------------------------------------------------------
 
91
 
 
92
    /**
 
93
     * Constructor for write operations.
 
94
     */
 
95
    public ReportTableConverter()
 
96
    {   
 
97
    }
 
98
 
 
99
    /**
 
100
     * Constructor for read operations.
 
101
     * 
 
102
     * @param batchHandler BatchHandler
 
103
     * @param reportTableStore ReportTableStore
 
104
     * @param importObjectService ImportObjectService
 
105
     */
 
106
    public ReportTableConverter( BatchHandler batchHandler, 
 
107
        ReportTableStore reportTableStore,
 
108
        ImportObjectService importObjectService )
 
109
    {
 
110
        this.batchHandler = batchHandler;
 
111
        this.reportTableStore = reportTableStore;
 
112
        this.importObjectService = importObjectService;
 
113
    }
 
114
 
 
115
    // -------------------------------------------------------------------------
 
116
    // XMLConverter implementation
 
117
    // -------------------------------------------------------------------------
 
118
 
 
119
    public void write( XMLWriter writer, ExportParams params )
 
120
    {
 
121
        Collection<ReportTable> reportTables = params.getReportTables();
 
122
        
 
123
        if ( reportTables != null && reportTables.size() > 0 )
 
124
        {
 
125
            writer.openElement( COLLECTION_NAME );
 
126
            
 
127
            for ( ReportTable reportTable : reportTables )
 
128
            {
 
129
                writer.openElement( ELEMENT_NAME );
 
130
                
 
131
                writer.writeElement( FIELD_ID, String.valueOf( reportTable.getId() ) );
 
132
                writer.writeElement( FIELD_NAME, reportTable.getName() );
 
133
                writer.writeElement( FIELD_TABLE_NAME, reportTable.getTableName() );
 
134
                writer.writeElement( FIELD_EXISTING_TABLE_NAME, reportTable.getExistingTableName() );
 
135
                writer.writeElement( FIELD_MODE, reportTable.getMode() );
 
136
                writer.writeElement( FIELD_REGRESSION, String.valueOf( reportTable.isRegression() ) );
 
137
                
 
138
                writer.writeElement( FIELD_DO_INDICATORS, String.valueOf( reportTable.isDoIndicators() ) );
 
139
                writer.writeElement( FIELD_DO_CATEGORY_OPTION_COMBOS, String.valueOf( reportTable.isDoCategoryOptionCombos() ) );
 
140
                writer.writeElement( FIELD_DO_PERIODS, String.valueOf( reportTable.isDoPeriods() ) );
 
141
                writer.writeElement( FIELD_DO_ORGANISATION_UNITS, String.valueOf( reportTable.isDoUnits() ) );
 
142
                
 
143
                writer.writeElement( FIELD_REPORTING_MONTH, String.valueOf( reportTable.getRelatives().isReportingMonth() ) );
 
144
                writer.writeElement( FIELD_LAST_3_MONTHS, String.valueOf( reportTable.getRelatives().isLast3Months() ) );
 
145
                writer.writeElement( FIELD_LAST_6_MONTHS, String.valueOf( reportTable.getRelatives().isLast6Months() ) );
 
146
                writer.writeElement( FIELD_LAST_9_MONTHS, String.valueOf( reportTable.getRelatives().isLast9Months() ) );
 
147
                writer.writeElement( FIELD_LAST_12_MONTHS, String.valueOf( reportTable.getRelatives().isLast12Months() ) );
 
148
                writer.writeElement( FIELD_SO_FAR_THIS_YEAR, String.valueOf( reportTable.getRelatives().isSoFarThisYear() ) );
 
149
                writer.writeElement( FIELD_SO_FAR_THIS_FINANCIAL_YEAR, String.valueOf( reportTable.getRelatives().isSoFarThisFinancialYear() ) );
 
150
                writer.writeElement( FIELD_LAST_3_TO_6_MONTHS, String.valueOf( reportTable.getRelatives().isLast3To6Months() ) );
 
151
                writer.writeElement( FIELD_LAST_6_TO_9_MONTHS, String.valueOf( reportTable.getRelatives().isLast6To9Months() ) );
 
152
                writer.writeElement( FIELD_LAST_9_TO_12_MONTHS, String.valueOf( reportTable.getRelatives().isLast9To12Months() ) );
 
153
                writer.writeElement( FIELD_LAST_12_INDIVIDUAL_MONHTS, String.valueOf( reportTable.getRelatives().isLast12IndividualMonths() ) );
 
154
                writer.writeElement( FIELD_INDIVIDUAL_MONTHS_THIS_YEAR, String.valueOf( reportTable.getRelatives().isIndividualMonthsThisYear() ) );
 
155
                writer.writeElement( FIELD_INDIVIDUAL_QUARTERS_THIS_YEAR, String.valueOf( reportTable.getRelatives().isIndividualQuartersThisYear() ) );
 
156
                
 
157
                writer.writeElement( FIELD_PARAM_REPORTING_MONTH, String.valueOf( reportTable.getReportParams().isParamReportingMonth() ) );
 
158
                writer.writeElement( FIELD_PARAM_PARENT_ORG_UNIT, String.valueOf( reportTable.getReportParams().isParamParentOrganisationUnit() ) );
 
159
                writer.writeElement( FIELD_PARAM_ORG_UNIT, String.valueOf( reportTable.getReportParams().isParamOrganisationUnit() ) ); 
 
160
                
 
161
                writer.closeElement();
 
162
            }
 
163
            
 
164
            writer.closeElement();
 
165
        }
 
166
    }
 
167
    
 
168
    public void read( XMLReader reader, ImportParams params )
 
169
    {
 
170
        while ( reader.moveToStartElement( ELEMENT_NAME, COLLECTION_NAME ) )
 
171
        {
 
172
            final Map<String, String> values = reader.readElements( ELEMENT_NAME );
 
173
            
 
174
            final ReportTable reportTable = new ReportTable();
 
175
            
 
176
            final RelativePeriods relatives = new RelativePeriods();
 
177
            reportTable.setRelatives( relatives );
 
178
            
 
179
            final ReportParams reportParams = new ReportParams();
 
180
            reportTable.setReportParams( reportParams );
 
181
            
 
182
            reportTable.setId( Integer.parseInt( values.get( FIELD_ID ) ) );
 
183
            reportTable.setName( values.get( FIELD_NAME ) );
 
184
            reportTable.setTableName( values.get( FIELD_TABLE_NAME ) );
 
185
            reportTable.setExistingTableName( values.get( FIELD_EXISTING_TABLE_NAME ) );
 
186
            reportTable.setMode( values.get( FIELD_MODE ) );
 
187
            reportTable.setRegression( Boolean.parseBoolean( values.get( FIELD_REGRESSION ) ) );
 
188
            
 
189
            reportTable.setDoIndicators( Boolean.parseBoolean( values.get( FIELD_DO_INDICATORS ) ) );
 
190
            reportTable.setDoCategoryOptionCombos( Boolean.parseBoolean( values.get( FIELD_DO_CATEGORY_OPTION_COMBOS ) ) );
 
191
            reportTable.setDoPeriods( Boolean.parseBoolean( values.get( FIELD_DO_PERIODS ) ) );
 
192
            reportTable.setDoUnits( Boolean.parseBoolean( values.get( FIELD_DO_ORGANISATION_UNITS ) ) );
 
193
            
 
194
            reportTable.getRelatives().setReportingMonth( Boolean.parseBoolean( values.get( FIELD_REPORTING_MONTH ) ) );            
 
195
            reportTable.getRelatives().setLast3Months( Boolean.parseBoolean( values.get( FIELD_LAST_3_MONTHS ) ) );
 
196
            reportTable.getRelatives().setLast6Months( Boolean.parseBoolean( values.get( FIELD_LAST_6_MONTHS ) ) );
 
197
            reportTable.getRelatives().setLast9Months( Boolean.parseBoolean( values.get( FIELD_LAST_9_MONTHS ) ) );
 
198
            reportTable.getRelatives().setLast12Months( Boolean.parseBoolean( values.get( FIELD_LAST_12_MONTHS ) ) );
 
199
            reportTable.getRelatives().setSoFarThisYear( Boolean.parseBoolean( values.get( FIELD_SO_FAR_THIS_YEAR ) ) );
 
200
            reportTable.getRelatives().setSoFarThisFinancialYear( Boolean.parseBoolean( values.get( FIELD_SO_FAR_THIS_FINANCIAL_YEAR ) ) );
 
201
            reportTable.getRelatives().setLast3To6Months( Boolean.parseBoolean( values.get( FIELD_LAST_3_TO_6_MONTHS ) ) );
 
202
            reportTable.getRelatives().setLast6To9Months( Boolean.parseBoolean( values.get( FIELD_LAST_6_TO_9_MONTHS ) ) );
 
203
            reportTable.getRelatives().setLast9To12Months( Boolean.parseBoolean( values.get( FIELD_LAST_9_TO_12_MONTHS ) ) );
 
204
            reportTable.getRelatives().setLast12IndividualMonths( Boolean.parseBoolean( values.get( FIELD_LAST_12_INDIVIDUAL_MONHTS ) ) );
 
205
            reportTable.getRelatives().setIndividualMonthsThisYear( Boolean.parseBoolean( values.get( FIELD_INDIVIDUAL_MONTHS_THIS_YEAR ) ) );
 
206
            reportTable.getRelatives().setIndividualQuartersThisYear( Boolean.parseBoolean( values.get( FIELD_INDIVIDUAL_QUARTERS_THIS_YEAR ) ) );
 
207
            
 
208
            reportTable.getReportParams().setParamReportingMonth( Boolean.parseBoolean( values.get( FIELD_PARAM_REPORTING_MONTH ) ) );
 
209
            reportTable.getReportParams().setParamParentOrganisationUnit( Boolean.parseBoolean( values.get( FIELD_PARAM_PARENT_ORG_UNIT ) ) );
 
210
            reportTable.getReportParams().setParamOrganisationUnit( Boolean.parseBoolean( values.get( FIELD_PARAM_ORG_UNIT ) ) );
 
211
            
 
212
            NameMappingUtil.addReportTableMapping( reportTable.getId(), reportTable.getName() );
 
213
            
 
214
            read( reportTable, GroupMemberType.NONE, params );
 
215
        }        
 
216
    }
 
217
}