~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/service/DefaultImportObjectService.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.service;
 
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.expression.Expression.SEPARATOR;
 
31
 
 
32
import java.util.Collection;
 
33
import java.util.HashSet;
 
34
import java.util.Map;
 
35
import java.util.Set;
 
36
import java.util.regex.Matcher;
 
37
import java.util.regex.Pattern;
 
38
 
 
39
import org.apache.commons.logging.Log;
 
40
import org.apache.commons.logging.LogFactory;
 
41
import org.hisp.dhis.jdbc.BatchHandlerFactory;
 
42
import org.hisp.dhis.jdbc.BatchHandler;
 
43
import org.hisp.dhis.jdbc.batchhandler.CategoryCategoryOptionAssociationBatchHandler;
 
44
import org.hisp.dhis.jdbc.batchhandler.CategoryComboCategoryAssociationBatchHandler;
 
45
import org.hisp.dhis.jdbc.batchhandler.CompleteDataSetRegistrationBatchHandler;
 
46
import org.hisp.dhis.jdbc.batchhandler.DataDictionaryBatchHandler;
 
47
import org.hisp.dhis.jdbc.batchhandler.DataDictionaryDataElementBatchHandler;
 
48
import org.hisp.dhis.jdbc.batchhandler.DataDictionaryIndicatorBatchHandler;
 
49
import org.hisp.dhis.jdbc.batchhandler.DataElementBatchHandler;
 
50
import org.hisp.dhis.jdbc.batchhandler.DataElementCategoryBatchHandler;
 
51
import org.hisp.dhis.jdbc.batchhandler.DataElementCategoryComboBatchHandler;
 
52
import org.hisp.dhis.jdbc.batchhandler.DataElementCategoryOptionBatchHandler;
 
53
import org.hisp.dhis.jdbc.batchhandler.DataElementGroupBatchHandler;
 
54
import org.hisp.dhis.jdbc.batchhandler.DataElementGroupMemberBatchHandler;
 
55
import org.hisp.dhis.jdbc.batchhandler.DataSetBatchHandler;
 
56
import org.hisp.dhis.jdbc.batchhandler.DataSetMemberBatchHandler;
 
57
import org.hisp.dhis.jdbc.batchhandler.DataSetSourceAssociationBatchHandler;
 
58
import org.hisp.dhis.jdbc.batchhandler.DataValueBatchHandler;
 
59
import org.hisp.dhis.jdbc.batchhandler.ExtendedDataElementBatchHandler;
 
60
import org.hisp.dhis.jdbc.batchhandler.GroupSetBatchHandler;
 
61
import org.hisp.dhis.jdbc.batchhandler.GroupSetMemberBatchHandler;
 
62
import org.hisp.dhis.jdbc.batchhandler.IndicatorBatchHandler;
 
63
import org.hisp.dhis.jdbc.batchhandler.IndicatorGroupBatchHandler;
 
64
import org.hisp.dhis.jdbc.batchhandler.IndicatorGroupMemberBatchHandler;
 
65
import org.hisp.dhis.jdbc.batchhandler.IndicatorTypeBatchHandler;
 
66
import org.hisp.dhis.jdbc.batchhandler.OrganisationUnitBatchHandler;
 
67
import org.hisp.dhis.jdbc.batchhandler.OrganisationUnitGroupBatchHandler;
 
68
import org.hisp.dhis.jdbc.batchhandler.OrganisationUnitGroupMemberBatchHandler;
 
69
import org.hisp.dhis.jdbc.batchhandler.PeriodBatchHandler;
 
70
import org.hisp.dhis.jdbc.batchhandler.ReportTableBatchHandler;
 
71
import org.hisp.dhis.jdbc.batchhandler.ReportTableCategoryOptionComboBatchHandler;
 
72
import org.hisp.dhis.jdbc.batchhandler.ReportTableDataElementBatchHandler;
 
73
import org.hisp.dhis.jdbc.batchhandler.ReportTableDataSetBatchHandler;
 
74
import org.hisp.dhis.jdbc.batchhandler.ReportTableIndicatorBatchHandler;
 
75
import org.hisp.dhis.jdbc.batchhandler.ReportTableOrganisationUnitBatchHandler;
 
76
import org.hisp.dhis.jdbc.batchhandler.ReportTablePeriodBatchHandler;
 
77
import org.hisp.dhis.jdbc.batchhandler.SourceBatchHandler;
 
78
import org.hisp.dhis.cache.HibernateCacheManager;
 
79
import org.hisp.dhis.datadictionary.DataDictionary;
 
80
import org.hisp.dhis.datadictionary.DataDictionaryService;
 
81
import org.hisp.dhis.datadictionary.ExtendedDataElement;
 
82
import org.hisp.dhis.dataelement.CalculatedDataElement;
 
83
import org.hisp.dhis.dataelement.DataElement;
 
84
import org.hisp.dhis.dataelement.DataElementCategory;
 
85
import org.hisp.dhis.dataelement.DataElementCategoryCombo;
 
86
import org.hisp.dhis.dataelement.DataElementCategoryComboService;
 
87
import org.hisp.dhis.dataelement.DataElementCategoryOption;
 
88
import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 
89
import org.hisp.dhis.dataelement.DataElementCategoryOptionComboService;
 
90
import org.hisp.dhis.dataelement.DataElementCategoryOptionService;
 
91
import org.hisp.dhis.dataelement.DataElementGroup;
 
92
import org.hisp.dhis.dataelement.DataElementService;
 
93
import org.hisp.dhis.dataset.CompleteDataSetRegistration;
 
94
import org.hisp.dhis.dataset.DataSet;
 
95
import org.hisp.dhis.dataset.DataSetService;
 
96
import org.hisp.dhis.datavalue.DataValue;
 
97
import org.hisp.dhis.datavalue.DataValueService;
 
98
import org.hisp.dhis.expression.ExpressionService;
 
99
import org.hisp.dhis.importexport.GroupMemberAssociation;
 
100
import org.hisp.dhis.importexport.GroupMemberType;
 
101
import org.hisp.dhis.importexport.ImportDataValue;
 
102
import org.hisp.dhis.importexport.ImportDataValueService;
 
103
import org.hisp.dhis.importexport.ImportObject;
 
104
import org.hisp.dhis.importexport.ImportObjectService;
 
105
import org.hisp.dhis.importexport.ImportObjectStatus;
 
106
import org.hisp.dhis.importexport.ImportObjectStore;
 
107
import org.hisp.dhis.importexport.locking.LockingManager;
 
108
import org.hisp.dhis.importexport.mapping.GroupMemberAssociationVerifier;
 
109
import org.hisp.dhis.importexport.mapping.NameMappingUtil;
 
110
import org.hisp.dhis.importexport.mapping.ObjectMappingGenerator;
 
111
import org.hisp.dhis.indicator.Indicator;
 
112
import org.hisp.dhis.indicator.IndicatorGroup;
 
113
import org.hisp.dhis.indicator.IndicatorService;
 
114
import org.hisp.dhis.indicator.IndicatorType;
 
115
import org.hisp.dhis.olap.OlapURL;
 
116
import org.hisp.dhis.olap.OlapURLService;
 
117
import org.hisp.dhis.organisationunit.OrganisationUnit;
 
118
import org.hisp.dhis.organisationunit.OrganisationUnitGroup;
 
119
import org.hisp.dhis.organisationunit.OrganisationUnitGroupService;
 
120
import org.hisp.dhis.organisationunit.OrganisationUnitGroupSet;
 
121
import org.hisp.dhis.organisationunit.OrganisationUnitLevel;
 
122
import org.hisp.dhis.organisationunit.OrganisationUnitService;
 
123
import org.hisp.dhis.period.Period;
 
124
import org.hisp.dhis.reporttable.ReportTable;
 
125
import org.hisp.dhis.reporttable.ReportTableStore;
 
126
import org.hisp.dhis.validation.ValidationRule;
 
127
import org.hisp.dhis.validation.ValidationRuleService;
 
128
 
 
129
/**
 
130
 * @author Lars Helge Overland
 
131
 * @version $Id: DefaultImportObjectService.java 5946 2008-10-16 15:46:43Z larshelg $
 
132
 */
 
133
public class DefaultImportObjectService<T>
 
134
    implements ImportObjectService
 
