View Javadoc
1   /**
2    * This file Copyright (c) 2021 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.admincentral.compatibility;
35  
36  import info.magnolia.event.EventBus;
37  import info.magnolia.event.SystemEventBus;
38  import info.magnolia.i18nsystem.I18nizer;
39  import info.magnolia.ui.admincentral.AdmincentralModule;
40  import info.magnolia.ui.api.app.launcherlayout.AppLauncherGroupDefinition;
41  import info.magnolia.ui.api.app.launcherlayout.AppLauncherGroupEntryDefinition;
42  import info.magnolia.ui.api.app.launcherlayout.AppLauncherLayoutDefinition;
43  import info.magnolia.ui.api.app.launcherlayout.AppLauncherLayoutManager;
44  import info.magnolia.ui.api.app.launcherlayout.AppLauncherLayoutManagerImpl;
45  import info.magnolia.ui.api.app.launcherlayout.ConfiguredAppLauncherGroupDefinition;
46  import info.magnolia.ui.api.app.launcherlayout.ConfiguredAppLauncherLayoutDefinition;
47  import info.magnolia.ui.api.app.registry.AppDescriptorRegistry;
48  
49  import java.util.Collection;
50  import java.util.stream.Collectors;
51  import java.util.stream.Stream;
52  
53  import javax.inject.Inject;
54  import javax.inject.Named;
55  import javax.inject.Provider;
56  import javax.inject.Singleton;
57  
58  /**
59   * Compatibility implementation of {@link AppLauncherLayoutManager} merging old and new app launcher layout configurations.
60   */
61  @Singleton
62  public class CompatibilityAppLauncherLayoutManager extends AppLauncherLayoutManagerImpl {
63  
64      private final Provider<AdmincentralModule> oldAdmincentralModule;
65      private final Provider<info.magnolia.admincentral.AdmincentralModule> newAdmincentralModule;
66  
67      @Inject
68      public CompatibilityAppLauncherLayoutManager(AppDescriptorRegistry appDescriptorRegistry, @Named(SystemEventBus.NAME) EventBus systemEventBus, Provider<I18nizer> i18nizerProvider, Provider<AdmincentralModule> oldAdmincentralModule, Provider<info.magnolia.admincentral.AdmincentralModule> newAdmincentralModule) {
69          super(appDescriptorRegistry, systemEventBus, i18nizerProvider);
70          this.oldAdmincentralModule = oldAdmincentralModule;
71          this.newAdmincentralModule = newAdmincentralModule;
72      }
73  
74      @Override
75      public void setLayout(AppLauncherLayoutDefinition layout) {
76          super.setLayout(mergeLayouts(
77                  newAdmincentralModule.get().getAppLauncherLayout(),
78                  oldAdmincentralModule.get().getAppLauncherLayout())
79          );
80      }
81  
82      AppLauncherLayoutDefinition mergeLayouts(AppLauncherLayoutDefinition newConfig, AppLauncherLayoutDefinition oldConfig) {
83          final ConfiguredAppLauncherLayoutDefinition mergedConfig = new ConfiguredAppLauncherLayoutDefinition();
84  
85          mergedConfig.setDefaultGroup(newConfig.getDefaultGroup() == null ? oldConfig.getDefaultGroup() : newConfig.getDefaultGroup());
86  
87          mergedConfig.setHiddenApps(Stream.of(newConfig.getHiddenApps(), oldConfig.getHiddenApps())
88                  .flatMap(Collection::stream)
89                  .collect(Collectors.toList())
90          );
91  
92          Stream<AppLauncherGroupDefinition> oldGroupsMergedWithNewGroups = mergeOldWithNewGroups(newConfig, oldConfig);
93          mergedConfig.setGroups(Stream.concat(oldGroupsMergedWithNewGroups, getUniqueGroupsFromNewConfig(newConfig, oldConfig)).collect(Collectors.toList()));
94          return mergedConfig;
95      }
96  
97      private Stream<AppLauncherGroupDefinition> getUniqueGroupsFromNewConfig(AppLauncherLayoutDefinition newConfig, AppLauncherLayoutDefinition oldConfig) {
98          return newConfig.getGroups().stream().filter(appLauncherGroupDefinition -> !getGroupsFromConfig(oldConfig, appLauncherGroupDefinition.getName()).findFirst().isPresent());
99      }
100 
101     private Stream<AppLauncherGroupDefinition> mergeOldWithNewGroups(AppLauncherLayoutDefinition newConfig, AppLauncherLayoutDefinition oldConfig) {
102         return oldConfig.getGroups().stream()
103                 .map(oldLauncherGroupDefinition -> {
104                     final ConfiguredAppLauncherGroupDefinition groupDefinition = new ConfiguredAppLauncherGroupDefinition();
105                     groupDefinition.setName(oldLauncherGroupDefinition.getName());
106                     groupDefinition.setLabel(oldLauncherGroupDefinition.getLabel());
107                     groupDefinition.setPermissions(oldLauncherGroupDefinition.getPermissions());
108                     groupDefinition.setClientGroup(oldLauncherGroupDefinition.isClientGroup());
109                     groupDefinition.setColor(oldLauncherGroupDefinition.getColor());
110                     groupDefinition.setPermanent(oldLauncherGroupDefinition.isPermanent());
111                     groupDefinition.setApps(Stream.concat(
112                             getGroupsFromConfig(newConfig, oldLauncherGroupDefinition.getName()),
113                             oldLauncherGroupDefinition.getApps().stream()
114                     ).collect(Collectors.toList()));
115                     return groupDefinition;
116                 });
117     }
118 
119     private Stream<AppLauncherGroupEntryDefinition> getGroupsFromConfig(AppLauncherLayoutDefinition newConfig, String groupName) {
120         return newConfig.getGroups().stream()
121                 .filter(appLauncherGroupDefinition -> appLauncherGroupDefinition.getName().equals(groupName))
122                 .flatMap(appLauncherGroupDefinition -> appLauncherGroupDefinition.getApps().stream());
123         }
124 }