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