1
package org.hisp.dhis.light.searchService;
3
import java.util.ArrayList;
4
import java.util.Collection;
5
import java.util.HashMap;
6
import java.util.HashSet;
7
import org.hisp.dhis.organisationunit.OrganisationUnit;
8
import org.hisp.dhis.user.CurrentUserService;
9
import org.hisp.dhis.user.User;
10
import org.hisp.dhis.user.UserService;
13
* @author Fredrik Haaland
17
/** Size of result **/
18
private int queryResultSize;
20
private UserService userService;
22
/** The maximum of results displayed to the user **/
23
final private int MAX_SEARCH_RESULT = 20;
25
private CurrentUserService currentUserService;
27
public UserService getUserService() {
31
public void setUserService(UserService userService) {
32
this.userService = userService;
35
public CurrentUserService getCurrentUserService() {
36
return currentUserService;
39
public void setCurrentUserService(CurrentUserService currentUserService) {
40
this.currentUserService = currentUserService;
43
public int getQueryResultSize() {
44
return queryResultSize;
47
public void setQueryResultSize(int queryResultSize) {
48
this.queryResultSize = queryResultSize;
52
* The implementation of searching organization units.
57
public ArrayList<HashMap<String, String>> searchOrgUnits(String query) {
58
// Structures that the Velocity may iterate over
59
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
61
// The results matching the query
62
HashSet<OrganisationUnit> resultUnits = new HashSet<OrganisationUnit>();
64
// The mapping, used by velocity
65
HashMap<String, String> searchResult;
67
// Count results, so we do'nt exceed the maximum
70
// Fetch units matching the query as prefix
71
// resultUnits.addAll(
72
// getOrganisationUnitService().getAllOrganisationUnitsWithPrefix( query
75
// Then, store the meta-data
76
setQueryResultSize(resultUnits.size());
78
// --------------------------------
79
// Filter out based on the query
80
// --------------------------------
82
// Cache lower-case version of query to simplify comparison
83
String cachedQueryPhrase = query.toLowerCase();
85
// Refine the search if the result size is too large
86
if (resultUnits.size() > MAX_SEARCH_RESULT) {
87
// Do some post-processing on related organizational units
88
// since we can assume that the person want somebody close to where
89
// the user is assigned to
90
HashSet<OrganisationUnit> userUnits = new HashSet<OrganisationUnit>();
91
userUnits.addAll(getCurrentUserService().getCurrentUser()
92
.getOrganisationUnits());
94
// Add parent of all the ones the user is assigned to
95
HashSet<OrganisationUnit> parentUnits = new HashSet<OrganisationUnit>();
96
for (OrganisationUnit unit : userUnits) {
97
if (unit.getParent() != null)
98
parentUnits.add(unit.getParent());
101
for (OrganisationUnit unit : parentUnits) {
102
if (unit.getChildren() != null)
103
userUnits.addAll(unit.getChildren());
106
// Add all back to original set, which is the related set
107
userUnits.addAll(parentUnits);
108
userUnits.retainAll(resultUnits);
110
// Now append them in the most-likely-one order
111
for (OrganisationUnit unit : userUnits) {
112
searchResult = new HashMap<String, String>();
113
searchResult.put("id", Integer.toString(unit.getId()));
114
searchResult.put("name", unit.getName());
115
list.add(searchResult);
117
if (++resultCount == MAX_SEARCH_RESULT)
121
resultUnits.removeAll(userUnits);
123
for (OrganisationUnit unit : resultUnits) {
124
searchResult = new HashMap<String, String>();
125
searchResult.put("id", Integer.toString(unit.getId()));
126
searchResult.put("name", unit.getName());
127
list.add(searchResult);
129
if (resultCount++ == MAX_SEARCH_RESULT)
135
// Assign the results in a form used by Velocity
136
for (OrganisationUnit unit : resultUnits) {
137
if (unit.getName().toLowerCase().startsWith(cachedQueryPhrase)) {
138
searchResult = new HashMap<String, String>();
139
searchResult.put("id", Integer.toString(unit.getId()));
140
searchResult.put("name", unit.getName());
141
list.add(searchResult);
143
if (++resultCount == MAX_SEARCH_RESULT)
148
// Set size to give user some meta-data
149
setQueryResultSize(resultCount);
156
* The implementation of searching users.
162
public ArrayList<HashMap<String, String>> searchContacts(String query) {
163
// Structures that the Velocity may iterate over
164
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
166
// The mapping, used by velocity
167
HashMap<String, String> searchResult;
169
// No-brainer, just get them all
170
Collection<User> contacts = getUserService().getAllUsers();
172
// Cache lower-case version of query to simplify comparison
173
String cachedQueryPhrase = query.toLowerCase();
176
for (User contact : contacts) {
177
if (contact.getFirstName().toLowerCase()
178
.startsWith(cachedQueryPhrase)
179
|| contact.getSurname().toLowerCase()
180
.startsWith(cachedQueryPhrase)) {
182
searchResult = new HashMap<String, String>();
183
searchResult.put("id", Integer.toString(contact.getId()));
184
searchResult.put("name", contact.getName());
185
list.add(searchResult);
187
if (++i == MAX_SEARCH_RESULT)
192
// Set size to give user some meta-data
193
setQueryResultSize(i);