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

« back to all changes in this revision

Viewing changes to dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-dataadmin/src/main/java/org/hisp/dhis/dataadmin/action/databrowser/SearchAction.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.dataadmin.action.databrowser;
 
2
 
 
3
/*
 
4
 * Copyright (c) 2004-${year}, 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.text.ParseException;
 
31
import java.text.SimpleDateFormat;
 
32
import java.util.Date;
 
33
import java.util.Iterator;
 
34
import java.util.List;
 
35
 
 
36
import org.hisp.dhis.databrowser.DataBrowserService;
 
37
import org.hisp.dhis.databrowser.DataBrowserTable;
 
38
import org.hisp.dhis.databrowser.MetaValue;
 
39
import org.hisp.dhis.dataset.DataSetService;
 
40
import org.hisp.dhis.organisationunit.OrganisationUnit;
 
41
import org.hisp.dhis.ouwt.manager.OrganisationUnitSelectionManager;
 
42
import org.hisp.dhis.period.PeriodService;
 
43
import org.hisp.dhis.period.PeriodType;
 
44
import org.hisp.dhis.util.SessionUtils;
 
45
 
 
46
import com.opensymphony.xwork.Action;
 
47
 
 
48
/**
 
49
 * @author espenjac, joakibj
 
50
 * @version $Id$
 
51
 */
 
52
public class SearchAction
 
53
    implements Action
 
