File Source: PlanetModel.java
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.weblogger.ui.rendering.model;
20
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Map;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.roller.weblogger.WebloggerException;
28 import org.apache.roller.planet.business.PlanetFactory;
29 import org.apache.roller.planet.business.PlanetManager;
30 import org.apache.roller.planet.pojos.Planet;
31 import org.apache.roller.planet.pojos.PlanetGroup;
32 import org.apache.roller.planet.pojos.Subscription;
33 import org.apache.roller.weblogger.business.URLStrategy;
34 import org.apache.roller.weblogger.business.WebloggerFactory;
35 import org.apache.roller.weblogger.pojos.ThemeTemplate;
36 import org.apache.roller.weblogger.pojos.Weblog;
37 import org.apache.roller.weblogger.ui.rendering.pagers.Pager;
38 import org.apache.roller.weblogger.ui.rendering.pagers.PlanetEntriesPager;
39 import org.apache.roller.weblogger.ui.rendering.util.WeblogPageRequest;
40 import org.apache.roller.weblogger.ui.rendering.util.WeblogRequest;
41
42 /**
43 * Model that provides access to planet aggregations, feeds and subscriptions.
44 */
/*
P/P * Method: void org.apache.roller.weblogger.ui.rendering.model.PlanetModel()
*
* Postconditions:
* this.pageLink == null
* this.urlStrategy == null
* this.weblog == null
* this.weblogRequest == null
* this.pageNum == 0
*/
45 public class PlanetModel implements Model {
46
47 public static final String DEFAULT_PLANET_HANDLE = "default";
48
/*
P/P * Method: org.apache.roller.weblogger.ui.rendering.model.PlanetModel__static_init
*
* Postconditions:
* init'ed(log)
*/
49 private static Log log = LogFactory.getLog(PlanetModel.class);
50
51 private WeblogRequest weblogRequest = null;
52 private URLStrategy urlStrategy = null;
53 private String pageLink = null;
54 private int pageNum = 0;
55 private Weblog weblog = null;
56
57
58 public String getModelName() {
/*
P/P * Method: String getModelName()
*
* Postconditions:
* return_value == &"planet"
*/
59 return "planet";
60 }
61
62 public void init(Map initData) throws WebloggerException {
63
64 // we expect the init data to contain a weblogRequest object
/*
P/P * Method: void init(Map)
*
* Preconditions:
* initData != null
*
* Presumptions:
* java.util.Map:get(...)@65 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@79 != null
*
* Postconditions:
* possibly_updated(this.pageLink)
* possibly_updated(this.pageNum)
* init'ed(this.urlStrategy)
* init'ed(this.weblog)
* (soft) this.weblogRequest != null
*
* Test Vectors:
* java.util.Map:get(...)@77: Inverse{null}, Addr_Set{null}
* org.apache.roller.weblogger.ui.rendering.util.WeblogPageRequest:instanceof(...)@70: {0}, {1}
*/
65 this.weblogRequest = (WeblogRequest) initData.get("parsedRequest");
66 if(this.weblogRequest == null) {
67 throw new WebloggerException("expected weblogRequest from init data");
68 }
69
70 if (weblogRequest instanceof WeblogPageRequest) {
71 ThemeTemplate weblogPage = ((WeblogPageRequest)weblogRequest).getWeblogPage();
72 pageLink = (weblogPage != null) ? weblogPage.getLink() : null;
73 pageNum = ((WeblogPageRequest)weblogRequest).getPageNum();
74 }
75
76 // look for url strategy
77 urlStrategy = (URLStrategy) initData.get("urlStrategy");
78 if(urlStrategy == null) {
79 urlStrategy = WebloggerFactory.getWeblogger().getUrlStrategy();
80 }
81
82 // extract weblog object
83 weblog = weblogRequest.getWeblog();
84 }
85
86
87 /**
88 * Get pager for PlanetEntry objects from 'all' and
89 * 'exernal' Planet groups. in reverse chrono order.
90 * @param offset Offset into results (for paging)
91 * @param len Max number of results to return
92 */
93 public Pager getAggregationPager(int sinceDays, int length) {
94
/*
P/P * Method: Pager getAggregationPager(int, int)
*
* Preconditions:
* init'ed(this.pageLink)
* init'ed(this.pageNum)
* this.urlStrategy != null
* init'ed(this.weblog)
* this.weblogRequest != null
* (soft) length <= 232-2
* (soft) org/apache/roller/weblogger/ui/rendering/pagers/PlanetEntriesPager.log != null
* (soft) sinceDays <= 231
*
* Postconditions:
* return_value == &new PlanetEntriesPager(getAggregationPager#1)
* new ArrayList(getItems#2) num objects == 1
* new PlanetEntriesPager(getAggregationPager#1) num objects == 1
* return_value.entries == &new ArrayList(getItems#2)
* return_value.feedURL == null
* return_value.groupHandle == null
* return_value.length == length
* (soft) return_value.length <= 232-2
* init'ed(return_value.locale)
* init'ed(return_value.more)
* ...
*/
95 String pagerUrl = urlStrategy.getWeblogPageURL(weblog,
96 weblogRequest.getLocale(), pageLink,
97 null, null, null, null, 0, false);
98
99 return new PlanetEntriesPager(
100 urlStrategy,
101 null,
102 null,
103 pagerUrl,
104 weblogRequest.getLocale(),
105 sinceDays,
106 pageNum,
107 length);
108 }
109
110
111 /**
112 * Get pager for WeblogEntry objects from specified
113 * Planet groups in reverse chrono order.
114 * @param offset Offset into results (for paging)
115 * @param len Max number of results to return
116 */
117 public Pager getAggregationPager(String groupHandle, int sinceDays, int length) {
118
/*
P/P * Method: Pager getAggregationPager(String, int, int)
*
* Preconditions:
* init'ed(this.pageLink)
* init'ed(this.pageNum)
* this.urlStrategy != null
* init'ed(this.weblog)
* this.weblogRequest != null
* (soft) length <= 232-2
* (soft) org/apache/roller/weblogger/ui/rendering/pagers/PlanetEntriesPager.log != null
* (soft) sinceDays <= 231
*
* Postconditions:
* return_value == &new PlanetEntriesPager(getAggregationPager#1)
* new ArrayList(getItems#2) num objects == 1
* new PlanetEntriesPager(getAggregationPager#1) num objects == 1
* return_value.entries == &new ArrayList(getItems#2)
* return_value.feedURL == null
* return_value.groupHandle == groupHandle
* init'ed(return_value.groupHandle)
* return_value.length == length
* (soft) return_value.length <= 232-2
* init'ed(return_value.locale)
* ...
*/
119 String pagerUrl = urlStrategy.getWeblogPageURL(weblog,
120 weblogRequest.getLocale(), pageLink,
121 null, null, null, null, 0, false);
122
123 return new PlanetEntriesPager(
124 urlStrategy,
125 null,
126 groupHandle,
127 pagerUrl,
128 weblogRequest.getLocale(),
129 sinceDays,
130 pageNum,
131 length);
132 }
133
134
135 /**
136 * Get pager for WeblogEntry objects from specified
137 * Planet feed in reverse chrono order.
138 * @param offset Offset into results (for paging)
139 * @param len Max number of results to return
140 */
141 public Pager getFeedPager(String feedURL, int length) {
142
/*
P/P * Method: Pager getFeedPager(String, int)
*
* Preconditions:
* init'ed(this.pageLink)
* init'ed(this.pageNum)
* this.urlStrategy != null
* init'ed(this.weblog)
* this.weblogRequest != null
* (soft) length <= 232-2
* (soft) org/apache/roller/weblogger/ui/rendering/pagers/PlanetEntriesPager.log != null
*
* Postconditions:
* return_value == &new PlanetEntriesPager(getFeedPager#1)
* new ArrayList(getItems#2) num objects == 1
* new PlanetEntriesPager(getFeedPager#1) num objects == 1
* return_value.entries == &new ArrayList(getItems#2)
* return_value.feedURL == feedURL
* init'ed(return_value.feedURL)
* return_value.groupHandle == null
* return_value.length == length
* (soft) return_value.length <= 232-2
* init'ed(return_value.locale)
* ...
*/
143 String pagerUrl = urlStrategy.getWeblogPageURL(weblog,
144 weblogRequest.getLocale(), pageLink,
145 null, null, null, null, 0, false);
146
147 return new PlanetEntriesPager(
148 urlStrategy,
149 feedURL,
150 null,
151 pagerUrl,
152 weblogRequest.getLocale(),
153 -1,
154 pageNum,
155 length);
156 }
157
158
159 /**
160 * Get PlanetSubscription objects in descending order by Planet ranking.
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 getRankedSubscriptions(int sinceDays, int length) {
/*
P/P * Method: List getRankedSubscriptions(int, int)
*
* Preconditions:
* (soft) log != null
*
* Postconditions:
* return_value == &new ArrayList(getRankedSubscriptions#1*)
* new ArrayList(getRankedSubscriptions#1*) num objects == 1
*/
166 return getRankedSubscriptions(null, sinceDays, length);
167 }
168
169
170 /**
171 * Get PlanetSubscription objects in descending order by Planet ranking.
172 * @param groupHandle Only consider weblogs updated in the last sinceDays
173 * @param sinceDays Only consider weblogs updated in the last sinceDays
174 * @param offset Offset into results (for paging)
175 * @param len Max number of results to return
176 */
177 public List getRankedSubscriptions(String groupHandle, int sinceDays, int length) {
/*
P/P * Method: List getRankedSubscriptions(String, int, int)
*
* Preconditions:
* (soft) log != null
*
* Presumptions:
* org.apache.roller.planet.business.Planet:getPlanetManager(...)@180 != null
* org.apache.roller.planet.business.PlanetFactory:getPlanet(...)@180 != null
* org.apache.roller.planet.business.PlanetManager:getTopSubscriptions(...)@183 != null
*
* Postconditions:
* return_value == &new ArrayList(getRankedSubscriptions#1)
* new ArrayList(getRankedSubscriptions#1) num objects == 1
*/
178 List list = new ArrayList();
179 try {
180 PlanetManager planetManager = PlanetFactory.getPlanet().getPlanetManager();
181 Planet defaultPlanet = planetManager.getPlanet(DEFAULT_PLANET_HANDLE);
182 PlanetGroup planetGroup = planetManager.getGroup(defaultPlanet, groupHandle);
183 List subs = planetManager.getTopSubscriptions(planetGroup, 0, length);
184 for (Iterator it = subs.iterator(); it.hasNext();) {
185 Subscription sub = (Subscription) it.next();
186 // TODO needs pojo wrapping from planet
187 list.add(sub);
188 }
189 } catch (Exception e) {
190 log.error("ERROR: get ranked blogs", e);
191 }
192 return list;
193 }
194
195 }
SofCheck Inspector Build Version : 2.18479
| PlanetModel.java |
2009-Jan-02 14:25:40 |
| PlanetModel.class |
2009-Sep-04 03:12:44 |