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

« back to all changes in this revision

Viewing changes to dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/resourcetable/DefaultResourceTableService.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.resourcetable;
 
2
 
 
3
/*
 
4
 * Copyright (c) 2004-2007, University of Oslo
 
5
 * All rights reserved.
 
6
 *
 
7
 * Redistribution and use in source and binary forms, with or without
 
8
 * modification, are permitted provided that the following conditions are met:
 
9
 * * Redistributions of source code must retain the above copyright notice, this
 
10
 *   list of conditions and the following disclaimer.
 
11
 * * Redistributions in binary form must reproduce the above copyright notice,
 
12
 *   this list of conditions and the following disclaimer in the documentation
 
13
 *   and/or other materials provided with the distribution.
 
14
 * * Neither the name of the HISP project nor the names of its contributors may
 
15
 *   be used to endorse or promote products derived from this software without
 
16
 *   specific prior written permission.
 
17
 *
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
21
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 
22
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 
23
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
24
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 
25
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
28
 */
 
29
 
 
30
import java.util.Collection;
 
31
import java.util.HashMap;
 
32
import java.util.Map;
 
33
 
 
34
import org.apache.commons.logging.Log;
 
35
import org.apache.commons.logging.LogFactory;
 
36
import org.hisp.dhis.resourcetable.DataElementCategoryOptionComboName;
 
37
import org.hisp.dhis.resourcetable.GroupSetStructure;
 
38
import org.hisp.dhis.resourcetable.OrganisationUnitStructure;
 
39
import org.hisp.dhis.resourcetable.ResourceTableService;
 
40
import org.hisp.dhis.resourcetable.ResourceTableStore;
 
41
import org.hisp.dhis.jdbc.BatchHandlerFactory;
 
42
import org.hisp.dhis.jdbc.BatchHandler;
 
43
import org.hisp.dhis.jdbc.batchhandler.GroupSetStructureBatchHandler;
 
44
import org.hisp.dhis.jdbc.batchhandler.OrganisationUnitStructureBatchHandler;
 
45
import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 
46
import org.hisp.dhis.dataelement.DataElementCategoryOptionComboService;
 
47
import org.hisp.dhis.organisationunit.OrganisationUnit;
 
48
import org.hisp.dhis.organisationunit.OrganisationUnitGroup;
 
49
import org.hisp.dhis.organisationunit.OrganisationUnitGroupService;
 
50
import org.hisp.dhis.organisationunit.OrganisationUnitGroupSet;
 
51
import org.hisp.dhis.organisationunit.OrganisationUnitService;
 
52
 
 
53
/**
 
54
 * @author Lars Helge Overland
 
55
 * @version $Id: DefaultResourceTableService.java 5459 2008-06-26 01:12:03Z larshelg $
 
56
 */
 
57
public class DefaultResourceTableService
 
58
    implements ResourceTableService
 
