1
package org.hisp.dhis.dataelement;
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.HashMap;
33
import java.util.Iterator;
35
import java.util.regex.Matcher;
36
import java.util.regex.Pattern;
38
import org.hisp.dhis.hierarchy.HierarchyViolationException;
39
import org.hisp.dhis.system.util.UUIdUtils;
42
* @author Kristian Nordal
43
* @version $Id: DefaultDataElementService.java 5243 2008-05-25 10:18:58Z
46
public class DefaultDataElementService
47
implements DataElementService
49
// -------------------------------------------------------------------------
51
// -------------------------------------------------------------------------
53
private DataElementStore dataElementStore;
55
public void setDataElementStore( DataElementStore dataElementStore )
57
this.dataElementStore = dataElementStore;
60
// -------------------------------------------------------------------------
62
// -------------------------------------------------------------------------
64
public int addDataElement( DataElement dataElement )
66
if ( dataElement.getUuid() == null )
68
dataElement.setUuid( UUIdUtils.getUUId() );
71
return dataElementStore.addDataElement( dataElement );
74
public void updateDataElement( DataElement dataElement )
76
dataElementStore.updateDataElement( dataElement );
79
public void deleteDataElement( DataElement dataElement )
80
throws HierarchyViolationException
82
dataElementStore.deleteDataElement( dataElement );
85
public DataElement getDataElement( int id )
87
return dataElementStore.getDataElement( id );
90
public DataElement getDataElement( String uuid )
92
return dataElementStore.getDataElement( uuid );
95
public Collection<DataElement> getAllDataElements()
97
return dataElementStore.getAllDataElements();
100
public Collection<CalculatedDataElement> getCalculatedDataElements()
102
Collection<CalculatedDataElement> calculatedDataElements = new ArrayList<CalculatedDataElement>();
104
for ( DataElement dataElement : getAllDataElements() )
106
if ( dataElement instanceof CalculatedDataElement )
108
calculatedDataElements.add( (CalculatedDataElement) dataElement );
112
return calculatedDataElements;
115
public Collection<DataElement> getNonCalculatedDataElements()
117
Collection<DataElement> dataElements = getAllDataElements();
119
dataElements.removeAll( getCalculatedDataElements() );
124
public Collection<DataElement> getDataElements( Collection<Integer> identifiers )
126
Collection<DataElement> objects = new ArrayList<DataElement>();
128
for ( Integer id : identifiers )
130
objects.add( getDataElement( id ) );
136
public Collection<DataElement> getAggregateableDataElements()
138
return dataElementStore.getAggregateableDataElements();
141
public Collection<DataElement> getAllActiveDataElements()
143
return dataElementStore.getAllActiveDataElements();
146
public DataElement getDataElementByName( String name )
148
return dataElementStore.getDataElementByName( name );
151
public DataElement getDataElementByAlternativeName( String alternativeName )
153
return dataElementStore.getDataElementByAlternativeName( alternativeName );
156
public DataElement getDataElementByShortName( String shortName )
158
return dataElementStore.getDataElementByShortName( shortName );
161
public DataElement getDataElementByCode( String code )
163
return dataElementStore.getDataElementByCode( code );
166
public Collection<DataElement> getDataElementsByAggregationOperator( String aggregationOperator )
168
return dataElementStore.getDataElementsByAggregationOperator( aggregationOperator );
171
public Collection<DataElement> getDataElementsByType( String type )
173
return dataElementStore.getDataElementsByType( type );
176
public Collection<DataElement> getDataElementByCategoryCombo( DataElementCategoryCombo categoryCombo )
178
return dataElementStore.getDataElementByCategoryCombo( categoryCombo );
181
// -------------------------------------------------------------------------
182
// CalculatedDataElement
183
// -------------------------------------------------------------------------
185
public Collection<CalculatedDataElement> getAllCalculatedDataElements()
187
return dataElementStore.getAllCalculatedDataElements();
190
public CalculatedDataElement getCalculatedDataElementByDataElement( DataElement dataElement )
192
return dataElementStore.getCalculatedDataElementByDataElement( dataElement );
195
public Collection<CalculatedDataElement> getCalculatedDataElementsByDataElements(
196
Collection<DataElement> dataElements )
198
return dataElementStore.getCalculatedDataElementsByDataElements( dataElements );
201
public Map<DataElement, Integer> getDataElementFactors( CalculatedDataElement calculatedDataElement )
203
Map<DataElement, Integer> factorMap = new HashMap<DataElement, Integer>();
205
Pattern pattern = Pattern.compile( "\\[(\\d+)\\]\\s*\\*\\s*(\\d+)" );
207
// ---------------------------------------------------------------------
208
// In readable form: \[(\d+)\]\s*\*\s*(\d+)
209
// Meaning any expression on the form "[id] * factor"
210
// ---------------------------------------------------------------------
212
Matcher matcher = pattern.matcher( calculatedDataElement.getExpression().getExpression() );
214
while ( matcher.find() )
216
// -----------------------------------------------------------------
219
// -----------------------------------------------------------------
221
factorMap.put( getDataElement( Integer.parseInt( matcher.group( 1 ) ) ), Integer.parseInt( matcher
228
public Map<String, Integer> getOperandFactors( CalculatedDataElement calculatedDataElement )
230
Map<String, Integer> factorMap = new HashMap<String, Integer>();
232
Pattern pattern = Pattern.compile( "\\[(\\d+\\.\\d+)\\]\\s*\\*\\s*(-?\\d+)" );
234
// ---------------------------------------------------------------------
235
// In readable form: \[(\d+)\]\s*\*\s*(\d+)
236
// Meaning any expression on the form "[id] * factor"
237
// ---------------------------------------------------------------------
239
Matcher matcher = pattern.matcher( calculatedDataElement.getExpression().getExpression() );
241
while ( matcher.find() )
243
// -----------------------------------------------------------------
244
// Key: Datelementid.optioncomboid
246
// -----------------------------------------------------------------
248
factorMap.put( matcher.group( 1 ), Integer.parseInt( matcher.group( 2 ) ) );
254
public Collection<String> getOperandIds( CalculatedDataElement calculatedDataElement )
256
Collection<String> operands = new ArrayList<String>();
258
Pattern pattern = Pattern.compile( "\\[(\\d+\\.\\d+)\\]" );
260
// ---------------------------------------------------------------------
261
// In readable form: \[(\d+)\]\s*\*\s*(\d+)
262
// Meaning any expression on the form "[id] * factor"
263
// ---------------------------------------------------------------------
265
Matcher matcher = pattern.matcher( calculatedDataElement.getExpression().getExpression() );
267
while ( matcher.find() )
269
// -----------------------------------------------------------------
270
// Datelementid.optioncomboid
271
// -----------------------------------------------------------------
273
operands.add( matcher.group( 1 ) );
279
// -------------------------------------------------------------------------
281
// -------------------------------------------------------------------------
283
public int addDataElementGroup( DataElementGroup dataElementGroup )
285
if ( dataElementGroup.getUuid() == null )
287
dataElementGroup.setUuid( UUIdUtils.getUUId() );
290
return dataElementStore.addDataElementGroup( dataElementGroup );
293
public void updateDataElementGroup( DataElementGroup dataElementGroup )
295
dataElementStore.updateDataElementGroup( dataElementGroup );
298
public void deleteDataElementGroup( DataElementGroup dataElementGroup )
300
dataElementStore.deleteDataElementGroup( dataElementGroup );
303
public DataElementGroup getDataElementGroup( int id )
305
return dataElementStore.getDataElementGroup( id );
308
public DataElementGroup getDataElementGroup( String uuid )
310
return dataElementStore.getDataElementGroup( uuid );
313
public Collection<DataElementGroup> getAllDataElementGroups()
315
return dataElementStore.getAllDataElementGroups();
318
public DataElementGroup getDataElementGroupByName( String name )
320
return dataElementStore.getDataElementGroupByName( name );
323
public Collection<DataElementGroup> getGroupsContainingDataElement( DataElement dataElement )
325
Collection<DataElementGroup> groups = getAllDataElementGroups();
327
Iterator<DataElementGroup> iterator = groups.iterator();
329
while ( iterator.hasNext() )
331
if ( !iterator.next().getMembers().contains( dataElement ) )