1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
64
65
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
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
161 }
162
163 @Override
164 public void openChooseDialog(UiContext uiContext, String targetTreeRootPath, String selectedId, ChooseDialogCallback callback) {
165
166 }
167 }