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