~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/mapping/NameMappingUtil.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.mapping;
 
2
 
 
3
/*
 
4
 * Copyright (c) 2004-2007, University of Oslo
 
5
 * All rights reserved.
 
6
 *
 
7
 * Redistribution and use in source and binary forms, with or without
 
8
 * modification, are permitted provided that the following conditions are met:
 
9
 * * Redistributions of source code must retain the above copyright notice, this
 
10
 *   list of conditions and the following disclaimer.
 
11
 * * Redistributions in binary form must reproduce the above copyright notice,
 
12
 *   this list of conditions and the following disclaimer in the documentation
 
13
 *   and/or other materials provided with the distribution.
 
14
 * * Neither the name of the HISP project nor the names of its contributors may
 
15
 *   be used to endorse or promote products derived from this software without
 
16
 *   specific prior written permission.
 
17
 *
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
21
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 
22
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 
23
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
24
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 
25
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
28
 */
 
29
 
 
30
import java.util.HashMap;
 
31
import java.util.Map;
 
32
 
 
33
import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 
34
import org.hisp.dhis.period.Period;
 
35
import org.hisp.dhis.system.util.LoggingHashMap;
 
36
 
 
37
/**
 
38
 * @author Lars Helge Overland
 
39
 * @version $Id: NameMappingUtil.java 5727 2008-09-18 17:48:54Z larshelg $
 
40
 */
 
41
public class NameMappingUtil
 
