File Source: PlanetSubscriptions.java
1 /*
2 * Copyright 2005 Sun Microsystems, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not 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.
15 */
16
17 package org.apache.roller.weblogger.planet.ui;
18
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Set;
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.roller.RollerException;
27 import org.apache.roller.planet.business.PlanetFactory;
28 import org.apache.roller.planet.business.PlanetManager;
29 import org.apache.roller.planet.business.fetcher.FeedFetcher;
30 import org.apache.roller.planet.pojos.PlanetGroup;
31 import org.apache.roller.planet.pojos.Subscription;
32 import org.apache.roller.weblogger.config.WebloggerRuntimeConfig;
33
34
35 /**
36 * Manage planet group subscriptions, default group is "all".
37 */
38 public class PlanetSubscriptions extends PlanetUIAction {
39
/*
P/P * Method: org.apache.roller.weblogger.planet.ui.PlanetSubscriptions__static_init
*
* Postconditions:
* init'ed(log)
*/
40 private static final Log log = LogFactory.getLog(PlanetSubscriptions.class);
41
42 // id of the group we are working in
43 private String groupHandle = null;
44
45 // the planet group we are working in
46 private PlanetGroup group = null;
47
48 // the subscription to deal with
49 private String subUrl = null;
50
51
/*
P/P * Method: void org.apache.roller.weblogger.planet.ui.PlanetSubscriptions()
*
* Postconditions:
* this.actionName == &"planetSubscriptions"
* this.desiredMenu == &"admin"
* this.group == null
* this.groupHandle == null
* this.planet == null
* this.subUrl == null
* this.pageTitle == &"planetSubscriptions.title"
*/
52 public PlanetSubscriptions() {
53 this.actionName = "planetSubscriptions";
54 this.desiredMenu = "admin";
55 this.pageTitle = "planetSubscriptions.title";
56 }
57
58
59 @Override
60 public String requiredUserRole() {
/*
P/P * Method: String requiredUserRole()
*
* Postconditions:
* return_value == &"admin"
*/
61 return "admin";
62 }
63
64 @Override
65 public boolean isWeblogRequired() {
/*
P/P * Method: bool isWeblogRequired()
*
* Postconditions:
* return_value == 0
*/
66 return false;
67 }
68
69
70 @Override
71 public void myPrepare() {
72
/*
P/P * Method: void myPrepare()
*
* Preconditions:
* init'ed(this.groupHandle)
* (soft) init'ed(this.planet)
* (soft) org/apache/roller/weblogger/planet/ui/PlanetUIAction.log != null
*
* Presumptions:
* org.apache.commons.logging.LogFactory:getLog(...)@40 != null
* org.apache.roller.planet.business.Planet:getPlanetManager(...)@73 != null
* org.apache.roller.planet.business.PlanetFactory:getPlanet(...)@73 != null
*
* Postconditions:
* possibly_updated(this.group)
* this.groupHandle == One-of{old this.groupHandle, &"all"}
* this.groupHandle != null
* init'ed(this.planet)
*
* Test Vectors:
* this.groupHandle: Inverse{null}, Addr_Set{null}
*/
73 PlanetManager pmgr = PlanetFactory.getPlanet().getPlanetManager();
74
75 // lookup group we are operating on, if none specified then use default
76 if (getGroupHandle() == null) {
77 setGroupHandle("all");
78 }
79
80 try {
81 setGroup(pmgr.getGroup(getPlanet(), getGroupHandle()));
82 } catch (RollerException ex) {
83 log.error("Error looking up planet group - "+getGroupHandle(), ex);
84 }
85 }
86
87
88 /**
89 * Populate page model and forward to subscription page
90 */
91 public String execute() {
/*
P/P * Method: String execute()
*
* Postconditions:
* return_value == &"list"
*/
92 return LIST;
93 }
94
95
96 /**
97 * Save subscription, add to current group
98 */
99 public String save() {
100
/*
P/P * Method: String save()
*
* Preconditions:
* init'ed(this.subUrl)
* (soft) this.group != null
*
* Presumptions:
* org.apache.commons.logging.LogFactory:getLog(...)@40 != null
* org.apache.roller.planet.business.Planet:getFeedFetcher(...)@112 != null
* org.apache.roller.planet.business.Planet:getPlanetManager(...)@104 != null
* org.apache.roller.planet.business.PlanetFactory:getPlanet(...)@104 != null
* org.apache.roller.planet.business.PlanetFactory:getPlanet(...)@112 != null
* ...
*
* Postconditions:
* return_value == &"list"
* this.subUrl == One-of{old this.subUrl, null}
* init'ed(this.subUrl)
*
* Test Vectors:
* org.apache.roller.planet.business.PlanetManager:getSubscription(...)@107: Inverse{null}, Addr_Set{null}
* org.apache.roller.weblogger.planet.ui.PlanetSubscriptions:hasActionErrors(...)@103: {1}, {0}
*/
101 myValidate();
102
103 if(!hasActionErrors()) try {
104 PlanetManager pmgr = PlanetFactory.getPlanet().getPlanetManager();
105
106 // check if this subscription already exists before adding it
107 Subscription sub = pmgr.getSubscription(getSubUrl());
108 if(sub == null) {
109 log.debug("Adding New Subscription - "+getSubUrl());
110
111 // sub doesn't exist yet, so we need to fetch it
112 FeedFetcher fetcher = PlanetFactory.getPlanet().getFeedFetcher();
113 sub = fetcher.fetchSubscription(getSubUrl());
114
115 // save new sub
116 pmgr.saveSubscription(sub);
117 } else {
118 log.debug("Adding Existing Subscription - "+getSubUrl());
119
120 // Subscription already exists
121 addMessage("planetSubscription.foundExisting", sub.getTitle());
122 }
123
124 // add the sub to the group
125 group.getSubscriptions().add(sub);
126 sub.getGroups().add(group);
127 pmgr.saveGroup(group);
128
129 // flush changes
130 PlanetFactory.getPlanet().flush();
131
132 // clear field after success
133 setSubUrl(null);
134
135 addMessage("planetSubscription.success.saved");
136
137 } catch (RollerException ex) {
138 log.error("Unexpected error saving subscription", ex);
139 addError("planetSubscriptions.error.duringSave", ex.getRootCauseMessage());
140 }
141
142 return LIST;
143 }
144
145
146 /**
147 * Delete subscription, reset form
148 */
149 public String delete() {
150
/*
P/P * Method: String delete()
*
* Preconditions:
* init'ed(this.subUrl)
* (soft) this.group != null
*
* Presumptions:
* org.apache.commons.logging.LogFactory:getLog(...)@40 != null
* org.apache.roller.planet.business.Planet:getPlanetManager(...)@153 != null
* org.apache.roller.planet.business.PlanetFactory:getPlanet(...)@153 != null
* org.apache.roller.planet.business.PlanetFactory:getPlanet(...)@160 != null
* org.apache.roller.planet.business.PlanetManager:getSubscription(...)@156 != null
* ...
*
* Postconditions:
* return_value == &"list"
* this.subUrl == One-of{old this.subUrl, null}
* init'ed(this.subUrl)
*
* Test Vectors:
* this.subUrl: Addr_Set{null}, Inverse{null}
*/
151 if(getSubUrl() != null) try {
152
153 PlanetManager pmgr = PlanetFactory.getPlanet().getPlanetManager();
154
155 // remove subscription
156 Subscription sub = pmgr.getSubscription(getSubUrl());
157 getGroup().getSubscriptions().remove(sub);
158 sub.getGroups().remove(getGroup());
159 pmgr.saveGroup(getGroup());
160 PlanetFactory.getPlanet().flush();
161
162 // clear field after success
163 setSubUrl(null);
164
165 addMessage("planetSubscription.success.deleted");
166
167 } catch (RollerException ex) {
168 log.error("Error removing planet subscription", ex);
169 addError("planetSubscription.error.deleting");
170 }
171
172 return LIST;
173 }
174
175
176 /**
177 * Validate posted subscription, fill in blanks via Technorati
178 */
179 private void myValidate() {
180
/*
P/P * Method: void myValidate()
*
* Preconditions:
* init'ed(this.subUrl)
*
* Test Vectors:
* org.apache.commons.lang.StringUtils:isEmpty(...)@181: {0}, {1}
*/
181 if(StringUtils.isEmpty(getSubUrl())) {
182 addError("planetSubscription.error.feedUrl");
183 }
184 }
185
186
187 public List<Subscription> getSubscriptions() {
188
/*
P/P * Method: List getSubscriptions()
*
* Preconditions:
* init'ed(this.group)
*
* Presumptions:
* init'ed(java.util.Collections.EMPTY_LIST)
* java.util.Iterator:next(...)@195 != null
* org.apache.roller.planet.pojos.PlanetGroup:getSubscriptions(...)@191 != null
* org.apache.roller.planet.pojos.Subscription:getFeedURL(...)@197 != null
*
* Postconditions:
* return_value == One-of{java.util.Collections.EMPTY_LIST, &new ArrayList(getSubscriptions#1)}
* (soft) init'ed(return_value)
* new ArrayList(getSubscriptions#1) num objects <= 1
*
* Test Vectors:
* this.group: Addr_Set{null}, Inverse{null}
* java.lang.String:startsWith(...)@197: {1}, {0}
* java.util.Iterator:hasNext(...)@195: {0}, {1}
*/
189 List<Subscription> subs = Collections.EMPTY_LIST;
190 if(getGroup() != null) {
191 Set<Subscription> subsSet = getGroup().getSubscriptions();
192
193 // iterate over list and build display list
194 subs = new ArrayList();
195 for( Subscription sub : subsSet ) {
196 // only include external subs for display
197 if(!sub.getFeedURL().startsWith("weblogger:")) {
198 subs.add(sub);
199 }
200 }
201 }
202
203 return subs;
204 }
205
206
207 public String getGroupHandle() {
/*
P/P * Method: String getGroupHandle()
*
* Preconditions:
* init'ed(this.groupHandle)
*
* Postconditions:
* return_value == this.groupHandle
* init'ed(return_value)
*/
208 return groupHandle;
209 }
210
211 public void setGroupHandle(String groupHandle) {
/*
P/P * Method: void setGroupHandle(String)
*
* Postconditions:
* this.groupHandle == groupHandle
* init'ed(this.groupHandle)
*/
212 this.groupHandle = groupHandle;
213 }
214
215 public PlanetGroup getGroup() {
/*
P/P * Method: PlanetGroup getGroup()
*
* Preconditions:
* init'ed(this.group)
*
* Postconditions:
* return_value == this.group
* init'ed(return_value)
*/
216 return group;
217 }
218
219 public void setGroup(PlanetGroup group) {
/*
P/P * Method: void setGroup(PlanetGroup)
*
* Postconditions:
* this.group == group
* init'ed(this.group)
*/
220 this.group = group;
221 }
222
223 public String getSubUrl() {
/*
P/P * Method: String getSubUrl()
*
* Preconditions:
* init'ed(this.subUrl)
*
* Postconditions:
* return_value == this.subUrl
* init'ed(return_value)
*/
224 return subUrl;
225 }
226
227 public void setSubUrl(String subUrl) {
/*
P/P * Method: void setSubUrl(String)
*
* Postconditions:
* this.subUrl == subUrl
* init'ed(this.subUrl)
*/
228 this.subUrl = subUrl;
229 }
230
231 }
SofCheck Inspector Build Version : 2.18479
| PlanetSubscriptions.java |
2009-Jan-02 14:24:46 |
| PlanetSubscriptions.class |
2009-Sep-04 03:12:46 |