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.admincentral;
35  
36  import info.magnolia.config.source.ConfigurationSourceFactory;
37  import info.magnolia.jcr.predicate.AbstractPredicate;
38  import info.magnolia.jcr.wrapper.ExtendingNodeWrapper;
39  import info.magnolia.module.ModuleLifecycle;
40  import info.magnolia.module.ModuleLifecycleContext;
41  import info.magnolia.ui.admincentral.shellapp.pulse.PulseDefinition;
42  import info.magnolia.ui.admincentral.shellapp.pulse.item.registry.ItemViewDefinitionRegistry;
43  import info.magnolia.ui.admincentral.usermenu.definition.UserMenuDefinition;
44  import info.magnolia.ui.api.app.launcherlayout.AppLauncherLayoutDefinition;
45  import info.magnolia.ui.api.app.launcherlayout.AppLauncherLayoutManager;
46  import info.magnolia.ui.dialog.definition.ConfiguredFormDialogDefinition;
47  
48  import javax.inject.Inject;
49  import javax.jcr.Node;
50  import javax.jcr.RepositoryException;
51  
52  /**
53   * Binds the {@link ItemViewDefinitionRegistry} to the configuration sources (JCR and YAML).
54   * Initializes app launcher layout.
55   */
56  public class AdmincentralModule implements ModuleLifecycle {
57  
58      private UserMenuDefinition userControl;
59  
60      private ItemViewDefinitionRegistry itemViewDefinitionRegistry;
61  
62      private AppLauncherLayoutManager appLauncherLayoutManager;
63      private ConfigurationSourceFactory configurationSourceFactory;
64      private AppLauncherLayoutDefinition appLauncherLayout;
65      private PulseDefinition pulse;
66  
67      @Inject
68      public AdmincentralModule(ItemViewDefinitionRegistry itemViewDefinitionRegistry, AppLauncherLayoutManager appLauncherLayoutManager,
69              ConfigurationSourceFactory configurationSourceFactory) {
70          this.itemViewDefinitionRegistry = itemViewDefinitionRegistry;
71          this.appLauncherLayoutManager = appLauncherLayoutManager;
72          this.configurationSourceFactory = configurationSourceFactory;
73      }
74  
75      @Override
76      public void start(ModuleLifecycleContext context) {
77          if (context.getPhase() == ModuleLifecycleContext.PHASE_SYSTEM_STARTUP) {
78              configurationSourceFactory.jcr().withFilter(new IsViewType()).withModulePath("messageViews").bindTo(itemViewDefinitionRegistry);
79              configurationSourceFactory.yaml().bindWithDefaults(itemViewDefinitionRegistry);
80          }
81          appLauncherLayoutManager.setLayout(getAppLauncherLayout());
82      }
83  
84      /**
85       * For cglib (when module is decorated).
86       */
87      public AdmincentralModule() {
88      }
89  
90      @Override
91      public void stop(ModuleLifecycleContext context) {
92      }
93  
94      public UserMenuDefinition getUserMenu() {
95          return userControl;
96      }
97  
98      public void setUserMenu(UserMenuDefinition userControl) {
99          this.userControl = userControl;
100     }
101 
102     public AppLauncherLayoutDefinition getAppLauncherLayout() {
103         return appLauncherLayout;
104     }
105 
106     public void setAppLauncherLayout(AppLauncherLayoutDefinition appLauncherLayout) {
107         this.appLauncherLayout = appLauncherLayout;
108     }
109 
110     public PulseDefinition getPulse() {
111         return pulse;
112     }
113 
114     public void setPulse(PulseDefinition pulse) {
115         this.pulse = pulse;
116     }
117 
118     /**
119      * Evaluates if the considered node can be treated as a {@link info.magnolia.ui.admincentral.shellapp.pulse.item.definition.ItemViewDefinition}.
120      */
121     private class IsViewType extends AbstractPredicate<Node> {
122 
123         public static final String MESSAGE_VIEW_CONFIG_NODE_NAME = "messageViews";
124 
125         @Override
126         public boolean evaluateTyped(Node node) {
127             try {
128                 if (node.hasProperty(ConfiguredFormDialogDefinition.EXTEND_PROPERTY_NAME)) {
129                     node = new ExtendingNodeWrapper(node);
130                 }
131                 return MESSAGE_VIEW_CONFIG_NODE_NAME.equals(node.getParent().getName());
132             } catch (RepositoryException e) {
133                 throw new RuntimeException(e);
134             }
135         }
136     }
137 }