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.annotation.deprecation.MgnlDeprecated;
37  
38  import info.magnolia.i18nsystem.SimpleTranslator;
39  import info.magnolia.objectfactory.Components;
40  import info.magnolia.ui.contentapp.browser.BrowserSubAppDescriptor;
41  import info.magnolia.ui.contentapp.configuration.ContentViewDefinition;
42  import info.magnolia.ui.contentapp.configuration.TreeViewDefinition;
43  import info.magnolia.ui.contentapp.configuration.WorkbenchDefinition;
44  import info.magnolia.ui.contentapp.configuration.column.ColumnDefinition;
45  import info.magnolia.ui.contentapp.configuration.column.ConfiguredColumnDefinition;
46  import info.magnolia.ui.contentapp.preview.JcrPreviewDefinition;
47  import info.magnolia.ui.datasource.jcr.JcrDatasourceDefinition;
48  import info.magnolia.ui.chooser.definition.AppAwareWorkbenchChooserDefinition;
49  import info.magnolia.ui.datasource.DatasourceDefinition;
50  import info.magnolia.ui.field.TextFieldDefinition;
51  import info.magnolia.ui.vaadin.integration.contentconnector.ConfiguredJcrContentConnectorDefinition;
52  import info.magnolia.ui.vaadin.integration.contentconnector.NodeTypeDefinition;
53  import info.magnolia.ui.workbench.tree.TreePresenterDefinition;
54  
55  import java.util.ArrayList;
56  import java.util.Collections;
57  import java.util.List;
58  import java.util.Optional;
59  import java.util.stream.Collectors;
60  
61  import org.apache.commons.lang3.StringUtils;
62  
63  import com.machinezoo.noexception.Exceptions;
64  
65  /**
66   * Compatibility chooser definition which can auto-resolve datasource from old apps.
67   *
68   * @param <T> type of items to be chosen
69   * @deprecated since 6.2.3. Just for compatibility reasons.
70   */
71  @Deprecated
72  @MgnlDeprecated(since = "6.2.3.", description = "Just for compatibility reasons.")
73  public class CompatibilityAppAwareWorkbenchChooserDefinition<T> extends AppAwareWorkbenchChooserDefinition<T> {
74  
75      private DatasourceDefinition datasource;
76  
77      @Override
78      public DatasourceDefinition getDatasource() {
79          if (datasource == null) {
80              try {
81                  datasource = super.getDatasource();
82              } catch (IllegalStateException e) {
83                  // App is most likely from old framework
84                  datasource = resolveDatasourceFromAppName();
85              }
86          }
87          return datasource;
88      }
89  
90      @Override
91      public WorkbenchDefinition<T> getWorkbench() {
92          try {
93              return super.getWorkbench();
94          } catch (IllegalStateException e) {
95              // App is most likely from old framework
96              return resolveWorkbenchDefinitionFromAppName();
97          }
98      }
99  
100     private WorkbenchDefinition<T> resolveWorkbenchDefinitionFromAppName() {
101         Optional<List<ColumnDefinition<T>>> columns = resolveBrowserDescriptorFromAppName()
102                 .map(BrowserSubAppDescriptor::getWorkbench)
103                 .map(workbench -> workbench.getContentViews()
104                         .stream()
105                         .filter(TreePresenterDefinition.class::isInstance)
106                         .map(TreePresenterDefinition.class::cast)
107                         .findFirst())
108                 .map(treePresenterDefinition -> treePresenterDefinition
109                         .map(TreePresenterDefinition::getColumns)
110                         .get().stream()
111                         .filter(info.magnolia.ui.workbench.column.definition.ColumnDefinition::isDisplayInChooseDialog)
112                         .filter(columnDefinition -> !"path".equals(columnDefinition.getName()))
113                         .map(oldColumnDefinition -> {
114                             final ConfiguredColumnDefinition<T> column;
115                             if (oldColumnDefinition.getClass().getName().equals("info.magnolia.dam.app.assets.column.AssetNameColumnDefinition")) {
116                                 column = ((ConfiguredColumnDefinition) Exceptions.wrap().get(() -> Class.forName("info.magnolia.dam.app.contentview.column.JcrAssetNameColumnDefinition").newInstance()));
117                             } else {
118                                 column = new ConfiguredColumnDefinition<>();
119                             }
120                             column.setName(oldColumnDefinition.getPropertyName());
121                             column.setLabel(StringUtils.defaultString(oldColumnDefinition.getLabel(), oldColumnDefinition.getName()));
122                             addFiltering(column);
123                             return column;
124                         })
125                         .collect(Collectors.toList()));
126 
127         TreeViewDefinition<T> treeDefinition = new TreeViewDefinition<>();
128         treeDefinition.setColumns(columns.orElse(Collections.emptyList()));
129         treeDefinition.setReadOnly(true);
130 
131         List<ContentViewDefinition<T>> contentViews = new ArrayList<>();
132         contentViews.add(treeDefinition);
133 
134         WorkbenchDefinition<T> definition = new WorkbenchDefinition<>();
135         definition.setContentViews(contentViews);
136         return definition;
137     }
138 
139     private void addFiltering(ConfiguredColumnDefinition<T> column) {
140         final TextFieldDefinition textFieldDefinition = new TextFieldDefinition();
141         textFieldDefinition.setPlaceholder(Components.getComponent(SimpleTranslator.class).translate("fields.filterComponent.placeholder"));
142         column.setFilterComponent(textFieldDefinition);
143     }
144 
145     private JcrDatasourceDefinition resolveDatasourceFromAppName() {
146         return resolveBrowserDescriptorFromAppName()
147                 .map(BrowserSubAppDescriptor::getContentConnector)
148                 .filter(ConfiguredJcrContentConnectorDefinition.class::isInstance)
149                 .map(ConfiguredJcrContentConnectorDefinition.class::cast)
150                 .map(contentConnectorDefinition -> {
151                     JcrDatasourceDefinition jcrDatasourceDefinition = new JcrDatasourceDefinition();
152                     jcrDatasourceDefinition.setWorkspace(contentConnectorDefinition.getWorkspace());
153                     jcrDatasourceDefinition.setRootPath(contentConnectorDefinition.getRootPath());
154                     jcrDatasourceDefinition.setAllowedNodeTypes(contentConnectorDefinition.getNodeTypes().stream()
155                             .map(NodeTypeDefinition::getName)
156                             .collect(Collectors.toSet()));
157                     jcrDatasourceDefinition.setIncludeProperties(contentConnectorDefinition.isIncludeProperties());
158                     jcrDatasourceDefinition.setPreview(new JcrPreviewDefinition());
159                     return jcrDatasourceDefinition;
160                 })
161                 .orElse(null);
162     }
163 
164     private Optional<BrowserSubAppDescriptor> resolveBrowserDescriptorFromAppName() {
165         return Optional.ofNullable(getAppName())
166                 .map(getAppDescriptorRegistry()::getProvider)
167                 .map(provider -> provider.get().getSubApps().values()
168                         .stream()
169                         .filter(BrowserSubAppDescriptor .class::isInstance)
170                         .map(BrowserSubAppDescriptor .class::cast)
171                         .findFirst()
172                         .orElseThrow(() -> new IllegalStateException("Could not resolve browser descriptor from app " + getAppName())));
173     }
174 }