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.app.subapps.aggregationconfig;
35
36 import info.magnolia.cms.security.AccessManager;
37 import info.magnolia.cms.security.Permission;
38 import info.magnolia.context.MgnlContext;
39 import info.magnolia.i18nsystem.SimpleTranslator;
40 import info.magnolia.jcr.util.NodeUtil;
41 import info.magnolia.jcr.util.PropertyUtil;
42 import info.magnolia.module.data.commands.ImportCommand;
43 import info.magnolia.module.rssaggregator.RSSAggregatorConstants;
44 import info.magnolia.module.rssaggregator.RSSAggregatorNodeTypes;
45 import info.magnolia.module.rssaggregator.app.subapps.aggregationconfig.view.RSSAggregatorConfigurationView;
46 import info.magnolia.module.rssaggregator.generator.CollectStatisticsCommand;
47 import info.magnolia.module.rssaggregator.generator.PlanetDataGenerator;
48 import info.magnolia.repository.RepositoryConstants;
49 import info.magnolia.ui.api.app.AppContext;
50 import info.magnolia.ui.api.context.UiContext;
51 import info.magnolia.ui.api.view.View;
52 import info.magnolia.ui.vaadin.overlay.MessageStyleTypeEnum;
53
54 import javax.inject.Inject;
55 import javax.jcr.Node;
56 import javax.jcr.NodeIterator;
57 import javax.jcr.RepositoryException;
58 import javax.jcr.Session;
59
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62
63
64
65
66
67
68 public class RSSAggregatorConfigurationPresenter implements RSSAggregatorConfigurationView.Listener {
69
70 private static final String IMPORT_ERROR_LABEL = "rssAggregator.config.notification.import_error";
71
72 private static final String TASKS = "/modules/data/config/importers/rssaggregator/automatedExecution";
73
74 private static final String PLANET_DATA_PATH = "/modules/scheduler/config/jobs/generatePlanetData";
75
76 private static final String PLANET_STATISTICS_PATH = "/modules/scheduler/config/jobs/collectPlanetStatistics";
77
78 private static final String IMPORT_COMPLETE_LABEL = "rssAggregator.config.notification.import_complete";
79
80 private static final String SETTINGS_CHANGED_LABEL = "rssAggregator.config.notification.settings_changed";
81
82 public static final String PLANET_DATA_CHANGED_LABEL = "rssAggregator.config.notification.planet_data_changed";
83
84 public static final String STATISTICS_COLLECTED_LABEL = "rssAggregator.config.notification.stats_collected";
85
86 private Logger log = LoggerFactory.getLogger(getClass());
87
88 private RSSAggregatorConfigurationView view;
89
90 private UiContext uiContext;
91
92 private SimpleTranslator translator;
93
94 @Inject
95 public RSSAggregatorConfigurationPresenter(RSSAggregatorConfigurationView view, AppContext uiContext, SimpleTranslator translator) {
96 this.view = view;
97 this.uiContext = uiContext;
98 this.translator = translator;
99 }
100
101 public View start() {
102 view.setListener(this);
103 return view;
104 }
105
106 protected boolean checkPermissions(String repository, String basePath, long permissionType) {
107 AccessManager accessManager = MgnlContext.getAccessManager(repository);
108 if (accessManager != null) {
109 if (!accessManager.isGranted(basePath, permissionType)) {
110 return false;
111 }
112 }
113 return true;
114 }
115
116 @Override
117 public void onManualImport() {
118 if (!checkPermissions(RSSAggregatorConstants.WORKSPACE, "/", Permission.WRITE)) {
119 uiContext.openNotification(MessageStyleTypeEnum.ERROR, false, translator.translate(IMPORT_ERROR_LABEL));
120 } else {
121 ImportCommand cmd = new ImportCommand();
122 cmd.setImporter("rssaggregator");
123 cmd.execute(MgnlContext.getInstance());
124 uiContext.openNotification(MessageStyleTypeEnum.INFO, true, translator.translate(IMPORT_COMPLETE_LABEL));
125 }
126 }
127
128 @Override
129 public void onAutomaticImportChanged(int periodMinutes) {
130 try {
131 Session session = MgnlContext.getJCRSession("config");
132 Node execution = session.getNode(TASKS);
133 execution.setProperty("enabled", true);
134 execution.setProperty("cron", "0 0/" + periodMinutes + " * * * *");
135 session.save();
136 uiContext.openNotification(MessageStyleTypeEnum.INFO, true, translator.translate(SETTINGS_CHANGED_LABEL));
137 } catch (RepositoryException e) {
138 displayException(e, "Failed to update automatic update period");
139 }
140
141 }
142
143 @Override
144 public void onAutomaticPlanetUpdateChanged(int value) {
145 try {
146 Session session = MgnlContext.getJCRSession("config");
147 Node planetDataScheduler = session.getNode(PLANET_DATA_PATH);
148 PropertyUtil.setProperty(planetDataScheduler, "active", true);
149 PropertyUtil.setProperty(planetDataScheduler, "cron", "0 0/" + value + " * * * *");
150 session.save();
151 uiContext.openNotification(MessageStyleTypeEnum.INFO, true, SETTINGS_CHANGED_LABEL);
152 } catch (RepositoryException e) {
153 displayException(e, "Failed to change planet update period: ");
154 }
155 }
156
157 @Override
158 public void onPlanetDataManualUpdate() {
159 try {
160 PlanetDataGenerator pdg = new PlanetDataGenerator();
161 pdg.execute(MgnlContext.getInstance());
162 uiContext.openNotification(MessageStyleTypeEnum.INFO, true, translator.translate(PLANET_DATA_CHANGED_LABEL));
163 } catch (Exception e) {
164 displayException(e, "Failed to update planet data: ");
165 }
166
167 }
168
169 @Override
170 public void onPlanetStatisticsManualUpdate() {
171 CollectStatisticsCommand csc = new CollectStatisticsCommand();
172 try {
173 csc.execute(MgnlContext.getInstance());
174 uiContext.openNotification(MessageStyleTypeEnum.INFO, true, translator.translate(STATISTICS_COLLECTED_LABEL));
175 } catch (Exception e) {
176 displayException(e, "Failed to collect statistics: ");
177 }
178 }
179
180 @Override
181 public void onPlanetStatisticsAutomaticUpdatePeriodChanged(int value) {
182 try {
183 Session session = MgnlContext.getJCRSession("config");
184 Node planetStatisticsScheduler = session.getNode(PLANET_STATISTICS_PATH);
185 PropertyUtil.setProperty(planetStatisticsScheduler, "active", true);
186 PropertyUtil.setProperty(planetStatisticsScheduler, "cron", "0 0/" + value + " * * * *");
187 session.save();
188 uiContext.openNotification(MessageStyleTypeEnum.INFO, true, translator.translate(SETTINGS_CHANGED_LABEL));
189 } catch (RepositoryException e) {
190 displayException(e, "Failed to update planet update period: ");
191 }
192
193 }
194
195 private void displayException(Exception e, String msg) {
196 uiContext.openNotification(MessageStyleTypeEnum.ERROR, false, msg + e.getMessage());
197 log.error(msg, e);
198 }
199
200 public void updateConfiguration() {
201 view.setDataImportPeriod(parseAutoUpdatePeriod(TASKS));
202 view.setPlanetStatsImportPeriod(parseAutoUpdatePeriod(PLANET_STATISTICS_PATH));
203 view.setPlanetDataImportPeriod(parseAutoUpdatePeriod(PLANET_DATA_PATH));
204
205 boolean isPlanetVisible = false;
206 try {
207 Node root = MgnlContext.getJCRSession(RSSAggregatorConstants.WORKSPACE).getRootNode();
208 NodeIterator it = root.getNodes();
209 while (!isPlanetVisible && it.hasNext()) {
210 Node node = it.nextNode();
211 if (NodeUtil.isNodeType(node, RSSAggregatorNodeTypes.RSSAggregator.NAME) && node.hasProperty("planetFeed")) {
212 isPlanetVisible = node.getProperty("planetFeed").getBoolean();
213 }
214 }
215 } catch (RepositoryException e) {
216 log.warn("Problem occurred while checking planet config visibility ", e);
217 }
218
219 view.setPlanetConfigVisible(isPlanetVisible);
220 }
221
222 private int parseAutoUpdatePeriod(String path) {
223 try {
224 Session session = MgnlContext.getJCRSession(RepositoryConstants.CONFIG);
225 Node execution = session.getNode(path);
226 String[] cron = execution.getProperty("cron").getString().split(" ");
227 if (cron[1].indexOf('/') < 0) {
228 return 0;
229 } else {
230 return Integer.parseInt(cron[1].substring(cron[1].indexOf('/') + 1));
231 }
232 } catch (RepositoryException e) {
233 return 0;
234 }
235 }
236 }