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 info.magnolia.cms.beans.config.ServerConfiguration;
37  import info.magnolia.cms.security.User;
38  import info.magnolia.context.Context;
39  import info.magnolia.i18nsystem.SimpleTranslator;
40  import info.magnolia.objectfactory.Components;
41  import info.magnolia.ui.vaadin.layout.SmallAppLayout;
42  
43  import java.util.HashMap;
44  import java.util.Map;
45  import java.util.Map.Entry;
46  
47  import javax.inject.Inject;
48  
49  import com.vaadin.data.HasValue;
50  import com.vaadin.ui.CheckBox;
51  import com.vaadin.ui.Component;
52  import com.vaadin.ui.FormLayout;
53  import com.vaadin.v7.data.Item;
54  import com.vaadin.v7.data.Property;
55  import com.vaadin.v7.ui.Label;
56  
57  /**
58   * Implementation of the {@link AboutView} interface, for the community edition.
59   */
60  public class AboutViewImpl implements AboutView {
61  
62      protected final SmallAppLayout root = new SmallAppLayout();
63  
64      private Item dataSource;
65  
66      private Map<String, Property.Viewer> dataBindings = new HashMap<>();
67  
68      protected final SimpleTranslator i18n;
69  
70      private Listener listener;
71  
72      private User currentUser;
73  
74      @Inject
75      public AboutViewImpl(SimpleTranslator i18n, Context context, ServerConfiguration serverConfiguration) {
76          this.i18n = i18n;
77          this.currentUser = context.getUser();
78          root.setDescription(i18n.translate("about.app.main.description"));
79          root.addSection(createInstallationSection(), i18n.translate("about.app.main.installation.title"));
80          root.addSection(createUsageMetricsSection(serverConfiguration.isUsageMetrics()), i18n.translate("about.app.main.privacy.title"));
81      }
82  
83      /**
84       * @deprecated since 6.1. Use {@link #AboutViewImpl(info.magnolia.i18nsystem.SimpleTranslator, info.magnolia.context.Context, info.magnolia.cms.beans.config.ServerConfiguration)} instead.
85       */
86      @Deprecated
87      public AboutViewImpl(SimpleTranslator i18n) {
88          this(i18n, Components.getComponent(Context.class), Components.getComponent(ServerConfiguration.class));
89      }
90  
91      private Component createUsageMetricsSection(boolean isUsageMetrics) {
92          CheckBox consent = new CheckBox();
93          consent.setCaptionAsHtml(true);
94          consent.setCaption(i18n.translate("about.app.main.privacy.label"));
95          consent.setValue(isUsageMetrics);
96          consent.addValueChangeListener((HasValue.ValueChangeListener<Boolean>) event -> listener.handleConsent(event.getValue()));
97          consent.setReadOnly(!currentUser.hasRole("superuser"));
98          return new FormLayout(consent);
99      }
100 
101     private Component createInstallationSection() {
102 
103         // build and bind fields
104         Component mgnlEdition = buildAndBind(MAGNOLIA_EDITION_KEY, i18n.translate("about.app.main.mgnledition"));
105         Component mgnlVersion = buildAndBind(MAGNOLIA_VERSION_KEY, i18n.translate("about.app.main.mgnlversion"));
106         Component mgnlInstance = buildAndBind(MAGNOLIA_INSTANCE_KEY, i18n.translate("about.app.main.instance"));
107 
108         Component osInfo = buildAndBind(OS_INFO_KEY, i18n.translate("about.app.main.os"));
109         Component javaInfo = buildAndBind(JAVA_INFO_KEY, i18n.translate("about.app.main.java"));
110         Component serverInfo = buildAndBind(SERVER_INFO_KEY, i18n.translate("about.app.main.server"));
111         Component dbInfo = buildAndBind(DB_INFO_KEY, i18n.translate("about.app.main.dbinfo"));
112         Component dbDriverInfo = buildAndBind(DB_DRIVER_INFO_KEY, i18n.translate("about.app.main.dbdriverinfo"));
113         Component jcrInfo = buildAndBind(JCR_INFO_KEY, i18n.translate("about.app.main.repository"));
114 
115         // layout
116         FormLayout layout = new FormLayout();
117 
118         layout.addComponent(createFieldsetTitle(i18n.translate("about.app.main.magnolia.title")));
119         layout.addComponents(mgnlEdition, mgnlVersion, mgnlInstance);
120 
121         Component environmentTitle = createFieldsetTitle(i18n.translate("about.app.main.environment.title"));
122         environmentTitle.addStyleName("spacer");
123         layout.addComponents(environmentTitle, osInfo, javaInfo, serverInfo, dbInfo, dbDriverInfo, jcrInfo);
124 
125         return layout;
126     }
127 
128     protected Component createFieldsetTitle(String title) {
129         Label fieldsetTitle = new Label(title);
130         fieldsetTitle.addStyleName("fieldset-title");
131         return fieldsetTitle;
132     }
133 
134     protected Component buildAndBind(String key, String caption) {
135         Label field = new Label();
136         field.setCaption(caption);
137         dataBindings.put(key, field);
138         return field;
139     }
140 
141     @Override
142     public void setDataSource(Item item) {
143         this.dataSource = item;
144         refresh();
145     }
146 
147     @Override
148     public void setListener(Listener listener) {
149         this.listener = listener;
150     }
151 
152     protected Item getDataSource() {
153         return this.dataSource;
154     }
155 
156     private void refresh() {
157         for (Entry<String, Property.Viewer> entry : dataBindings.entrySet()) {
158             Property.Viewer field = entry.getValue();
159             Property<?> property = dataSource.getItemProperty(entry.getKey());
160             field.setPropertyDataSource(property);
161         }
162     }
163 
164     @Override
165     public Component asVaadinComponent() {
166         return root;
167     }
168 
169 }