59
{
 
60
    private static final Log LOG = LogFactory.getLog( DefaultResourceTableService.class );
 
61
    
 
62
    // -------------------------------------------------------------------------
 
63
    // Dependencies
 
64
    // -------------------------------------------------------------------------
 
65
 
 
66
    private ResourceTableStore resourceTableStore;
 
67
 
 
68
    public void setResourceTableStore( ResourceTableStore resourceTableStore )
 
69
    {
 
70
        this.resourceTableStore = resourceTableStore;
 
71
    }
 
72
    
 
73
    private OrganisationUnitService organisationUnitService;
 
74
 
 
75
    public void setOrganisationUnitService( OrganisationUnitService organisationUnitService )
 
76
    {
 
77
        this.organisationUnitService = organisationUnitService;
 
78
    }
 
79
 
 
80
    private OrganisationUnitGroupService organisationUnitGroupService;
 
81
 
 
82
    public void setOrganisationUnitGroupService( OrganisationUnitGroupService organisationUnitGroupService )
 
83
    {
 
84
        this.organisationUnitGroupService = organisationUnitGroupService;
 
85
    }
 
86
    
 
87
    private DataElementCategoryOptionComboService categoryOptionComboService;
 
88
 
 
89
    public void setCategoryOptionComboService( DataElementCategoryOptionComboService categoryOptionComboService )
 
90
    {
 
91
        this.categoryOptionComboService = categoryOptionComboService;
 
92
    }
 
93
    
 
94
    private BatchHandlerFactory batchHandlerFactory;
 
95
 
 
96
    public void setBatchHandlerFactory( BatchHandlerFactory batchHandlerFactory )
 
97
    {
 
98
        this.batchHandlerFactory = batchHandlerFactory;
 
99
    }
 
100
    
 
101
    // -------------------------------------------------------------------------
 
102
    // OrganisationUnitStructure
 
103
    // -------------------------------------------------------------------------
 
104
    
 
105
    public void generateOrganisationUnitStructures()
 
106
    {
 
107
        resourceTableStore.deleteOrganisationUnitStructures();
 
108
        
 
109
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( OrganisationUnitStructureBatchHandler.class );
 
110
        
 
111
        batchHandler.init();
 
112
        
 
113
        for ( int i = 0; i < 8; i++ )
 
114
        {
 
115
            int level = i + 1;
 
116
            
 
117
            Collection<OrganisationUnit> units = organisationUnitService.getOrganisationUnitsAtLevel( level );
 
118
            
 
119
            for ( OrganisationUnit unit : units )
 
120
            {
 
121
                OrganisationUnitStructure structure = new OrganisationUnitStructure();
 
122
 
 
123
                structure.setOrganisationUnitId( unit.getId() );
 
124
                structure.setLevel( level );
 
125
                
 
126
                Map<Integer, Integer> identifiers = new HashMap<Integer, Integer>();
 
127
                Map<Integer, String> geoCodes = new HashMap<Integer, String>();
 
128
                
 
129
                for ( int j = level; j > 0; j-- )
 
130
                {
 
131
                    identifiers.put( j, unit.getId() );
 
132
                    geoCodes.put( j, unit.getGeoCode() );
 
133
                    
 
134
                    unit = unit.getParent();
 
135
                }
 
136
                
 
137
                structure.setIdLevel1( identifiers.get( 1 ) );
 
138
                structure.setIdLevel2( identifiers.get( 2 ) );
 
139
                structure.setIdLevel3( identifiers.get( 3 ) );
 
140
                structure.setIdLevel4( identifiers.get( 4 ) );
 
141
                structure.setIdLevel5( identifiers.get( 5 ) );
 
142
                structure.setIdLevel6( identifiers.get( 6 ) );
 
143
                structure.setIdLevel7( identifiers.get( 7 ) );
 
144
                structure.setIdLevel8( identifiers.get( 8 ) );  
 
145
                
 
146
                structure.setGeoCodeLevel1( geoCodes.get( 1 ) );
 
147
                structure.setGeoCodeLevel2( geoCodes.get( 2 ) );
 
148
                structure.setGeoCodeLevel3( geoCodes.get( 3 ) );
 
149
                structure.setGeoCodeLevel4( geoCodes.get( 4 ) );
 
150
                structure.setGeoCodeLevel5( geoCodes.get( 5 ) );
 
151
                structure.setGeoCodeLevel6( geoCodes.get( 6 ) );
 
152
                structure.setGeoCodeLevel7( geoCodes.get( 7 ) );
 
153
                structure.setGeoCodeLevel8( geoCodes.get( 8 ) );
 
154
                
 
155
                batchHandler.addObject( structure );
 
156
            }
 
157
        }
 
158
        
 
159
        batchHandler.flush();
 
160
    }
 
161
    
 
162
    // -------------------------------------------------------------------------
 
163
    // GroupSetStructure
 
164
    // -------------------------------------------------------------------------
 
165
 
 
166
    public void generateGroupSetStructures()
 
167
    {
 
168
        resourceTableStore.deleteGroupSetStructures();
 
169
        
 
170
        Collection<OrganisationUnitGroupSet> exclusiveGroupSets = organisationUnitGroupService
 
171
            .getExclusiveOrganisationUnitGroupSets();
 
172
 
 
173
        BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( GroupSetStructureBatchHandler.class );
 
174
        
 
175
        batchHandler.init();
 
176
        
 
177
        for ( OrganisationUnitGroupSet groupSet : exclusiveGroupSets )
 
178
        {
 
179
            Collection<OrganisationUnitGroup> groups = groupSet.getOrganisationUnitGroups();
 
180
 
 
181
            for ( OrganisationUnitGroup group : groups )
 
182
            {
 
183
                Collection<OrganisationUnit> units = group.getMembers();
 
184
 
 
185
                for ( OrganisationUnit unit : units )
 
186
                {
 
187
                    GroupSetStructure groupSetStructure = new GroupSetStructure( unit.getId(), group.getId(), groupSet.getId() );
 
188
 
 
189
                    try
 
190
                    {
 
191
                        batchHandler.addObject( groupSetStructure );
 
192
                    }
 
193
                    catch ( Exception ex )
 
194
                    {
 
195
                        LOG.warn( "Error occured - skipping OrganisationUnitGroupSetStructure", ex );
 
196
                    }
 
197
                }
 
198
            }
 
199
        }
 
200
        
 
201
        batchHandler.flush();
 
202
    }
 
203
 
 
204
    // -------------------------------------------------------------------------
 
205
    // DataElementCategoryOptionComboName
 
206
    // -------------------------------------------------------------------------
 
207
    
 
208
    public void generateCategoryOptionComboNames()
 
209
    {
 
210
        resourceTableStore.deleteDataElementCategoryOptionComboNames();
 
211
        
 
212
        Collection<DataElementCategoryOptionCombo> combos = categoryOptionComboService.getAllDataElementCategoryOptionCombos();
 
213
        
 
214
        for ( DataElementCategoryOptionCombo combo : combos )
 
215
        {
 
216
            String name = categoryOptionComboService.getOptionNames( combo );
 
217
            
 
218
            DataElementCategoryOptionComboName entry = new DataElementCategoryOptionComboName( combo.getId(), name );
 
219
            
 
220
            resourceTableStore.addDataElementCategoryOptionComboName( entry );
 
221
        }
 
222
    }
 
223
}