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

« back to all changes in this revision

Viewing changes to dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataelement/DefaultDataElementService.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.dataelement;
 
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.HashMap;
 
33
import java.util.Iterator;
 
34
import java.util.Map;
 
35
import java.util.regex.Matcher;
 
36
import java.util.regex.Pattern;
 
37
 
 
38
import org.hisp.dhis.hierarchy.HierarchyViolationException;
 
39
import org.hisp.dhis.system.util.UUIdUtils;
 
40
 
 
41
/**
 
42
 * @author Kristian Nordal
 
43
 * @version $Id: DefaultDataElementService.java 5243 2008-05-25 10:18:58Z
 
44
 *          larshelg $
 
45
 */
 
46
public class DefaultDataElementService
 
47
    implements DataElementService
 
48
{
 
49
    // -------------------------------------------------------------------------
 
50
    // Dependencies
 
51
    // -------------------------------------------------------------------------
 
52
 
 
53
    private DataElementStore dataElementStore;
 
54
 
 
55
    public void setDataElementStore( DataElementStore dataElementStore )
 
56
    {
 
57
        this.dataElementStore = dataElementStore;
 
58
    }
 
59
 
 
60
    // -------------------------------------------------------------------------
 
61
    // DataElement
 
62
    // -------------------------------------------------------------------------
 
63
 
 
64
    public int addDataElement( DataElement dataElement )
 
65
    {
 
66
        if ( dataElement.getUuid() == null )
 
67
        {
 
68
            dataElement.setUuid( UUIdUtils.getUUId() );
 
69
        }
 
70
 
 
71
        return dataElementStore.addDataElement( dataElement );
 
72
    }
 
73
 
 
74
    public void updateDataElement( DataElement dataElement )
 
75
    {
 
76
        dataElementStore.updateDataElement( dataElement );
 
77
    }
 
78
 
 
79
    public void deleteDataElement( DataElement dataElement )
 
80
        throws HierarchyViolationException
 
81
    {
 
82
        dataElementStore.deleteDataElement( dataElement );
 
83
    }
 
84
 
 
85
    public DataElement getDataElement( int id )
 
86
    {
 
87
        return dataElementStore.getDataElement( id );
 
88
    }
 
89
 
 
90
    public DataElement getDataElement( String uuid )
 
91
    {
 
92
        return dataElementStore.getDataElement( uuid );
 
93
    }
 
94
 
 
95
    public Collection<DataElement> getAllDataElements()
 
96
    {
 
97
        return dataElementStore.getAllDataElements();
 
98
    }
 
99
    
 
100
    public Collection<CalculatedDataElement> getCalculatedDataElements()
 
101
    {
 
102
        Collection<CalculatedDataElement> calculatedDataElements = new ArrayList<CalculatedDataElement>();
 
103
        
 
104
        for ( DataElement dataElement : getAllDataElements() )
 
105
        {
 
106
            if ( dataElement instanceof CalculatedDataElement )
 
107
            {
 
108
                calculatedDataElements.add( (CalculatedDataElement) dataElement );
 
109
            }
 
110
        }
 
111
        
 
112
        return calculatedDataElements;
 
113
    }
 
114
    
 
115
    public Collection<DataElement> getNonCalculatedDataElements()
 
116
    {
 
117
        Collection<DataElement> dataElements = getAllDataElements();
 
118
        
 
119
        dataElements.removeAll( getCalculatedDataElements() );
 
120
        
 
121
        return dataElements;
 
122
    }
 
123
    
 
124
    public Collection<DataElement> getDataElements( Collection<Integer> identifiers )
 
125
    {
 
126
        Collection<DataElement> objects = new ArrayList<DataElement>();
 
127
        
 
128
        for ( Integer id : identifiers )
 
129
        {
 
130
            objects.add( getDataElement( id ) );
 
131
        }
 
132
        
 
133
        return objects;
 
134
    }
 
135
 
 
136
    public Collection<DataElement> getAggregateableDataElements()
 
137
    {
 
138
        return dataElementStore.getAggregateableDataElements();
 
139
    }
 
140
 
 
141
    public Collection<DataElement> getAllActiveDataElements()
 
142
    {
 
143
        return dataElementStore.getAllActiveDataElements();
 
144
    }
 
145
 
 
146
    public DataElement getDataElementByName( String name )
 
147
    {
 
148
        return dataElementStore.getDataElementByName( name );
 
149
    }
 
150
 
 
151
    public DataElement getDataElementByAlternativeName( String alternativeName )
 
152
    {
 
153
        return dataElementStore.getDataElementByAlternativeName( alternativeName );
 
154
    }
 
155
 
 
156
    public DataElement getDataElementByShortName( String shortName )
 
157
    {
 
158
        return dataElementStore.getDataElementByShortName( shortName );
 
159
    }
 
160
 
 
161
    public DataElement getDataElementByCode( String code )
 
162
    {
 
163
        return dataElementStore.getDataElementByCode( code );
 
164
    }
 
165
 
 
166
    public Collection<DataElement> getDataElementsByAggregationOperator( String aggregationOperator )
 
167
    {
 
168
        return dataElementStore.getDataElementsByAggregationOperator( aggregationOperator );
 
169
    }
 
170
 
 
171
    public Collection<DataElement> getDataElementsByType( String type )
 
172
    {
 
173
        return dataElementStore.getDataElementsByType( type );
 
174
    }
 
175
    
 
176
    public Collection<DataElement> getDataElementByCategoryCombo( DataElementCategoryCombo categoryCombo )
 
177
    {
 
178
        return dataElementStore.getDataElementByCategoryCombo( categoryCombo );
 
179
    }
 
180
    
 
181
    // -------------------------------------------------------------------------
 
182
    // CalculatedDataElement
 
183
    // -------------------------------------------------------------------------
 
184
 
 
185
    public Collection<CalculatedDataElement> getAllCalculatedDataElements()
 
186
    {
 
187
        return dataElementStore.getAllCalculatedDataElements();
 
188
    }
 
189
 
 
190
    public CalculatedDataElement getCalculatedDataElementByDataElement( DataElement dataElement )
 
191
    {
 
192
        return dataElementStore.getCalculatedDataElementByDataElement( dataElement );
 
193
    }
 
194
 
 
195
    public Collection<CalculatedDataElement> getCalculatedDataElementsByDataElements(
 
196
        Collection<DataElement> dataElements )
 
197
    {
 
198
        return dataElementStore.getCalculatedDataElementsByDataElements( dataElements );
 
199
    }
 
200
 
 
201
    public Map<DataElement, Integer> getDataElementFactors( CalculatedDataElement calculatedDataElement )
 
202
    {
 
203
        Map<DataElement, Integer> factorMap = new HashMap<DataElement, Integer>();
 
204
 
 
205
        Pattern pattern = Pattern.compile( "\\[(\\d+)\\]\\s*\\*\\s*(\\d+)" );
 
206
 
 
207
        // ---------------------------------------------------------------------
 
208
        // In readable form: \[(\d+)\]\s*\*\s*(\d+)
 
209
        // Meaning any expression on the form "[id] * factor"
 
210
        // ---------------------------------------------------------------------
 
211
 
 
212
        Matcher matcher = pattern.matcher( calculatedDataElement.getExpression().getExpression() );
 
213
 
 
214
        while ( matcher.find() )
 
215
        {
 
216
            // -----------------------------------------------------------------
 
217
            // Key: Datelementid
 
218
            // Value: Factor
 
219
            // -----------------------------------------------------------------
 
220
 
 
221
            factorMap.put( getDataElement( Integer.parseInt( matcher.group( 1 ) ) ), Integer.parseInt( matcher
 
222
                .group( 2 ) ) );
 
223
        }
 
224
 
 
225
        return factorMap;
 
226
    }
 
227
 
 
228
    public Map<String, Integer> getOperandFactors( CalculatedDataElement calculatedDataElement )
 
229
    {
 
230
        Map<String, Integer> factorMap = new HashMap<String, Integer>();
 
231
 
 
232
        Pattern pattern = Pattern.compile( "\\[(\\d+\\.\\d+)\\]\\s*\\*\\s*(-?\\d+)" );
 
233
 
 
234
        // ---------------------------------------------------------------------
 
235
        // In readable form: \[(\d+)\]\s*\*\s*(\d+)
 
236
        // Meaning any expression on the form "[id] * factor"
 
237
        // ---------------------------------------------------------------------
 
238
 
 
239
        Matcher matcher = pattern.matcher( calculatedDataElement.getExpression().getExpression() );
 
240
 
 
241
        while ( matcher.find() )
 
242
        {
 
243
            // -----------------------------------------------------------------
 
244
            // Key: Datelementid.optioncomboid
 
245
            // Value: Factor
 
246
            // -----------------------------------------------------------------
 
247
 
 
248
            factorMap.put( matcher.group( 1 ), Integer.parseInt( matcher.group( 2 ) ) );
 
249
        }
 
250
 
 
251
        return factorMap;
 
252
    }
 
253
 
 
254
    public Collection<String> getOperandIds( CalculatedDataElement calculatedDataElement )
 
255
    {
 
256
        Collection<String> operands = new ArrayList<String>();
 
257
 
 
258
        Pattern pattern = Pattern.compile( "\\[(\\d+\\.\\d+)\\]" );
 
259
 
 
260
        // ---------------------------------------------------------------------
 
261
        // In readable form: \[(\d+)\]\s*\*\s*(\d+)
 
262
        // Meaning any expression on the form "[id] * factor"
 
263
        // ---------------------------------------------------------------------
 
264
 
 
265
        Matcher matcher = pattern.matcher( calculatedDataElement.getExpression().getExpression() );
 
266
 
 
267
        while ( matcher.find() )
 
268
        {
 
269
            // -----------------------------------------------------------------
 
270
            // Datelementid.optioncomboid
 
271
            // -----------------------------------------------------------------
 
272
 
 
273
            operands.add( matcher.group( 1 ) );
 
274
        }
 
275
 
 
276
        return operands;
 
277
    }
 
278
 
 
279
    // -------------------------------------------------------------------------
 
280
    // DataElementGroup
 
281
    // -------------------------------------------------------------------------
 
282
 
 
283
    public int addDataElementGroup( DataElementGroup dataElementGroup )
 
284
    {
 
285
        if ( dataElementGroup.getUuid() == null )
 
286
        {
 
287
            dataElementGroup.setUuid( UUIdUtils.getUUId() );
 
288
        }
 
289
 
 
290
        return dataElementStore.addDataElementGroup( dataElementGroup );
 
291
    }
 
292
 
 
293
    public void updateDataElementGroup( DataElementGroup dataElementGroup )
 
294
    {
 
295
        dataElementStore.updateDataElementGroup( dataElementGroup );
 
296
    }
 
297
 
 
298
    public void deleteDataElementGroup( DataElementGroup dataElementGroup )
 
299
    {
 
300
        dataElementStore.deleteDataElementGroup( dataElementGroup );
 
301
    }
 
302
 
 
303
    public DataElementGroup getDataElementGroup( int id )
 
304
    {
 
305
        return dataElementStore.getDataElementGroup( id );
 
306
    }
 
307
 
 
308
    public DataElementGroup getDataElementGroup( String uuid )
 
309
    {
 
310
        return dataElementStore.getDataElementGroup( uuid );
 
311
    }
 
312
 
 
313
    public Collection<DataElementGroup> getAllDataElementGroups()
 
314
    {
 
315
        return dataElementStore.getAllDataElementGroups();
 
316
    }
 
317
 
 
318
    public DataElementGroup getDataElementGroupByName( String name )
 
319
    {
 
320
        return dataElementStore.getDataElementGroupByName( name );
 
321
    }
 
322
 
 
323
    public Collection<DataElementGroup> getGroupsContainingDataElement( DataElement dataElement )
 
324
    {
 
325
        Collection<DataElementGroup> groups = getAllDataElementGroups();
 
326
 
 
327
        Iterator<DataElementGroup> iterator = groups.iterator();
 
328
 
 
329
        while ( iterator.hasNext() )
 
330
        {
 
331
            if ( !iterator.next().getMembers().contains( dataElement ) )
 
332
            {
 
333
                iterator.remove();
 
334
            }
 
335
        }
 
336
 
 
337
        return groups;
 
338
    }
 
339
}