42
{
 
43
    private static ThreadLocal<Map<Object, String>> categoryMap = new ThreadLocal<Map<Object, String>>();
 
44
    private static ThreadLocal<Map<Object, String>> categoryOptionMap = new ThreadLocal<Map<Object, String>>();
 
45
    private static ThreadLocal<Map<Object, String>> categoryComboMap = new ThreadLocal<Map<Object, String>>();
 
46
    private static ThreadLocal<Map<Object, DataElementCategoryOptionCombo>> categoryOptionComboMap = new ThreadLocal<Map<Object, DataElementCategoryOptionCombo>>();    
 
47
    private static ThreadLocal<Map<Object, String>> dataElementMap = new ThreadLocal<Map<Object, String>>();
 
48
    private static ThreadLocal<Map<Object, String>> dataElementGroupMap = new ThreadLocal<Map<Object, String>>();
 
49
    private static ThreadLocal<Map<Object, String>> indicatorMap = new ThreadLocal<Map<Object, String>>();
 
50
    private static ThreadLocal<Map<Object, String>> indicatorGroupMap = new ThreadLocal<Map<Object, String>>();
 
51
    private static ThreadLocal<Map<Object, String>> indicatorTypeMap = new ThreadLocal<Map<Object, String>>();
 
52
    private static ThreadLocal<Map<Object, String>> dataDictionaryMap = new ThreadLocal<Map<Object,String>>();
 
53
    private static ThreadLocal<Map<Object, Period>> periodMap = new ThreadLocal<Map<Object, Period>>();
 
54
    private static ThreadLocal<Map<Object, String>> dataSetMap = new ThreadLocal<Map<Object,String>>();
 
55
    private static ThreadLocal<Map<Object, String>> organisationUnitMap = new ThreadLocal<Map<Object, String>>();
 
56
    private static ThreadLocal<Map<Object, String>> organisationUnitGroupMap = new ThreadLocal<Map<Object, String>>();
 
57
    private static ThreadLocal<Map<Object, String>> organisationUnitGroupSetMap = new ThreadLocal<Map<Object, String>>();
 
58
    private static ThreadLocal<Map<Object, String>> reportTableMap = new ThreadLocal<Map<Object,String>>();
 
59
    private static ThreadLocal<Map<Object, String>> dataElementAggregationOperatorMap = new ThreadLocal<Map<Object,String>>();
 
60
    
 
61
    // -------------------------------------------------------------------------
 
62
    // Control
 
63
    // -------------------------------------------------------------------------
 
64
    
 
65
    public static void clearMapping()
 
66
    {
 
67
        categoryMap.remove();
 
68
        categoryOptionMap.remove();
 
69
        categoryComboMap.remove();
 
70
        categoryOptionComboMap.remove();
 
71
        dataElementMap.remove();
 
72
        dataElementGroupMap.remove();
 
73
        indicatorMap.remove();
 
74
        indicatorGroupMap.remove();
 
75
        indicatorTypeMap.remove();
 
76
        dataDictionaryMap.remove();
 
77
        periodMap.remove();
 
78
        dataSetMap.remove();
 
79
        organisationUnitMap.remove();
 
80
        organisationUnitGroupMap.remove();
 
81
        organisationUnitGroupSetMap.remove();
 
82
        reportTableMap.remove();
 
83
        dataElementAggregationOperatorMap.remove();
 
84
    }
 
85
 
 
86
    // -------------------------------------------------------------------------
 
87
    // Category
 
88
    // -------------------------------------------------------------------------
 
89
 
 
90
    /**
 
91
     * Adds a map entry with DataElementCategory identifier as key and name as value.
 
92
     */
 
93
    public static void addCategoryMapping( Object categoryId, String categoryName )
 
94
    {
 
95
        put( categoryMap, categoryId, categoryName );
 
96
    }
 
97
    
 
98
    /**
 
99
     * Returns a Map with all DataElementCategory identifier and name entries.
 
100
     */
 
101
    public static Map<Object, String> getCategoryMap()
 
102
    {
 
103
        return categoryMap.get() != null ? new HashMap<Object, String>( categoryMap.get() ) : new HashMap<Object, String>();
 
104
    }
 
105
 
 
106
    // -------------------------------------------------------------------------
 
107
    // CategoryOption
 
108
    // -------------------------------------------------------------------------
 
109
 
 
110
    /**
 
111
     * Adds a map entry with DataElementCategoryOption identifier as key and name as value.
 
112
     */
 
113
    public static void addCategoryOptionMapping( Object categoryOptionId, String categoryOptionName )
 
114
    {
 
115
        put( categoryOptionMap, categoryOptionId, categoryOptionName );
 
116
    }
 
117
    
 
118
    /**
 
119
     * Returns a Map with all DataElementCategoryOption identifier and name entries.
 
120
     */
 
121
    public static Map<Object, String> getCategoryOptionMap()
 
122
    {
 
123
        return categoryOptionMap.get() != null ? new HashMap<Object, String>( categoryOptionMap.get() ) : new HashMap<Object, String>();
 
124
    }
 
125
 
 
126
    // -------------------------------------------------------------------------
 
127
    // CategoryCombo
 
128
    // -------------------------------------------------------------------------
 
129
 
 
130
    /**
 
131
     * Adds a map entry with DataElementCategoryCombo identifier as key and name as value.
 
132
     */
 
133
    public static void addCategoryComboMapping( Object categoryComboId, String categoryComboName )
 
134
    {
 
135
        put( categoryComboMap, categoryComboId, categoryComboName );
 
136
    }
 
137
    
 
138
    /**
 
139
     * Returns a Map with all DataElementCategoryCombo identifier and name entries.
 
140
     */
 
141
    public static Map<Object, String> getCategoryComboMap()
 
142
    {
 
143
        return categoryComboMap.get() != null ? new HashMap<Object, String>( categoryComboMap.get() ) : new HashMap<Object, String>();
 
144
    }
 
145
 
 
146
    // -------------------------------------------------------------------------
 
147
    // CategoryOptionCombo
 
148
    // -------------------------------------------------------------------------
 
149
 
 
150
    /**
 
151
     * Adds a map entry with DataElementCategoryOptionCombo identifier as key
 
152
     * and the DataElementCategoryOptionCombo as value.
 
153
     */
 
154
    public static void addCategoryOptionComboMapping( Object categoryOptionComboId, DataElementCategoryOptionCombo categoryOptionCombo )
 
155
    {
 
156
        Map<Object, DataElementCategoryOptionCombo> map = categoryOptionComboMap.get();
 
157
        
 
158
        if ( map == null )
 
159
        {
 
160
            map = new HashMap<Object, DataElementCategoryOptionCombo>();
 
161
        }
 
162
        
 
163
        if ( !map.containsKey( categoryOptionComboId ) )
 
164
        {
 
165
            map.put( categoryOptionComboId, categoryOptionCombo );
 
166
            
 
167
            categoryOptionComboMap.set( map );
 
168
        }
 
169
    }
 
170
    
 
171
    /**
 
172
     * Returns a Map with all DataElementCategoryOption identifier and
 
173
     * DataElementCategoryOptionCombo entries.
 
174
     */
 
175
    public static Map<Object, DataElementCategoryOptionCombo> getCategoryOptionComboMap()
 
176
    {
 
177
        return categoryOptionComboMap.get() != null ? new HashMap<Object, DataElementCategoryOptionCombo>( categoryOptionComboMap.get() ) : new HashMap<Object, DataElementCategoryOptionCombo>();
 
178
    }
 
179
 
 
180
    // -------------------------------------------------------------------------
 
181
    // DataElement
 
182
    // -------------------------------------------------------------------------
 
183
 
 
184
    /**
 
185
     * Adds a map entry with DataElement identifier as key and name as value.
 
186
     */
 
187
    public static void addDataElementMapping( Object dataElementId, String dataElementName )
 
188
    {
 
189
        put( dataElementMap, dataElementId, dataElementName );
 
190
    }
 
191
    
 
192
    /**
 
193
     * Returns a map with all DataElement identifier and name entries.
 
194
     */
 
195
    public static Map<Object, String> getDataElementMap()
 
196
    {
 
197
        return dataElementMap.get() != null ? new HashMap<Object, String>( dataElementMap.get() ) : new HashMap<Object, String>();
 
198
    }
 
199
 
 
200
    // -------------------------------------------------------------------------
 
201
    // DataElementGroup
 
202
    // -------------------------------------------------------------------------
 
203
 
 
204
    /**
 
205
     * Adds a map entry with DataElementGroup identifier as key and name as value.
 
206
     */
 
207
    public static void addDataElementGroupMapping( Object groupId, String groupName )
 
208
    {
 
209
        put( dataElementGroupMap, groupId, groupName );
 
210
    }
 
211
    
 
212
    /**
 
213
     * Returns a map with all DataElementGroup identifier and name entries. 
 
214
     */
 
215
    public static Map<Object, String> getDataElementGroupMap()
 
216
    {
 
217
        return dataElementGroupMap.get() != null ? new HashMap<Object, String>( dataElementGroupMap.get() ) : new HashMap<Object, String>();
 
218
    }
 
219
 
 
220
    // -------------------------------------------------------------------------
 
221
    // Indicator
 
222
    // -------------------------------------------------------------------------
 
223
 
 
224
    /**
 
225
     * Adds a map entry with Indicator identifier as key and name as value.
 
226
     */
 
227
    public static void addIndicatorMapping( Object indicatorId, String indicatorName )
 
228
    {
 
229
        put( indicatorMap, indicatorId, indicatorName );
 
230
    }
 
231
    
 
232
    /**
 
233
     * Returns a map with all Indicator identifier and name entries.
 
234
     */
 
235
    public static Map<Object, String> getIndicatorMap()
 
236
    {
 
237
        return indicatorMap.get() != null ? new HashMap<Object, String>( indicatorMap.get() ) : new HashMap<Object, String>();
 
238
    }
 
239
 
 
240
    // -------------------------------------------------------------------------
 
241
    // IndicatorGroup
 
242
    // -------------------------------------------------------------------------
 
243
 
 
244
    /**
 
245
     * Adds a map entry with IndicatorGroup identifier as key and name as value.
 
246
     */
 
247
    public static void addIndicatorGroupMapping( Object groupId, String groupName )
 
248
    {
 
249
        put( indicatorGroupMap, groupId, groupName );
 
250
    }
 
251
    
 
252
    /**
 
253
     * Returns a map with all Indicator identifier and name entries.
 
254
     */
 
255
    public static Map<Object, String> getIndicatorGroupMap()
 
256
    {
 
257
        return indicatorGroupMap.get() != null ? new HashMap<Object, String>( indicatorGroupMap.get() ) : new HashMap<Object, String>();
 
258
    }
 
259
 
 
260
    // -------------------------------------------------------------------------
 
261
    // IndicatorType
 
262
    // -------------------------------------------------------------------------
 
263
 
 
264
    /**
 
265
     * Adds a map entry with IndicatorType identifier as key and name as value.
 
266
     */
 
267
    public static void addIndicatorTypeMapping( Object typeId, String typeName )
 
268
    {
 
269
        put( indicatorTypeMap, typeId, typeName );
 
270
    }
 
271
    
 
272
    /**
 
273
     * Returns a map with all IndicatorType identifier and name entries.
 
274
     */
 
275
    public static Map<Object, String> getIndicatorTypeMap()
 
276
    {
 
277
        return indicatorTypeMap.get() != null ? new HashMap<Object, String>( indicatorTypeMap.get() ) : new HashMap<Object, String>();
 
278
    }
 
279
 
 
280
    // -------------------------------------------------------------------------
 
281
    // DataDictionary
 
282
    // -------------------------------------------------------------------------
 
283
 
 
284
    /**
 
285
     * Adds a map entry with DataDictionary identifier as key and name as value.
 
286
     */
 
287
    public static void addDataDictionaryMapping( Object dictionaryId, String dictionaryName )
 
288
    {
 
289
        put( dataDictionaryMap, dictionaryId, dictionaryName );
 
290
    }
 
291
    
 
292
    /**
 
293
     * Returns a map with all DataDictionary identifier and name entries.
 
294
     */
 
295
    public static Map<Object, String> getDataDictionaryMap()
 
296
    {
 
297
        return dataDictionaryMap.get() != null ? new HashMap<Object, String>( dataDictionaryMap.get() ) : new HashMap<Object, String>();
 
298
    }
 
299
    
 
300
    // -------------------------------------------------------------------------
 
301
    // Period
 
302
    // -------------------------------------------------------------------------
 
303
 
 
304
    /**
 
305
     * Adds a map entry with Period identifier as key and the period as value.
 
306
     */
 
307
    public static void addPeriodMapping( Object periodId, Period period )
 
308
    {
 
309
        Map<Object, Period> map = periodMap.get();
 
310
        
 
311
        if ( map == null )
 
312
        {
 
313
            map = new HashMap<Object, Period>();
 
314
        }
 
315
        
 
316
        map.put( periodId, period );
 
317
        
 
318
        periodMap.set( map );
 
319
    }
 
320
        
 
321
    /**
 
322
     * Returns a map with all Period identifier and period entries.
 
323
     */
 
324
    public static Map<Object, Period> getPeriodMap()
 
325
    {
 
326
        return periodMap.get() != null ? new HashMap<Object, Period>( periodMap.get() ) : new HashMap<Object, Period>();
 
327
    }
 
328
    
 
329
    // -------------------------------------------------------------------------
 
330
    // DataSet
 
331
    // -------------------------------------------------------------------------
 
332
 
 
333
    /**
 
334
     * Adds a map entry with DataSet identifier as key and name as value.
 
335
     */
 
336
    public static void addDataSetMapping( Object dataSetId, String dataSetName )
 
337
    {
 
338
        put( dataSetMap, dataSetId, dataSetName );
 
339
    }
 
340
    
 
341
    /**
 
342
     * Returns a map with all DataSet identifier and name entries.
 
343
     */
 
344
    public static Map<Object, String> getDataSetMap()
 
345
    {
 
346
        return dataSetMap.get() != null ? new HashMap<Object, String>( dataSetMap.get() ) : new HashMap<Object, String>();
 
347
    }
 
348
    
 
349
    // -------------------------------------------------------------------------
 
350
    // OrganisationUnit
 
351
    // -------------------------------------------------------------------------
 
352
 
 
353
    /**
 
354
     * Adds a map entry with OrganisationUnit identifier as key and name as value.
 
355
     */
 
356
    public static void addOrganisationUnitMapping( Object organisationUnitId, String organisationUnitName )
 
357
    {
 
358
        put( organisationUnitMap, organisationUnitId, organisationUnitName );
 
359
    }
 
360
    
 
361
    /**
 
362
     * Returns a map with all OrganisationUnit identifier and name entries.
 
363
     */
 
364
    public static Map<Object, String> getOrganisationUnitMap()
 
365
    {
 
366
        return organisationUnitMap.get() != null ? new HashMap<Object, String>( organisationUnitMap.get() ) : new HashMap<Object, String>();
 
367
    }
 
368
 
 
369
    // -------------------------------------------------------------------------
 
370
    // OrganisationUnitGroup
 
371
    // -------------------------------------------------------------------------
 
372
 
 
373
    /**
 
374
     * Adds a map entry with OrganisationUnitGroup identifier as key and name as value.
 
375
     */
 
376
    public static void addOrganisationUnitGroupMapping( Object groupId, String groupName )
 
377
    {
 
378
        put( organisationUnitGroupMap, groupId, groupName );
 
379
    }
 
380
    
 
381
    /**
 
382
     * Returns a map with all OrganisationUnitGroup identifier and name entries.
 
383
     */
 
384
    public static Map<Object, String> getOrganisationUnitGroupMap()
 
385
    {
 
386
        return organisationUnitGroupMap.get() != null ? new HashMap<Object, String>( organisationUnitGroupMap.get() ) : new HashMap<Object, String>();
 
387
    }
 
388
    
 
389
    // -------------------------------------------------------------------------
 
390
    // OrganisationUnitGroupSet
 
391
    // -------------------------------------------------------------------------
 
392
    
 
393
    /**
 
394
     * Adds a map entry with OrganisationUnitGroupSet identifier as key and name as value.
 
395
     */
 
396
    public static void addGroupSetMapping( Object groupSetId, String groupSetName )
 
397
    {
 
398
        put( organisationUnitGroupSetMap, groupSetId, groupSetName );
 
399
    }
 
400
        
 
401
    /**
 
402
     * Returns a map with all OrganisationUnitGroupSet identifier and name entries.
 
403
     */
 
404
    public static Map<Object, String> getGroupSetMap()
 
405
    {
 
406
        return organisationUnitGroupSetMap.get() != null ? new HashMap<Object, String>( organisationUnitGroupSetMap.get() ) : new HashMap<Object, String>();
 
407
    }
 
408
 
 
409
    // -------------------------------------------------------------------------
 
410
    // ReportTable
 
411
    // -------------------------------------------------------------------------
 
412
    
 
413
    /**
 
414
     * Adds a map entry with ReportTable identifier as key and name as value.
 
415
     */
 
416
    public static void addReportTableMapping( Object reportTableId, String reportTableName )
 
417
    {
 
418
        put( reportTableMap, reportTableId, reportTableName );
 
419
    }
 
420
    
 
421
    /**
 
422
     * Returns a map with all ReportTable identifier and name entries.
 
423
     */
 
424
    public static Map<Object, String> getReportTableMap()
 
425
    {
 
426
        return reportTableMap.get() != null ? new HashMap<Object, String>( reportTableMap.get() ) : new HashMap<Object, String>();
 
427
    }
 
428
 
 
429
    // -------------------------------------------------------------------------
 
430
    // ReportTable
 
431
    // -------------------------------------------------------------------------
 
432
    
 
433
    public static void addDataElementAggregationOperatorMapping( Object dataElementId, String aggregationOperator )
 
434
    {
 
435
        put( dataElementAggregationOperatorMap, dataElementId, aggregationOperator );
 
436
    }
 
437
    
 
438
    public static Map<Object, String> getDataElementAggregationOperatorMap()
 
439
    {
 
440
        return dataElementAggregationOperatorMap.get() != null ? new LoggingHashMap<Object, String>( dataElementAggregationOperatorMap.get() ) : new HashMap<Object, String>();
 
441
    }
 
442
    
 
443
    // -------------------------------------------------------------------------
 
444
    // Supportive methods
 
445
    // -------------------------------------------------------------------------
 
446
    
 
447
    private static void put( ThreadLocal<Map<Object, String>> threadLocal, Object key, String value )
 
448
    {
 
449
        Map<Object, String> map = threadLocal.get();
 
450
        
 
451
        if ( map == null )
 
452
        {
 
453
            map = new HashMap<Object, String>();
 
454
        }
 
455
        
 
456
        if ( !map.containsKey( key ) )
 
457
        {
 
458
            map.put( key, value );
 
459
        
 
460
            threadLocal.set( map );
 
461
        }
 
462
    }
 
463
}