View Javadoc
1   /**
2    * This file Copyright (c) 2016-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.ioc;
35  
36  import static java.util.Comparator.naturalOrder;
37  
38  import java.lang.annotation.Annotation;
39  import java.util.List;
40  import java.util.Objects;
41  import java.util.Optional;
42  
43  import org.apache.commons.lang3.StringUtils;
44  import org.slf4j.Logger;
45  import org.slf4j.LoggerFactory;
46  
47  /**
48   * Utility class which is capable of converting string identifiers
49   * into {@link com.google.inject.BindingAnnotation
50   * Magnolia UI binding annotations}.
51   *
52   * Useful when preparing the Guice bindings or looking up annotated types.
53   *
54   * @see UiBaseModule
55   * @see UiComponentConfigurationAggregator
56   * @see UiContextReference
57   */
58  public class UiAnnotations {
59  
60      private static final Logger log = LoggerFactory.getLogger(UiAnnotations.class);
61  
62      private UiAnnotations() {
63      }
64  
65      public static UiContextAnnotation forAdmincentral() {
66          return new AdmincentralImpl();
67      }
68  
69      public static UiContextAnnotation forM5Admincentral() {
70          return new AdmincentralImpl("m5");
71      }
72  
73      public static UiContextAnnotation forSubApp(String appName, String subAppName) {
74          return new SubAppImpl(appName, subAppName);
75      }
76  
77      public static UiContextAnnotation forSubApps() {
78          return new SubAppImpl("", "");
79      }
80  
81      public static UiContextAnnotation forApp(String appName) {
82          return new AppImpl(appName);
83      }
84  
85      public static UiContextAnnotation forApps() {
86          return new AppImpl("");
87      }
88  
89      public static UiContextAnnotation forView(String viewName) {
90          return new ViewImpl(viewName);
91      }
92  
93      public static UiContextAnnotation forViews() {
94          return new ViewImpl();
95      }
96  
97      static UiContextAnnotation cast(Annotation annotation) {
98          if (annotation instanceof UiContextAnnotation) {
99              return (UiContextAnnotation) annotation;
100         }
101 
102         throw new IllegalArgumentException("Provided annotation [" + annotation + "] is not an instance of UI context annotation");
103     }
104 
105     static Annotation parseAnnotation(String id, List<String> appNames) {
106         switch (id) {
107         case "admincentral":
108             return new AdmincentralImpl();
109         case "admincentral-m5":
110             return new AdmincentralImpl("m5");
111         case "app":
112             return new AppImpl();
113         case "subapp":
114             return new SubAppImpl();
115         case "choosedialog":
116             return new ViewImpl("choosedialog");
117         case "mediaeditor":
118             return new ViewImpl("mediaeditor");
119         default:
120             final String appWithSubApp = StringUtils.removeStart(id, "app-");
121             // Find the app name that matches the components id most (longest app name that is a prefix of the id string)
122             final Optional<String> correspondingApp = appNames.stream().filter(appWithSubApp::startsWith).max(naturalOrder());
123 
124             // If such app even exists - check if sub-app is also provided and then return the resulting annotation
125             return correspondingApp.map(appName -> {
126                 if (Objects.equals(appName, appWithSubApp)) {
127                     return UiAnnotations.forApp(appName);
128                 }
129 
130                 final String subAppName = StringUtils.removeStart(appWithSubApp, appName + "-");
131                 return UiAnnotations.forSubApp(appName, subAppName);
132             }).orElseGet(() -> {
133                 // If no such app/sub-app is resolved gracefully - there's no such app in the system at all, make a guess, warn and return
134                 // the guessed annotation
135                 final String appName = StringUtils.substringBeforeLast(appWithSubApp, "-");
136                 final String subAppName = StringUtils.substringAfterLast(appWithSubApp, "-");
137                 if (StringUtils.isNotBlank(subAppName)) {
138                     log.warn("Failed to match {} to any app/sub-app, will treat it as sub-app {} of app {}", appWithSubApp, subAppName, appName);
139                     return  UiAnnotations.forSubApp(appName, subAppName);
140                 } else {
141                     log.warn("Failed to match {} to any app/sub-app, will treat it as app {}", appWithSubApp, appName);
142                     return  UiAnnotations.forApp(appName);
143                 }
144             });
145         }
146     }
147 }