File Source: Entries.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.struts2.editor;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.roller.weblogger.WebloggerException;
30 import org.apache.roller.weblogger.business.WebloggerFactory;
31 import org.apache.roller.weblogger.business.WeblogManager;
32 import org.apache.roller.weblogger.pojos.WeblogCategory;
33 import org.apache.roller.weblogger.pojos.WeblogPermission;
34 import org.apache.roller.weblogger.pojos.WeblogEntry;
35 import org.apache.roller.weblogger.ui.struts2.pagers.EntriesPager;
36 import org.apache.roller.weblogger.ui.struts2.util.KeyValueObject;
37 import org.apache.roller.weblogger.ui.struts2.util.UIAction;
38
39
40 /**
41 * A list view of entries in a weblog.
42 */
43 public class Entries extends UIAction {
44
/*
P/P * Method: org.apache.roller.weblogger.ui.struts2.editor.Entries__static_init
*
* Postconditions:
* init'ed(log)
*/
45 private static Log log = LogFactory.getLog(Entries.class);
46
47 // number of comments to show per page
48 private static final int COUNT = 30;
49
50 // bean for managing submitted data
51 private EntriesBean bean = new EntriesBean();
52
53 // pager for the entries we are viewing
54 private EntriesPager pager = null;
55
56 // first entry in the list
57 private WeblogEntry firstEntry = null;
58
59 // last entry in the list
60 private WeblogEntry lastEntry = null;
61
62
/*
P/P * Method: void org.apache.roller.weblogger.ui.struts2.editor.Entries()
*
* Postconditions:
* this.actionName == &"entries"
* this.bean == &new EntriesBean(Entries#1)
* this.desiredMenu == &"editor"
* this.firstEntry == null
* this.lastEntry == null
* this.pager == null
* this.bean.categoryPath == null
* this.bean.endDateString == null
* this.bean.startDateString == null
* this.bean.tagsAsString == null
* ...
*/
63 public Entries() {
64 this.actionName = "entries";
65 this.desiredMenu = "editor";
66 this.pageTitle = "weblogEntryQuery.title";
67 }
68
69
70 @Override
71 public short requiredWeblogPermissions() {
/*
P/P * Method: short requiredWeblogPermissions()
*
* Presumptions:
* init'ed(org.apache.roller.weblogger.pojos.WeblogPermission.AUTHOR)
*
* Postconditions:
* return_value == org.apache.roller.weblogger.pojos.WeblogPermission.AUTHOR
* (soft) init'ed(return_value)
*/
72 return WeblogPermission.AUTHOR;
73 }
74
75
76 public String execute() {
77
/*
P/P * Method: String execute()
*
* Preconditions:
* log != null
* this.bean != null
* init'ed(this.bean.categoryPath)
* init'ed(this.bean.endDateString)
* init'ed(this.bean.sortBy)
* init'ed(this.bean.startDateString)
* init'ed(this.bean.status)
* init'ed(this.bean.tagsAsString)
* init'ed(this.bean.text)
* (soft) this.bean.page in -71_582_788..143_165_576
*
* Presumptions:
* java.util.List:size(...)@108 >= -231+1
* java.util.List:size(...)@113 >= -231+1
* org.apache.roller.weblogger.business.WeblogManager:getWeblogEntries(...)@88 != null
* org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@87 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@87 != null
*
* Postconditions:
* return_value == &"list"
* possibly_updated(this.firstEntry)
* possibly_updated(this.lastEntry)
* this.pager == &new EntriesPager(execute#4)
* new EntriesPager(execute#4) num objects == 1
*
* Test Vectors:
* java.util.List:size(...)@104: {-231..0}, {1..232-1}
* java.util.List:size(...)@107: {-231..30}, {31..232-1}
* org.apache.commons.logging.Log:isDebugEnabled(...)@78: {0}, {1}
*/
78 if (log.isDebugEnabled()) {
79 log.debug("entries bean is ...\n"+getBean().toString());
80 }
81
82 List<WeblogEntry> entries = null;
83 boolean hasMore = false;
84 try {
85 String status = getBean().getStatus();
86
87 WeblogManager wmgr = WebloggerFactory.getWeblogger().getWeblogManager();
88 List<WeblogEntry> rawEntries = wmgr.getWeblogEntries(
89 getActionWeblog(),
90 null,
91 getBean().getStartDate(),
92 getBean().getEndDate(),
93 getBean().getCategoryPath(),
94 getBean().getTags(),
95 ("ALL".equals(status)) ? null : status,
96 getBean().getText(),
97 getBean().getSortBy(),
98 null,
99 null,
100 getBean().getPage() * COUNT,
101 COUNT + 1);
102 entries = new ArrayList<WeblogEntry>();
103 entries.addAll(rawEntries);
+ 104 if (entries != null && entries.size() > 0) {
105 log.debug("query found "+rawEntries.size()+" results");
106
107 if(rawEntries.size() > COUNT) {
108 entries.remove(entries.size()-1);
109 hasMore = true;
110 }
111
112 setFirstEntry((WeblogEntry)entries.get(0));
113 setLastEntry((WeblogEntry)entries.get(entries.size()-1));
114 }
115 } catch (WebloggerException ex) {
116 log.error("Error looking up entries", ex);
117 // TODO: i18n
118 addError("Error looking up entries");
119 }
120
121 // build entries pager
122 String baseUrl = buildBaseUrl();
123 setPager(new EntriesPager(baseUrl, getBean().getPage(), entries, hasMore));
124
125 return LIST;
126 }
127
128
129 // use the action data to build a url representing this action, including query data
130 private String buildBaseUrl() {
131
/*
P/P * Method: String buildBaseUrl()
*
* Preconditions:
* this.bean != null
* init'ed(this.bean.categoryPath)
* init'ed(this.bean.endDateString)
* init'ed(this.bean.sortBy)
* init'ed(this.bean.startDateString)
* init'ed(this.bean.status)
* init'ed(this.bean.tagsAsString)
* init'ed(this.bean.text)
*
* Presumptions:
* org.apache.roller.weblogger.business.Weblogger:getUrlStrategy(...)@156 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@156 != null
* org.apache.roller.weblogger.ui.struts2.editor.Entries:getActionWeblog(...)@156 != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* org.apache.commons.lang.StringUtils:isEmpty(...)@134: {1}, {0}
* org.apache.commons.lang.StringUtils:isEmpty(...)@137: {1}, {0}
* org.apache.commons.lang.StringUtils:isEmpty(...)@140: {1}, {0}
* org.apache.commons.lang.StringUtils:isEmpty(...)@143: {1}, {0}
* org.apache.commons.lang.StringUtils:isEmpty(...)@146: {1}, {0}
* org.apache.commons.lang.StringUtils:isEmpty(...)@149: {1}, {0}
* org.apache.commons.lang.StringUtils:isEmpty(...)@152: {1}, {0}
*/
132 Map<String, String> params = new HashMap();
133
134 if(!StringUtils.isEmpty(getBean().getCategoryPath())) {
135 params.put("bean.categoryPath", getBean().getCategoryPath());
136 }
137 if(!StringUtils.isEmpty(getBean().getTagsAsString())) {
138 params.put("bean.tagsAsString", getBean().getTagsAsString());
139 }
140 if(!StringUtils.isEmpty(getBean().getText())) {
141 params.put("bean.text", getBean().getText());
142 }
143 if(!StringUtils.isEmpty(getBean().getStartDateString())) {
144 params.put("bean.startDateString", getBean().getStartDateString());
145 }
146 if(!StringUtils.isEmpty(getBean().getEndDateString())) {
147 params.put("bean.endDateString", getBean().getEndDateString());
148 }
149 if(!StringUtils.isEmpty(getBean().getStatus())) {
150 params.put("bean.status", getBean().getStatus());
151 }
152 if(!StringUtils.isEmpty(getBean().getSortBy())) {
153 params.put("bean.sortBy", getBean().getSortBy());
154 }
155
156 return WebloggerFactory.getWeblogger().getUrlStrategy().getActionURL("entries", "/roller-ui/authoring",
157 getActionWeblog().getHandle(), params, false);
158 }
159
160
161 /**
162 * Get the list of all categories for the action weblog, not including root.
163 */
164 public List<WeblogCategory> getCategories() {
165 // make list of categories with first option being being a transient
166 // category just meant to represent the default option of any category
/*
P/P * Method: List getCategories()
*
* Preconditions:
* (soft) log != null
*
* Presumptions:
* init'ed(java.util.Collections.EMPTY_LIST)
* org.apache.roller.weblogger.business.Weblogger:getWeblogManager(...)@176 != null
* org.apache.roller.weblogger.business.WebloggerFactory:getWeblogger(...)@176 != null
*
* Postconditions:
* return_value == &new ArrayList(getCategories#1)
* new ArrayList(getCategories#1) num objects == 1
*/
167 List<WeblogCategory> cats = new ArrayList();
168
169 WeblogCategory tmpCat = new WeblogCategory();
170 tmpCat.setName("Any");
171 tmpCat.setPath("");
172 cats.add(tmpCat);
173
174 List<WeblogCategory> weblogCats = Collections.EMPTY_LIST;
175 try {
176 WeblogManager wmgr = WebloggerFactory.getWeblogger().getWeblogManager();
177 weblogCats = wmgr.getWeblogCategories(getActionWeblog(), false);
178 } catch (WebloggerException ex) {
179 log.error("Error getting category list for weblog - "+getWeblog(), ex);
180 }
181
182 cats.addAll(weblogCats);
183
184 return cats;
185 }
186
187
188 public List<KeyValueObject> getSortByOptions() {
/*
P/P * Method: List getSortByOptions()
*
* Postconditions:
* return_value == &new ArrayList(getSortByOptions#1)
* new ArrayList(getSortByOptions#1) num objects == 1
*/
189 List<KeyValueObject> opts = new ArrayList();
190
191 opts.add(new KeyValueObject("pubTime", getText("weblogEntryQuery.label.pubTime")));
192 opts.add(new KeyValueObject("updateTime", getText("weblogEntryQuery.label.updateTime")));
193
194 return opts;
195 }
196
197 public List<KeyValueObject> getStatusOptions() {
/*
P/P * Method: List getStatusOptions()
*
* Postconditions:
* return_value == &new ArrayList(getStatusOptions#1)
* new ArrayList(getStatusOptions#1) num objects == 1
*/
198 List<KeyValueObject> opts = new ArrayList();
199
200 opts.add(new KeyValueObject("ALL", getText("weblogEntryQuery.label.allEntries")));
201 opts.add(new KeyValueObject("DRAFT", getText("weblogEntryQuery.label.draftOnly")));
202 opts.add(new KeyValueObject("PUBLISHED", getText("weblogEntryQuery.label.publishedOnly")));
203 opts.add(new KeyValueObject("PENDING", getText("weblogEntryQuery.label.pendingOnly")));
204 opts.add(new KeyValueObject("SCHEDULED", getText("weblogEntryQuery.label.scheduledOnly")));
205
206 return opts;
207 }
208
209
210 public EntriesBean getBean() {
/*
P/P * Method: EntriesBean getBean()
*
* Preconditions:
* init'ed(this.bean)
*
* Postconditions:
* return_value == this.bean
* init'ed(return_value)
*/
211 return bean;
212 }
213
214 public void setBean(EntriesBean bean) {
/*
P/P * Method: void setBean(EntriesBean)
*
* Postconditions:
* this.bean == bean
* init'ed(this.bean)
*/
215 this.bean = bean;
216 }
217
218 public WeblogEntry getFirstEntry() {
/*
P/P * Method: WeblogEntry getFirstEntry()
*
* Preconditions:
* init'ed(this.firstEntry)
*
* Postconditions:
* return_value == this.firstEntry
* init'ed(return_value)
*/
219 return firstEntry;
220 }
221
222 public void setFirstEntry(WeblogEntry firstEntry) {
/*
P/P * Method: void setFirstEntry(WeblogEntry)
*
* Postconditions:
* this.firstEntry == firstEntry
* init'ed(this.firstEntry)
*/
223 this.firstEntry = firstEntry;
224 }
225
226 public WeblogEntry getLastEntry() {
/*
P/P * Method: WeblogEntry getLastEntry()
*
* Preconditions:
* init'ed(this.lastEntry)
*
* Postconditions:
* return_value == this.lastEntry
* init'ed(return_value)
*/
227 return lastEntry;
228 }
229
230 public void setLastEntry(WeblogEntry lastEntry) {
/*
P/P * Method: void setLastEntry(WeblogEntry)
*
* Postconditions:
* this.lastEntry == lastEntry
* init'ed(this.lastEntry)
*/
231 this.lastEntry = lastEntry;
232 }
233
234 public EntriesPager getPager() {
/*
P/P * Method: EntriesPager getPager()
*
* Preconditions:
* init'ed(this.pager)
*
* Postconditions:
* return_value == this.pager
* init'ed(return_value)
*/
235 return pager;
236 }
237
238 public void setPager(EntriesPager pager) {
/*
P/P * Method: void setPager(EntriesPager)
*
* Postconditions:
* this.pager == pager
* init'ed(this.pager)
*/
239 this.pager = pager;
240 }
241
242 }
SofCheck Inspector Build Version : 2.18479
| Entries.java |
2009-Jan-02 14:25:18 |
| Entries.class |
2009-Sep-04 03:12:45 |