001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/profile/tags/sakai_2-4-1/profile-app/src/java/org/sakaiproject/tool/profile/SearchTool.java $
003: * $Id: SearchTool.java 8424 2006-04-27 20:23:44Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.tool.profile;
021:
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import javax.faces.event.ValueChangeEvent;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.sakaiproject.api.app.profile.Profile;
031: import org.sakaiproject.api.app.profile.ProfileManager;
032: import org.sakaiproject.util.ResourceLoader;
033:
034: /**
035: * @author rshastri <a href="mailto:rshastri@iupui.edu ">Rashmi Shastri</a>
036: */
037: public class SearchTool {
038: private ResourceLoader msgs = new ResourceLoader(
039: "org.sakaiproject.tool.profile.bundle.Messages");
040:
041: private static final Log LOG = LogFactory.getLog(SearchTool.class);
042:
043: private DecoratedProfile profile;
044:
045: private String searchKeyword;
046:
047: private List searchResults;
048:
049: private List currentSearchResults;
050:
051: private int noOfRecDisplayedFrom = 0;
052:
053: private int noOfRecDisplayedTo = 0;
054:
055: private int numberOfSearchedRecordsDisplayedPerPage = 10;
056:
057: private String displayNoOfRec = "10";
058:
059: private boolean showPrevious = false;
060:
061: private boolean showNext = false;
062:
063: private boolean showSearchResults = false;
064:
065: private boolean showNoMatchFound = false;
066:
067: private boolean redirectToSearchedProfile = false;
068:
069: protected ProfileManager profileService;
070:
071: public SearchTool() {
072: this .reset(msgs.getString("java.search_keyword"));
073: }
074:
075: public String getDisplayPage() {
076: LOG.debug("getDisplayPage()");
077: if (redirectToSearchedProfile) {
078: return "displaySearchedProfile";
079: } else {
080: return "main";
081: }
082: }
083:
084: public void processValueChangeForDisplayNSearchResult(
085: ValueChangeEvent vce) {
086: if (LOG.isDebugEnabled())
087: LOG
088: .debug("processValueChangeForDisplayNSearchResult(ValueChangeEvent "
089: + vce + ")");
090: setDisplayNoOfRec(((String) vce.getNewValue()));
091: LOG.debug("Show these many rec :" + (String) vce.getNewValue());
092: processActionDisplayFirst();
093: }
094:
095: public String processActionDisplayFirst() {
096: LOG.debug("processActionDisplayFirst()");
097: try {
098: if ((searchResults != null)
099: && (searchResults.size() > 1)
100: && (searchResults.size() <= numberOfSearchedRecordsDisplayedPerPage)) {
101: showPrevious = false;
102: showNext = false;
103: showSearchResults = true;
104: currentSearchResults = searchResults;
105: noOfRecDisplayedFrom = 1;
106: noOfRecDisplayedTo = searchResults.size();
107:
108: return getDisplayPage();
109: } else if ((searchResults != null)
110: && (searchResults.size() > numberOfSearchedRecordsDisplayedPerPage)) {
111: currentSearchResults = searchResults.subList(0,
112: (numberOfSearchedRecordsDisplayedPerPage));
113: noOfRecDisplayedFrom = 1;
114: noOfRecDisplayedTo = numberOfSearchedRecordsDisplayedPerPage;
115: showPrevious = false;
116: showNext = true;
117: showSearchResults = true;
118:
119: return getDisplayPage();
120: } else {
121: return profile.processActionDisplayProfile();
122: }
123: } catch (Exception e) {
124: LOG.error(e.getMessage(), e);
125:
126: return null;
127: }
128: }
129:
130: public String processActionDisplayNext() {
131: LOG.debug("processActionDisplayNext()");
132: try {
133: if ((searchResults != null)
134: && (searchResults.size() > (noOfRecDisplayedTo))) {
135: this .showNoMatchFound = false;
136: this .showSearchResults = true;
137: this .showPrevious = true;
138:
139: if (searchResults.size() > (noOfRecDisplayedTo + numberOfSearchedRecordsDisplayedPerPage)) {
140: currentSearchResults = searchResults
141: .subList(
142: (noOfRecDisplayedTo),
143: (noOfRecDisplayedTo + numberOfSearchedRecordsDisplayedPerPage));
144: noOfRecDisplayedFrom = noOfRecDisplayedTo + 1;
145: noOfRecDisplayedTo = noOfRecDisplayedTo
146: + numberOfSearchedRecordsDisplayedPerPage;
147: showNext = true;
148:
149: return getDisplayPage();
150: }
151:
152: if (searchResults.size() == (noOfRecDisplayedTo + numberOfSearchedRecordsDisplayedPerPage)) {
153: currentSearchResults = searchResults
154: .subList(
155: (noOfRecDisplayedTo),
156: (noOfRecDisplayedTo + numberOfSearchedRecordsDisplayedPerPage));
157: noOfRecDisplayedFrom = noOfRecDisplayedTo + 1;
158: noOfRecDisplayedTo = noOfRecDisplayedTo
159: + numberOfSearchedRecordsDisplayedPerPage;
160: showNext = false;
161:
162: return getDisplayPage();
163: }
164:
165: if (searchResults.size() < (noOfRecDisplayedTo + numberOfSearchedRecordsDisplayedPerPage)) {
166: currentSearchResults = searchResults.subList(
167: (noOfRecDisplayedTo),
168: (searchResults.size()));
169: noOfRecDisplayedFrom = noOfRecDisplayedTo + 1;
170: noOfRecDisplayedTo = searchResults.size();
171: showNext = false;
172:
173: return getDisplayPage();
174: }
175: }
176:
177: return getDisplayPage();
178: } catch (Exception e) {
179: LOG.error(e.getMessage(), e);
180:
181: return null;
182: }
183: }
184:
185: public String processActionDisplayPrevious() {
186: LOG.debug("processActionDisplayPrevious()");
187: try {
188: if ((searchResults != null)
189: && ((noOfRecDisplayedFrom) > 1)
190: && (noOfRecDisplayedFrom >= numberOfSearchedRecordsDisplayedPerPage)) {
191: this .showNext = true;
192: this .showNoMatchFound = false;
193: this .showSearchResults = true;
194:
195: if ((noOfRecDisplayedFrom - numberOfSearchedRecordsDisplayedPerPage) == 1) {
196: showPrevious = false;
197: currentSearchResults = searchResults.subList(0,
198: (numberOfSearchedRecordsDisplayedPerPage));
199: noOfRecDisplayedFrom = 1;
200: noOfRecDisplayedTo = numberOfSearchedRecordsDisplayedPerPage;
201: } else {
202: showPrevious = true;
203: currentSearchResults = searchResults
204: .subList(
205: (noOfRecDisplayedFrom
206: - numberOfSearchedRecordsDisplayedPerPage - 1),
207: (noOfRecDisplayedFrom - 1));
208: noOfRecDisplayedTo = noOfRecDisplayedFrom - 1;
209: noOfRecDisplayedFrom = noOfRecDisplayedFrom
210: - numberOfSearchedRecordsDisplayedPerPage;
211: }
212: } else {
213: showPrevious = false;
214: }
215:
216: return getDisplayPage();
217: } catch (Exception e) {
218: LOG.error(e.getMessage(), e);
219:
220: return null;
221: }
222: }
223:
224: public String processActionDisplayLast() {
225: LOG.debug("processActionDisplayLast()");
226: try {
227: // Single page display
228: if ((searchResults != null)
229: && (searchResults.size() > 1)
230: && (searchResults.size() <= numberOfSearchedRecordsDisplayedPerPage)) {
231: showPrevious = false;
232: showNext = false;
233: currentSearchResults = searchResults;
234:
235: return this .getDisplayPage();
236: } else if ((searchResults != null)
237: && (searchResults.size() > numberOfSearchedRecordsDisplayedPerPage)) {
238: int displayRecForLastPage = searchResults.size()
239: % numberOfSearchedRecordsDisplayedPerPage;
240:
241: if (displayRecForLastPage == 0) {
242: currentSearchResults = searchResults
243: .subList(
244: (searchResults.size() - numberOfSearchedRecordsDisplayedPerPage),
245: (searchResults.size()));
246: noOfRecDisplayedFrom = searchResults.size()
247: - numberOfSearchedRecordsDisplayedPerPage
248: + 1;
249: } else {
250: // there is a remainder
251: currentSearchResults = searchResults
252: .subList(
253: (searchResults.size() - displayRecForLastPage),
254: (searchResults.size()));
255: noOfRecDisplayedFrom = searchResults.size()
256: - displayRecForLastPage + 1;
257: }
258:
259: noOfRecDisplayedTo = searchResults.size();
260: showPrevious = true;
261: showNext = false;
262:
263: return this .getDisplayPage();
264: } else
265: // search result is exactly 1
266: {
267: return profile.processActionDisplayProfile();
268: }
269: } catch (Exception e) {
270: LOG.error(e.getMessage(), e);
271:
272: return null;
273: }
274: }
275:
276: public String processActionSearch() {
277: LOG.debug("processActionSearch()");
278: try {
279: this .reset(searchKeyword);
280: // Find User mutable profiles only
281: if (searchKeyword == null
282: || searchKeyword.trim().length() < 1) {
283: this .showNoMatchFound = true;
284: return "main";
285: }
286: List profiles = profileService.findProfiles(searchKeyword);
287: searchResults = new ArrayList();
288:
289: if ((profiles != null) && (profiles.size() > 0)) {
290: Iterator profileIterator = profiles.iterator();
291:
292: while (profileIterator.hasNext()) {
293: profile = new DecoratedProfile(
294: (Profile) profileIterator.next());
295: searchResults.add(profile);
296: }
297:
298: return processActionDisplayFirst();
299: } else {
300: this .showNoMatchFound = true;
301:
302: return "main";
303: }
304: } catch (Exception e) {
305: LOG.error(e.getMessage(), e);
306:
307: return null;
308: }
309: }
310:
311: public boolean isShowNext() {
312: LOG.debug("isShowNext()");
313: return showNext;
314: }
315:
316: public boolean isShowPrevious() {
317: LOG.debug("isShowPrevious()");
318: return showPrevious;
319: }
320:
321: public List getCurrentSearchResults() {
322: LOG.debug("getCurrentSearchResults()");
323: return currentSearchResults;
324: }
325:
326: public void setCurrentSearchResults(List currentSearchResults) {
327: if (LOG.isDebugEnabled())
328: LOG.debug("setCurrentSearchResults(List"
329: + currentSearchResults + ")");
330: this .currentSearchResults = currentSearchResults;
331: }
332:
333: public boolean isShowNoMatchFound() {
334: LOG.debug("isShowNoMatchFound()");
335: return showNoMatchFound;
336: }
337:
338: public boolean isShowSearchResults() {
339: LOG.debug("isShowSearchResults()");
340: return (showSearchResults);
341: }
342:
343: public void reset(String searchKeyword) {
344: if (LOG.isDebugEnabled())
345: LOG.debug("reset(String" + searchKeyword + ")");
346: this .searchKeyword = searchKeyword;
347: this .profile = null;
348: this .searchResults = null;
349: this .currentSearchResults = null;
350: this .noOfRecDisplayedFrom = 0;
351: this .noOfRecDisplayedTo = 0;
352: this .showPrevious = false;
353: this .showNext = false;
354: this .showSearchResults = false;
355: this .showNoMatchFound = false;
356: this .redirectToSearchedProfile = false;
357: }
358:
359: public String processCancel() {
360: LOG.debug("processCancel()");
361: this .reset("Search Keyword");
362: return "main";
363: }
364:
365: public String getDisplayNoOfRec() {
366: LOG.debug("getDisplayNoOfRec()");
367: try {
368: if ((this .displayNoOfRec != null)
369: && (Integer.parseInt(displayNoOfRec) != 0)) {
370: numberOfSearchedRecordsDisplayedPerPage = Integer
371: .parseInt(displayNoOfRec);
372: }
373: } catch (NumberFormatException e) {
374: LOG.error(e.getMessage(), e);
375: }
376:
377: return displayNoOfRec;
378: }
379:
380: public void setDisplayNoOfRec(String no_of_searched_rec_per_page) {
381: if (LOG.isDebugEnabled())
382: LOG.debug("setDisplayNoOfRec(String "
383: + no_of_searched_rec_per_page + ")");
384: displayNoOfRec = no_of_searched_rec_per_page;
385:
386: try {
387: if ((this .displayNoOfRec != null)
388: && (Integer.parseInt(displayNoOfRec) != 0)) {
389: numberOfSearchedRecordsDisplayedPerPage = Integer
390: .parseInt(displayNoOfRec);
391: }
392: } catch (NumberFormatException e) {
393: LOG.error(e.getMessage(), e);
394: }
395: }
396:
397: public DecoratedProfile getProfile() {
398: LOG.debug("getProfile()");
399: return profile;
400: }
401:
402: /**
403: * @return
404: */
405: public ProfileManager getProfileService() {
406: LOG.debug("getProfileService()");
407: return profileService;
408: }
409:
410: /**
411: * @return
412: */
413: public String getSearchKeyword() {
414: LOG.debug("getSearchKeyword()");
415: return searchKeyword;
416: }
417:
418: /**
419: * @return
420: */
421: public List getSearchResults() {
422: LOG.debug("getSearchResults()");
423: return searchResults;
424: }
425:
426: /**
427: * @param profileService
428: */
429: public void setProfileService(ProfileManager profileService) {
430: if (LOG.isDebugEnabled())
431: LOG.debug("setProfileService(ProfileManager "
432: + profileService + ")");
433: this .profileService = profileService;
434: }
435:
436: /**
437: * @param profile
438: */
439: public void setProfile(DecoratedProfile profile) {
440: if (LOG.isDebugEnabled())
441: LOG.debug("setProfile(DecoratedProfile " + profile + ")");
442: this .profile = profile;
443: }
444:
445: /**
446: * @param searchKeyword
447: */
448: public void setSearchKeyword(String searchKeyword) {
449: if (LOG.isDebugEnabled())
450: LOG.debug("setSearchResults(String " + searchKeyword + ")");
451:
452: this .searchKeyword = searchKeyword;
453: }
454:
455: /**
456: * @param searchResults
457: */
458: public void setSearchResults(List searchResults) {
459: if (LOG.isDebugEnabled())
460: LOG.debug("setSearchResults(List " + searchResults + ")");
461: this .searchResults = searchResults;
462: }
463:
464: public class DecoratedProfile {
465: protected Profile inProfile;
466:
467: /**
468: * @param newProfile
469: */
470: public DecoratedProfile(Profile newProfile) {
471: if (LOG.isDebugEnabled())
472: LOG
473: .debug("DecoratedProfile(Profile" + newProfile
474: + ")");
475: inProfile = newProfile;
476: }
477:
478: /**
479: * @return
480: */
481: public Profile getProfile() {
482: LOG.debug("getProfile()");
483: return inProfile;
484: }
485:
486: /**
487: * @return
488: */
489: public String processActionDisplayProfile() {
490: LOG.debug("processActionDisplayProfile()");
491: try {
492: profile = this ;
493: redirectToSearchedProfile = true;
494: return "displaySearchedProfile";
495: } catch (Exception e) {
496: LOG.error(e.getMessage(), e);
497: return null;
498: }
499: }
500:
501: public boolean isDisplayCompleteProfile() {
502: LOG.debug("isDisplayCompleteProfile()");
503: return profileService.displayCompleteProfile(inProfile);
504: }
505:
506: public boolean isDisplayPictureURL() {
507: LOG.debug("isDisplayPictureURL()");
508: return profileService.isDisplayPictureURL(inProfile);
509: }
510:
511: public boolean isDisplayUniversityPhoto() {
512: LOG.debug("isDisplayUniversityPhoto()");
513: return profileService.isDisplayUniversityPhoto(inProfile);
514: }
515:
516: public boolean isDisplayUniversityPhotoUnavailable() {
517: LOG.debug("isDisplayUniversityPhotoUnavailable()");
518: return profileService
519: .isDisplayUniversityPhotoUnavailable(inProfile);
520: }
521:
522: public boolean isDisplayNoPicture() {
523: LOG.debug("isDisplayPhoto()");
524: return profileService.isDisplayNoPhoto(inProfile);
525: }
526:
527: }
528: }
|