001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.ui.rendering.pagers;
020:
021: import java.util.ArrayList;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.roller.business.Roller;
029: import org.apache.roller.business.RollerFactory;
030: import org.apache.roller.business.UserManager;
031: import org.apache.roller.pojos.UserData;
032: import org.apache.roller.pojos.wrapper.UserDataWrapper;
033:
034: /**
035: * Paging through a collection of users.
036: */
037: public class UsersPager extends AbstractPager {
038:
039: private static Log log = LogFactory.getLog(UsersPager.class);
040:
041: private String letter = null;
042: private String locale = null;
043: private int sinceDays = -1;
044: private int length = 0;
045:
046: // collection for the pager
047: private List users;
048:
049: // are there more items?
050: private boolean more = false;
051:
052: public UsersPager(String baseUrl, String locale, int sinceDays,
053: int page, int length) {
054:
055: super (baseUrl, page);
056:
057: this .locale = locale;
058: this .sinceDays = sinceDays;
059: this .length = length;
060:
061: // initialize the collection
062: getItems();
063: }
064:
065: public UsersPager(String baseUrl, String letter, String locale,
066: int sinceDays, int page, int length) {
067:
068: super (baseUrl, page);
069:
070: this .letter = letter;
071: this .locale = locale;
072: this .sinceDays = sinceDays;
073: this .length = length;
074:
075: // initialize the collection
076: getItems();
077: }
078:
079: public String getNextLink() {
080: // need to add letter param if it exists
081: if (letter != null) {
082: int page = getPage() + 1;
083: if (hasMoreItems()) {
084: Map params = new HashMap();
085: params.put("page", "" + page);
086: params.put("letter", letter);
087: return createURL(getUrl(), params);
088: }
089: return null;
090: } else {
091: return super .getNextLink();
092: }
093: }
094:
095: public String getPrevLink() {
096: // need to add letter param if it exists
097: if (letter != null) {
098: int page = getPage() - 1;
099: if (page >= 0) {
100: Map params = new HashMap();
101: params.put("page", "" + page);
102: params.put("letter", letter);
103: return createURL(getUrl(), params);
104: }
105: return null;
106: } else {
107: return super .getPrevLink();
108: }
109: }
110:
111: public List getItems() {
112:
113: if (users == null) {
114: // calculate offset
115: int offset = getPage() * length;
116:
117: List results = new ArrayList();
118: try {
119: Roller roller = RollerFactory.getRoller();
120: UserManager umgr = roller.getUserManager();
121: List rawUsers = null;
122: if (letter == null) {
123: rawUsers = umgr.getUsers(offset, length + 1);
124: } else {
125: rawUsers = umgr.getUsersByLetter(letter.charAt(0),
126: offset, length + 1);
127: }
128:
129: // check if there are more results for paging
130: if (rawUsers.size() > length) {
131: more = true;
132: rawUsers.remove(rawUsers.size() - 1);
133: }
134:
135: // wrap the results
136: for (Iterator it = rawUsers.iterator(); it.hasNext();) {
137: UserData user = (UserData) it.next();
138: results.add(UserDataWrapper.wrap(user));
139: }
140:
141: } catch (Exception e) {
142: log.error("ERROR: fetching user list", e);
143: }
144:
145: users = results;
146: }
147:
148: return users;
149: }
150:
151: public boolean hasMoreItems() {
152: return more;
153: }
154:
155: }
|