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