View Javadoc
1   /**
2    * This file Copyright (c) 2019 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.form.field.definition.migration;
35  
36  import info.magnolia.ui.contentapp.browser.BrowserSubAppDescriptor;
37  import info.magnolia.ui.contentapp.configuration.ContentViewDefinition;
38  import info.magnolia.ui.contentapp.configuration.TreeViewDefinition;
39  import info.magnolia.ui.contentapp.configuration.WorkbenchDefinition;
40  import info.magnolia.ui.contentapp.configuration.column.ColumnDefinition;
41  import info.magnolia.ui.contentapp.configuration.column.ConfiguredColumnDefinition;
42  import info.magnolia.ui.datasource.jcr.JcrDatasourceDefinition;
43  import info.magnolia.ui.chooser.definition.AppAwareWorkbenchChooserDefinition;
44  import info.magnolia.ui.datasource.DatasourceDefinition;
45  import info.magnolia.ui.vaadin.integration.contentconnector.ConfiguredJcrContentConnectorDefinition;
46  import info.magnolia.ui.vaadin.integration.contentconnector.NodeTypeDefinition;
47  import info.magnolia.ui.workbench.tree.TreePresenterDefinition;
48  
49  import java.util.ArrayList;
50  import java.util.Collections;
51  import java.util.List;
52  import java.util.Optional;
53  import java.util.stream.Collectors;
54  
55  import org.apache.commons.lang3.StringUtils;
56  
57  
58  /**
59   * Compatibility chooser definition which can auto-resolve datasource from old apps.
60   *
61   * @param <T> type of items to be chosen
62   */
63  public class CompatibilityAppAwareWorkbenchChooserDefinition<T> extends AppAwareWorkbenchChooserDefinition<T> {
64  
65      @Override
66      public DatasourceDefinition getDatasource() {
67          try {
68              return super.getDatasource();
69          } catch (IllegalStateException e) {
70              // App is most likely from old framework
71              return resolveDatasourceFromAppName();
72          }
73      }
74  
75      @Override
76      public WorkbenchDefinition<T> getWorkbench() {
77          try {
78              return super.getWorkbench();
79          } catch (IllegalStateException e) {
80              // App is most likely from old framework
81              return resolveWorkbenchDefinitionFromAppName();
82          }
83      }
84  
85      private WorkbenchDefinition<T> resolveWorkbenchDefinitionFromAppName() {
86          Optional<List<ColumnDefinition<T>>> columns = resolveBrowserDescriptorFromAppName()
87                  .map(BrowserSubAppDescriptor::getWorkbench)
88                  .map(workbench -> workbench.getContentViews()
89                          .stream()
90                          .filter(TreePresenterDefinition.class::isInstance)
91                          .map(TreePresenterDefinition.class::cast)
92                          .findFirst())
93                  .map(treePresenterDefinition -> treePresenterDefinition
94                          .map(TreePresenterDefinition::getColumns)
95                          .get().stream()
96                          .filter(info.magnolia.ui.workbench.column.definition.ColumnDefinition::isDisplayInChooseDialog)
97                          .filter(columnDefinition -> !"path".equals(columnDefinition.getName()))
98                          .map(oldColumnDefinition -> {
99                              ConfiguredColumnDefinition<T> column = new ConfiguredColumnDefinition<>();
100                             column.setName(oldColumnDefinition.getPropertyName());
101                             column.setLabel(StringUtils.defaultString(oldColumnDefinition.getLabel(), oldColumnDefinition.getName()));
102                             return column;
103                         })
104                         .collect(Collectors.toList()));
105 
106         TreeViewDefinition<T> treeDefinition = new TreeViewDefinition<>();
107         treeDefinition.setColumns(columns.orElse(Collections.emptyList()));
108 
109         List<ContentViewDefinition<T>> contentViews = new ArrayList<>();
110         contentViews.add(treeDefinition);
111 
112         WorkbenchDefinition<T> definition = new WorkbenchDefinition<>();
113         definition.setContentViews(contentViews);
114         return definition;
115     }
116 
117     private JcrDatasourceDefinition resolveDatasourceFromAppName() {
118         return resolveBrowserDescriptorFromAppName()
119                 .map(BrowserSubAppDescriptor::getContentConnector)
120                 .filter(ConfiguredJcrContentConnectorDefinition.class::isInstance)
121                 .map(ConfiguredJcrContentConnectorDefinition.class::cast)
122                 .map(contentConnectorDefinition -> {
123                     JcrDatasourceDefinition jcrDatasourceDefinition = new JcrDatasourceDefinition();
124                     jcrDatasourceDefinition.setWorkspace(contentConnectorDefinition.getWorkspace());
125                     jcrDatasourceDefinition.setRootPath(contentConnectorDefinition.getRootPath());
126                     jcrDatasourceDefinition.setAllowedNodeTypes(contentConnectorDefinition.getNodeTypes().stream()
127                             .map(NodeTypeDefinition::getName)
128                             .collect(Collectors.toSet()));
129                     jcrDatasourceDefinition.setIncludeProperties(contentConnectorDefinition.isIncludeProperties());
130                     return jcrDatasourceDefinition;
131                 })
132                 .orElse(null);
133     }
134 
135     private Optional<BrowserSubAppDescriptor> resolveBrowserDescriptorFromAppName() {
136         return Optional.ofNullable(getAppName())
137                 .map(getAppDescriptorRegistry()::getProvider)
138                 .map(provider -> provider.get().getSubApps().values()
139                         .stream()
140                         .filter(BrowserSubAppDescriptor .class::isInstance)
141                         .map(BrowserSubAppDescriptor .class::cast)
142                         .findFirst()
143                         .orElseThrow(() -> new IllegalStateException("Could not resolve browser descriptor from app " + getAppName())));
144     }
145 }