1
package org.hisp.dhis.resourcetable;
4
* Copyright (c) 2004-2007, University of Oslo
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions are met:
9
* * Redistributions of source code must retain the above copyright notice, this
10
* list of conditions and the following disclaimer.
11
* * Redistributions in binary form must reproduce the above copyright notice,
12
* this list of conditions and the following disclaimer in the documentation
13
* and/or other materials provided with the distribution.
14
* * Neither the name of the HISP project nor the names of its contributors may
15
* be used to endorse or promote products derived from this software without
16
* specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
import java.util.Collection;
31
import java.util.HashMap;
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;
54
* @author Lars Helge Overland
55
* @version $Id: DefaultResourceTableService.java 5459 2008-06-26 01:12:03Z larshelg $
57
public class DefaultResourceTableService
58
implements ResourceTableService
60
private static final Log LOG = LogFactory.getLog( DefaultResourceTableService.class );
62
// -------------------------------------------------------------------------
64
// -------------------------------------------------------------------------
66
private ResourceTableStore resourceTableStore;
68
public void setResourceTableStore( ResourceTableStore resourceTableStore )
70
this.resourceTableStore = resourceTableStore;
73
private OrganisationUnitService organisationUnitService;
75
public void setOrganisationUnitService( OrganisationUnitService organisationUnitService )
77
this.organisationUnitService = organisationUnitService;
80
private OrganisationUnitGroupService organisationUnitGroupService;
82
public void setOrganisationUnitGroupService( OrganisationUnitGroupService organisationUnitGroupService )
84
this.organisationUnitGroupService = organisationUnitGroupService;
87
private DataElementCategoryOptionComboService categoryOptionComboService;
89
public void setCategoryOptionComboService( DataElementCategoryOptionComboService categoryOptionComboService )
91
this.categoryOptionComboService = categoryOptionComboService;
94
private BatchHandlerFactory batchHandlerFactory;
96
public void setBatchHandlerFactory( BatchHandlerFactory batchHandlerFactory )
98
this.batchHandlerFactory = batchHandlerFactory;
101
// -------------------------------------------------------------------------
102
// OrganisationUnitStructure
103
// -------------------------------------------------------------------------
105
public void generateOrganisationUnitStructures()
107
resourceTableStore.deleteOrganisationUnitStructures();
109
BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( OrganisationUnitStructureBatchHandler.class );
113
for ( int i = 0; i < 8; i++ )
117
Collection<OrganisationUnit> units = organisationUnitService.getOrganisationUnitsAtLevel( level );
119
for ( OrganisationUnit unit : units )
121
OrganisationUnitStructure structure = new OrganisationUnitStructure();
123
structure.setOrganisationUnitId( unit.getId() );
124
structure.setLevel( level );
126
Map<Integer, Integer> identifiers = new HashMap<Integer, Integer>();
127
Map<Integer, String> geoCodes = new HashMap<Integer, String>();
129
for ( int j = level; j > 0; j-- )
131
identifiers.put( j, unit.getId() );
132
geoCodes.put( j, unit.getGeoCode() );
134
unit = unit.getParent();
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 ) );
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 ) );
155
batchHandler.addObject( structure );
159
batchHandler.flush();
162
// -------------------------------------------------------------------------
164
// -------------------------------------------------------------------------
166
public void generateGroupSetStructures()
168
resourceTableStore.deleteGroupSetStructures();
170
Collection<OrganisationUnitGroupSet> exclusiveGroupSets = organisationUnitGroupService
171
.getExclusiveOrganisationUnitGroupSets();
173
BatchHandler batchHandler = batchHandlerFactory.createBatchHandler( GroupSetStructureBatchHandler.class );
177
for ( OrganisationUnitGroupSet groupSet : exclusiveGroupSets )
179
Collection<OrganisationUnitGroup> groups = groupSet.getOrganisationUnitGroups();
181
for ( OrganisationUnitGroup group : groups )
183
Collection<OrganisationUnit> units = group.getMembers();
185
for ( OrganisationUnit unit : units )
187
GroupSetStructure groupSetStructure = new GroupSetStructure( unit.getId(), group.getId(), groupSet.getId() );
191
batchHandler.addObject( groupSetStructure );
193
catch ( Exception ex )
195
LOG.warn( "Error occured - skipping OrganisationUnitGroupSetStructure", ex );
201
batchHandler.flush();
204
// -------------------------------------------------------------------------
205
// DataElementCategoryOptionComboName
206
// -------------------------------------------------------------------------
208
public void generateCategoryOptionComboNames()
210
resourceTableStore.deleteDataElementCategoryOptionComboNames();
212
Collection<DataElementCategoryOptionCombo> combos = categoryOptionComboService.getAllDataElementCategoryOptionCombos();
214
for ( DataElementCategoryOptionCombo combo : combos )
216
String name = categoryOptionComboService.getOptionNames( combo );
218
DataElementCategoryOptionComboName entry = new DataElementCategoryOptionComboName( combo.getId(), name );
220
resourceTableStore.addDataElementCategoryOptionComboName( entry );