54
{
 
55
    private static final String KEY_PERIODTYPEID = "periodTypeId";
 
56
    
 
57
    // -------------------------------------------------------------------------
 
58
    // Dependencies
 
59
    // -------------------------------------------------------------------------
 
60
 
 
61
    private DataBrowserService dataBrowserService;
 
62
 
 
63
    public void setDataBrowserService( DataBrowserService dataBrowserService )
 
64
    {
 
65
        this.dataBrowserService = dataBrowserService;
 
66
    }
 
67
 
 
68
    private DataSetService dataSetService;
 
69
 
 
70
    public void setDataSetService( DataSetService dataSetService )
 
71
    {
 
72
        this.dataSetService = dataSetService;
 
73
    }
 
74
 
 
75
    private PeriodService periodService;
 
76
 
 
77
    public void setPeriodService( PeriodService periodService )
 
78
    {
 
79
        this.periodService = periodService;
 
80
    }
 
81
 
 
82
    private OrganisationUnitSelectionManager selectionManager;
 
83
 
 
84
    public void setSelectionManager( OrganisationUnitSelectionManager selectionManager )
 
85
    {
 
86
        this.selectionManager = selectionManager;
 
87
    }
 
88
 
 
89
    // -------------------------------------------------------------------------
 
90
    // Input / output
 
91
    // -------------------------------------------------------------------------
 
92
 
 
93
    private OrganisationUnit selectedUnit;
 
94
 
 
95
    private DataBrowserTable dataBrowserTable;
 
96
 
 
97
    public long getQueryTime()
 
98
    {
 
99
        return dataBrowserTable.getQueryTime();
 
100
    }
 
101
 
 
102
    public int getQueryCount()
 
103
    {
 
104
        return dataBrowserTable.getQueryCount();
 
105
    }
 
106
 
 
107
    public List<MetaValue> getAllColumns()
 
108
    {
 
109
        return dataBrowserTable.getColumns();
 
110
    }
 
111
 
 
112
    public DataBrowserTable getDataBrowserTable()
 
113
    {
 
114
        return dataBrowserTable;
 
115
    }
 
116
 
 
117
    public List<List<Integer>> getAllCounts()
 
118
    {
 
119
        return dataBrowserTable.getCounts();
 
120
    }
 
121
 
 
122
    public Iterator<MetaValue> getRowNamesIterator()
 
123
    {
 
124
        return dataBrowserTable.getRows().iterator();
 
125
    }
 
126
 
 
127
    private String searchOption;
 
128
 
 
129
    public String getSearchOption()
 
130
    {
 
131
        return searchOption;
 
132
    }
 
133
 
 
134
    public void setSearchOption( String searchOption )
 
135
    {
 
136
        this.searchOption = searchOption;
 
137
    }
 
138
 
 
139
    private String toDate;
 
140
 
 
141
    public String getToDate()
 
142
    {
 
143
        return toDate;
 
144
    }
 
145
 
 
146
    public void setToDate( String toDate )
 
147
    {
 
148
        this.toDate = toDate;
 
149
    }
 
150
 
 
151
    private String fromDate;
 
152
 
 
153
    public String getFromDate()
 
154
    {
 
155
        return fromDate;
 
156
    }
 
157
 
 
158
    public void setFromDate( String fromDate )
 
159
    {
 
160
        this.fromDate = fromDate;
 
161
    }
 
162
 
 
163
    private String periodTypeId;
 
164
 
 
165
    public String getPeriodTypeId()
 
166
    {
 
167
        return periodTypeId;
 
168
    }
 
169
 
 
170
    public void setPeriodTypeId( String periodTypeId )
 
171
    {
 
172
        SessionUtils.setSessionVar( KEY_PERIODTYPEID, periodTypeId );
 
173
    }
 
174
 
 
175
    private String parent;
 
176
 
 
177
    public String getParent()
 
178
    {
 
179
        return parent;
 
180
    }
 
181
 
 
182
    public void setParent( String parent )
 
183
    {
 
184
        this.parent = parent;
 
185
    }
 
186
 
 
187
    private String tmpParent;
 
188
 
 
189
    public String getTmpParent()
 
190
    {
 
191
        return tmpParent;
 
192
    }
 
193
 
 
194
    private long requestTime;
 
195
 
 
196
    public long getRequestTime()
 
197
    {
 
198
        return requestTime;
 
199
    }    
 
200
 
 
201
    public String getParentName()
 
202
    {
 
203
        if ( searchOption.equals( "OrganisationUnit" ) )
 
204
        {
 
205
            return selectedUnit.getName();
 
206
        }
 
207
 
 
208
        if ( tmpParent == null )
 
209
        {
 
210
            return "";
 
211
        }
 
212
 
 
213
        if ( searchOption.equals( "DataSet" ) )
 
214
        {
 
215
            return dataSetService.getDataSet( Integer.parseInt( tmpParent ) ).getName();
 
216
        }
 
217
 
 
218
        return "";
 
219
    }
 
220
 
 
221
    // -------------------------------------------------------------------------
 
222
    // Action implementation
 
223
    // -------------------------------------------------------------------------
 
224
 
 
225
    public String execute()
 
226
    {
 
227
        long before = System.currentTimeMillis();
 
228
        
 
229
        periodTypeId = (String) SessionUtils.getSessionVar( KEY_PERIODTYPEID );
 
230
 
 
231
        // Check if the second selected date is later than the first selected date
 
232
        
 
233
        if ( fromDate.length() == 0 && toDate.length() == 0 )
 
234
        {
 
235
            if ( checkDates( fromDate, toDate ) )
 
236
            {
 
237
                return ERROR;
 
238
            }
 
239
        }
 
240
 
 
241
        PeriodType periodType = periodService.getPeriodTypeByName( periodTypeId );
 
242
 
 
243
        if ( searchOption.equals( "DataSet" ) )
 
244
        {
 
245
            // Get all dataset objects
 
246
            
 
247
            if ( fromDate.length() == 0 && toDate.length() == 0 )
 
248
            {
 
249
                dataBrowserTable = dataBrowserService.getAllCountDataSetsByPeriodType( periodType );
 
250
            }
 
251
            else
 
252
            {
 
253
                dataBrowserTable = dataBrowserService.getCountDataSetsInPeriod( fromDate, toDate, periodType );
 
254
            }
 
255
 
 
256
            if ( parent != null )
 
257
            {
 
258
                // Show dataelement
 
259
 
 
260
                Integer parentInt = Integer.parseInt( parent );
 
261
 
 
262
                if ( fromDate.length() == 0 && toDate.length() == 0 )
 
263
                {
 
264
                    dataBrowserTable = dataBrowserService.getAllCountDataElementsByPeriodType( parentInt, periodType );
 
265
                }
 
266
                else
 
267
                {
 
268
                    dataBrowserTable = dataBrowserService.getCountDataElementsInPeriod( parentInt, fromDate, toDate,
 
269
                        periodType );
 
270
                }
 
271
 
 
272
                tmpParent = parent;
 
273
            }
 
274
 
 
275
        }
 
276
        else if ( searchOption.equals( "OrganisationUnit" ) )
 
277
        {
 
278
            selectedUnit = selectionManager.getSelectedOrganisationUnit();
 
279
 
 
280
            if ( selectedUnit != null )
 
281
            {
 
282
 
 
283
                if ( fromDate.length() == 0 && toDate.length() == 0 )
 
284
                {
 
285
                    dataBrowserTable = dataBrowserService.getAllCountOrgUnitsByPeriodType( selectedUnit.getId(), periodType );
 
286
                }
 
287
                else
 
288
                {
 
289
                    dataBrowserTable = dataBrowserService.getCountOrgUnitsInPeriod( selectedUnit.getId(), fromDate, toDate, periodType );
 
290
                }
 
291
 
 
292
                requestTime = System.currentTimeMillis() - before;
 
293
            }
 
294
            else
 
295
            {
 
296
                return ERROR;
 
297
            }
 
298
        }
 
299
        else
 
300
        {
 
301
            return ERROR;
 
302
        }
 
303
        
 
304
        requestTime = System.currentTimeMillis() - before;
 
305
        
 
306
        return SUCCESS;
 
307
    }
 
308
 
 
309
    // -------------------------------------------------------------------------
 
310
    // Supportive methods
 
311
    // -------------------------------------------------------------------------
 
312
 
 
313
    /**
 
314
     * This is a helper method for checking if the fromDate is later than the
 
315
     * toDate. This is necessary in case a user sends the dates with HTTP GET.
 
316
     * 
 
317
     * @param fromDate
 
318
     * @param toDate
 
319
     * @return
 
320
     */
 
321
    private boolean checkDates( String fromDate, String toDate )
 
322
    {
 
323
        String formatString = "yyyy-MM-dd";
 
324
        SimpleDateFormat sdf = new SimpleDateFormat( formatString );
 
325
 
 
326
        Date date1 = new Date();
 
327
        Date date2 = new Date();
 
328
 
 
329
        try
 
330
        {
 
331
            date1 = sdf.parse( fromDate );
 
332
            date2 = sdf.parse( toDate );
 
333
        }
 
334
        catch ( ParseException e )
 
335
        {            
 
336
            return false; // The user hasn't specified any dates
 
337
        }
 
338
        
 
339
        if ( !date1.before( date2 ) )
 
340
        {
 
341
            return true; // Return true if date2 is earlier than date1
 
342
        }
 
343
        else
 
344
        {
 
345
            return false;
 
346
        }
 
347
    }
 
348
}