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.model;
020:
021: import java.util.ArrayList;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.apache.roller.RollerException;
028: import org.apache.roller.planet.business.PlanetManager;
029: import org.apache.roller.business.Roller;
030: import org.apache.roller.business.RollerFactory;
031: import org.apache.roller.planet.pojos.PlanetSubscriptionData;
032: import org.apache.roller.pojos.Template;
033: import org.apache.roller.pojos.WebsiteData;
034: import org.apache.roller.planet.pojos.wrapper.PlanetSubscriptionDataWrapper;
035: import org.apache.roller.ui.rendering.pagers.Pager;
036: import org.apache.roller.ui.rendering.pagers.PlanetEntriesPager;
037: import org.apache.roller.ui.rendering.util.WeblogPageRequest;
038: import org.apache.roller.ui.rendering.util.WeblogRequest;
039: import org.apache.roller.util.URLUtilities;
040:
041: /**
042: * Model that provides access to planet aggregations, feeds and subscriptions.
043: */
044: public class PlanetModel implements Model {
045:
046: private static Log log = LogFactory.getLog(PlanetModel.class);
047:
048: private WeblogRequest weblogRequest = null;
049: private String pageLink = null;
050: private int pageNum = 0;
051: private WebsiteData weblog = null;
052:
053: public String getModelName() {
054: return "planet";
055: }
056:
057: public void init(Map initData) throws RollerException {
058:
059: // we expect the init data to contain a weblogRequest object
060: this .weblogRequest = (WeblogRequest) initData
061: .get("weblogRequest");
062: if (this .weblogRequest == null) {
063: throw new RollerException(
064: "expected weblogRequest from init data");
065: }
066:
067: if (weblogRequest instanceof WeblogPageRequest) {
068: Template weblogPage = ((WeblogPageRequest) weblogRequest)
069: .getWeblogPage();
070: pageLink = (weblogPage != null) ? weblogPage.getLink()
071: : null;
072: pageNum = ((WeblogPageRequest) weblogRequest).getPageNum();
073: }
074:
075: // extract weblog object
076: weblog = weblogRequest.getWeblog();
077: }
078:
079: /**
080: * Get pager for PlanetEntry objects from 'all' and
081: * 'exernal' Planet groups. in reverse chrono order.
082: * @param offset Offset into results (for paging)
083: * @param len Max number of results to return
084: */
085: public Pager getAggregationPager(int sinceDays, int length) {
086:
087: String pagerUrl = URLUtilities.getWeblogPageURL(weblog,
088: weblogRequest.getLocale(), pageLink, null, null, null,
089: null, 0, false);
090:
091: return new PlanetEntriesPager(null, null, pagerUrl,
092: weblogRequest.getLocale(), sinceDays, pageNum, length);
093: }
094:
095: /**
096: * Get pager for WeblogEntry objects from specified
097: * Planet groups in reverse chrono order.
098: * @param offset Offset into results (for paging)
099: * @param len Max number of results to return
100: */
101: public Pager getAggregationPager(String groupHandle, int sinceDays,
102: int length) {
103:
104: String pagerUrl = URLUtilities.getWeblogPageURL(weblog,
105: weblogRequest.getLocale(), pageLink, null, null, null,
106: null, 0, false);
107:
108: return new PlanetEntriesPager(null, groupHandle, pagerUrl,
109: weblogRequest.getLocale(), sinceDays, pageNum, length);
110: }
111:
112: /**
113: * Get pager for WeblogEntry objects from specified
114: * Planet feed in reverse chrono order.
115: * @param offset Offset into results (for paging)
116: * @param len Max number of results to return
117: */
118: public Pager getFeedPager(String feedURL, int length) {
119:
120: String pagerUrl = URLUtilities.getWeblogPageURL(weblog,
121: weblogRequest.getLocale(), pageLink, null, null, null,
122: null, 0, false);
123:
124: return new PlanetEntriesPager(feedURL, null, pagerUrl,
125: weblogRequest.getLocale(), -1, pageNum, length);
126: }
127:
128: /**
129: * Get PlanetSubscription objects in descending order by Planet ranking.
130: * @param sinceDays Only consider weblogs updated in the last sinceDays
131: * @param offset Offset into results (for paging)
132: * @param len Max number of results to return
133: */
134: public List getRankedSubscriptions(int sinceDays, int length) {
135: return getRankedSubscriptions(null, sinceDays, length);
136: }
137:
138: /**
139: * Get PlanetSubscription objects in descending order by Planet ranking.
140: * @param groupHandle Only consider weblogs updated in the last sinceDays
141: * @param sinceDays Only consider weblogs updated in the last sinceDays
142: * @param offset Offset into results (for paging)
143: * @param len Max number of results to return
144: */
145: public List getRankedSubscriptions(String groupHandle,
146: int sinceDays, int length) {
147: List list = new ArrayList();
148: try {
149: Roller roller = RollerFactory.getRoller();
150: PlanetManager planetManager = roller.getPlanetManager();
151: List subs = planetManager.getTopSubscriptions(groupHandle,
152: 0, length);
153: for (Iterator it = subs.iterator(); it.hasNext();) {
154: PlanetSubscriptionData sub = (PlanetSubscriptionData) it
155: .next();
156: list.add(PlanetSubscriptionDataWrapper.wrap(sub));
157: }
158: } catch (Exception e) {
159: log.error("ERROR: get ranked blogs", e);
160: }
161: return list;
162: }
163:
164: }
|