1
package org.hisp.dhis.importexport.dhis14.file.importer;
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.expression.Expression.SEPARATOR;
32
import java.io.InputStream;
33
import java.util.HashMap;
34
import java.util.HashSet;
35
import java.util.List;
39
import org.apache.commons.logging.Log;
40
import org.apache.commons.logging.LogFactory;
41
import org.hisp.dhis.cache.HibernateCacheManager;
42
import org.hisp.dhis.dataelement.DataElementCategoryCombo;
43
import org.hisp.dhis.dataelement.DataElementCategoryComboService;
44
import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
45
import org.hisp.dhis.dataelement.DataElementCategoryOptionComboService;
46
import org.hisp.dhis.dataelement.DataElementService;
47
import org.hisp.dhis.dataset.DataSetService;
48
import org.hisp.dhis.datavalue.DataValueService;
49
import org.hisp.dhis.expression.ExpressionService;
50
import org.hisp.dhis.importexport.ImportInternalProcess;
51
import org.hisp.dhis.importexport.ImportObjectService;
52
import org.hisp.dhis.importexport.ImportParams;
53
import org.hisp.dhis.importexport.analysis.ImportAnalyser;
54
import org.hisp.dhis.importexport.dhis14.file.query.QueryManager;
55
import org.hisp.dhis.importexport.dhis14.file.rowhandler.CalculatedDataElementRowHandler;
56
import org.hisp.dhis.importexport.dhis14.file.rowhandler.DataElementGroupMemberRowHandler;
57
import org.hisp.dhis.importexport.dhis14.file.rowhandler.DataElementGroupRowHandler;
58
import org.hisp.dhis.importexport.dhis14.file.rowhandler.DataElementRowHandler;
59
import org.hisp.dhis.importexport.dhis14.file.rowhandler.DataSetMemberRowHandler;
60
import org.hisp.dhis.importexport.dhis14.file.rowhandler.DataSetOrganisationUnitAssociationRowHandler;
61
import org.hisp.dhis.importexport.dhis14.file.rowhandler.DataSetRowHandler;
62
import org.hisp.dhis.importexport.dhis14.file.rowhandler.GroupSetMemberRowHandler;
63
import org.hisp.dhis.importexport.dhis14.file.rowhandler.GroupSetRowHandler;
64
import org.hisp.dhis.importexport.dhis14.file.rowhandler.IndicatorGroupMemberRowHandler;
65
import org.hisp.dhis.importexport.dhis14.file.rowhandler.IndicatorGroupRowHandler;
66
import org.hisp.dhis.importexport.dhis14.file.rowhandler.IndicatorRowHandler;
67
import org.hisp.dhis.importexport.dhis14.file.rowhandler.IndicatorTypeRowHandler;
68
import org.hisp.dhis.importexport.dhis14.file.rowhandler.OnChangePeriodRowHandler;
69
import org.hisp.dhis.importexport.dhis14.file.rowhandler.OrganisationUnitGroupMemberRowHandler;
70
import org.hisp.dhis.importexport.dhis14.file.rowhandler.OrganisationUnitGroupRowHandler;
71
import org.hisp.dhis.importexport.dhis14.file.rowhandler.OrganisationUnitRelationshipRowHandler;
72
import org.hisp.dhis.importexport.dhis14.file.rowhandler.OrganisationUnitRowHandler;
73
import org.hisp.dhis.importexport.dhis14.file.rowhandler.PeriodRowHandler;
74
import org.hisp.dhis.importexport.dhis14.file.rowhandler.RoutineDataValueRowHandler;
75
import org.hisp.dhis.importexport.dhis14.file.rowhandler.SemiPermanentDataValueRowHandler;
76
import org.hisp.dhis.importexport.dhis14.object.Dhis14CalculatedDataElementEntry;
77
import org.hisp.dhis.importexport.dhis14.util.Dhis14PeriodUtil;
78
import org.hisp.dhis.importexport.mapping.NameMappingUtil;
79
import org.hisp.dhis.importexport.mapping.ObjectMappingGenerator;
80
import org.hisp.dhis.indicator.IndicatorService;
81
import org.hisp.dhis.jdbc.BatchHandler;
82
import org.hisp.dhis.jdbc.BatchHandlerFactory;
83
import org.hisp.dhis.jdbc.batchhandler.DataElementBatchHandler;
84
import org.hisp.dhis.jdbc.batchhandler.DataElementGroupBatchHandler;
85
import org.hisp.dhis.jdbc.batchhandler.DataElementGroupMemberBatchHandler;
86
import org.hisp.dhis.jdbc.batchhandler.DataSetBatchHandler;
87
import org.hisp.dhis.jdbc.batchhandler.DataSetMemberBatchHandler;
88
import org.hisp.dhis.jdbc.batchhandler.DataSetSourceAssociationBatchHandler;
89
import org.hisp.dhis.jdbc.batchhandler.DataValueBatchHandler;
90
import org.hisp.dhis.jdbc.batchhandler.GroupSetBatchHandler;
91
import org.hisp.dhis.jdbc.batchhandler.GroupSetMemberBatchHandler;
92
import org.hisp.dhis.jdbc.batchhandler.ImportDataValueBatchHandler;
93
import org.hisp.dhis.jdbc.batchhandler.IndicatorBatchHandler;
94
import org.hisp.dhis.jdbc.batchhandler.IndicatorGroupBatchHandler;
95
import org.hisp.dhis.jdbc.batchhandler.IndicatorGroupMemberBatchHandler;
96
import org.hisp.dhis.jdbc.batchhandler.IndicatorTypeBatchHandler;
97
import org.hisp.dhis.jdbc.batchhandler.OrganisationUnitBatchHandler;
98
import org.hisp.dhis.jdbc.batchhandler.OrganisationUnitGroupBatchHandler;
99
import org.hisp.dhis.jdbc.batchhandler.OrganisationUnitGroupMemberBatchHandler;
100
import org.hisp.dhis.jdbc.batchhandler.PeriodBatchHandler;
101
import org.hisp.dhis.jdbc.batchhandler.SourceBatchHandler;
102
import org.hisp.dhis.organisationunit.OrganisationUnitGroupService;
103
import org.hisp.dhis.organisationunit.OrganisationUnitService;
104
import org.hisp.dhis.period.PeriodService;
105
import org.hisp.dhis.system.util.AppendingHashMap;
106
import org.hisp.dhis.system.util.DateUtils;
108
import com.ibatis.sqlmap.client.event.RowHandler;
111
* @author Lars Helge Overland
112
* @version $Id: DefaultDhis14FileImportService.java 6425 2008-11-22 00:08:57Z larshelg $
114
public class DefaultDhis14FileImportService
115
extends ImportInternalProcess
117
private final Log log = LogFactory.getLog( DefaultDhis14FileImportService.class );
119
// -------------------------------------------------------------------------
121
// -------------------------------------------------------------------------
123
private QueryManager queryManager;
125
public void setQueryManager( QueryManager queryManager )
127
this.queryManager = queryManager;
130
private ObjectMappingGenerator objectMappingGenerator;
132
public void setObjectMappingGenerator( ObjectMappingGenerator objectMappingGenerator )
134
this.objectMappingGenerator = objectMappingGenerator;
137
private BatchHandlerFactory batchHandlerFactory;
139
public void setBatchHandlerFactory( BatchHandlerFactory batchHandlerFactory )
141
this.batchHandlerFactory = batchHandlerFactory;
144
private ImportObjectService importObjectService;
146
public void setImportObjectService( ImportObjectService importObjectService )
148
this.importObjectService = importObjectService;
151
private ExpressionService expressionService;
153
public void setExpressionService( ExpressionService expressionService )
155
this.expressionService = expressionService;
158
private DataElementService dataElementService;
160
public void setDataElementService( DataElementService dataElementService )
162
this.dataElementService = dataElementService;
165
private DataElementCategoryComboService categoryComboService;
167
public void setCategoryComboService( DataElementCategoryComboService categoryComboService )
169
this.categoryComboService = categoryComboService;
172
private DataElementCategoryOptionComboService categoryOptionComboService;
174
public void setCategoryOptionComboService( DataElementCategoryOptionComboService categoryOptionComboService )
176
this.categoryOptionComboService = categoryOptionComboService;
179
private PeriodService periodService;
181
public void setPeriodService( PeriodService periodService )
183
this.periodService = periodService;
186
private DataSetService dataSetService;
188
public void setDataSetService( DataSetService dataSetService )
190
this.dataSetService = dataSetService;
193
private OrganisationUnitService organisationUnitService;
195
public void setOrganisationUnitService( OrganisationUnitService organisationUnitService )
197
this.organisationUnitService = organisationUnitService;
200
private OrganisationUnitGroupService organisationUnitGroupService;
202
public void setOrganisationUnitGroupService( OrganisationUnitGroupService organisationUnitGroupService )
204
this.organisationUnitGroupService = organisationUnitGroupService;
207
private IndicatorService indicatorService;
209
public void setIndicatorService( IndicatorService indicatorService )
211
this.indicatorService = indicatorService;
214
private DataValueService dataValueService;
216
public void setDataValueService( DataValueService dataValueService )
218
this.dataValueService = dataValueService;
221
private ImportAnalyser importAnalyser;
223
public void setImportAnalyser( ImportAnalyser importAnalyser )
225
this.importAnalyser = importAnalyser;
228
private HibernateCacheManager cacheManager;
230
public void setCacheManager( HibernateCacheManager cacheManager )
232
this.cacheManager = cacheManager;
235
// -------------------------------------------------------------------------
237
// -------------------------------------------------------------------------
239
public DefaultDhis14FileImportService()
244
// -------------------------------------------------------------------------
245
// ImportInternalProcess implementation
246
// -------------------------------------------------------------------------
249
public void importData( ImportParams params, InputStream inputStream )
251
if ( params.isPreview() )
253
importObjectService.deleteImportObjects();
256
importDataElements( params );
257
importCalculatedDataElements( params, createCalculatedDataElementEntryMap() );
258
importIndicatorTypes( params );
259
importIndicators( params );
260
importDataElementGroups( params );
261
importDataElementGroupMembers( params );
262
importIndicatorGroups( params );
263
importIndicatorGroupMembers( params );
265
importDataSets( params );
266
importDataSetMembers( params );
268
importOrganisationUnits( params );
269
importOrganisationUnitGroups( params );
270
importOrganisationUnitGroupMembers( params );
271
importGroupSets( params );
272
importGroupSetMembers( params );
273
importOrganisationUnitRelationships( params );
274
importOrganisationUnitHierarchy();
276
importDataSetOrganisationUnitAssociations( params );
278
if ( params.isDataValues() && !params.isAnalysis() )
280
importPeriods( params );
281
importRoutineDataValues( params );
283
importOnChangePeriods( params );
284
importSemiPermanentDataValues( params );
287
if ( params.isAnalysis() )
289
setOutput( importAnalyser.getImportAnalysis() );
292
setMessage( "import_process_done" );
294
NameMappingUtil.clearMapping();
295
Dhis14PeriodUtil.clear();
297
cacheManager.clearCache();
300
// -------------------------------------------------------------------------
301
// DataElement and Indicator
302
// -------------------------------------------------------------------------
304
private void importDataElements( ImportParams params )
306
setMessage( "importing_data_elements" );
308
BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( DataElementBatchHandler.class );
310
DataElementCategoryCombo categoryCombo = categoryComboService.
311
getDataElementCategoryComboByName( DataElementCategoryCombo.DEFAULT_CATEGORY_COMBO_NAME );
313
RowHandler rowHandler = new DataElementRowHandler( batchHandler,
322
queryManager.queryWithRowhandler( "getDataElements", rowHandler );
324
batchHandler.flush();
326
log.info( "Imported DataElements" );
329
private void importCalculatedDataElements( ImportParams params, Map<Integer, String> calculatedEntryMap )
331
setMessage( "importing_data_elements" );
333
DataElementCategoryCombo categoryCombo = categoryComboService.
334
getDataElementCategoryComboByName( DataElementCategoryCombo.DEFAULT_CATEGORY_COMBO_NAME );
336
RowHandler rowHandler = new CalculatedDataElementRowHandler( importObjectService,
343
objectMappingGenerator.getDataElementMapping( params.skipMapping() ),
344
getCategoryOptionComboMapping() );
346
queryManager.queryWithRowhandler( "getCalculatedDataElements", rowHandler );
348
log.info( "Imported CalculatedDataElements" );
351
private void importIndicatorTypes( ImportParams params )
353
setMessage( "importing_indicator_types" );
355
BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( IndicatorTypeBatchHandler.class );
357
RowHandler rowHandler = new IndicatorTypeRowHandler( batchHandler,
364
queryManager.queryWithRowhandler( "getIndicatorTypes", rowHandler );
366
batchHandler.flush();
368
log.info( "Imported IndicatorTypes" );
371
private void importIndicators( ImportParams params )
373
setMessage( "importing_indicators" );
375
BatchHandler indicatorBatchHandler = batchHandlerFactory.createBatchHandler( IndicatorBatchHandler.class );
376
BatchHandler dataElementBatchHandler = batchHandlerFactory.createBatchHandler( DataElementBatchHandler.class );
377
BatchHandler indicatorTypeBatchHandler = batchHandlerFactory.createBatchHandler( IndicatorTypeBatchHandler.class );
379
RowHandler rowHandler = new IndicatorRowHandler( indicatorBatchHandler,
382
objectMappingGenerator.getIndicatorTypeMapping( params.skipMapping() ),
383
objectMappingGenerator.getDataElementMapping( params.skipMapping() ),
384
categoryOptionComboService.getDefaultDataElementCategoryOptionCombo(),
388
indicatorBatchHandler.init();
389
dataElementBatchHandler.init();
390
indicatorTypeBatchHandler.init();
392
queryManager.queryWithRowhandler( "getIndicators", rowHandler );
394
indicatorBatchHandler.flush();
395
dataElementBatchHandler.flush();
396
indicatorTypeBatchHandler.flush();
398
log.info( "Imported Indicators" );
401
private void importDataElementGroups( ImportParams params )
403
setMessage( "importing_data_element_groups" );
405
BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( DataElementGroupBatchHandler.class );
407
RowHandler rowHandler = new DataElementGroupRowHandler( batchHandler,
414
queryManager.queryWithRowhandler( "getDataElementGroups", rowHandler );
416
batchHandler.flush();
418
log.info( "Imported DataElementGroups" );
421
private void importIndicatorGroups( ImportParams params )
423
setMessage( "importing_indicator_groups" );
425
BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( IndicatorGroupBatchHandler.class );
427
RowHandler rowHandler = new IndicatorGroupRowHandler( batchHandler,
434
queryManager.queryWithRowhandler( "getIndicatorGroups", rowHandler );
436
batchHandler.flush();
438
log.info( "Imported IndicatorGroups" );
441
private void importDataElementGroupMembers( ImportParams params )
443
setMessage( "importing_data_element_group_members" );
445
BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( DataElementGroupMemberBatchHandler.class );
447
RowHandler rowHandler = new DataElementGroupMemberRowHandler( batchHandler,
449
objectMappingGenerator.getDataElementMapping( params.skipMapping() ),
450
objectMappingGenerator.getDataElementGroupMapping( params.skipMapping() ),
455
queryManager.queryWithRowhandler( "getDataElementGroupMembers", rowHandler );
457
batchHandler.flush();
459
log.info( "Imported DataElementGroup members" );
462
private void importIndicatorGroupMembers( ImportParams params )
464
setMessage( "importing_indicator_group_members" );
466
BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( IndicatorGroupMemberBatchHandler.class );
468
RowHandler rowHandler = new IndicatorGroupMemberRowHandler( batchHandler,
470
objectMappingGenerator.getIndicatorMapping( params.skipMapping() ),
471
objectMappingGenerator.getIndicatorGroupMapping( params.skipMapping() ),
476
queryManager.queryWithRowhandler( "getIndicatorGroupMembers", rowHandler );
478
batchHandler.flush();
480
log.info( "Imported IndicatorGroup members" );
483
// -------------------------------------------------------------------------
485
// -------------------------------------------------------------------------
487
private void importDataSets( ImportParams params )
489
setMessage( "importing_data_sets" );
491
BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( DataSetBatchHandler.class );
493
RowHandler rowHandler = new DataSetRowHandler( batchHandler,
496
objectMappingGenerator.getPeriodTypeMapping(),
502
queryManager.queryWithRowhandler( "getDataSets", rowHandler );
504
batchHandler.flush();
506
log.info( "Imported DataSets" );
509
private void importDataSetMembers( ImportParams params )
511
setMessage( "importing_data_set_members" );
513
BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( DataSetMemberBatchHandler.class );
515
RowHandler rowHandler = new DataSetMemberRowHandler( batchHandler,
517
objectMappingGenerator.getDataElementMapping( params.skipMapping() ),
518
objectMappingGenerator.getDataSetMapping( params.skipMapping() ),
523
queryManager.queryWithRowhandler( "getDataSetMembers", rowHandler );
525
batchHandler.flush();
527
log.info( "Imported DataSet members" );
530
// -------------------------------------------------------------------------
532
// -------------------------------------------------------------------------
534
private void importOrganisationUnits( ImportParams params )
536
setMessage( "importing_organisation_units" );
538
BatchHandler sourceBatchHandler = batchHandlerFactory.createBatchHandler( SourceBatchHandler.class );
539
BatchHandler organisationUnitBatchHandler = batchHandlerFactory.createBatchHandler( OrganisationUnitBatchHandler.class );
541
RowHandler rowHandler = new OrganisationUnitRowHandler( organisationUnitBatchHandler,
544
organisationUnitService,
548
sourceBatchHandler.init();
549
organisationUnitBatchHandler.init();
551
queryManager.queryWithRowhandler( "getOrganisationUnits", rowHandler );
553
sourceBatchHandler.flush();
554
organisationUnitBatchHandler.flush();
556
log.info( "Imported OrganisationUnits" );
559
private void importOrganisationUnitGroups( ImportParams params )
561
setMessage( "importing_organisation_unit_groups" );
563
BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( OrganisationUnitGroupBatchHandler.class );
565
RowHandler rowHandler = new OrganisationUnitGroupRowHandler( batchHandler,
567
organisationUnitGroupService,
572
queryManager.queryWithRowhandler( "getOrganisationUnitGroups", rowHandler );
574
batchHandler.flush();
576
log.info( "Imported OrganisationUnitGroups" );
579
private void importOrganisationUnitGroupMembers( ImportParams params )
581
setMessage( "importing_organisation_unit_group_members" );
583
BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( OrganisationUnitGroupMemberBatchHandler.class );
585
RowHandler rowHandler = new OrganisationUnitGroupMemberRowHandler( batchHandler,
587
objectMappingGenerator.getOrganisationUnitMapping( params.skipMapping() ),
588
objectMappingGenerator.getOrganisationUnitGroupMapping( params.skipMapping() ),
593
queryManager.queryWithRowhandler( "getOrganisationUnitGroupMembers", rowHandler );
595
batchHandler.flush();
597
log.info( "Imported OrganisationUnitGroup members" );
600
private void importGroupSets( ImportParams params )
602
setMessage( "importing_organisation_unit_group_sets" );
604
BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( GroupSetBatchHandler.class );
606
RowHandler rowHandler = new GroupSetRowHandler( batchHandler,
608
organisationUnitGroupService,
613
queryManager.queryWithRowhandler( "getOrganisationUnitGroupSets", rowHandler );
615
batchHandler.flush();
617
log.info( "Imported OrganisationUnitGroupSets" );
620
private void importGroupSetMembers( ImportParams params )
622
setMessage( "importing_organisation_unit_group_set_members" );
624
BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( GroupSetMemberBatchHandler.class );
626
RowHandler rowHandler = new GroupSetMemberRowHandler( batchHandler,
628
objectMappingGenerator.getOrganisationUnitGroupMapping( params.skipMapping() ),
629
objectMappingGenerator.getOrganisationUnitGroupSetMapping( params.skipMapping() ),
634
queryManager.queryWithRowhandler( "getOrganisationUnitGroupSetMembers", rowHandler );
636
batchHandler.flush();
638
log.info( "Imported OrganisationUnitGroupSet members" );
641
private void importOrganisationUnitRelationships( ImportParams params )
643
setMessage( "importing_organisation_unit_relationships" );
645
BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( OrganisationUnitBatchHandler.class );
647
RowHandler rowHandler = new OrganisationUnitRelationshipRowHandler( batchHandler,
649
organisationUnitService,
650
objectMappingGenerator.getOrganisationUnitMapping( params.skipMapping() ),
655
queryManager.queryWithRowhandler( "getOrganisationUnitRelationships", rowHandler );
657
batchHandler.flush();
659
log.info( "Imported OrganisationUnitRelationships" );
662
private void importOrganisationUnitHierarchy()
664
setMessage( "importing_organisation_unit_hierarchy" );
666
organisationUnitService.addOrganisationUnitHierarchy( DateUtils.getEpoch() );
668
log.info( "Imported OrganisationUnitHierarchy" );
671
// -------------------------------------------------------------------------
672
// DataSet - OrganisationUnit Associations
673
// -------------------------------------------------------------------------
675
private void importDataSetOrganisationUnitAssociations( ImportParams params )
677
setMessage( "importing_data_set_organisation_unit_associations" );
679
BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( DataSetSourceAssociationBatchHandler.class );
681
RowHandler rowHandler = new DataSetOrganisationUnitAssociationRowHandler( batchHandler,
683
objectMappingGenerator.getDataSetMapping( params.skipMapping() ),
684
objectMappingGenerator.getOrganisationUnitMapping( params.skipMapping() ),
689
queryManager.queryWithRowhandler( "getDataSetOrganisationUnitAssociations", rowHandler );
691
batchHandler.flush();
693
log.info( "Imported DataSet OrganisationUnit Associations" );
696
// -------------------------------------------------------------------------
698
// -------------------------------------------------------------------------
700
private void importPeriods( ImportParams params )
702
setMessage( "importing_periods" );
704
BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( PeriodBatchHandler.class );
706
RowHandler rowHandler = new PeriodRowHandler( batchHandler,
709
objectMappingGenerator.getPeriodTypeMapping(),
711
getPeriodWithDataIdentifiers() );
715
queryManager.queryWithRowhandler( "getPeriods", rowHandler );
717
batchHandler.flush();
719
log.info( "Imported Periods" );
722
// -------------------------------------------------------------------------
724
// -------------------------------------------------------------------------
726
private void importRoutineDataValues( ImportParams params )
728
setMessage( "importing_routine_data_values" );
730
DataElementCategoryCombo categoryCombo = categoryComboService.getDataElementCategoryComboByName( DataElementCategoryCombo.DEFAULT_CATEGORY_COMBO_NAME );
732
DataElementCategoryOptionCombo categoryOptionCombo = categoryCombo.getOptionCombos().iterator().next();
734
BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( DataValueBatchHandler.class );
736
BatchHandler importDataValueBatchHandler = batchHandlerFactory.createBatchHandler( ImportDataValueBatchHandler.class );
738
RowHandler rowHandler = new RoutineDataValueRowHandler( batchHandler,
739
importDataValueBatchHandler,
741
objectMappingGenerator.getDataElementMapping( params.skipMapping() ),
742
objectMappingGenerator.getPeriodMapping( params.skipMapping() ),
743
objectMappingGenerator.getOrganisationUnitMapping( params.skipMapping() ),
749
importDataValueBatchHandler.init();
751
if ( params.getLastUpdated() == null )
753
queryManager.queryWithRowhandler( "getRoutineDataValues", rowHandler );
757
queryManager.queryWithRowhandler( "getRoutineDataValuesLastUpdated", rowHandler, params.getLastUpdated() );
760
batchHandler.flush();
762
importDataValueBatchHandler.flush();
764
log.info( "Imported RoutineDataValues" );
767
// -------------------------------------------------------------------------
769
// -------------------------------------------------------------------------
771
private void importOnChangePeriods( ImportParams params )
773
setMessage( "importing_on_change_periods" );
775
BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( PeriodBatchHandler.class );
777
RowHandler rowHandler = new OnChangePeriodRowHandler( batchHandler,
780
objectMappingGenerator.getPeriodTypeMapping(),
785
queryManager.queryWithRowhandler( "getOnChangePeriods", rowHandler );
787
batchHandler.flush();
789
log.info( "Imported OnChangePeriods" );
792
// -------------------------------------------------------------------------
793
// SemiPermanentDataValue
794
// -------------------------------------------------------------------------
796
private void importSemiPermanentDataValues( ImportParams params )
798
setMessage( "importing_semi_permanent_data_values" );
800
DataElementCategoryCombo categoryCombo = categoryComboService.getDataElementCategoryComboByName( DataElementCategoryCombo.DEFAULT_CATEGORY_COMBO_NAME );
802
DataElementCategoryOptionCombo categoryOptionCombo = categoryCombo.getOptionCombos().iterator().next();
804
BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( DataValueBatchHandler.class );
806
BatchHandler importDataValueBatchHandler = batchHandlerFactory.createBatchHandler( ImportDataValueBatchHandler.class );
808
RowHandler rowHandler = new SemiPermanentDataValueRowHandler( batchHandler,
809
importDataValueBatchHandler,
811
objectMappingGenerator.getDataElementMapping( params.skipMapping() ),
812
objectMappingGenerator.getPeriodObjectMapping( params.skipMapping() ),
813
objectMappingGenerator.getOrganisationUnitMapping( params.skipMapping() ),
819
importDataValueBatchHandler.init();
821
if ( params.getLastUpdated() == null )
823
queryManager.queryWithRowhandler( "getSemiPermanentDataValues", rowHandler );
827
queryManager.queryWithRowhandler( "getSemiPermanentDataValuesLastUpdated", rowHandler, params.getLastUpdated() );
830
batchHandler.flush();
832
importDataValueBatchHandler.flush();
834
log.info( "Imported SemiPermanentDataValues" );
837
// -------------------------------------------------------------------------
838
// Supportive methods
839
// -------------------------------------------------------------------------
842
* Creates a map where key is the calculated data element identifier and the
843
* value if the formula.
845
private Map<Integer, String> createCalculatedDataElementEntryMap()
847
int categoryOptionComboId = categoryOptionComboService.getDefaultDataElementCategoryOptionCombo().getId();
849
List<?> calculatedDataElements = queryManager.queryForList( "getCalculatedDataElementEntries", null );
851
Map<Integer, String> map = new AppendingHashMap<Integer, String>(); // Calculated data element id, formula
853
//TODO factor should be double
855
for ( Object element : calculatedDataElements )
857
Dhis14CalculatedDataElementEntry calculated = (Dhis14CalculatedDataElementEntry) element;
859
String formula = "([" + calculated.getDataElementId() + SEPARATOR + categoryOptionComboId + "]*" + calculated.getFactor() + ")";
861
if ( map.containsKey( calculated.getCalculatedDataElementId() ) )
863
formula = "+" + formula;
866
map.put( calculated.getCalculatedDataElementId(), formula );
873
* Returns a list of distinct period identifiers from the RoutineDataValue table,
874
* ie. periods which have registered data. Could be used to avoid importing
875
* periods without data.
877
private Set<Integer> getPeriodWithDataIdentifiers()
879
Set<Integer> identifiers = new HashSet<Integer>();
881
List<?> list = queryManager.queryForList( "getDistinctPeriodIdentifiers", null );
883
for ( Object id : list )
885
identifiers.add( (Integer) id );
892
* Returns a mapping for category option combo. Since DHIS 1.4 does not have
893
* this it will always be default.
895
private Map<Object, Integer> getCategoryOptionComboMapping()
897
Integer categoryOptionComboId = categoryOptionComboService.getDefaultDataElementCategoryOptionCombo().getId();
899
Map<Object, Integer> mapping = new HashMap<Object, Integer>();
901
mapping.put( categoryOptionComboId, categoryOptionComboId );