KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > rendering > model > PlanetModel


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.ui.rendering.model;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.roller.RollerException;
28 import org.apache.roller.model.PlanetManager;
29 import org.apache.roller.model.Roller;
30 import org.apache.roller.model.RollerFactory;
31 import org.apache.roller.pojos.PlanetSubscriptionData;
32 import org.apache.roller.pojos.Template;
33 import org.apache.roller.pojos.WebsiteData;
34 import org.apache.roller.pojos.wrapper.PlanetSubscriptionDataWrapper;
35 import org.apache.roller.ui.rendering.pagers.Pager;
36 import org.apache.roller.ui.rendering.pagers.PlanetEntriesPager;
37 import org.apache.roller.ui.rendering.util.WeblogPageRequest;
38 import org.apache.roller.ui.rendering.util.WeblogRequest;
39 import org.apache.roller.util.URLUtilities;
40
41
42 /**
43  * Model that provides access to planet aggregations, feeds and subscriptions.
44  */

45 public class PlanetModel implements Model {
46     
47     private static Log log = LogFactory.getLog(PlanetModel.class);
48     
49     private WeblogRequest weblogRequest = null;
50     private String JavaDoc pageLink = null;
51     private int pageNum = 0;
52     private WebsiteData weblog = null;
53     
54     
55     public String JavaDoc getModelName() {
56         return "planet";
57     }
58     
59     public void init(Map JavaDoc initData) throws RollerException {
60         
61         // we expect the init data to contain a weblogRequest object
62
this.weblogRequest = (WeblogRequest) initData.get("weblogRequest");
63         if(this.weblogRequest == null) {
64             throw new RollerException("expected weblogRequest from init data");
65         }
66         
67         if (weblogRequest instanceof WeblogPageRequest) {
68             Template weblogPage = ((WeblogPageRequest)weblogRequest).getWeblogPage();
69             pageLink = (weblogPage != null) ? weblogPage.getLink() : null;
70             pageNum = ((WeblogPageRequest)weblogRequest).getPageNum();
71         }
72         
73         // extract weblog object
74
weblog = weblogRequest.getWeblog();
75     }
76     
77     
78     /**
79      * Get pager for PlanetEntry objects from 'all' and
80      * 'exernal' Planet groups. in reverse chrono order.
81      * @param offset Offset into results (for paging)
82      * @param len Max number of results to return
83      */

84     public Pager getAggregationPager(int sinceDays, int length) {
85         
86         String JavaDoc pagerUrl = URLUtilities.getWeblogPageURL(weblog,
87                 weblogRequest.getLocale(), pageLink,
88                 null, null, null, 0, false);
89         
90         return new PlanetEntriesPager(
91             null,
92             null,
93             pagerUrl,
94             weblogRequest.getLocale(),
95             sinceDays,
96             pageNum,
97             length);
98     }
99     
100     
101     /**
102      * Get pager for WeblogEntry objects from specified
103      * Planet groups in reverse chrono order.
104      * @param offset Offset into results (for paging)
105      * @param len Max number of results to return
106      */

107     public Pager getAggregationPager(String JavaDoc groupHandle, int sinceDays, int length) {
108         
109         String JavaDoc pagerUrl = URLUtilities.getWeblogPageURL(weblog,
110                 weblogRequest.getLocale(), pageLink,
111                 null, null, null, 0, false);
112         
113         return new PlanetEntriesPager(
114             null,
115             groupHandle,
116             pagerUrl,
117             weblogRequest.getLocale(),
118             sinceDays,
119             pageNum,
120             length);
121     }
122     
123     
124     /**
125      * Get pager for WeblogEntry objects from specified
126      * Planet feed in reverse chrono order.
127      * @param offset Offset into results (for paging)
128      * @param len Max number of results to return
129      */

130     public Pager getFeedPager(String JavaDoc feedURL, int length) {
131         
132         String JavaDoc pagerUrl = URLUtilities.getWeblogPageURL(weblog,
133                 weblogRequest.getLocale(), pageLink,
134                 null, null, null, 0, false);
135         
136         return new PlanetEntriesPager(
137             feedURL,
138             null,
139             pagerUrl,
140             weblogRequest.getLocale(),
141             -1,
142             pageNum,
143             length);
144     }
145     
146     
147     /**
148      * Get PlanetSubscription objects in descending order by Planet ranking.
149      * @param sinceDays Only consider weblogs updated in the last sinceDays
150      * @param offset Offset into results (for paging)
151      * @param len Max number of results to return
152      */

153     public List JavaDoc getRankedSubscriptions(int sinceDays, int length) {
154         return getRankedSubscriptions(null, sinceDays, length);
155     }
156     
157     
158     /**
159      * Get PlanetSubscription objects in descending order by Planet ranking.
160      * @param groupHandle Only consider weblogs updated in the last sinceDays
161      * @param sinceDays Only consider weblogs updated in the last sinceDays
162      * @param offset Offset into results (for paging)
163      * @param len Max number of results to return
164      */

165     public List JavaDoc getRankedSubscriptions(String JavaDoc groupHandle, int sinceDays, int length) {
166         List JavaDoc list = new ArrayList JavaDoc();
167         try {
168             Roller roller = RollerFactory.getRoller();
169             PlanetManager planetManager = roller.getPlanetManager();
170             List JavaDoc subs = planetManager.getTopSubscriptions(groupHandle, 0, length);
171             for (Iterator JavaDoc it = subs.iterator(); it.hasNext();) {
172                 PlanetSubscriptionData sub = (PlanetSubscriptionData) it.next();
173                 list.add(PlanetSubscriptionDataWrapper.wrap(sub));
174             }
175         } catch (Exception JavaDoc e) {
176             log.error("ERROR: get ranked blogs", e);
177         }
178         return list;
179     }
180     
181 }
182
Popular Tags