1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.rssaggregator.pages;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.core.HierarchyManager;
38 import info.magnolia.cms.i18n.Messages;
39 import info.magnolia.cms.i18n.MessagesManager;
40 import info.magnolia.cms.security.AccessDeniedException;
41 import info.magnolia.cms.security.AccessManager;
42 import info.magnolia.cms.security.Permission;
43 import info.magnolia.context.MgnlContext;
44 import info.magnolia.jcr.util.PropertyUtil;
45 import info.magnolia.module.admininterface.TemplatedMVCHandler;
46 import info.magnolia.module.data.commands.ImportCommand;
47 import info.magnolia.module.rssaggregator.generator.CollectStatisticsCommand;
48 import info.magnolia.module.rssaggregator.generator.PlanetDataGenerator;
49
50 import java.io.IOException;
51
52 import javax.jcr.Node;
53 import javax.jcr.Session;
54 import javax.servlet.ServletException;
55 import javax.servlet.http.HttpServletRequest;
56 import javax.servlet.http.HttpServletResponse;
57
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
60
61
62
63
64
65 public class RSSAggregatorPage extends TemplatedMVCHandler {
66
67
68
69
70 public static final long serialVersionUID = 222L;
71
72
73
74
75 public static final String VIEW_EXPORT="rssaggregator";
76
77 private static final String TASKS = "/modules/data/config/importers/rssaggregator/automatedExecution";
78
79 private static final String PLANET_DATA_PATH = "/modules/scheduler/config/jobs/generatePlanetData";
80
81 private static final String PLANET_STATISTICS_PATH = "/modules/scheduler/config/jobs/collectPlanetStatistics";
82
83 private static final String RSSAGGREGATOR_PAGE = "RSSAggregatorPage";
84
85 private static Logger log = LoggerFactory.getLogger(RSSAggregatorPage.class);
86
87 protected boolean mgnlKeepVersions;
88
89 protected int mgnlMaxEntriesPerFile;
90
91 private boolean manualRefresh;
92
93 private boolean manualRefreshStatistics;
94
95 private boolean manualRefreshData;
96
97 private String mgnlImportRepeat;
98
99 private String mgnlPlanetImportRepeat;
100
101 private String mgnlPlanetStatisticsImportRepeat;
102
103
104
105
106
107 public boolean isMgnlKeepVersions() {
108 return this.mgnlKeepVersions;
109 }
110
111
112
113
114
115 public void setMgnlKeepVersions(boolean mgnlKeepVersions) {
116 this.mgnlKeepVersions = mgnlKeepVersions;
117 }
118
119
120
121
122
123 public boolean isManualBackup() {
124 return this.manualRefresh;
125 }
126
127
128
129
130
131 public void setManualRefresh(boolean backup) {
132 this.manualRefresh = backup;
133 }
134
135
136
137
138
139 public boolean isManualRefreshStatistics() {
140 return manualRefreshStatistics;
141 }
142
143
144
145
146
147 public void setManualRefreshStatistics(boolean manualRefreshStatistics) {
148 this.manualRefreshStatistics = manualRefreshStatistics;
149 }
150
151 public boolean isManualRefreshData() {
152 return manualRefreshData;
153 }
154
155 public void setManualRefreshData(boolean manualRefreshData) {
156 this.manualRefreshData = manualRefreshData;
157 }
158
159
160
161
162
163
164 public RSSAggregatorPage(String name, HttpServletRequest request, HttpServletResponse response) {
165 super(name, request, response);
166 }
167
168
169
170
171 @Override
172 public String getCommand() {
173 if (this.manualRefresh) {
174 return "manualRefresh";
175 } else {
176 if (this.manualRefreshStatistics) {
177 return "manualRefreshStatistics";
178 }
179 }
180 return super.getCommand();
181 }
182
183
184
185
186 public String manualRefresh() throws Exception {
187 if (!checkPermissions(request, "data", "/rssaggregator", Permission.WRITE)) {
188 throw new ServletException(new AccessDeniedException(
189 "Write permission needed for feed update. User not allowed to WRITE to data workspace."));
190 }
191 ImportCommand cmd = new ImportCommand();
192 cmd.setImporter("rssaggregator");
193 cmd.execute(MgnlContext.getInstance());
194 return RSSAGGREGATOR_PAGE;
195 }
196
197 public String automaticUpdate() throws Exception {
198 HierarchyManager hm = MgnlContext.getHierarchyManager("config");
199 Content execution = hm.getContent(TASKS);
200 execution.getNodeData("enabled").setValue(true);
201 execution.getNodeData("cron").setValue( "0 0/" + getMgnlImportRepeat() + " * * * *");
202 hm.save();
203
204 return RSSAGGREGATOR_PAGE;
205 }
206
207 public String manualRefreshData() throws Exception {
208 PlanetDataGenerator pdg = new PlanetDataGenerator();
209 pdg.execute(MgnlContext.getInstance());
210
211 return RSSAGGREGATOR_PAGE;
212 }
213
214 public String automaticPlanetUpdate() throws Exception {
215 Session session = MgnlContext.getJCRSession("config");
216 Node planetDataScheduler = session.getNode(PLANET_DATA_PATH);
217 PropertyUtil.setProperty(planetDataScheduler, "active", true);
218 PropertyUtil.setProperty(planetDataScheduler, "cron", "0 0/" + getMgnlPlanetImportRepeat() + " * * * *");
219 session.save();
220
221 return RSSAGGREGATOR_PAGE;
222 }
223
224 public String manualRefreshStatistics() throws Exception {
225 CollectStatisticsCommand csc = new CollectStatisticsCommand();
226 csc.execute(MgnlContext.getInstance());
227
228 return RSSAGGREGATOR_PAGE;
229 }
230
231 public String automaticPlanetStatisticsUpdate() throws Exception {
232 Session session = MgnlContext.getJCRSession("config");
233 Node planetStatisticsScheduler = session.getNode(PLANET_STATISTICS_PATH);
234 PropertyUtil.setProperty(planetStatisticsScheduler, "active", true);
235 PropertyUtil.setProperty(planetStatisticsScheduler, "cron", "0 0/" + getMgnlPlanetStatisticsImportRepeat() + " * * * *");
236 session.save();
237
238 return RSSAGGREGATOR_PAGE;
239 }
240
241 private String getMgnlImportRepeat() {
242 return mgnlImportRepeat;
243 }
244
245 public String getMgnlPlanetImportRepeat() {
246 return mgnlPlanetImportRepeat;
247 }
248
249 public String getMgnlPlanetStatisticsImportRepeat() {
250 return mgnlPlanetStatisticsImportRepeat;
251 }
252
253 public int getAutoUpdate() throws Exception {
254 HierarchyManager hm = MgnlContext.getHierarchyManager("config");
255 Content execution = hm.getContent(TASKS);
256 String[] cron = execution.getNodeData("cron").getString().split(" ");
257 if (cron[1].indexOf('/') < 0) {
258 return 0;
259 } else {
260 return Integer.parseInt(cron[1].substring(cron[1].indexOf('/') + 1));
261 }
262 }
263
264
265
266
267
268
269 protected boolean checkPermissions(HttpServletRequest request, String repository, String basePath,
270 long permissionType) {
271
272 AccessManager accessManager = MgnlContext.getAccessManager(repository);
273 if (accessManager != null) {
274 if (!accessManager.isGranted(basePath, permissionType)) {
275 return false;
276 }
277 }
278 return true;
279 }
280
281 @Override
282 public void renderHtml(String view) throws IOException {
283
284 if(VIEW_EXPORT.equals(view)){
285 return;
286 }
287 super.renderHtml(view);
288 }
289
290 public Messages getMessages() {
291 return MessagesManager.getMessages("info.magnolia.module.rssaggregator.messages");
292 }
293
294 public void setMgnlImportRepeat(String mgnlImportRepeat) {
295 this.mgnlImportRepeat = mgnlImportRepeat;
296 }
297
298 public void setMgnlPlanetImportRepeat(String mgnlPlanetImportRepeat) {
299 this.mgnlPlanetImportRepeat = mgnlPlanetImportRepeat;
300 }
301
302 public void setMgnlPlanetStatisticsImportRepeat(String mgnlPlanetStatisticsImportRepeat) {
303 this.mgnlPlanetStatisticsImportRepeat = mgnlPlanetStatisticsImportRepeat;
304 }
305 }