View Javadoc

1   /**
2    * This file Copyright (c) 2013 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * Presenter class, holds logic of
66   * {@link info.magnolia.module.rssaggregator.app.subapps.aggregationconfig.RSSAggregatorConfigurationSubApp} sub-app.
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 }