View Javadoc
1   /**
2    * This file Copyright (c) 2013-2018 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.about.app;
35  
36  import static info.magnolia.about.app.AboutView.*;
37  
38  import info.magnolia.i18nsystem.SimpleTranslator;
39  import info.magnolia.objectfactory.Components;
40  import info.magnolia.ui.AlertBuilder;
41  import info.magnolia.usagemetrics.UsageMetricsModule;
42  
43  import javax.inject.Inject;
44  
45  import org.apache.commons.lang3.StringUtils;
46  import org.slf4j.Logger;
47  import org.slf4j.LoggerFactory;
48  
49  import com.vaadin.ui.Notification;
50  import com.vaadin.v7.data.Item;
51  import com.vaadin.v7.data.util.ObjectProperty;
52  import com.vaadin.v7.data.util.PropertysetItem;
53  
54  /**
55   * The AboutPresenter.
56   */
57  public class AboutPresenter implements AboutView.Listener {
58  
59      private static final Logger log = LoggerFactory.getLogger(AboutPresenter.class);
60  
61      static final String INSTANCE_AUTHOR_I18N_KEY = "about.app.main.instance.author";
62      static final String INSTANCE_PUBLIC_I18N_KEY = "about.app.main.instance.public";
63      static final String UNKNOWN_PROPERTY_I18N_KEY = "about.app.main.unknown";
64  
65      protected final SimpleTranslator i18n;
66      private final String unknownProperty;
67  
68      private final AboutView view;
69      private final InstanceConfigurationProvider configProvider;
70      private UsageMetricsModule usageMetricsModule;
71  
72      /**
73       * @deprecated since 5.5, get view data through {@link #getInstallationInfo()}
74       */
75      @Deprecated
76      protected Item viewData = new PropertysetItem();
77  
78      /**
79       * @deprecated since 6.1. Use {@link #AboutPresenter(AboutView, InstanceConfigurationProvider, info.magnolia.i18nsystem.SimpleTranslator, info.magnolia.usagemetrics.UsageMetricsModule)} instead.
80       */
81      @Deprecated
82      public AboutPresenter(final AboutView view, final InstanceConfigurationProvider configProvider, final SimpleTranslator i18n) {
83          this(view, configProvider, i18n, Components.getComponent(UsageMetricsModule.class));
84      }
85  
86      @Inject
87      public AboutPresenter(final AboutView view, final InstanceConfigurationProvider configProvider, final SimpleTranslator i18n, UsageMetricsModule usageMetricsModule) {
88          this.view = view;
89          this.configProvider = configProvider;
90          this.i18n = i18n;
91          this.unknownProperty = i18n.translate(UNKNOWN_PROPERTY_I18N_KEY);
92          this.usageMetricsModule = usageMetricsModule;
93      }
94  
95      public AboutView start() {
96          Item viewData = getInstallationInfo();
97          // preserve compatibility
98          if (!this.viewData.getItemPropertyIds().isEmpty()) {
99              this.viewData.getItemPropertyIds().forEach(
100                     propertyId -> viewData.addItemProperty(propertyId,
101                             AboutPresenter.this.viewData.getItemProperty(propertyId)));
102         }
103         view.setDataSource(viewData);
104         view.setListener(this);
105         return view;
106     }
107 
108     protected Item getInstallationInfo() {
109         PropertysetItem viewData = new PropertysetItem();
110         // Magnolia information
111         String mgnlEdition = getEditionName();
112         String mgnlVersion = configProvider.getMagnoliaVersion();
113         String authorInstance = configProvider.isAdmin() ?
114                 i18n.translate(INSTANCE_AUTHOR_I18N_KEY) :
115                 i18n.translate(INSTANCE_PUBLIC_I18N_KEY);
116 
117         // System information
118         String osInfo = String.format("%s %s (%s)", configProvider.getOSName(),
119                 configProvider.getOSVersion(), configProvider.getOSArch());
120         String javaInfo = String.format("%s %s (build %s)", configProvider.getJavaVendor(),
121                 configProvider.getJavaVersion(), configProvider.getJavaRuntimeVersion());
122         String serverInfo = configProvider.getApplicationServer();
123 
124         String dbInfo = configProvider.getDatabase();
125         String dbDriverInfo = configProvider.getDatabaseDriver();
126         if (StringUtils.isBlank(dbInfo)) {
127             dbInfo = i18n.translate(UNKNOWN_PROPERTY_I18N_KEY);
128         }
129         if (StringUtils.isBlank(dbDriverInfo)) {
130             dbInfo = i18n.translate(UNKNOWN_PROPERTY_I18N_KEY);
131         }
132 
133         String jcrInfo = String.format("%s %s", configProvider.getJcrName(),
134                 configProvider.getJcrVersion());
135 
136         // Prepare information for the view
137         addItemProperty(viewData, MAGNOLIA_EDITION_KEY, mgnlEdition);
138         addItemProperty(viewData, MAGNOLIA_VERSION_KEY, mgnlVersion);
139         addItemProperty(viewData, MAGNOLIA_INSTANCE_KEY, authorInstance);
140         addItemProperty(viewData, OS_INFO_KEY, osInfo);
141         addItemProperty(viewData, JAVA_INFO_KEY, javaInfo);
142         addItemProperty(viewData, SERVER_INFO_KEY, serverInfo);
143         addItemProperty(viewData, JCR_INFO_KEY, jcrInfo);
144         addItemProperty(viewData, DB_INFO_KEY, dbInfo);
145         addItemProperty(viewData, DB_DRIVER_INFO_KEY, dbDriverInfo);
146 
147         return viewData;
148     }
149 
150     /**
151      * Adds a property to the given Item data-source; blank values will be translated as 'unknown'.
152      */
153     protected void addItemProperty(Item viewData, String key, String value) {
154         viewData.addItemProperty(key, new ObjectProperty<>(StringUtils.defaultIfBlank(value, unknownProperty)));
155     }
156 
157     /**
158      * @deprecated since 5.5, use {@link #addItemProperty(Item, String, String)} instead.
159      */
160     @Deprecated
161     protected void addViewProperty(String key, String value) {
162         viewData.addItemProperty(key, new ObjectProperty<>(StringUtils.defaultIfBlank(value, unknownProperty)));
163     }
164 
165     protected AboutView getView() {
166         return view;
167     }
168 
169     protected String getEditionName() {
170         return configProvider.getEditionName();
171     }
172 
173     /**
174      * @deprecated since 5.5.2, use {@link InstanceConfigurationProvider#getConnectionString()} instead.
175      */
176     @Deprecated
177     String[] getConnectionString() {
178         return configProvider.getConnectionString();
179     }
180 
181     /**
182      * @deprecated since 5.5.2, use {@link InstanceConfigurationProvider#getRepositoryName()} instead.
183      */
184     @Deprecated
185     String getRepoName() {
186         return configProvider.getRepositoryName();
187     }
188 
189     @Override
190     public void handleConsent(boolean consent) {
191         usageMetricsModule.handleConsent(consent);
192         AlertBuilder.alert(i18n.translate("about.app.main.privacy.warning.title"))
193                 .withLevel(Notification.Type.WARNING_MESSAGE)
194                 .withBody(i18n.translate("about.app.main.privacy.warning.body"))
195                 .withOkButtonCaption(i18n.translate("about.app.main.privacy.warning.confirm"))
196                 .buildAndOpen();
197     }
198 }