View Javadoc
1   /**
2    * This file Copyright (c) 2012-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.ui.framework.app;
35  
36  import static info.magnolia.ui.api.message.MessageType.ERROR;
37  import static java.util.Optional.ofNullable;
38  import static java.util.stream.Collectors.toList;
39  
40  import info.magnolia.context.MgnlContext;
41  import info.magnolia.i18nsystem.SimpleTranslator;
42  import info.magnolia.objectfactory.Components;
43  import info.magnolia.ui.api.app.App;
44  import info.magnolia.ui.api.app.AppContext;
45  import info.magnolia.ui.api.app.AppView;
46  import info.magnolia.ui.api.app.ChooseDialogCallback;
47  import info.magnolia.ui.api.app.SubAppDescriptor;
48  import info.magnolia.ui.api.context.UiContext;
49  import info.magnolia.ui.api.location.DefaultLocation;
50  import info.magnolia.ui.api.location.Location;
51  import info.magnolia.ui.api.message.Message;
52  import info.magnolia.util.OptionalConsumer;
53  
54  import java.util.List;
55  import java.util.stream.Stream;
56  
57  import javax.inject.Inject;
58  
59  import org.apache.commons.lang3.StringUtils;
60  
61  
62  /**
63   * Basic app implementation with default behavior suitable for most apps.
64   *
65   * @see info.magnolia.ui.api.app.App
66   */
67  public class BaseApp implements App {
68  
69      protected AppContext appContext;
70  
71      private AppView view;
72  
73      private SimpleTranslator i18n;
74  
75      @Inject
76      protected BaseApp(AppContext appContext, AppView view, SimpleTranslator i18n) {
77          this.appContext = appContext;
78          this.view = view;
79          this.i18n = i18n;
80          view.setListener(appContext);
81      }
82  
83      /**
84       * since 6.2 use {@link #BaseApp(AppContext, AppView, SimpleTranslator)} instead
85       */
86      @Deprecated
87      protected BaseApp(AppContext appContext, AppView view) {
88          this(appContext, view, Components.getComponent(SimpleTranslator.class));
89      }
90  
91      @Override
92      public void locationChanged(Location location) {
93          Messagel#Message">Message unknownSubAppMessage = new Message(ERROR,
94                  i18n.translate("ui-framework.actions.openSubApp.subject", location.getSubAppId()),
95                  i18n.translate("ui-framework.actions.openSubApp.message", location.getAppName(), location.getSubAppId()));
96          openSubApp(location, () -> appContext.sendLocalMessage(unknownSubAppMessage));
97      }
98  
99      @Override
100     public void start(Location location) {
101         view.setAppName(location.getAppName());
102 
103         List<SubAppDescriptor> subAppDescriptors = getSubAppDescriptorStream().collect(toList());
104 
105         subAppDescriptors.stream()
106                 .filter(subAppDescriptor -> !subAppDescriptor.isClosable())
107                 .map(SubAppDescriptor::getName)
108                 .map(subAppName -> getDefaultLocation(location, subAppName))
109                 .forEach(appContext::openSubApp);
110 
111         openSubApp(location, () -> subAppDescriptors.stream()
112                 .findFirst()
113                 .map(SubAppDescriptor::getName)
114                 .map(subAppName -> getDefaultLocation(location, subAppName))
115                 .ifPresent(l -> {
116                     Messagee/Message.html#Message">Message message = new Message();
117                     message.setSubject(i18n.translate("ui-framework.openSubApp.url.subject", location.getSubAppId()));
118                     message.setMessage(i18n.translate("ui-framework.openSubApp.url.message",
119                             getRequestURL(location), getRequestURL(l), location.getAppName(), location.getSubAppId()));
120 
121                     appContext.sendLocalMessage(message);
122                     appContext.openSubApp(l);
123                 }));
124 
125     }
126 
127     private DefaultLocation getDefaultLocation(Location location, String subAppName) {
128         return new DefaultLocation(location.getAppType(), location.getAppName(), subAppName);
129     }
130 
131     private String getRequestURL(Location location) {
132         return StringUtils.join(MgnlContext.getWebContext().getRequest().getRequestURL(), "#", location.toString());
133     }
134 
135     private OptionalConsumer<SubAppDescriptor> openSubApp(Location location, Runnable unknownSubAppHandler) {
136         String subAppId = ofNullable(location.getSubAppId()).orElse("null");
137         return OptionalConsumer.of(getSubAppDescriptorStream().filter(s -> s.getName().equals(subAppId)).findFirst())
138                 .ifPresent(s -> appContext.openSubApp(location)).ifNotPresent(unknownSubAppHandler);
139     }
140 
141     private Stream<SubAppDescriptor> getSubAppDescriptorStream() {
142         return appContext.getAppDescriptor().getSubApps().values().stream();
143     }
144 
145     protected AppContext getAppContext() {
146         return appContext;
147     }
148 
149     @Override
150     public void stop() {
151     }
152 
153     @Override
154     public AppView getView() {
155         return view;
156     }
157 
158     @Override
159     public void openChooseDialog(UiContext overlayLayer, String selectedId, ChooseDialogCallback callback) {
160         // Choose dialogs are a feature of content apps, invoking this on a BaseApp will not do anything.
161     }
162 
163     @Override
164     public void openChooseDialog(UiContext uiContext, String targetTreeRootPath, String selectedId, ChooseDialogCallback callback) {
165         // Choose dialogs are a feature of content apps, invoking this on a BaseApp will not do anything.
166     }
167 }