135
{
 
136
    private Log log = LogFactory.getLog( DefaultImportObjectService.class );
 
137
    
 
138
    // -------------------------------------------------------------------------
 
139
    // Dependencies
 
140
    // -------------------------------------------------------------------------
 
141
 
 
142
    private ImportObjectStore importObjectStore;
 
143
    
 
144
    public void setImportObjectStore( ImportObjectStore importObjectStore )
 
145
    {
 
146
        this.importObjectStore = importObjectStore;
 
147
    }
 
148
    
 
149
    private ImportDataValueService importDataValueService;
 
150
 
 
151
    public void setImportDataValueService( ImportDataValueService importDataValueService )
 
152
    {
 
153
        this.importDataValueService = importDataValueService;
 
154
    }
 
155
 
 
156
    private DataElementCategoryOptionService categoryOptionService;
 
157
 
 
158
    public void setCategoryOptionService( DataElementCategoryOptionService categoryOptionService )
 
159
    {
 
160
        this.categoryOptionService = categoryOptionService;
 
161
    }
 
162
    
 
163
    private DataElementCategoryComboService categoryComboService;
 
164
 
 
165
    public void setCategoryComboService( DataElementCategoryComboService categoryComboService )
 
166
    {
 
167
        this.categoryComboService = categoryComboService;
 
168
    }
 
169
 
 
170
    private DataElementCategoryOptionComboService categoryOptionComboService;
 
171
 
 
172
    public void setCategoryOptionComboService( DataElementCategoryOptionComboService categoryOptionComboService )
 
173
    {
 
174
        this.categoryOptionComboService = categoryOptionComboService;
 
175
    }
 
176
    
 
177
    private DataElementService dataElementService;
 
178
 
 
179
    public void setDataElementService( DataElementService dataElementService )
 
180
    {
 
181
        this.dataElementService = dataElementService;
 
182
    }
 
183
    
 
184
    private IndicatorService indicatorService;
 
185
 
 
186
    public void setIndicatorService( IndicatorService indicatorService )
 
187
    {
 
188
        this.indicatorService = indicatorService;
 
189
    }
 
190
 
 
191
    private DataDictionaryService dataDictionaryService;
 
192
 
 
193
    public void setDataDictionaryService( DataDictionaryService dataDictionaryService )
 
194
    {
 
195
        this.dataDictionaryService = dataDictionaryService;
 
196
    }
 
197
    
 
198
    private DataSetService dataSetService;
 
199
 
 
200
    public void setDataSetService( DataSetService dataSetService )
 
201
    {
 
202
        this.dataSetService = dataSetService;
 
203
    }
 
204
    
 
205
    private OrganisationUnitService organisationUnitService;
 
206
 
 
207
    public void setOrganisationUnitService( OrganisationUnitService organisationUnitService )
 
208
    {
 
209
        this.organisationUnitService = organisationUnitService;
 
210
    }
 
211
 
 
212
    private OrganisationUnitGroupService organisationUnitGroupService;
 
213
 
 
214
    public void setOrganisationUnitGroupService( OrganisationUnitGroupService organisationUnitGroupService )
 
215
    {
 
216
        this.organisationUnitGroupService = organisationUnitGroupService;
 
217
    }
 
218
    
 
219
    private ValidationRuleService validationRuleService;
 
220
 
 
221
    public void setValidationRuleService( ValidationRuleService validationRuleService )
 
222
    {
 
223
        this.validationRuleService = validationRuleService;
 
224
    }
 
225
    
 
226
    private ExpressionService expressionService;
 
227
 
 
228
    public void setExpressionService( ExpressionService expressionService )
 
229
    {
 
230
        this.expressionService = expressionService;
 
231
    }
 
232
    
 
233
    private ReportTableStore reportTableStore;
 
234
 
 
235
    public void setReportTableStore( ReportTableStore reportTableStore )
 
236
    {
 
237
        this.reportTableStore = reportTableStore;
 
238
    }
 
239
    
 
240
    private OlapURLService olapURLService;
 
241
 
 
242
    public void setOlapURLService( OlapURLService olapURLService )
 
243
    {
 
244
        this.olapURLService = olapURLService;
 
245
    }
 
246
    
 
247
    private DataValueService dataValueService;
 
248
 
 
249
    public void setDataValueService( DataValueService dataValueService )
 
250
    {
 
251
        this.dataValueService = dataValueService;
 
252
    }
 
253
 
 
254
    private BatchHandlerFactory batchHandlerFactory;
 
255
 
 
256
    public void setBatchHandlerFactory( BatchHandlerFactory batchHandlerFactory )
 
257
    {
 
258
        this.batchHandlerFactory = batchHandlerFactory;
 
259
    }
 
260
    
 
261
    private ObjectMappingGenerator objectMappingGenerator;
 
262
 
 
263
    public void setObjectMappingGenerator( ObjectMappingGenerator objectMappingGenerator )
 
264
    {
 
265
        this.objectMappingGenerator = objectMappingGenerator;
 
266
    }
 
267
    
 
268
    private HibernateCacheManager cacheManager;
 
269
 
 
270
    public void setCacheManager( HibernateCacheManager cacheManager )
 
271
    {
 
272
        this.cacheManager = cacheManager;
 
273
    }
 
274
    
 
275
    private LockingManager lockingManager;
 
276
 
 
277
    public void setLockingManager( LockingManager lockingManager )
 
278
    {
 
279
        this.lockingManager = lockingManager;
 
280
    }
 
281
        
 
282
    // -------------------------------------------------------------------------
 
283
    // ImportObjectService implementation
 
284
    // -------------------------------------------------------------------------
 
285
 
 
286
    // -------------------------------------------------------------------------
 
287
    // ImportObject operations
 
288
    // -------------------------------------------------------------------------
 
289
 
 
290
    public int addImportObject( ImportObjectStatus status, GroupMemberType groupMemberType, Object object )
 
291
    {
 
292
        ImportObject importObject = new ImportObject( status, object.getClass().getName(), groupMemberType, object );
 
293
        
 
294
        return importObjectStore.addImportObject( importObject );
 
295
    }
 
296
 
 
297
    public int addImportObject( ImportObjectStatus status, Object object, Object compareObject )
 
298
    {
 
299
        ImportObject importObject = new ImportObject( status, object.getClass().getName(), object, compareObject );
 
300
        
 
301
        return importObjectStore.addImportObject( importObject );
 
302
    }
 
303
 
 
304
    public int addImportObject( ImportObjectStatus status, GroupMemberType groupMemberType, Object object, Object compareObject )
 
305
    {
 
306
        ImportObject importObject = new ImportObject( status, object.getClass().getName(), groupMemberType, object, compareObject );
 
307
        
 
308
        return importObjectStore.addImportObject( importObject );
 
309
    }
 
310
    
 
311
    public ImportObject getImportObject( int id )
 
312
    {
 
313
        return importObjectStore.getImportObject( id );
 
314
    }
 
315
 
 
316
    public Collection<ImportObject> getImportObjects( Class<?> clazz )
 
317
    {
 
318
        return importObjectStore.getImportObjects( clazz );
 
319
    }
 
320
    
 
321
    public Collection<ImportObject> getImportObjects( ImportObjectStatus status, Class<?> clazz )
 
322
    {
 
323
        return importObjectStore.getImportObjects( status, clazz );
 
324
    }
 
325
    
 
326
    public Collection<ImportObject> getImportObjects( GroupMemberType groupMemberType )
 
327
    {
 
328
        return importObjectStore.getImportObjects( groupMemberType );
 
329
    }
 
330
    
 
331
    public void deleteImportObject( int importObjectId )
 
332
    {
 
333
        ImportObject importObject = importObjectStore.getImportObject( importObjectId );
 
334
        
 
335
        if ( importObject != null )
 
336
        {
 
337
            importObjectStore.deleteImportObject( importObject );
 
338
        }
 
339
    }
 
340
 
 
341
    public void deleteImportObjects( Class<?> clazz )
 
342
    {
 
343
        importObjectStore.deleteImportObjects( clazz );
 
344
    }
 
345
    
 
346
    public void deleteImportObjects()
 
347
    {
 
348
        importObjectStore.deleteImportObjects();        
 
349
        importDataValueService.deleteImportDataValues();
 
350
    }
 
351
    
 
352
    //TODO Refactor: this code is not extensible and is error-prone in terms of cascading deletion of associated objects
 
353
    
 
354
    public void cascadeDeleteImportObject( int importObjectId )
 
355
    {
 
356
        ImportObject importObject = importObjectStore.getImportObject( importObjectId );
 
357
        
 
358
        if ( importObject != null )
 
359
        {
 
360
            if ( importObject.getClassName().equals( DataElement.class.getName() ) ||
 
361
                 importObject.getClassName().equals( CalculatedDataElement.class.getName() ) )
 
362
            {
 
363
                DataElement element = (DataElement) importObject.getObject();            
 
364
                
 
365
                deleteMemberAssociations( GroupMemberType.DATAELEMENTGROUP, element.getId() );                
 
366
                deleteMemberAssociations( GroupMemberType.DATASET, element.getId() );                
 
367
                deleteMemberAssociations( GroupMemberType.DATADICTIONARY_DATAELEMENT, element.getId() );                
 
368
                deleteMemberAssociations( GroupMemberType.REPORTTABLE_DATAELEMENT, element.getId() );
 
369
                
 
370
                deleteIndicatorsContainingDataElement( element.getId() );
 
371
                
 
372
                importDataValueService.deleteImportDataValuesByDataElement( element.getId() );
 
373
            }
 
374
            else if ( importObject.getClassName().equals( DataElementGroup.class.getName() ) )
 
375
            {
 
376
                DataElementGroup group = (DataElementGroup) importObject.getObject();
 
377
                
 
378
                deleteGroupAssociations( GroupMemberType.DATAELEMENTGROUP, group.getId() );
 
379
            }
 
380
            else if ( importObject.getClassName().equals( IndicatorType.class.getName() ) )
 
381
            {
 
382
                IndicatorType type = (IndicatorType) importObject.getObject();
 
383
                
 
384
                deleteIndicatorsWithIndicatorType( type.getId() );
 
385
            }
 
386
            else if ( importObject.getClassName().equals( Indicator.class.getName() ) )
 
387
            {
 
388
                Indicator indicator = (Indicator) importObject.getObject();
 
389
                
 
390
                deleteMemberAssociations( GroupMemberType.INDICATORGROUP, indicator.getId() );                
 
391
                deleteMemberAssociations( GroupMemberType.DATADICTIONARY_INDICATOR, indicator.getId() );                
 
392
                deleteMemberAssociations( GroupMemberType.REPORTTABLE_INDICATOR, indicator.getId() );
 
393
            }
 
394
            else if ( importObject.getClassName().equals( IndicatorGroup.class.getName() ) )
 
395
            {
 
396
                IndicatorGroup group = (IndicatorGroup) importObject.getObject();
 
397
                
 
398
                deleteGroupAssociations( GroupMemberType.INDICATORGROUP, group.getId() );
 
399
            }
 
400
            else if ( importObject.getClassName().equals( DataDictionary.class.getName() ) )
 
401
            {
 
402
                DataDictionary dictionary = (DataDictionary) importObject.getObject();
 
403
                
 
404
                deleteGroupAssociations( GroupMemberType.DATADICTIONARY_DATAELEMENT, dictionary.getId() );                
 
405
                deleteGroupAssociations( GroupMemberType.DATADICTIONARY_INDICATOR, dictionary.getId() );
 
406
            }
 
407
            else if ( importObject.getClassName().equals( DataSet.class.getName() ) )
 
408
            {
 
409
                DataSet dataSet = (DataSet) importObject.getObject();
 
410
                
 
411
                deleteGroupAssociations( GroupMemberType.DATASET, dataSet.getId() );             
 
412
                deleteMemberAssociations( GroupMemberType.REPORTTABLE_DATASET, dataSet.getId() );
 
413
                
 
414
                deleteCompleteDataSetRegistrationsByDataSet( dataSet.getId() );
 
415
            }
 
416
            else if ( importObject.getClassName().equals( OrganisationUnit.class.getName() ) )
 
417
            {
 
418
                OrganisationUnit unit = (OrganisationUnit) importObject.getObject();
 
419
                
 
420
                deleteMemberAssociations( GroupMemberType.ORGANISATIONUNITGROUP, unit.getId() );                
 
421
                deleteGroupAssociations( GroupMemberType.ORGANISATIONUNITRELATIONSHIP, unit.getId() );                
 
422
                deleteMemberAssociations( GroupMemberType.ORGANISATIONUNITRELATIONSHIP, unit.getId() );                
 
423
                deleteMemberAssociations( GroupMemberType.REPORTTABLE_ORGANISATIONUNIT, unit.getId() );
 
424
                
 
425
                deleteCompleteDataSetRegistrationsBySource( unit.getId() );
 
426
                
 
427
                importDataValueService.deleteImportDataValuesBySource( unit.getId() );
 
428
            }
 
429
            else if ( importObject.getClassName().equals( OrganisationUnitGroup.class.getName() ) )
 
430
            {
 
431
                OrganisationUnitGroup group = (OrganisationUnitGroup) importObject.getObject();
 
432
                
 
433
                deleteGroupAssociations( GroupMemberType.ORGANISATIONUNITGROUP, group.getId() );                
 
434
                deleteMemberAssociations( GroupMemberType.ORGANISATIONUNITGROUPSET, group.getId() );
 
435
            }
 
436
            else if ( importObject.getClassName().equals( OrganisationUnitGroupSet.class.getName() ) )
 
437
            {
 
438
                OrganisationUnitGroupSet groupSet = (OrganisationUnitGroupSet) importObject.getObject();
 
439
                
 
440
                deleteGroupAssociations( GroupMemberType.ORGANISATIONUNITGROUPSET, groupSet.getId() );
 
441
            }
 
442
            else if ( importObject.getClassName().equals( ReportTable.class.getName() ) )
 
443
            {
 
444
                ReportTable reportTable = (ReportTable) importObject.getObject();
 
445
                
 
446
                deleteGroupAssociations( GroupMemberType.REPORTTABLE_DATAELEMENT, reportTable.getId() );
 
447
                deleteGroupAssociations( GroupMemberType.REPORTTABLE_CATEGORY_OPTION_COMBO, reportTable.getId() );
 
448
                deleteGroupAssociations( GroupMemberType.REPORTTABLE_INDICATOR, reportTable.getId() );
 
449
                deleteGroupAssociations( GroupMemberType.REPORTTABLE_DATASET, reportTable.getId() );                
 
450
                deleteGroupAssociations( GroupMemberType.REPORTTABLE_PERIOD, reportTable.getId() );
 
451
                deleteGroupAssociations( GroupMemberType.REPORTTABLE_ORGANISATIONUNIT, reportTable.getId() );
 
452
            }
 
453
        }
 
454
        
 
455
        deleteImportObject( importObjectId );
 
456
    }
 
457
    
 
458
    public void cascadeDeleteImportObjects( Class<?> clazz )
 
459
    {
 
460
        importObjectStore.deleteImportObjects( clazz );
 
461
        
 
462
        if ( clazz.equals( DataElement.class ) ||
 
463
             clazz.equals( CalculatedDataElement.class ) )
 
464
        {
 
465
            importObjectStore.deleteImportObjects( DataElementCategoryOptionCombo.class );            
 
466
            importObjectStore.deleteImportObjects( DataElementCategoryCombo.class );            
 
467
            importObjectStore.deleteImportObjects( DataElementCategory.class );            
 
468
            importObjectStore.deleteImportObjects( DataElementCategoryOption.class );
 
469
            importObjectStore.deleteImportObjects( GroupMemberType.CATEGORY_CATEGORYOPTION );
 
470
            importObjectStore.deleteImportObjects( GroupMemberType.CATEGORYCOMBO_CATEGORY );
 
471
            
 
472
            importObjectStore.deleteImportObjects( GroupMemberType.DATAELEMENTGROUP );
 
473
            
 
474
            importObjectStore.deleteImportObjects( DataSet.class );
 
475
            importObjectStore.deleteImportObjects( GroupMemberType.DATASET );
 
476
            importObjectStore.deleteImportObjects( GroupMemberType.DATASET_SOURCE );
 
477
            importObjectStore.deleteImportObjects( CompleteDataSetRegistration.class );
 
478
            
 
479
            importObjectStore.deleteImportObjects( Indicator.class );
 
480
            importObjectStore.deleteImportObjects( GroupMemberType.INDICATORGROUP );
 
481
            importObjectStore.deleteImportObjects( GroupMemberType.DATADICTIONARY_INDICATOR ); 
 
482
            importObjectStore.deleteImportObjects( GroupMemberType.REPORTTABLE_INDICATOR );
 
483
            
 
484
            importObjectStore.deleteImportObjects( GroupMemberType.DATADICTIONARY_DATAELEMENT );
 
485
            importObjectStore.deleteImportObjects( GroupMemberType.REPORTTABLE_DATAELEMENT );
 
486
            
 
487
            importDataValueService.deleteImportDataValues();
 
488
        }
 
489
        else if ( clazz.equals( DataElementGroup.class ) )
 
490
        {
 
491
            importObjectStore.deleteImportObjects( GroupMemberType.DATAELEMENTGROUP );
 
492
        }
 
493
        else if ( clazz.equals( IndicatorType.class ) )
 
494
        {
 
495
            importObjectStore.deleteImportObjects( Indicator.class );
 
496
            importObjectStore.deleteImportObjects( GroupMemberType.INDICATORGROUP );
 
497
            importObjectStore.deleteImportObjects( GroupMemberType.DATADICTIONARY_INDICATOR );
 
498
            importObjectStore.deleteImportObjects( GroupMemberType.REPORTTABLE_INDICATOR );
 
499
        }
 
500
        else if ( clazz.equals( Indicator.class ) )
 
501
        {
 
502
            importObjectStore.deleteImportObjects( GroupMemberType.INDICATORGROUP );
 
503
            importObjectStore.deleteImportObjects( GroupMemberType.DATADICTIONARY_INDICATOR );            
 
504
            importObjectStore.deleteImportObjects( GroupMemberType.REPORTTABLE_INDICATOR );            
 
505
        }
 
506
        else if ( clazz.equals( IndicatorGroup.class ) )
 
507
        {
 
508
            importObjectStore.deleteImportObjects( GroupMemberType.INDICATORGROUP );
 
509
        }
 
510
        else if ( clazz.equals( DataDictionary.class ) )
 
511
        {
 
512
            importObjectStore.deleteImportObjects( GroupMemberType.DATADICTIONARY_DATAELEMENT );            
 
513
            importObjectStore.deleteImportObjects( GroupMemberType.DATADICTIONARY_INDICATOR );            
 
514
        }
 
515
        else if ( clazz.equals( DataSet.class ) )
 
516
        {
 
517
            importObjectStore.deleteImportObjects( GroupMemberType.DATASET );
 
518
            importObjectStore.deleteImportObjects( GroupMemberType.DATASET_SOURCE );
 
519
            importObjectStore.deleteImportObjects( CompleteDataSetRegistration.class );          
 
520
            importObjectStore.deleteImportObjects( GroupMemberType.REPORTTABLE_DATASET );     
 
521
        }
 
522
        else if ( clazz.equals( OrganisationUnit.class ) )
 
523
        {
 
524
            importObjectStore.deleteImportObjects( GroupMemberType.ORGANISATIONUNITGROUP );            
 
525
            importObjectStore.deleteImportObjects( GroupMemberType.ORGANISATIONUNITRELATIONSHIP );            
 
526
            importObjectStore.deleteImportObjects( GroupMemberType.REPORTTABLE_ORGANISATIONUNIT );
 
527
            importObjectStore.deleteImportObjects( GroupMemberType.DATASET_SOURCE );
 
528
            
 
529
            importObjectStore.deleteImportObjects( CompleteDataSetRegistration.class );
 
530
            
 
531
            importDataValueService.deleteImportDataValues();
 
532
        }
 
533
        else if ( clazz.equals( OrganisationUnitGroup.class ) )
 
534
        {
 
535
            importObjectStore.deleteImportObjects( GroupMemberType.ORGANISATIONUNITGROUP );   
 
536
        }
 
537
        else if ( clazz.equals( OrganisationUnitGroupSet.class ) )
 
538
        {
 
539
            importObjectStore.deleteImportObjects( GroupMemberType.ORGANISATIONUNITGROUPSET );
 
540
        }
 
541
        else if ( clazz.equals( ReportTable.class ) )
 
542
        {
 
543
            importObjectStore.deleteImportObjects( GroupMemberType.REPORTTABLE_DATAELEMENT );
 
544
            importObjectStore.deleteImportObjects( GroupMemberType.REPORTTABLE_CATEGORY_OPTION_COMBO );
 
545
            importObjectStore.deleteImportObjects( GroupMemberType.REPORTTABLE_INDICATOR );
 
546
            importObjectStore.deleteImportObjects( GroupMemberType.REPORTTABLE_DATASET );
 
547
            importObjectStore.deleteImportObjects( GroupMemberType.REPORTTABLE_PERIOD );
 
548
            importObjectStore.deleteImportObjects( GroupMemberType.REPORTTABLE_ORGANISATIONUNIT );
 
549
        }
 
550
    }
 
551
    
 
552
    // -------------------------------------------------------------------------
 
553
    // Object
 
554
    // -------------------------------------------------------------------------
 
555
    
 
556
    public void matchObject( int importObjectId, int existingObjectId )
 
557
    {
 
558
        ImportObject importObject = importObjectStore.getImportObject( importObjectId );
 
559
        
 
560
        Object object = importObject.getObject();
 
561
 
 
562
        // ---------------------------------------------------------------------
 
563
        // Updates the name of the import object to the name of the existing
 
564
        // object.
 
565
        // ---------------------------------------------------------------------
 
566
        
 
567
        if ( object.getClass().equals( DataElement.class ) ||
 
568
             object.getClass().equals( CalculatedDataElement.class ) )
 
569
        {
 
570
            DataElement element = (DataElement) object;
 
571
            
 
572
            element.setName( dataElementService.getDataElement( existingObjectId ).getName() );
 
573
        }
 
574
        else if ( object.getClass().equals( DataElementGroup.class ) )
 
575
        {
 
576
            DataElementGroup group = (DataElementGroup) object;
 
577
            
 
578
            group.setName( dataElementService.getDataElementGroup( existingObjectId ).getName() );
 
579
        }
 
580
        else if ( object.getClass().equals( IndicatorType.class ) )
 
581
        {
 
582
            IndicatorType type = (IndicatorType) object;
 
583
            
 
584
            type.setName( indicatorService.getIndicatorType( existingObjectId ).getName() );
 
585
        }
 
586
        else if ( object.getClass().equals( Indicator.class ) )
 
587
        {
 
588
            Indicator indicator = (Indicator) object;
 
589
            
 
590
            indicator.setName( indicatorService.getIndicator( existingObjectId ).getName() );
 
591
        }
 
592
        else if ( object.getClass().equals( IndicatorGroup.class ) )
 
593
        {
 
594
            IndicatorGroup group = (IndicatorGroup) object;
 
595
            
 
596
            group.setName( indicatorService.getIndicatorGroup( existingObjectId ).getName() );
 
597
        }
 
598
        else if ( object.getClass().equals( DataDictionary.class ) )
 
599
        {
 
600
            DataDictionary dictionary = (DataDictionary) object;
 
601
            
 
602
            dictionary.setName( dataDictionaryService.getDataDictionary( existingObjectId ).getName() );
 
603
        }
 
604
        else if ( object.getClass().equals( DataSet.class ) )
 
605
        {
 
606
            DataSet dataSet = (DataSet) object;
 
607
            
 
608
            dataSet.setName( dataSetService.getDataSet( existingObjectId ).getName() );
 
609
        }
 
610
        else if ( object.getClass().equals( OrganisationUnit.class ) )
 
611
        {
 
612
            OrganisationUnit unit = (OrganisationUnit) object;
 
613
            
 
614
            unit.setName( organisationUnitService.getOrganisationUnit( existingObjectId ).getName() );
 
615
        }
 
616
        else if ( object.getClass().equals( OrganisationUnitGroup.class ) )
 
617
        {
 
618
            OrganisationUnitGroup group = (OrganisationUnitGroup) object;
 
619
            
 
620
            group.setName( organisationUnitGroupService.getOrganisationUnitGroup( existingObjectId ).getName() );
 
621
        }
 
622
        else if ( object.getClass().equals( OrganisationUnitGroupSet.class ) )
 
623
        {
 
624
            OrganisationUnitGroupSet groupSet = (OrganisationUnitGroupSet) object;
 
625
            
 
626
            groupSet.setName( organisationUnitGroupService.getOrganisationUnitGroupSet( existingObjectId ).getName() );
 
627
        }
 
628
        else if ( object.getClass().equals( OrganisationUnitLevel.class ) )
 
629
        {
 
630
            OrganisationUnitLevel level = (OrganisationUnitLevel) object;
 
631
            
 
632
            level.setName( organisationUnitService.getOrganisationUnitLevel( existingObjectId ).getName() );
 
633
        }
 
634
        else if ( object.getClass().equals( ValidationRule.class ) )
 
635
        {
 
636
            ValidationRule validationRule = (ValidationRule) object;
 
637
            
 
638
            validationRule.setName( validationRuleService.getValidationRule( existingObjectId ).getName() );
 
639
        }
 
640
        else if ( object.getClass().equals( ReportTable.class ) )
 
641
        {
 
642
            ReportTable reportTable = (ReportTable) object;
 
643
            
 
644
            reportTable.setName( reportTableStore.getReportTable( existingObjectId ).getName() );
 
645
        }
 
646
        else if ( object.getClass().equals( OlapURL.class ) )
 
647
        {
 
648
            OlapURL url = (OlapURL) object;
 
649
            
 
650
            url.setName( olapURLService.getOlapURL( existingObjectId ).getName() );
 
651
        }
 
652
        else if ( object.getClass().equals( DataValue.class ) )
 
653
        {
 
654
            DataValue dataValue = (DataValue) object;
 
655
            
 
656
            object = updateDataValue( dataValue, dataValueService.getDataValue( dataValue.getSource(), dataValue.getDataElement(), dataValue.getPeriod() ) );
 
657
        }
 
658
 
 
659
        // ---------------------------------------------------------------------
 
660
        // Sets the status of the import object to match, these objects will
 
661
        // later be ignored on import all but is needed for matching of
 
662
        // associations.
 
663
        // ---------------------------------------------------------------------
 
664
        
 
665
        importObject.setStatus( ImportObjectStatus.MATCH );
 
666
        
 
667
        importObjectStore.updateImportObject( importObject );
 
668
    }
 
669
    
 
670
    // -------------------------------------------------------------------------
 
671
    // Import
 
672
    // -------------------------------------------------------------------------
 
673
    
 
674
    public void importAll()
 
675
    {
 
676
        importCategoryOptions();
 
677
        importCategories();
 
678
        importCategoryCombos();
 
679
        importCategoryOptionCombos();
 
680
        importCategoryCategoryOptionAssociations();
 
681
        importCategoryComboCategoryAssociations();
 
682
        importDataElements();
 
683
        importCalculatedDataElements();
 
684
        importDataElementGroups();
 
685
        importDataElementGroupMembers();
 
686
        importIndicatorTypes();
 
687
        importIndicators();
 
688
        importIndicatorGroups();
 
689
        importIndicatorGroupMembers();
 
690
        importDataDictionaries();
 
691
        importDataDictionaryDataElements();
 
692
        importDataDictionaryIndicators();
 
693
        importDataSets();
 
694
        importDataSetMembers();
 
695
        importOrganisationUnits();
 
696
        importOrganisationUnitRelationships();
 
697
        importOrganisationUnitGroups();
 
698
        importOrganisationUnitGroupMembers();
 
699
        importOrganisationUnitGroupSets();
 
700
        importOrganisationUnitGroupSetMembers();
 
701
        importOrganisationUnitLevels();
 
702
        importDataSetSourceAssociations();
 
703
        importValidationRules();
 
704
        importPeriods();
 
705
        importReportTables();
 
706
        importReportTableDataElements();
 
707
        importReportTableCategoryOptionCombos();
 
708
        importReportTableIndicators();
 
709
        importReportTableDataSets();
 
710
        importReportTablePeriods();
 
711
        importReportTableOrganisationUnits();
 
712
        importOlapURLs();
 
713
        importCompleteDataSetRegistrations();        
 
714
        importDataValues();
 
715
        
 
716
        NameMappingUtil.clearMapping();
 
717
        
 
718
        cacheManager.clearCache();
 
719
    }
 
720
 
 
721
    // -------------------------------------------------------------------------
 
722
    // Import - object supportive methods
 
723
    // -------------------------------------------------------------------------
 
724
 
 
725
    private void importCategoryOptions()
 
726
    {
 
727
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( DataElementCategoryOptionBatchHandler.class );
 
728
        
 
729
        batchHandler.init();
 
730
        
 
731
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( DataElementCategoryOption.class );
 
732
        
 
733
        for ( ImportObject importObject : importObjects )
 
734
        {
 
735
            DataElementCategoryOption object = (DataElementCategoryOption) importObject.getObject();
 
736
            
 
737
            NameMappingUtil.addCategoryOptionMapping( object.getId(), object.getName() );
 
738
            
 
739
            if ( importObject.getStatus() == ImportObjectStatus.UPDATE )
 
740
            {
 
741
                DataElementCategoryOption compareObject = (DataElementCategoryOption) importObject.getCompareObject();
 
742
                
 
743
                object.setId( compareObject.getId() );
 
744
            }
 
745
            
 
746
            importObject.setObject( object );
 
747
            
 
748
            addOrUpdateObject( batchHandler, importObject );
 
749
        }
 
750
        
 
751
        batchHandler.flush();
 
752
        
 
753
        importObjectStore.deleteImportObjects( DataElementCategoryOption.class );
 
754
        
 
755
        log.info( "Imported DataElementCategoryOptions" );
 
756
    }
 
757
    
 
758
    private void importCategories()
 
759
    {
 
760
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( DataElementCategoryBatchHandler.class );
 
761
        
 
762
        batchHandler.init();
 
763
        
 
764
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( DataElementCategory.class );
 
765
 
 
766
        for ( ImportObject importObject : importObjects )
 
767
        {
 
768
            DataElementCategory object = (DataElementCategory) importObject.getObject();
 
769
            
 
770
            NameMappingUtil.addCategoryMapping( object.getId(), object.getName() );
 
771
            
 
772
            if ( importObject.getStatus() == ImportObjectStatus.UPDATE )
 
773
            {
 
774
                DataElementCategory compareObject = (DataElementCategory) importObject.getCompareObject();
 
775
                
 
776
                object.setId( compareObject.getId() );
 
777
            }            
 
778
 
 
779
            importObject.setObject( object );
 
780
            
 
781
            addOrUpdateObject( batchHandler, importObject );
 
782
        }
 
783
 
 
784
        batchHandler.flush();
 
785
        
 
786
        importObjectStore.deleteImportObjects( DataElementCategory.class );
 
787
        
 
788
        log.info( "Imported DataElementCategories" );
 
789
    }
 
790
    
 
791
    private void importCategoryCombos()
 
792
    {
 
793
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( DataElementCategoryComboBatchHandler.class );
 
794
        
 
795
        batchHandler.init();
 
796
        
 
797
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( DataElementCategoryCombo.class );
 
798
 
 
799
        for ( ImportObject importObject : importObjects )
 
800
        {
 
801
            DataElementCategoryCombo object = (DataElementCategoryCombo) importObject.getObject();
 
802
            
 
803
            NameMappingUtil.addCategoryComboMapping( object.getId(), object.getName() );
 
804
            
 
805
            if ( importObject.getStatus() == ImportObjectStatus.UPDATE )
 
806
            {
 
807
                DataElementCategoryCombo compareObject = (DataElementCategoryCombo) importObject.getCompareObject();
 
808
                
 
809
                object.setId( compareObject.getId() );
 
810
            }
 
811
            
 
812
            importObject.setObject( object );
 
813
            
 
814
            addOrUpdateObject( batchHandler, importObject );
 
815
        }
 
816
        
 
817
        batchHandler.flush();
 
818
 
 
819
        importObjectStore.deleteImportObjects( DataElementCategoryCombo.class );
 
820
        
 
821
        log.info( "Imported DataElementCategoryCombos" );
 
822
    }
 
823
    
 
824
    private void importCategoryOptionCombos()
 
825
    {
 
826
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( DataElementCategoryOptionCombo.class );
 
827
 
 
828
        Map<Object, Integer> categoryComboMapping = objectMappingGenerator.getCategoryComboMapping( false );
 
829
        Map<Object, Integer> categoryOptionMapping = objectMappingGenerator.getCategoryOptionMapping( false );
 
830
        
 
831
        for ( ImportObject importObject : importObjects )
 
832
        {
 
833
            DataElementCategoryOptionCombo object = (DataElementCategoryOptionCombo) importObject.getObject();
 
834
            
 
835
            int categoryOptionComboId = object.getId();
 
836
            
 
837
            if ( importObject.getStatus() == ImportObjectStatus.UPDATE )
 
838
            {
 
839
                DataElementCategoryOptionCombo compareObject = (DataElementCategoryOptionCombo) importObject.getCompareObject();
 
840
                
 
841
                object.setId( compareObject.getId() );
 
842
            }
 
843
            
 
844
            int categoryComboId = categoryComboMapping.get( object.getCategoryCombo().getId() );
 
845
            
 
846
            object.setCategoryCombo( categoryComboService.getDataElementCategoryCombo( categoryComboId ) );
 
847
            
 
848
            Set<DataElementCategoryOption> categoryOptions = new HashSet<DataElementCategoryOption>();
 
849
            
 
850
            for ( DataElementCategoryOption categoryOption : object.getCategoryOptions() )
 
851
            {
 
852
                int categoryOptionId = categoryOptionMapping.get( categoryOption.getId() );
 
853
                
 
854
                categoryOptions.add( categoryOptionService.getDataElementCategoryOption( categoryOptionId ) );
 
855
            }
 
856
            
 
857
            object.setCategoryOptions( categoryOptions );
 
858
 
 
859
            NameMappingUtil.addCategoryOptionComboMapping( categoryOptionComboId, object );
 
860
            
 
861
            if ( importObject.getStatus() == ImportObjectStatus.NEW )
 
862
            {
 
863
                categoryOptionComboService.addDataElementCategoryOptionCombo( object );
 
864
            }
 
865
            else if ( importObject.getStatus() == ImportObjectStatus.UPDATE )
 
866
            {
 
867
                categoryOptionComboService.updateDataElementCategoryOptionCombo( object );
 
868
            }
 
869
        }
 
870
 
 
871
        importObjectStore.deleteImportObjects( DataElementCategoryOptionCombo.class );
 
872
        
 
873
        log.info( "Imported DataElementCategoryOptionCombos" );
 
874
    }
 
875
 
 
876
    private void importCategoryCategoryOptionAssociations()
 
877
    {
 
878
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( CategoryCategoryOptionAssociationBatchHandler.class );
 
879
        
 
880
        importGroupMemberAssociation( batchHandler, GroupMemberType.CATEGORY_CATEGORYOPTION, 
 
881
            objectMappingGenerator.getCategoryMapping( false ), 
 
882
            objectMappingGenerator.getCategoryOptionMapping( false ) );
 
883
        
 
884
        log.info( "Imported CategoryCategoryOption associations" );
 
885
    }
 
886
    
 
887
    private void importCategoryComboCategoryAssociations()
 
888
    {
 
889
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( CategoryComboCategoryAssociationBatchHandler.class );
 
890
        
 
891
        importGroupMemberAssociation( batchHandler, GroupMemberType.CATEGORYCOMBO_CATEGORY, 
 
892
            objectMappingGenerator.getCategoryComboMapping( false ),
 
893
            objectMappingGenerator.getCategoryMapping( false ) );
 
894
        
 
895
        log.info( "Imported CategoryComboCategory associations" );
 
896
    }
 
897
    
 
898
    private void importDataElements()
 
899
    {
 
900
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( DataElementBatchHandler.class );
 
901
        BatchHandler extendedDataElementBatchHandler = batchHandlerFactory.createBatchHandler( ExtendedDataElementBatchHandler.class );
 
902
        
 
903
        Map<Object, Integer> categoryComboMapping = objectMappingGenerator.getCategoryComboMapping( false );
 
904
        
 
905
        batchHandler.init();
 
906
        extendedDataElementBatchHandler.init();
 
907
        
 
908
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( DataElement.class );
 
909
        
 
910
        for ( ImportObject importObject : importObjects )
 
911
        {
 
912
            DataElement object = (DataElement) importObject.getObject();
 
913
 
 
914
            NameMappingUtil.addDataElementMapping( object.getId(), object.getName() );
 
915
            
 
916
            if ( importObject.getStatus() == ImportObjectStatus.NEW )
 
917
            {
 
918
                ExtendedDataElement extendedObject = object.getExtended();
 
919
                
 
920
                if ( extendedObject != null )
 
921
                {
 
922
                    int id = extendedDataElementBatchHandler.insertObject( extendedObject, true );
 
923
                    
 
924
                    extendedObject.setId( id );
 
925
                    
 
926
                    object.setExtended( extendedObject );
 
927
                }
 
928
            }
 
929
            else if ( importObject.getStatus() == ImportObjectStatus.UPDATE )//TODO
 
930
            {
 
931
                ExtendedDataElement extendedObject = object.getExtended();
 
932
                
 
933
                if ( extendedObject != null )
 
934
                {
 
935
                    ExtendedDataElement extendedCompareObject = ((DataElement)importObject.getCompareObject()).getExtended();
 
936
                    
 
937
                    extendedObject.setId( extendedCompareObject.getId() );
 
938
                    
 
939
                    extendedDataElementBatchHandler.updateObject( extendedObject );
 
940
                    
 
941
                    object.setExtended( extendedObject );
 
942
                }
 
943
                
 
944
                DataElement compareObject = (DataElement) importObject.getCompareObject();
 
945
                
 
946
                object.setId( compareObject.getId() );
 
947
            }
 
948
            
 
949
            object.getCategoryCombo().setId( categoryComboMapping.get( object.getCategoryCombo().getId() ) );
 
950
            
 
951
            importObject.setObject( object );
 
952
            
 
953
            addOrUpdateObject( batchHandler, importObject );
 
954
        }
 
955
        
 
956
        batchHandler.flush();
 
957
        extendedDataElementBatchHandler.flush();
 
958
        
 
959
        importObjectStore.deleteImportObjects( DataElement.class );
 
960
        
 
961
        log.info( "Imported DataElements" );
 
962
    }
 
963
    
 
964
    private void importCalculatedDataElements()
 
965
    {
 
966
        Map<Object, Integer> categoryComboMapping = objectMappingGenerator.getCategoryComboMapping( false );
 
967
        Map<Object, Integer> dataElementMapping = objectMappingGenerator.getDataElementMapping( false );
 
968
        Map<Object, Integer> categoryOptionComboMapping = objectMappingGenerator.getCategoryOptionComboMapping( false );
 
969
 
 
970
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( CalculatedDataElement.class );
 
971
 
 
972
        for ( ImportObject importObject : importObjects )
 
973
        {
 
974
            CalculatedDataElement object = (CalculatedDataElement) importObject.getObject();
 
975
 
 
976
            NameMappingUtil.addDataElementMapping( object.getId(), object.getName() );
 
977
 
 
978
            if ( importObject.getStatus() == ImportObjectStatus.UPDATE )
 
979
            {
 
980
                DataElement compareObject = (DataElement) importObject.getCompareObject();
 
981
                
 
982
                object.setId( compareObject.getId() );
 
983
            }
 
984
 
 
985
            object.getCategoryCombo().setId( categoryComboMapping.get( object.getCategoryCombo().getId() ) );
 
986
            object.getExpression().setExpression( expressionService.convertExpression( 
 
987
                object.getExpression().getExpression(), dataElementMapping, categoryOptionComboMapping ) );
 
988
            
 
989
            importObject.setObject( object );
 
990
            
 
991
            if ( importObject.getStatus() == ImportObjectStatus.NEW )
 
992
            {
 
993
                dataElementService.addDataElement( object );
 
994
            }
 
995
            else if ( importObject.getStatus() == ImportObjectStatus.UPDATE )
 
996
            {
 
997
                dataElementService.updateDataElement( object );
 
998
            }
 
999
        }
 
1000
        
 
1001
        importObjectStore.deleteImportObjects( CalculatedDataElement.class );
 
1002
        
 
1003
        log.info( "Imported CalculatedDataElements" );
 
1004
    }
 
1005
    
 
1006
    private void importDataElementGroups()
 
1007
    {
 
1008
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( DataElementGroupBatchHandler.class );
 
1009
 
 
1010
        batchHandler.init();
 
1011
            
 
1012
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( DataElementGroup.class );
 
1013
 
 
1014
        for ( ImportObject importObject : importObjects )
 
1015
        {
 
1016
            DataElementGroup object = (DataElementGroup) importObject.getObject();
 
1017
 
 
1018
            NameMappingUtil.addDataElementGroupMapping( object.getId(), object.getName() );
 
1019
            
 
1020
            if ( importObject.getStatus() == ImportObjectStatus.UPDATE )
 
1021
            {
 
1022
                DataElementGroup compareObject = (DataElementGroup) importObject.getCompareObject();
 
1023
                
 
1024
                object.setId( compareObject.getId() );
 
1025
            }
 
1026
            
 
1027
            importObject.setObject( object );
 
1028
            
 
1029
            addOrUpdateObject( batchHandler, importObject );
 
1030
        }
 
1031
        
 
1032
        batchHandler.flush();
 
1033
        
 
1034
        importObjectStore.deleteImportObjects( DataElementGroup.class );
 
1035
        
 
1036
        log.info( "Imported DataElementGroups" );
 
1037
    }
 
1038
    
 
1039
    private void importDataElementGroupMembers()
 
1040
    {
 
1041
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( DataElementGroupMemberBatchHandler.class );
 
1042
 
 
1043
        importGroupMemberAssociation( batchHandler, GroupMemberType.DATAELEMENTGROUP,
 
1044
            objectMappingGenerator.getDataElementGroupMapping( false ),
 
1045
            objectMappingGenerator.getDataElementMapping( false ) );
 
1046
        
 
1047
        log.info( "Imported DataElementGroup members" );
 
1048
    }
 
1049
 
 
1050
    private void importIndicatorTypes()
 
1051
    {
 
1052
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( IndicatorTypeBatchHandler.class );
 
1053
        
 
1054
        batchHandler.init();
 
1055
        
 
1056
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( IndicatorType.class );
 
1057
 
 
1058
        for ( ImportObject importObject : importObjects )
 
1059
        {   
 
1060
            IndicatorType object = (IndicatorType) importObject.getObject();
 
1061
 
 
1062
            NameMappingUtil.addIndicatorTypeMapping( object.getId(), object.getName() );
 
1063
            
 
1064
            if ( importObject.getStatus() == ImportObjectStatus.UPDATE )
 
1065
            {
 
1066
                IndicatorType compareObject = (IndicatorType) importObject.getCompareObject();
 
1067
                
 
1068
                object.setId( compareObject.getId() );
 
1069
            }
 
1070
            
 
1071
            importObject.setObject( object );
 
1072
            
 
1073
            addOrUpdateObject( batchHandler, importObject );
 
1074
        }
 
1075
        
 
1076
        batchHandler.flush();
 
1077
        
 
1078
        importObjectStore.deleteImportObjects( IndicatorType.class );
 
1079
        
 
1080
        log.info( "Imported IndicatorTypes" );
 
1081
    }    
 
1082
    
 
1083
    private void importIndicators()
 
1084
    {
 
1085
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( IndicatorBatchHandler.class );
 
1086
        BatchHandler extendedDataElementBatchHandler = batchHandlerFactory.createBatchHandler( ExtendedDataElementBatchHandler.class );
 
1087
        
 
1088
        Map<Object, Integer> indicatorTypeMapping = objectMappingGenerator.getIndicatorTypeMapping( false );
 
1089
        Map<Object, Integer> dataElementMapping = objectMappingGenerator.getDataElementMapping( false );
 
1090
        Map<Object, Integer> categoryOptionComboMapping = objectMappingGenerator.getCategoryOptionComboMapping( false );
 
1091
        
 
1092
        batchHandler.init();
 
1093
        extendedDataElementBatchHandler.init();
 
1094
                
 
1095
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( Indicator.class );
 
1096
 
 
1097
        for ( ImportObject importObject : importObjects )
 
1098
        {
 
1099
            Indicator object = (Indicator) importObject.getObject();
 
1100
 
 
1101
            NameMappingUtil.addIndicatorMapping( object.getId(), object.getName() );
 
1102
            
 
1103
            if ( importObject.getStatus() == ImportObjectStatus.NEW )
 
1104
            {
 
1105
                ExtendedDataElement extendedIndicator = object.getExtended();
 
1106
                
 
1107
                if ( extendedIndicator != null )
 
1108
                {
 
1109
                    int id = extendedDataElementBatchHandler.insertObject( extendedIndicator, true );
 
1110
                    
 
1111
                    extendedIndicator.setId( id );
 
1112
                    
 
1113
                    object.setExtended( extendedIndicator );
 
1114
                }
 
1115
            }
 
1116
            else if ( importObject.getStatus() == ImportObjectStatus.UPDATE )
 
1117
            {
 
1118
                ExtendedDataElement extendedObject = object.getExtended();
 
1119
                
 
1120
                if ( extendedObject != null )
 
1121
                {
 
1122
                    ExtendedDataElement extendedCompareObject = ((Indicator)importObject.getCompareObject()).getExtended();
 
1123
                    
 
1124
                    extendedObject.setId( extendedCompareObject.getId() );
 
1125
                    
 
1126
                    extendedDataElementBatchHandler.updateObject( extendedObject );
 
1127
                    
 
1128
                    object.setExtended( extendedObject );
 
1129
                }
 
1130
                
 
1131
                Indicator compareObject = (Indicator) importObject.getCompareObject();
 
1132
                
 
1133
                object.setId( compareObject.getId() );
 
1134
            }
 
1135
            
 
1136
            object.getIndicatorType().setId( indicatorTypeMapping.get( object.getIndicatorType().getId() ) );
 
1137
            object.setNumerator( expressionService.convertExpression( object.getNumerator(), dataElementMapping, categoryOptionComboMapping ) );
 
1138
            object.setDenominator( expressionService.convertExpression( object.getDenominator(), dataElementMapping, categoryOptionComboMapping ) );
 
1139
            
 
1140
            importObject.setObject( object );
 
1141
            
 
1142
            addOrUpdateObject( batchHandler, importObject );
 
1143
        }
 
1144
        
 
1145
        batchHandler.flush();
 
1146
        extendedDataElementBatchHandler.flush();
 
1147
        
 
1148
        importObjectStore.deleteImportObjects( Indicator.class );
 
1149
        
 
1150
        log.info( "Imported Indicators" );
 
1151
    }    
 
1152
    
 
1153
    private void importIndicatorGroups()
 
1154
    {
 
1155
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( IndicatorGroupBatchHandler.class );
 
1156
        
 
1157
        batchHandler.init();
 
1158
        
 
1159
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( IndicatorGroup.class );
 
1160
 
 
1161
        for ( ImportObject importObject : importObjects )
 
1162
        {   
 
1163
            IndicatorGroup object = (IndicatorGroup) importObject.getObject();
 
1164
 
 
1165
            NameMappingUtil.addIndicatorGroupMapping( object.getId(), object.getName() );
 
1166
            
 
1167
            if ( importObject.getStatus() == ImportObjectStatus.UPDATE )
 
1168
            {
 
1169
                IndicatorGroup compareObject = (IndicatorGroup) importObject.getCompareObject();
 
1170
                
 
1171
                object.setId( compareObject.getId() );
 
1172
            }
 
1173
            
 
1174
            importObject.setObject( object );
 
1175
            
 
1176
            addOrUpdateObject( batchHandler, importObject );          
 
1177
        }
 
1178
        
 
1179
        batchHandler.flush();
 
1180
        
 
1181
        importObjectStore.deleteImportObjects( IndicatorGroup.class );
 
1182
        
 
1183
        log.info( "Imported IndicatorGroups" );
 
1184
    }
 
1185
 
 
1186
    private void importIndicatorGroupMembers()
 
1187
    {
 
1188
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( IndicatorGroupMemberBatchHandler.class );
 
1189
 
 
1190
        importGroupMemberAssociation( batchHandler, GroupMemberType.INDICATORGROUP,
 
1191
            objectMappingGenerator.getIndicatorGroupMapping( false ),
 
1192
            objectMappingGenerator.getIndicatorMapping( false ) );
 
1193
        
 
1194
        log.info( "Imported IndicatorGroup members" );
 
1195
    }
 
1196
    
 
1197
    private void importDataDictionaries()
 
1198
    {
 
1199
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( DataDictionaryBatchHandler.class );
 
1200
        
 
1201
        batchHandler.init();
 
1202
        
 
1203
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( DataDictionary.class );
 
1204
        
 
1205
        for ( ImportObject importObject : importObjects )
 
1206
        {
 
1207
            DataDictionary object = (DataDictionary) importObject.getObject();
 
1208
            
 
1209
            NameMappingUtil.addDataDictionaryMapping( object.getId(), object.getName() );
 
1210
            
 
1211
            if ( importObject.getStatus() == ImportObjectStatus.UPDATE )
 
1212
            {
 
1213
                DataDictionary compareObject = (DataDictionary) importObject.getCompareObject();
 
1214
                
 
1215
                object.setId( compareObject.getId() );
 
1216
            }
 
1217
            
 
1218
            importObject.setObject( object );
 
1219
            
 
1220
            addOrUpdateObject( batchHandler, importObject );
 
1221
        }
 
1222
        
 
1223
        batchHandler.flush();
 
1224
        
 
1225
        importObjectStore.deleteImportObjects( DataDictionary.class );
 
1226
        
 
1227
        log.info( "Imported DataDictionaries" );
 
1228
    }
 
1229
    
 
1230
    private void importDataSets()
 
1231
    {
 
1232
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( DataSetBatchHandler.class );
 
1233
 
 
1234
        batchHandler.init();
 
1235
        
 
1236
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( DataSet.class );
 
1237
        
 
1238
        for ( ImportObject importObject : importObjects )
 
1239
        {
 
1240
            DataSet object = (DataSet) importObject.getObject();
 
1241
 
 
1242
            NameMappingUtil.addDataSetMapping( object.getId(), object.getName() );
 
1243
            
 
1244
            if ( importObject.getStatus() == ImportObjectStatus.UPDATE )
 
1245
            {
 
1246
                DataSet compareObject = (DataSet) importObject.getCompareObject();
 
1247
                
 
1248
                object.setId( compareObject.getId() );
 
1249
            }
 
1250
            
 
1251
            importObject.setObject( object );
 
1252
            
 
1253
            addOrUpdateObject( batchHandler, importObject );         
 
1254
        }
 
1255
        
 
1256
        batchHandler.flush();
 
1257
        
 
1258
        importObjectStore.deleteImportObjects( DataSet.class );
 
1259
        
 
1260
        log.info( "Imported DataSets" );
 
1261
    }
 
1262
    
 
1263
    private void importDataDictionaryDataElements()
 
1264
    {
 
1265
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( DataDictionaryDataElementBatchHandler.class );
 
1266
        
 
1267
        importGroupMemberAssociation( batchHandler, GroupMemberType.DATADICTIONARY_DATAELEMENT, 
 
1268
            objectMappingGenerator.getDataDictionaryMapping( false ),
 
1269
            objectMappingGenerator.getDataElementMapping( false ) );
 
1270
        
 
1271
        log.info( "Imported DataDictionary DataElements" );
 
1272
    }
 
1273
 
 
1274
    private void importDataDictionaryIndicators()
 
1275
    {
 
1276
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( DataDictionaryIndicatorBatchHandler.class );
 
1277
        
 
1278
        importGroupMemberAssociation( batchHandler, GroupMemberType.DATADICTIONARY_INDICATOR, 
 
1279
            objectMappingGenerator.getDataDictionaryMapping( false ),
 
1280
            objectMappingGenerator.getIndicatorMapping( false ) );
 
1281
        
 
1282
        log.info( "Imported DataDictionary Indicators" );
 
1283
    }
 
1284
 
 
1285
    private void importDataSetMembers()
 
1286
    {
 
1287
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( DataSetMemberBatchHandler.class );
 
1288
 
 
1289
        importGroupMemberAssociation( batchHandler, GroupMemberType.DATASET,
 
1290
            objectMappingGenerator.getDataSetMapping( false ), 
 
1291
            objectMappingGenerator.getDataElementMapping( false ) );
 
1292
        
 
1293
        log.info( "Imported DataSet members" );
 
1294
    }
 
1295
    
 
1296
    private void importOrganisationUnits()
 
1297
    {
 
1298
        BatchHandler sourceBatchHandler = batchHandlerFactory.createBatchHandler( SourceBatchHandler.class );
 
1299
        BatchHandler organisationUnitBatchHandler = batchHandlerFactory.createBatchHandler( OrganisationUnitBatchHandler.class );
 
1300
 
 
1301
        sourceBatchHandler.init();
 
1302
        organisationUnitBatchHandler.init();
 
1303
        
 
1304
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( OrganisationUnit.class );
 
1305
        
 
1306
        for ( ImportObject importObject : importObjects )
 
1307
        {
 
1308
            OrganisationUnit object = (OrganisationUnit) importObject.getObject();
 
1309
 
 
1310
            NameMappingUtil.addOrganisationUnitMapping( object.getId(), object.getName() );
 
1311
            
 
1312
            if ( importObject.getStatus() == ImportObjectStatus.NEW )
 
1313
            {
 
1314
                int id = sourceBatchHandler.insertObject( object, true );
 
1315
                
 
1316
                object.setId( id );
 
1317
            }
 
1318
            else if ( importObject.getStatus() == ImportObjectStatus.UPDATE )
 
1319
            {
 
1320
                OrganisationUnit compareObject = (OrganisationUnit) importObject.getCompareObject();
 
1321
                
 
1322
                object.setId( compareObject.getId() );
 
1323
            }
 
1324
            
 
1325
            importObject.setObject( object );
 
1326
            
 
1327
            addOrUpdateObject( organisationUnitBatchHandler, importObject );
 
1328
        }
 
1329
        
 
1330
        sourceBatchHandler.flush();
 
1331
        organisationUnitBatchHandler.flush();
 
1332
        
 
1333
        importObjectStore.deleteImportObjects( OrganisationUnit.class );
 
1334
        
 
1335
        log.info( "Imported OrganisationUnits" );
 
1336
    }
 
1337
    
 
1338
    private void importOrganisationUnitRelationships()
 
1339
    {
 
1340
        Map<Object, Integer> organisationUnitMapping = objectMappingGenerator.getOrganisationUnitMapping( false );
 
1341
        
 
1342
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( OrganisationUnitBatchHandler.class );
 
1343
 
 
1344
        batchHandler.init();
 
1345
        
 
1346
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( GroupMemberType.ORGANISATIONUNITRELATIONSHIP );
 
1347
        
 
1348
        for ( ImportObject importObject : importObjects )
 
1349
        {
 
1350
            GroupMemberAssociation object = (GroupMemberAssociation) importObject.getObject();
 
1351
            
 
1352
            OrganisationUnit child = organisationUnitService.getOrganisationUnit( organisationUnitMapping.get( object.getMemberId() ) );
 
1353
            
 
1354
            OrganisationUnit parent = organisationUnitService.getOrganisationUnit( organisationUnitMapping.get( object.getGroupId() ) );
 
1355
            
 
1356
            child.setParent( parent );
 
1357
            
 
1358
            batchHandler.updateObject( child );
 
1359
        }
 
1360
        
 
1361
        batchHandler.flush();
 
1362
        
 
1363
        importObjectStore.deleteImportObjects( GroupMemberType.ORGANISATIONUNITRELATIONSHIP );
 
1364
        
 
1365
        log.info( "Imported OrganisationUnit relationships" );
 
1366
    }
 
1367
 
 
1368
    private void importOrganisationUnitGroups()
 
1369
    {
 
1370
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( OrganisationUnitGroupBatchHandler.class );
 
1371
 
 
1372
        batchHandler.init();
 
1373
        
 
1374
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( OrganisationUnitGroup.class );
 
1375
        
 
1376
        for ( ImportObject importObject : importObjects )
 
1377
        {
 
1378
            OrganisationUnitGroup object = (OrganisationUnitGroup) importObject.getObject();
 
1379
 
 
1380
            NameMappingUtil.addOrganisationUnitGroupMapping( object.getId(), object.getName() );
 
1381
            
 
1382
            if ( importObject.getStatus() == ImportObjectStatus.UPDATE )
 
1383
            {
 
1384
                OrganisationUnitGroup compareObject = (OrganisationUnitGroup) importObject.getCompareObject();
 
1385
                
 
1386
                object.setId( compareObject.getId() );
 
1387
            }
 
1388
            
 
1389
            importObject.setObject( object );
 
1390
            
 
1391
            addOrUpdateObject( batchHandler, importObject );
 
1392
        }
 
1393
        
 
1394
        batchHandler.flush();
 
1395
        
 
1396
        importObjectStore.deleteImportObjects( OrganisationUnitGroup.class );
 
1397
        
 
1398
        log.info( "Imported OrganisationUnitGroups" );
 
1399
    }
 
1400
    
 
1401
    private void importOrganisationUnitGroupMembers()
 
1402
    {
 
1403
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( OrganisationUnitGroupMemberBatchHandler.class );
 
1404
        
 
1405
        importGroupMemberAssociation( batchHandler, GroupMemberType.ORGANISATIONUNITGROUP,
 
1406
            objectMappingGenerator.getOrganisationUnitGroupMapping( false ),
 
1407
            objectMappingGenerator.getOrganisationUnitMapping( false ) );
 
1408
        
 
1409
        log.info( "Imported OrganissationUnitGroup members" );
 
1410
    }
 
1411
 
 
1412
    private void importOrganisationUnitGroupSets()
 
1413
    {
 
1414
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( GroupSetBatchHandler.class );
 
1415
 
 
1416
        batchHandler.init();
 
1417
        
 
1418
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( OrganisationUnitGroupSet.class );
 
1419
        
 
1420
        for ( ImportObject importObject : importObjects )
 
1421
        {
 
1422
            OrganisationUnitGroupSet object = (OrganisationUnitGroupSet) importObject.getObject();
 
1423
 
 
1424
            NameMappingUtil.addGroupSetMapping( object.getId(), object.getName() );
 
1425
            
 
1426
            if ( importObject.getStatus() == ImportObjectStatus.UPDATE )
 
1427
            {
 
1428
                OrganisationUnitGroupSet compareObject = (OrganisationUnitGroupSet) importObject.getCompareObject();
 
1429
                
 
1430
                object.setId( compareObject.getId() );
 
1431
            }
 
1432
            
 
1433
            importObject.setObject( object );
 
1434
            
 
1435
            addOrUpdateObject( batchHandler, importObject );            
 
1436
        }
 
1437
        
 
1438
        batchHandler.flush();
 
1439
        
 
1440
        importObjectStore.deleteImportObjects( OrganisationUnitGroupSet.class );
 
1441
        
 
1442
        log.info( "Imported OrganisationUnitGroupSets" );
 
1443
    }
 
1444
 
 
1445
    private void importOrganisationUnitGroupSetMembers()
 
1446
    {
 
1447
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( GroupSetMemberBatchHandler.class );
 
1448
        
 
1449
        importGroupMemberAssociation( batchHandler, GroupMemberType.ORGANISATIONUNITGROUPSET,
 
1450
            objectMappingGenerator.getOrganisationUnitGroupSetMapping( false ),
 
1451
            objectMappingGenerator.getOrganisationUnitGroupMapping( false ) );
 
1452
        
 
1453
        log.info( "Imported OrganisationUnitGroupSet members" );
 
1454
    }
 
1455
 
 
1456
    private void importOrganisationUnitLevels()
 
1457
    {
 
1458
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( OrganisationUnitLevel.class );
 
1459
        
 
1460
        for ( ImportObject importObject : importObjects )
 
1461
        {
 
1462
            OrganisationUnitLevel object = (OrganisationUnitLevel) importObject.getObject();
 
1463
            
 
1464
            if ( importObject.getStatus() == ImportObjectStatus.UPDATE )
 
1465
            {
 
1466
                OrganisationUnitLevel compare = (OrganisationUnitLevel) importObject.getCompareObject();
 
1467
                
 
1468
                object = updateOrganisationUnitLevel( compare, object );
 
1469
                
 
1470
                organisationUnitService.updateOrganisationUnitLevel( object );                
 
1471
            }
 
1472
            else if ( importObject.getStatus() == ImportObjectStatus.NEW )
 
1473
            {
 
1474
                organisationUnitService.addOrganisationUnitLevel( object );
 
1475
            }
 
1476
        }
 
1477
        
 
1478
        importObjectStore.deleteImportObjects( OrganisationUnitLevel.class );
 
1479
        
 
1480
        log.info( "Imported OrganisationUnitLevels" );            
 
1481
    }
 
1482
 
 
1483
    private void importDataSetSourceAssociations()
 
1484
    {
 
1485
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( DataSetSourceAssociationBatchHandler.class );
 
1486
 
 
1487
        importGroupMemberAssociation( batchHandler, GroupMemberType.DATASET,
 
1488
            objectMappingGenerator.getDataSetMapping( false ), 
 
1489
            objectMappingGenerator.getOrganisationUnitMapping( false ) );
 
1490
        
 
1491
        log.info( "Imported DataSet Source associations" );
 
1492
    }
 
1493
    
 
1494
    private void importValidationRules()
 
1495
    {
 
1496
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( ValidationRule.class );
 
1497
        
 
1498
        for ( ImportObject importObject : importObjects )
 
1499
        {
 
1500
            ValidationRule object = (ValidationRule) importObject.getObject();
 
1501
            
 
1502
            if ( importObject.getStatus() == ImportObjectStatus.UPDATE )
 
1503
            {
 
1504
                ValidationRule compare = (ValidationRule) importObject.getCompareObject();
 
1505
                
 
1506
                validationRuleService.updateValidationRule( compare ); // Reload because of Expression
 
1507
                
 
1508
                object = updateValidationRule( compare, object );
 
1509
                
 
1510
                expressionService.updateExpression( object.getLeftSide() );
 
1511
                expressionService.updateExpression( object.getRightSide() );
 
1512
                                
 
1513
                validationRuleService.updateValidationRule( object );
 
1514
            }
 
1515
            else if ( importObject.getStatus() == ImportObjectStatus.NEW )
 
1516
            {
 
1517
                expressionService.addExpression( object.getLeftSide() );
 
1518
                expressionService.addExpression( object.getRightSide() );
 
1519
                                
 
1520
                validationRuleService.addValidationRule( object );
 
1521
            }
 
1522
        }
 
1523
        
 
1524
        importObjectStore.deleteImportObjects( ValidationRule.class );
 
1525
        
 
1526
        log.info( "Imported ValidationRules" );
 
1527
    }
 
1528
    
 
1529
    private void importPeriods()
 
1530
    {
 
1531
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( PeriodBatchHandler.class );
 
1532
        
 
1533
        batchHandler.init();
 
1534
        
 
1535
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( Period.class );
 
1536
        
 
1537
        for ( ImportObject importObject : importObjects )
 
1538
        {
 
1539
            Period period = (Period) importObject.getObject();
 
1540
            
 
1541
            NameMappingUtil.addPeriodMapping( period.getId(), period );
 
1542
            
 
1543
            addOrUpdateObject( batchHandler, importObject );
 
1544
        }
 
1545
        
 
1546
        batchHandler.flush();
 
1547
        
 
1548
        importObjectStore.deleteImportObjects( Period.class );
 
1549
        
 
1550
        log.info( "Imported Periods" );
 
1551
    }
 
1552
 
 
1553
    private void importReportTables()
 
1554
    {
 
1555
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( ReportTableBatchHandler.class );
 
1556
        
 
1557
        batchHandler.init();
 
1558
        
 
1559
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( ReportTable.class );
 
1560
        
 
1561
        for ( ImportObject importObject : importObjects )
 
1562
        {
 
1563
            ReportTable object = (ReportTable) importObject.getObject();
 
1564
            
 
1565
            NameMappingUtil.addReportTableMapping( object.getId(), object.getName() );
 
1566
            
 
1567
            if ( importObject.getStatus() == ImportObjectStatus.UPDATE )
 
1568
            {
 
1569
                ReportTable compareObject = (ReportTable) importObject.getCompareObject();
 
1570
                
 
1571
                object.setId( compareObject.getId() );
 
1572
            }
 
1573
            
 
1574
            importObject.setObject( object );
 
1575
            
 
1576
            addOrUpdateObject( batchHandler, importObject );
 
1577
        }
 
1578
        
 
1579
        batchHandler.flush();
 
1580
        
 
1581
        importObjectStore.deleteImportObjects( ReportTable.class );
 
1582
        
 
1583
        log.info( "Imported ReportTables" );
 
1584
    }
 
1585
 
 
1586
    private void importReportTableDataElements()
 
1587
    {
 
1588
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( ReportTableDataElementBatchHandler.class );
 
1589
        
 
1590
        importGroupMemberAssociation( batchHandler, GroupMemberType.REPORTTABLE_DATAELEMENT,
 
1591
            objectMappingGenerator.getReportTableMapping( false ),
 
1592
            objectMappingGenerator.getDataElementMapping( false ) );
 
1593
        
 
1594
        log.info( "Imported ReportTable DataElements" );
 
1595
    }
 
1596
 
 
1597
    private void importReportTableCategoryOptionCombos()
 
1598
    {
 
1599
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( ReportTableCategoryOptionComboBatchHandler.class );
 
1600
        
 
1601
        importGroupMemberAssociation( batchHandler, GroupMemberType.REPORTTABLE_CATEGORY_OPTION_COMBO,
 
1602
            objectMappingGenerator.getReportTableMapping( false ),
 
1603
            objectMappingGenerator.getCategoryOptionComboMapping( false ) );
 
1604
        
 
1605
        log.info( "Imported ReportTable CategoryOptionCombos" );
 
1606
    }
 
1607
 
 
1608
    private void importReportTableIndicators()
 
1609
    {
 
1610
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( ReportTableIndicatorBatchHandler.class );
 
1611
        
 
1612
        importGroupMemberAssociation( batchHandler, GroupMemberType.REPORTTABLE_INDICATOR,
 
1613
            objectMappingGenerator.getReportTableMapping( false ),
 
1614
            objectMappingGenerator.getIndicatorMapping( false ) );
 
1615
        
 
1616
        log.info( "Imported ReportTable Indicators" );
 
1617
    }
 
1618
 
 
1619
    private void importReportTableDataSets()
 
1620
    {
 
1621
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( ReportTableDataSetBatchHandler.class );
 
1622
        
 
1623
        importGroupMemberAssociation( batchHandler, GroupMemberType.REPORTTABLE_DATASET,
 
1624
            objectMappingGenerator.getReportTableMapping( false ),
 
1625
            objectMappingGenerator.getDataSetMapping( false ) );
 
1626
        
 
1627
        log.info( "Imported ReportTable DataSets" );
 
1628
    }
 
1629
 
 
1630
    private void importReportTablePeriods()
 
1631
    {
 
1632
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( ReportTablePeriodBatchHandler.class );
 
1633
        
 
1634
        importGroupMemberAssociation( batchHandler, GroupMemberType.REPORTTABLE_PERIOD,
 
1635
            objectMappingGenerator.getReportTableMapping( false ),
 
1636
            objectMappingGenerator.getPeriodMapping( false ) );
 
1637
        
 
1638
        log.info( "Imported ReportTable Periods" );
 
1639
    }
 
1640
 
 
1641
    private void importReportTableOrganisationUnits()
 
1642
    {
 
1643
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( ReportTableOrganisationUnitBatchHandler.class );
 
1644
        
 
1645
        importGroupMemberAssociation( batchHandler, GroupMemberType.REPORTTABLE_ORGANISATIONUNIT,
 
1646
            objectMappingGenerator.getReportTableMapping( false ),
 
1647
            objectMappingGenerator.getOrganisationUnitMapping( false ) );
 
1648
        
 
1649
        log.info( "Imported ReportTable OrganisationUnits" );
 
1650
    }
 
1651
 
 
1652
    private void importOlapURLs()
 
1653
    {
 
1654
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( OlapURL.class );
 
1655
        
 
1656
        for ( ImportObject importObject : importObjects )
 
1657
        {
 
1658
            OlapURL object = (OlapURL) importObject.getObject();
 
1659
            
 
1660
            if ( importObject.getStatus() == ImportObjectStatus.UPDATE )
 
1661
            {
 
1662
                OlapURL compare = (OlapURL) importObject.getObject();
 
1663
                
 
1664
                object = updateOlapURL( compare, object );
 
1665
                
 
1666
                olapURLService.updateOlapURL( object );
 
1667
            }
 
1668
            else if ( importObject.getStatus() == ImportObjectStatus.NEW )
 
1669
            {
 
1670
                olapURLService.saveOlapURL( object );
 
1671
            }            
 
1672
        }
 
1673
        
 
1674
        importObjectStore.deleteImportObjects( OlapURL.class );
 
1675
        
 
1676
        log.info( "Imported OlapURLs" );
 
1677
    }
 
1678
    
 
1679
    private void importCompleteDataSetRegistrations()
 
1680
    {
 
1681
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( CompleteDataSetRegistrationBatchHandler.class );
 
1682
 
 
1683
        batchHandler.init();
 
1684
        
 
1685
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( CompleteDataSetRegistration.class );
 
1686
 
 
1687
        Map<Object, Integer> dataSetMapping = objectMappingGenerator.getDataSetMapping( false );
 
1688
        Map<Object, Integer> periodMapping = objectMappingGenerator.getPeriodMapping( false );
 
1689
        Map<Object, Integer> sourceMapping = objectMappingGenerator.getOrganisationUnitMapping( false );
 
1690
        
 
1691
        for ( ImportObject importObject : importObjects )
 
1692
        {
 
1693
            CompleteDataSetRegistration registration = (CompleteDataSetRegistration) importObject.getObject();
 
1694
            
 
1695
            registration.getDataSet().setId( dataSetMapping.get( registration.getDataSet().getId() ) );
 
1696
            registration.getPeriod().setId( periodMapping.get( registration.getPeriod().getId() ) );
 
1697
            registration.getSource().setId( sourceMapping.get( registration.getSource().getId() ) );
 
1698
            
 
1699
            // -----------------------------------------------------------------
 
1700
            // Must check for existing registrations since this cannot be done
 
1701
            // during preview
 
1702
            // -----------------------------------------------------------------
 
1703
            
 
1704
            if ( !batchHandler.objectExists( registration ) )
 
1705
            {
 
1706
                batchHandler.addObject( registration );
 
1707
            }
 
1708
        }
 
1709
        
 
1710
        batchHandler.flush();
 
1711
        
 
1712
        importObjectStore.deleteImportObjects( CompleteDataSetRegistration.class );
 
1713
        
 
1714
        log.info( "Imported CompleteDataSetRegistrations" );
 
1715
    }
 
1716
    
 
1717
    private void importDataValues()
 
1718
    {
 
1719
        if ( lockingManager.currentImportContainsLockedData() )
 
1720
        {
 
1721
            log.warn( "Import file contained DataValues for locked periods" );
 
1722
        }
 
1723
        else
 
1724
        {
 
1725
            BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( DataValueBatchHandler.class );
 
1726
            
 
1727
            batchHandler.init();
 
1728
            
 
1729
            Map<Object, Integer> dataElementMapping = objectMappingGenerator.getDataElementMapping( false );
 
1730
            Map<Object, Integer> periodMapping = objectMappingGenerator.getPeriodMapping( false );
 
1731
            Map<Object, Integer> sourceMapping = objectMappingGenerator.getOrganisationUnitMapping( false );
 
1732
            Map<Object, Integer> categoryOptionComboMapping = objectMappingGenerator.getCategoryOptionComboMapping( false );
 
1733
            
 
1734
            Collection<ImportDataValue> importValues = importDataValueService.getImportDataValues( ImportObjectStatus.NEW );
 
1735
            
 
1736
            for ( ImportDataValue importValue : importValues )
 
1737
            {
 
1738
                DataValue value = importValue.getDataValue();
 
1739
                
 
1740
                value.getDataElement().setId( dataElementMapping.get( value.getDataElement().getId() ) );
 
1741
                value.getPeriod().setId( periodMapping.get( value.getPeriod().getId() ) );
 
1742
                value.getSource().setId( sourceMapping.get( value.getSource().getId() ) );
 
1743
                value.getOptionCombo().setId( categoryOptionComboMapping.get( value.getOptionCombo().getId() ) );
 
1744
                
 
1745
                // -----------------------------------------------------------------
 
1746
                // Must check for existing datavalues since this cannot be done
 
1747
                // during preview
 
1748
                // -----------------------------------------------------------------
 
1749
                
 
1750
                if ( !batchHandler.objectExists( value ) )
 
1751
                {
 
1752
                    batchHandler.addObject( value );
 
1753
                }
 
1754
            }
 
1755
            
 
1756
            batchHandler.flush();
 
1757
            
 
1758
            importDataValueService.deleteImportDataValues();
 
1759
            
 
1760
            log.info( "Imported DataValues" );
 
1761
        }
 
1762
    }
 
1763
    
 
1764
    // -------------------------------------------------------------------------
 
1765
    // Import - general supportive methods
 
1766
    // -------------------------------------------------------------------------
 
1767
 
 
1768
    private boolean containsIdentifier( String formula, int identifier )
 
1769
    {
 
1770
        if ( formula != null )
 
1771
        {        
 
1772
            Pattern pattern = Pattern.compile( "(\\[\\d+\\" + SEPARATOR + "\\d+\\])" );
 
1773
            Matcher matcher = pattern.matcher( formula );
 
1774
            
 
1775
            while ( matcher.find() )
 
1776
            {
 
1777
                String match = matcher.group();
 
1778
                
 
1779
                match = match.replaceAll( "[\\[\\]]", "" );
 
1780
                
 
1781
                String matchId = match.substring( 0, match.indexOf( SEPARATOR ) );                
 
1782
                
 
1783
                if ( matchId.equals( String.valueOf( identifier ) ) )
 
1784
                {
 
1785
                    return true;
 
1786
                }
 
1787
            }
 
1788
        }
 
1789
        
 
1790
        return false;
 
1791
    }
 
1792
    
 
1793
    private void addOrUpdateObject( BatchHandler batchHandler, ImportObject importObject )
 
1794
    {
 
1795
        if ( importObject.getStatus() == ImportObjectStatus.NEW )
 
1796
        {
 
1797
            batchHandler.addObject( importObject.getObject() );
 
1798
        }
 
1799
        else if ( importObject.getStatus() == ImportObjectStatus.UPDATE )
 
1800
        {
 
1801
            batchHandler.updateObject( importObject.getObject() );
 
1802
        }
 
1803
 
 
1804
        // ---------------------------------------------------------------------
 
1805
        // Ignoring ImportObjects of type MATCH
 
1806
        // ---------------------------------------------------------------------
 
1807
    }
 
1808
    
 
1809
    private void importGroupMemberAssociation( BatchHandler batchHandler, GroupMemberType type,
 
1810
        Map<Object, Integer> groupMapping, Map<Object, Integer> memberMapping )
 
1811
    {
 
1812
        GroupMemberAssociationVerifier.clear();
 
1813
        
 
1814
        batchHandler.init();
 
1815
        
 
1816
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( type );
 
1817
        
 
1818
        for ( ImportObject importObject : importObjects )
 
1819
        {
 
1820
            GroupMemberAssociation object = (GroupMemberAssociation) importObject.getObject();
 
1821
 
 
1822
            object.setGroupId( groupMapping.get( object.getGroupId() ) );
 
1823
            object.setMemberId( memberMapping.get( object.getMemberId() ) );
 
1824
            
 
1825
            if ( GroupMemberAssociationVerifier.isUnique( object, type ) && !batchHandler.objectExists( object ) )
 
1826
            {
 
1827
                batchHandler.addObject( object );
 
1828
            }            
 
1829
        }
 
1830
        
 
1831
        batchHandler.flush();
 
1832
        
 
1833
        importObjectStore.deleteImportObjects( type );
 
1834
    }
 
1835
 
 
1836
    // -------------------------------------------------------------------------
 
1837
    // Update - supportive methods
 
1838
    // -------------------------------------------------------------------------
 
1839
    
 
1840
    private OrganisationUnitLevel updateOrganisationUnitLevel( OrganisationUnitLevel original, OrganisationUnitLevel update )
 
1841
    {
 
1842
        original.setLevel( update.getLevel() );
 
1843
        original.setName( update.getName() );
 
1844
        
 
1845
        return original;
 
1846
    }
 
1847
    
 
1848
    private ValidationRule updateValidationRule( ValidationRule original, ValidationRule update )
 
1849
    {
 
1850
        original.setName( update.getName() );
 
1851
        original.setDescription( update.getDescription() );
 
1852
        original.setType( update.getType() );
 
1853
        original.setOperator( update.getOperator() );
 
1854
        original.getLeftSide().setExpression( update.getLeftSide().getExpression() );
 
1855
        original.getLeftSide().setDescription( update.getLeftSide().getDescription()  );
 
1856
        original.getLeftSide().setDataElementsInExpression( update.getLeftSide().getDataElementsInExpression() );
 
1857
        original.getRightSide().setExpression( update.getRightSide().getExpression() );
 
1858
        original.getRightSide().setDescription( update.getRightSide().getDescription() );
 
1859
        original.getRightSide().setDataElementsInExpression( update.getRightSide().getDataElementsInExpression() );
 
1860
        
 
1861
        return original;
 
1862
    }
 
1863
    
 
1864
    private DataValue updateDataValue( DataValue original, DataValue update )
 
1865
    {
 
1866
        original.setDataElement( update.getDataElement() );
 
1867
        original.setPeriod( update.getPeriod() );
 
1868
        original.setSource( update.getSource() );
 
1869
        original.setValue( update.getValue() );
 
1870
        original.setStoredBy( update.getStoredBy() );
 
1871
        original.setTimestamp( update.getTimestamp() );
 
1872
        original.setComment( update.getComment() );
 
1873
        original.setOptionCombo( update.getOptionCombo() );
 
1874
        
 
1875
        return original;
 
1876
    }
 
1877
    
 
1878
    private OlapURL updateOlapURL( OlapURL original, OlapURL update )
 
1879
    {
 
1880
        original.setName( update.getName() );
 
1881
        original.setUrl( update.getUrl() );
 
1882
        
 
1883
        return original;
 
1884
    }
 
1885
 
 
1886
    // -------------------------------------------------------------------------
 
1887
    // Cascade delete - supportive methods
 
1888
    // -------------------------------------------------------------------------
 
1889
    
 
1890
    private void deleteMemberAssociations( GroupMemberType groupMemberType, int memberId )
 
1891
    {
 
1892
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( groupMemberType );
 
1893
        
 
1894
        for ( ImportObject importObject : importObjects )
 
1895
        {
 
1896
            GroupMemberAssociation association = (GroupMemberAssociation) importObject.getObject();
 
1897
            
 
1898
            if ( association.getMemberId() == memberId )
 
1899
            {
 
1900
                importObjectStore.deleteImportObject( importObject );
 
1901
            }
 
1902
        }
 
1903
    }
 
1904
 
 
1905
    private void deleteGroupAssociations( GroupMemberType groupMemberType, int groupId )
 
1906
    {
 
1907
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( groupMemberType);
 
1908
        
 
1909
        for ( ImportObject importObject : importObjects )
 
1910
        {
 
1911
            GroupMemberAssociation association = (GroupMemberAssociation) importObject.getObject();
 
1912
            
 
1913
            if ( association.getGroupId() == groupId )
 
1914
            {
 
1915
                importObjectStore.deleteImportObject( importObject );
 
1916
            }
 
1917
        }   
 
1918
    }
 
1919
    
 
1920
    private void deleteIndicatorsContainingDataElement( int dataElementId )
 
1921
    {
 
1922
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( Indicator.class );
 
1923
        
 
1924
        for ( ImportObject importObject : importObjects )
 
1925
        {
 
1926
            Indicator indicator = (Indicator) importObject.getObject();
 
1927
            
 
1928
            if ( containsIdentifier( indicator.getNumerator(), dataElementId ) || containsIdentifier( indicator.getDenominator(), dataElementId ) )
 
1929
            {
 
1930
                importObjectStore.deleteImportObject( importObject );
 
1931
                
 
1932
                deleteMemberAssociations( GroupMemberType.INDICATORGROUP, indicator.getId() );
 
1933
                
 
1934
                deleteMemberAssociations( GroupMemberType.DATADICTIONARY_INDICATOR, indicator.getId() );
 
1935
                
 
1936
                deleteMemberAssociations( GroupMemberType.REPORTTABLE_INDICATOR, indicator.getId() );
 
1937
            }   
 
1938
        }   
 
1939
    }
 
1940
    
 
1941
    private void deleteIndicatorsWithIndicatorType( int indicatorTypeId )
 
1942
    {
 
1943
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( Indicator.class );
 
1944
        
 
1945
        for ( ImportObject importObject : importObjects )
 
1946
        {
 
1947
            Indicator indicator = (Indicator) importObject.getObject();
 
1948
            
 
1949
            if ( indicator.getIndicatorType().getId() == indicatorTypeId )
 
1950
            {
 
1951
                importObjectStore.deleteImportObject( importObject );
 
1952
                
 
1953
                deleteMemberAssociations( GroupMemberType.INDICATORGROUP, indicator.getId() );
 
1954
                
 
1955
                deleteMemberAssociations( GroupMemberType.DATADICTIONARY_INDICATOR, indicator.getId() );
 
1956
                
 
1957
                deleteMemberAssociations( GroupMemberType.REPORTTABLE_INDICATOR, indicator.getId() );
 
1958
            }
 
1959
        }
 
1960
    }
 
1961
    
 
1962
    private void deleteCompleteDataSetRegistrationsByDataSet( int dataSetId )
 
1963
    {
 
1964
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( CompleteDataSetRegistration.class );
 
1965
        
 
1966
        for ( ImportObject importObject : importObjects )
 
1967
        {
 
1968
            CompleteDataSetRegistration registration = (CompleteDataSetRegistration) importObject.getObject();
 
1969
            
 
1970
            if ( registration.getDataSet().getId() == dataSetId )
 
1971
            {
 
1972
                importObjectStore.deleteImportObject( importObject );
 
1973
            }
 
1974
        }
 
1975
    }
 
1976
 
 
1977
    private void deleteCompleteDataSetRegistrationsBySource( int sourceId )
 
1978
    {
 
1979
        Collection<ImportObject> importObjects = importObjectStore.getImportObjects( CompleteDataSetRegistration.class );
 
1980
        
 
1981
        for ( ImportObject importObject : importObjects )
 
1982
        {
 
1983
            CompleteDataSetRegistration registration = (CompleteDataSetRegistration) importObject.getObject();
 
1984
            
 
1985
            if ( registration.getSource().getId() == sourceId )
 
1986
            {
 
1987
                importObjectStore.deleteImportObject( importObject );
 
1988
            }
 
1989
        }
 
1990
    }
 
1991
}