~registry/dhis2-academy/group3_messaging_support

« back to all changes in this revision

Viewing changes to dhis-2/dhis-web/dhis-web-light/src/main/java/org/hisp/dhis/light/searchService/Search.java

  • Committer: michaelgunnulfsen at gmail
  • Date: 2011-11-15 17:14:22 UTC
  • Revision ID: michaelgunnulfsen@gmail.com-20111115171422-oz4g8q23i3xpnh42
added search class

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hisp.dhis.light.searchService;
 
2
 
 
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;
 
11
 
 
12
/**
 
13
 * @author Fredrik Haaland
 
14
 */
 
15
public class Search {
 
16
 
 
17
        /** Size of result **/
 
18
        private int queryResultSize;
 
19
 
 
20
        private UserService userService;
 
21
 
 
22
        /** The maximum of results displayed to the user **/
 
23
        final private int MAX_SEARCH_RESULT = 20;
 
24
 
 
25
        private CurrentUserService currentUserService;
 
26
 
 
27
        public UserService getUserService() {
 
28
                return userService;
 
29
        }
 
30
 
 
31
        public void setUserService(UserService userService) {
 
32
                this.userService = userService;
 
33
        }
 
34
 
 
35
        public CurrentUserService getCurrentUserService() {
 
36
                return currentUserService;
 
37
        }
 
38
 
 
39
        public void setCurrentUserService(CurrentUserService currentUserService) {
 
40
                this.currentUserService = currentUserService;
 
41
        }
 
42
 
 
43
        public int getQueryResultSize() {
 
44
                return queryResultSize;
 
45
        }
 
46
 
 
47
        public void setQueryResultSize(int queryResultSize) {
 
48
                this.queryResultSize = queryResultSize;
 
49
        }
 
50
 
 
51
        /**
 
52
         * The implementation of searching organization units.
 
53
         * 
 
54
         * @param query
 
55
         * @return
 
56
         */
 
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>>();
 
60
 
 
61
                // The results matching the query
 
62
                HashSet<OrganisationUnit> resultUnits = new HashSet<OrganisationUnit>();
 
63
 
 
64
                // The mapping, used by velocity
 
65
                HashMap<String, String> searchResult;
 
66
 
 
67
                // Count results, so we do'nt exceed the maximum
 
68
                int resultCount = 0;
 
69
 
 
70
                // Fetch units matching the query as prefix
 
71
                // resultUnits.addAll(
 
72
                // getOrganisationUnitService().getAllOrganisationUnitsWithPrefix( query
 
73
                // ) );
 
74
 
 
75
                // Then, store the meta-data
 
76
                setQueryResultSize(resultUnits.size());
 
77
 
 
78
                // --------------------------------
 
79
                // Filter out based on the query
 
80
                // --------------------------------
 
81
 
 
82
                // Cache lower-case version of query to simplify comparison
 
83
                String cachedQueryPhrase = query.toLowerCase();
 
84
 
 
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());
 
93
 
 
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());
 
99
                        }
 
100
 
 
101
                        for (OrganisationUnit unit : parentUnits) {
 
102
                                if (unit.getChildren() != null)
 
103
                                        userUnits.addAll(unit.getChildren());
 
104
                        }
 
105
 
 
106
                        // Add all back to original set, which is the related set
 
107
                        userUnits.addAll(parentUnits);
 
108
                        userUnits.retainAll(resultUnits);
 
109
 
 
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);
 
116
 
 
117
                                if (++resultCount == MAX_SEARCH_RESULT)
 
118
                                        break;
 
119
                        }
 
120
 
 
121
                        resultUnits.removeAll(userUnits);
 
122
 
 
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);
 
128
 
 
129
                                if (resultCount++ == MAX_SEARCH_RESULT)
 
130
                                        break;
 
131
                        }
 
132
                }
 
133
 
 
134
                else {
 
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);
 
142
 
 
143
                                        if (++resultCount == MAX_SEARCH_RESULT)
 
144
                                                break;
 
145
                                }
 
146
                        }
 
147
 
 
148
                        // Set size to give user some meta-data
 
149
                        setQueryResultSize(resultCount);
 
150
                }
 
151
 
 
152
                return list;
 
153
        }
 
154
 
 
155
        /**
 
156
         * The implementation of searching users.
 
157
         * 
 
158
         * @param query
 
159
         * @return
 
160
         */
 
161
 
 
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>>();
 
165
 
 
166
                // The mapping, used by velocity
 
167
                HashMap<String, String> searchResult;
 
168
 
 
169
                // No-brainer, just get them all
 
170
                Collection<User> contacts = getUserService().getAllUsers();
 
171
 
 
172
                // Cache lower-case version of query to simplify comparison
 
173
                String cachedQueryPhrase = query.toLowerCase();
 
174
 
 
175
                int i = 0;
 
176
                for (User contact : contacts) {
 
177
                        if (contact.getFirstName().toLowerCase()
 
178
                                        .startsWith(cachedQueryPhrase)
 
179
                                        || contact.getSurname().toLowerCase()
 
180
                                                        .startsWith(cachedQueryPhrase)) {
 
181
 
 
182
                                searchResult = new HashMap<String, String>();
 
183
                                searchResult.put("id", Integer.toString(contact.getId()));
 
184
                                searchResult.put("name", contact.getName());
 
185
                                list.add(searchResult);
 
186
 
 
187
                                if (++i == MAX_SEARCH_RESULT)
 
188
                                        break;
 
189
                        }
 
190
                }
 
191
 
 
192
                // Set size to give user some meta-data
 
193
                setQueryResultSize(i);
 
194
                return list;
 
195
        }
 
196
 
 
197
}