~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/dataintegrity/DefaultDataIntegrityService.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.dataintegrity;
 
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.ArrayList;
 
31
import java.util.Collection;
 
32
import java.util.HashSet;
 
33
import java.util.Iterator;
 
34
import java.util.List;
 
35
import java.util.Set;
 
36
 
 
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;
 
51
 
 
52
/**
 
53
 * @author Lars Helge Overland
 
54
 * @version $Id$
 
55
 */
 
56
public class DefaultDataIntegrityService
 
57
    implements DataIntegrityService
 
58
{
 
59
    private static final String FORMULA_SEPARATOR = "#";
 
60
    
 
61
    // -------------------------------------------------------------------------
 
62
    // Dependencies
 
63
    // -------------------------------------------------------------------------
 
64
 
 
65
    private DataElementService dataElementService;
 
66
 
 
67
    public void setDataElementService( DataElementService dataElementService )
 
68
    {
 
69
        this.dataElementService = dataElementService;
 
70
    }
 
71
 
 
72
    private IndicatorService indicatorService;
 
73
 
 
74
    public void setIndicatorService( IndicatorService indicatorService )
 
75
    {
 
76
        this.indicatorService = indicatorService;
 
77
    }
 
78
 
 
79
    private DataSetService dataSetService;
 
80
 
 
81
    public void setDataSetService( DataSetService dataSetService )
 
82
    {
 
83
        this.dataSetService = dataSetService;
 
84
    }
 
85
 
 
86
    private OrganisationUnitService organisationUnitService;
 
87
 
 
88
    public void setOrganisationUnitService( OrganisationUnitService organisationUnitService )
 
89
    {
 
90
        this.organisationUnitService = organisationUnitService;
 
91
    }
 
92
    
 
93
    private OrganisationUnitGroupService organisationUnitGroupService;
 
94
 
 
95
    public void setOrganisationUnitGroupService( OrganisationUnitGroupService organisationUnitGroupService )
 
96
    {
 
97
        this.organisationUnitGroupService = organisationUnitGroupService;
 
98
    }
 
99
 
 
100
    // -------------------------------------------------------------------------
 
101
    // DataIntegrityService implementation
 
102
    // -------------------------------------------------------------------------
 
103
 
 
104
    // -------------------------------------------------------------------------
 
105
    // DataElement
 
106
    // -------------------------------------------------------------------------
 
107
 
 
108
    public Collection<DataElement> getDataElementsWithoutDataSet()
 
109
    {
 
110
        Collection<DataSet> dataSets = dataSetService.getAllDataSets();
 
111
        
 
112
        Collection<DataElement> dataElements = dataElementService.getAllDataElements();
 
113
        
 
114
        Iterator<DataElement> iterator = dataElements.iterator();
 
115
        
 
116
        while ( iterator.hasNext() )
 
117
        {
 
118
            final DataElement element = iterator.next();
 
119
            
 
120
            for ( DataSet dataSet : dataSets )
 
121
            {
 
122
                if ( dataSet.getDataElements().contains( element ) )
 
123
                {
 
124
                    iterator.remove();
 
125
                    
 
126
                    break;
 
127
                }
 
128
            }
 
129
        }
 
130
        
 
131
        return dataElements;
 
132
    }
 
133
 
 
134
    public Collection<DataElement> getDataElementsWithoutGroups()
 
135
    {
 
136
        Collection<DataElementGroup> groups = dataElementService.getAllDataElementGroups();
 
137
        
 
138
        Collection<DataElement> dataElements = dataElementService.getAllDataElements();
 
139
        
 
140
        Iterator<DataElement> iterator = dataElements.iterator();
 
141
        
 
142
        while ( iterator.hasNext() )
 
143
        {
 
144
            final DataElement element = iterator.next();
 
145
            
 
146
            for ( DataElementGroup group : groups )
 
147
            {
 
148
                if ( group.getMembers().contains( element ) )
 
149
                {
 
150
                    iterator.remove();
 
151
                    
 
152
                    break;
 
153
                }
 
154
            }
 
155
        }
 
156
        
 
157
        return dataElements;
 
158
    }
 
159
 
 
160
    // -------------------------------------------------------------------------
 
161
    // DataSet
 
162
    // -------------------------------------------------------------------------
 
163
 
 
164
    public Collection<DataSet> getDataSetsNotAssignedToOrganisationUnits()
 
165
    {
 
166
        Collection<DataSet> dataSets = dataSetService.getAllDataSets();
 
167
        
 
168
        Iterator<DataSet> iterator = dataSets.iterator();
 
169
        
 
170
        while ( iterator.hasNext() )
 
171
        {
 
172
            final DataSet dataSet = iterator.next();
 
173
            
 
174
            if ( dataSet.getSources().size() > 0 )
 
175
            {
 
176
                iterator.remove();
 
177
            }
 
178
        }
 
179
        
 
180
        return dataSets;
 
181
    }    
 
182
 
 
183
    // -------------------------------------------------------------------------
 
184
    // Indicator
 
185
    // -------------------------------------------------------------------------
 
186
 
 
187
    public Collection<Indicator> getIndicatorsWithBlankFormulas()
 
188
    {
 
189
        Collection<Indicator> indicators = indicatorService.getAllIndicators();
 
190
        
 
191
        Iterator<Indicator> iterator = indicators.iterator();
 
192
        
 
193
        while ( iterator.hasNext() )
 
194
        {
 
195
            final Indicator indicator = iterator.next();
 
196
            
 
197
            if ( indicator.getNumerator() != null && 
 
198
                 indicator.getNumerator().trim().length() > 0 &&
 
199
                 indicator.getDenominator() != null &&
 
200
                 indicator.getDenominator().trim().length() > 0 )
 
201
            {
 
202
                iterator.remove();
 
203
            }
 
204
        }
 
205
        
 
206
        return indicators;
 
207
    }
 
208
    
 
209
    public Collection<Indicator> getIndicatorsWithIdenticalFormulas()
 
210
    {
 
211
        List<String> formulas = new ArrayList<String>();
 
212
        
 
213
        Set<Indicator> targets = new HashSet<Indicator>();
 
214
        
 
215
        Collection<Indicator> indicators = indicatorService.getAllIndicators();
 
216
        
 
217
        for ( Indicator indicator : indicators )
 
218
        {
 
219
            final String formula = indicator.getNumerator() + FORMULA_SEPARATOR + indicator.getDenominator();
 
220
            
 
221
            if ( formulas.contains( formula ) )
 
222
            {
 
223
                targets.add( indicator );
 
224
            }
 
225
            else
 
226
            {
 
227
                formulas.add( formula );
 
228
            }
 
229
        }
 
230
        
 
231
        return targets;
 
232
    }
 
233
 
 
234
    public Collection<Indicator> getIndicatorsWithoutGroups()
 
235
    {
 
236
        Collection<IndicatorGroup> groups = indicatorService.getAllIndicatorGroups();
 
237
        
 
238
        Collection<Indicator> indicators = indicatorService.getAllIndicators();
 
239
        
 
240
        Iterator<Indicator> iterator = indicators.iterator();
 
241
        
 
242
        while ( iterator.hasNext() )
 
243
        {
 
244
            final Indicator indicator = iterator.next();
 
245
            
 
246
            for ( IndicatorGroup group : groups )
 
247
            {
 
248
                if ( group.getMembers().contains( indicator ) )
 
249
                {
 
250
                    iterator.remove();
 
251
                    
 
252
                    break;
 
253
                }
 
254
            }
 
255
        }
 
256
        
 
257
        return indicators;
 
258
    }
 
259
    
 
260
    // -------------------------------------------------------------------------
 
261
    // OrganisationUnit
 
262
    // -------------------------------------------------------------------------
 
263
 
 
264
    public Collection<OrganisationUnit> getOrganisationUnitsWithCyclicReferences()
 
265
    {
 
266
        Collection<OrganisationUnit> organisationUnits = organisationUnitService.getAllOrganisationUnits();
 
267
        
 
268
        Set<OrganisationUnit> cyclic = new HashSet<OrganisationUnit>();
 
269
        
 
270
        Set<OrganisationUnit> visited = new HashSet<OrganisationUnit>();
 
271
        
 
272
        OrganisationUnit parent = null;
 
273
        
 
274
        for ( OrganisationUnit unit : organisationUnits )
 
275
        {
 
276
            parent = unit;
 
277
                        
 
278
            while ( ( parent = parent.getParent() ) != null )
 
279
            {
 
280
                if ( parent.equals( unit ) ) // Cyclic reference
 
281
                {
 
282
                    cyclic.add( unit );
 
283
 
 
284
                    break;                    
 
285
                }
 
286
                else if ( visited.contains( parent ) ) // Ends in cyclic reference but not part of it
 
287
                {
 
288
                    break;
 
289
                }
 
290
                else // Remember visited
 
291
                {
 
292
                    visited.add( parent );
 
293
                }
 
294
            }
 
295
            
 
296
            visited.clear();
 
297
        }
 
298
        
 
299
        return cyclic;
 
300
    }
 
301
 
 
302
    public Collection<OrganisationUnit> getOrphanedOrganisationUnits()
 
303
    {
 
304
        Collection<OrganisationUnit> organisationUnits = organisationUnitService.getAllOrganisationUnits();
 
305
        
 
306
        Set<OrganisationUnit> orphans = new HashSet<OrganisationUnit>();
 
307
        
 
308
        for ( OrganisationUnit unit : organisationUnits )
 
309
        {
 
310
            if ( unit.getParent() == null && ( unit.getChildren() == null || unit.getChildren().size() == 0 ) )
 
311
            {
 
312
                orphans.add( unit );
 
313
            }
 
314
        }
 
315
        
 
316
        return orphans;
 
317
    }
 
318
 
 
319
    public Collection<OrganisationUnit> getOrganisationUnitsWithoutGroups()
 
320
    {
 
321
        Collection<OrganisationUnitGroup> groups = organisationUnitGroupService.getAllOrganisationUnitGroups();
 
322
        
 
323
        Collection<OrganisationUnit> organisationUnits = organisationUnitService.getAllOrganisationUnits();
 
324
        
 
325
        Iterator<OrganisationUnit> iterator = organisationUnits.iterator();
 
326
        
 
327
        while ( iterator.hasNext() )
 
328
        {
 
329
            final OrganisationUnit unit = iterator.next();
 
330
            
 
331
            for ( OrganisationUnitGroup group : groups )
 
332
            {
 
333
                if ( group.getMembers().contains( unit ) )
 
334
                {
 
335
                    iterator.remove();
 
336
                    
 
337
                    break;
 
338
                }
 
339
            }
 
340
        }
 
341
        
 
342
        return organisationUnits;
 
343
    }
 
344
 
 
345
    public Collection<OrganisationUnit> getOrganisationUnitsViolatingCompulsoryGroupSets()
 
346
    {
 
347
        Collection<OrganisationUnitGroupSet> groupSets = organisationUnitGroupService.getCompulsoryOrganisationUnitGroupSets();
 
348
        
 
349
        Collection<OrganisationUnit> organisationUnits = organisationUnitService.getAllOrganisationUnits();
 
350
        
 
351
        Set<OrganisationUnit> targets = new HashSet<OrganisationUnit>();
 
352
        
 
353
        for ( OrganisationUnitGroupSet groupSet : groupSets )
 
354
        {
 
355
            organisationUnit: for ( OrganisationUnit unit : organisationUnits )
 
356
            {
 
357
                for ( OrganisationUnitGroup group : groupSet.getOrganisationUnitGroups() )
 
358
                {                    
 
359
                    if ( group.getMembers().contains( unit ) )
 
360
                    {
 
361
                        continue organisationUnit;
 
362
                    }
 
363
                    
 
364
                    targets.add( unit ); // Unit is not a member of any groups in the compulsory group set                    
 
365
                }
 
366
            }
 
367
        }
 
368
        
 
369
        return targets;
 
370
    }
 
371
 
 
372
    public Collection<OrganisationUnit> getOrganisationUnitsViolatingExclusiveGroupSets()
 
373
    {
 
374
        Collection<OrganisationUnitGroupSet> groupSets = organisationUnitGroupService.getExclusiveOrganisationUnitGroupSets();
 
375
        
 
376
        Collection<OrganisationUnit> organisationUnits = organisationUnitService.getAllOrganisationUnits();
 
377
        
 
378
        Set<OrganisationUnit> targets = new HashSet<OrganisationUnit>();
 
379
 
 
380
        for ( OrganisationUnitGroupSet groupSet : groupSets )
 
381
        {
 
382
            organisationUnit: for ( OrganisationUnit unit : organisationUnits )
 
383
            {
 
384
                int memberships = 0;
 
385
                
 
386
                for ( OrganisationUnitGroup group : groupSet.getOrganisationUnitGroups() )
 
387
                {
 
388
                    if ( group.getMembers().contains( unit ) )
 
389
                    {
 
390
                        if ( ++memberships > 1 )
 
391
                        {
 
392
                            targets.add( unit ); // Unit is member of more than one group in the exclusive group set
 
393
                            
 
394
                            continue organisationUnit;
 
395
                        }
 
396
                    }
 
397
                }
 
398
            }
 
399
        }
 
400
        
 
401
        return targets;
 
402
    }
 
403
 
 
404
    public Collection<OrganisationUnitGroup> getOrganisationUnitGroupsWithoutGroupSets()
 
405
    {
 
406
        Collection<OrganisationUnitGroupSet> groupSets = organisationUnitGroupService.getExclusiveOrganisationUnitGroupSets();
 
407
        
 
408
        Collection<OrganisationUnitGroup> groups = organisationUnitGroupService.getAllOrganisationUnitGroups();
 
409
        
 
410
        Iterator<OrganisationUnitGroup> iterator = groups.iterator();
 
411
        
 
412
        while ( iterator.hasNext() )
 
413
        {
 
414
            final OrganisationUnitGroup group = iterator.next();
 
415
            
 
416
            for ( OrganisationUnitGroupSet groupSet : groupSets )
 
417
            {
 
418
                if ( groupSet.getOrganisationUnitGroups().contains( group ) )
 
419
                {
 
420
                    iterator.remove();
 
421
                    
 
422
                    break;
 
423
                }
 
424
            }
 
425
        }
 
426
        
 
427
        return groups;
 
428
    }
 
429
}