1
package org.hisp.dhis.dataintegrity;
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.ArrayList;
31
import java.util.Collection;
32
import java.util.HashSet;
33
import java.util.Iterator;
34
import java.util.List;
37
import org.hisp.dhis.dataelement.DataElement;
38
import org.hisp.dhis.dataelement.DataElementGroup;
39
import org.hisp.dhis.dataelement.DataElementService;
40
import org.hisp.dhis.dataintegrity.DataIntegrityService;
41
import org.hisp.dhis.dataset.DataSet;
42
import org.hisp.dhis.dataset.DataSetService;
43
import org.hisp.dhis.indicator.Indicator;
44
import org.hisp.dhis.indicator.IndicatorGroup;
45
import org.hisp.dhis.indicator.IndicatorService;
46
import org.hisp.dhis.organisationunit.OrganisationUnit;
47
import org.hisp.dhis.organisationunit.OrganisationUnitGroup;
48
import org.hisp.dhis.organisationunit.OrganisationUnitGroupService;
49
import org.hisp.dhis.organisationunit.OrganisationUnitGroupSet;
50
import org.hisp.dhis.organisationunit.OrganisationUnitService;
53
* @author Lars Helge Overland
56
public class DefaultDataIntegrityService
57
implements DataIntegrityService
59
private static final String FORMULA_SEPARATOR = "#";
61
// -------------------------------------------------------------------------
63
// -------------------------------------------------------------------------
65
private DataElementService dataElementService;
67
public void setDataElementService( DataElementService dataElementService )
69
this.dataElementService = dataElementService;
72
private IndicatorService indicatorService;
74
public void setIndicatorService( IndicatorService indicatorService )
76
this.indicatorService = indicatorService;
79
private DataSetService dataSetService;
81
public void setDataSetService( DataSetService dataSetService )
83
this.dataSetService = dataSetService;
86
private OrganisationUnitService organisationUnitService;
88
public void setOrganisationUnitService( OrganisationUnitService organisationUnitService )
90
this.organisationUnitService = organisationUnitService;
93
private OrganisationUnitGroupService organisationUnitGroupService;
95
public void setOrganisationUnitGroupService( OrganisationUnitGroupService organisationUnitGroupService )
97
this.organisationUnitGroupService = organisationUnitGroupService;
100
// -------------------------------------------------------------------------
101
// DataIntegrityService implementation
102
// -------------------------------------------------------------------------
104
// -------------------------------------------------------------------------
106
// -------------------------------------------------------------------------
108
public Collection<DataElement> getDataElementsWithoutDataSet()
110
Collection<DataSet> dataSets = dataSetService.getAllDataSets();
112
Collection<DataElement> dataElements = dataElementService.getAllDataElements();
114
Iterator<DataElement> iterator = dataElements.iterator();
116
while ( iterator.hasNext() )
118
final DataElement element = iterator.next();
120
for ( DataSet dataSet : dataSets )
122
if ( dataSet.getDataElements().contains( element ) )
134
public Collection<DataElement> getDataElementsWithoutGroups()
136
Collection<DataElementGroup> groups = dataElementService.getAllDataElementGroups();
138
Collection<DataElement> dataElements = dataElementService.getAllDataElements();
140
Iterator<DataElement> iterator = dataElements.iterator();
142
while ( iterator.hasNext() )
144
final DataElement element = iterator.next();
146
for ( DataElementGroup group : groups )
148
if ( group.getMembers().contains( element ) )
160
// -------------------------------------------------------------------------
162
// -------------------------------------------------------------------------
164
public Collection<DataSet> getDataSetsNotAssignedToOrganisationUnits()
166
Collection<DataSet> dataSets = dataSetService.getAllDataSets();
168
Iterator<DataSet> iterator = dataSets.iterator();
170
while ( iterator.hasNext() )
172
final DataSet dataSet = iterator.next();
174
if ( dataSet.getSources().size() > 0 )
183
// -------------------------------------------------------------------------
185
// -------------------------------------------------------------------------
187
public Collection<Indicator> getIndicatorsWithBlankFormulas()
189
Collection<Indicator> indicators = indicatorService.getAllIndicators();
191
Iterator<Indicator> iterator = indicators.iterator();
193
while ( iterator.hasNext() )
195
final Indicator indicator = iterator.next();
197
if ( indicator.getNumerator() != null &&
198
indicator.getNumerator().trim().length() > 0 &&
199
indicator.getDenominator() != null &&
200
indicator.getDenominator().trim().length() > 0 )
209
public Collection<Indicator> getIndicatorsWithIdenticalFormulas()
211
List<String> formulas = new ArrayList<String>();
213
Set<Indicator> targets = new HashSet<Indicator>();
215
Collection<Indicator> indicators = indicatorService.getAllIndicators();
217
for ( Indicator indicator : indicators )
219
final String formula = indicator.getNumerator() + FORMULA_SEPARATOR + indicator.getDenominator();
221
if ( formulas.contains( formula ) )
223
targets.add( indicator );
227
formulas.add( formula );
234
public Collection<Indicator> getIndicatorsWithoutGroups()
236
Collection<IndicatorGroup> groups = indicatorService.getAllIndicatorGroups();
238
Collection<Indicator> indicators = indicatorService.getAllIndicators();
240
Iterator<Indicator> iterator = indicators.iterator();
242
while ( iterator.hasNext() )
244
final Indicator indicator = iterator.next();
246
for ( IndicatorGroup group : groups )
248
if ( group.getMembers().contains( indicator ) )
260
// -------------------------------------------------------------------------
262
// -------------------------------------------------------------------------
264
public Collection<OrganisationUnit> getOrganisationUnitsWithCyclicReferences()
266
Collection<OrganisationUnit> organisationUnits = organisationUnitService.getAllOrganisationUnits();
268
Set<OrganisationUnit> cyclic = new HashSet<OrganisationUnit>();
270
Set<OrganisationUnit> visited = new HashSet<OrganisationUnit>();
272
OrganisationUnit parent = null;
274
for ( OrganisationUnit unit : organisationUnits )
278
while ( ( parent = parent.getParent() ) != null )
280
if ( parent.equals( unit ) ) // Cyclic reference
286
else if ( visited.contains( parent ) ) // Ends in cyclic reference but not part of it
290
else // Remember visited
292
visited.add( parent );
302
public Collection<OrganisationUnit> getOrphanedOrganisationUnits()
304
Collection<OrganisationUnit> organisationUnits = organisationUnitService.getAllOrganisationUnits();
306
Set<OrganisationUnit> orphans = new HashSet<OrganisationUnit>();
308
for ( OrganisationUnit unit : organisationUnits )
310
if ( unit.getParent() == null && ( unit.getChildren() == null || unit.getChildren().size() == 0 ) )
319
public Collection<OrganisationUnit> getOrganisationUnitsWithoutGroups()
321
Collection<OrganisationUnitGroup> groups = organisationUnitGroupService.getAllOrganisationUnitGroups();
323
Collection<OrganisationUnit> organisationUnits = organisationUnitService.getAllOrganisationUnits();
325
Iterator<OrganisationUnit> iterator = organisationUnits.iterator();
327
while ( iterator.hasNext() )
329
final OrganisationUnit unit = iterator.next();
331
for ( OrganisationUnitGroup group : groups )
333
if ( group.getMembers().contains( unit ) )
342
return organisationUnits;
345
public Collection<OrganisationUnit> getOrganisationUnitsViolatingCompulsoryGroupSets()
347
Collection<OrganisationUnitGroupSet> groupSets = organisationUnitGroupService.getCompulsoryOrganisationUnitGroupSets();
349
Collection<OrganisationUnit> organisationUnits = organisationUnitService.getAllOrganisationUnits();
351
Set<OrganisationUnit> targets = new HashSet<OrganisationUnit>();
353
for ( OrganisationUnitGroupSet groupSet : groupSets )
355
organisationUnit: for ( OrganisationUnit unit : organisationUnits )
357
for ( OrganisationUnitGroup group : groupSet.getOrganisationUnitGroups() )
359
if ( group.getMembers().contains( unit ) )
361
continue organisationUnit;
364
targets.add( unit ); // Unit is not a member of any groups in the compulsory group set
372
public Collection<OrganisationUnit> getOrganisationUnitsViolatingExclusiveGroupSets()
374
Collection<OrganisationUnitGroupSet> groupSets = organisationUnitGroupService.getExclusiveOrganisationUnitGroupSets();
376
Collection<OrganisationUnit> organisationUnits = organisationUnitService.getAllOrganisationUnits();
378
Set<OrganisationUnit> targets = new HashSet<OrganisationUnit>();
380
for ( OrganisationUnitGroupSet groupSet : groupSets )
382
organisationUnit: for ( OrganisationUnit unit : organisationUnits )
386
for ( OrganisationUnitGroup group : groupSet.getOrganisationUnitGroups() )
388
if ( group.getMembers().contains( unit ) )
390
if ( ++memberships > 1 )
392
targets.add( unit ); // Unit is member of more than one group in the exclusive group set
394
continue organisationUnit;
404
public Collection<OrganisationUnitGroup> getOrganisationUnitGroupsWithoutGroupSets()
406
Collection<OrganisationUnitGroupSet> groupSets = organisationUnitGroupService.getExclusiveOrganisationUnitGroupSets();
408
Collection<OrganisationUnitGroup> groups = organisationUnitGroupService.getAllOrganisationUnitGroups();
410
Iterator<OrganisationUnitGroup> iterator = groups.iterator();
412
while ( iterator.hasNext() )
414
final OrganisationUnitGroup group = iterator.next();
416
for ( OrganisationUnitGroupSet groupSet : groupSets )
418
if ( groupSet.getOrganisationUnitGroups().contains( group ) )