1
package org.hisp.dhis.dataadmin.action.databrowser;
4
* Copyright (c) 2004-${year}, 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.text.ParseException;
31
import java.text.SimpleDateFormat;
32
import java.util.Date;
33
import java.util.Iterator;
34
import java.util.List;
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;
46
import com.opensymphony.xwork.Action;
49
* @author espenjac, joakibj
52
public class SearchAction
55
private static final String KEY_PERIODTYPEID = "periodTypeId";
57
// -------------------------------------------------------------------------
59
// -------------------------------------------------------------------------
61
private DataBrowserService dataBrowserService;
63
public void setDataBrowserService( DataBrowserService dataBrowserService )
65
this.dataBrowserService = dataBrowserService;
68
private DataSetService dataSetService;
70
public void setDataSetService( DataSetService dataSetService )
72
this.dataSetService = dataSetService;
75
private PeriodService periodService;
77
public void setPeriodService( PeriodService periodService )
79
this.periodService = periodService;
82
private OrganisationUnitSelectionManager selectionManager;
84
public void setSelectionManager( OrganisationUnitSelectionManager selectionManager )
86
this.selectionManager = selectionManager;
89
// -------------------------------------------------------------------------
91
// -------------------------------------------------------------------------
93
private OrganisationUnit selectedUnit;
95
private DataBrowserTable dataBrowserTable;
97
public long getQueryTime()
99
return dataBrowserTable.getQueryTime();
102
public int getQueryCount()
104
return dataBrowserTable.getQueryCount();
107
public List<MetaValue> getAllColumns()
109
return dataBrowserTable.getColumns();
112
public DataBrowserTable getDataBrowserTable()
114
return dataBrowserTable;
117
public List<List<Integer>> getAllCounts()
119
return dataBrowserTable.getCounts();
122
public Iterator<MetaValue> getRowNamesIterator()
124
return dataBrowserTable.getRows().iterator();
127
private String searchOption;
129
public String getSearchOption()
134
public void setSearchOption( String searchOption )
136
this.searchOption = searchOption;
139
private String toDate;
141
public String getToDate()
146
public void setToDate( String toDate )
148
this.toDate = toDate;
151
private String fromDate;
153
public String getFromDate()
158
public void setFromDate( String fromDate )
160
this.fromDate = fromDate;
163
private String periodTypeId;
165
public String getPeriodTypeId()
170
public void setPeriodTypeId( String periodTypeId )
172
SessionUtils.setSessionVar( KEY_PERIODTYPEID, periodTypeId );
175
private String parent;
177
public String getParent()
182
public void setParent( String parent )
184
this.parent = parent;
187
private String tmpParent;
189
public String getTmpParent()
194
private long requestTime;
196
public long getRequestTime()
201
public String getParentName()
203
if ( searchOption.equals( "OrganisationUnit" ) )
205
return selectedUnit.getName();
208
if ( tmpParent == null )
213
if ( searchOption.equals( "DataSet" ) )
215
return dataSetService.getDataSet( Integer.parseInt( tmpParent ) ).getName();
221
// -------------------------------------------------------------------------
222
// Action implementation
223
// -------------------------------------------------------------------------
225
public String execute()
227
long before = System.currentTimeMillis();
229
periodTypeId = (String) SessionUtils.getSessionVar( KEY_PERIODTYPEID );
231
// Check if the second selected date is later than the first selected date
233
if ( fromDate.length() == 0 && toDate.length() == 0 )
235
if ( checkDates( fromDate, toDate ) )
241
PeriodType periodType = periodService.getPeriodTypeByName( periodTypeId );
243
if ( searchOption.equals( "DataSet" ) )
245
// Get all dataset objects
247
if ( fromDate.length() == 0 && toDate.length() == 0 )
249
dataBrowserTable = dataBrowserService.getAllCountDataSetsByPeriodType( periodType );
253
dataBrowserTable = dataBrowserService.getCountDataSetsInPeriod( fromDate, toDate, periodType );
256
if ( parent != null )
260
Integer parentInt = Integer.parseInt( parent );
262
if ( fromDate.length() == 0 && toDate.length() == 0 )
264
dataBrowserTable = dataBrowserService.getAllCountDataElementsByPeriodType( parentInt, periodType );
268
dataBrowserTable = dataBrowserService.getCountDataElementsInPeriod( parentInt, fromDate, toDate,
276
else if ( searchOption.equals( "OrganisationUnit" ) )
278
selectedUnit = selectionManager.getSelectedOrganisationUnit();
280
if ( selectedUnit != null )
283
if ( fromDate.length() == 0 && toDate.length() == 0 )
285
dataBrowserTable = dataBrowserService.getAllCountOrgUnitsByPeriodType( selectedUnit.getId(), periodType );
289
dataBrowserTable = dataBrowserService.getCountOrgUnitsInPeriod( selectedUnit.getId(), fromDate, toDate, periodType );
292
requestTime = System.currentTimeMillis() - before;
304
requestTime = System.currentTimeMillis() - before;
309
// -------------------------------------------------------------------------
310
// Supportive methods
311
// -------------------------------------------------------------------------
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.
321
private boolean checkDates( String fromDate, String toDate )
323
String formatString = "yyyy-MM-dd";
324
SimpleDateFormat sdf = new SimpleDateFormat( formatString );
326
Date date1 = new Date();
327
Date date2 = new Date();
331
date1 = sdf.parse( fromDate );
332
date2 = sdf.parse( toDate );
334
catch ( ParseException e )
336
return false; // The user hasn't specified any dates
339
if ( !date1.before( date2 ) )
341
return true; // Return true if date2 is earlier than date1