View Javadoc
1   /**
2    * This file Copyright (c) 2021 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.dam.app.action;
35  
36  import info.magnolia.dam.api.Folder;
37  import info.magnolia.dam.api.Item;
38  import info.magnolia.dam.jcr.AssetNodeTypes;
39  import info.magnolia.dam.jcr.DamConstants;
40  import info.magnolia.dam.jcr.JcrFolder;
41  import info.magnolia.i18nsystem.I18nizer;
42  import info.magnolia.jcr.util.NodeNameHelper;
43  import info.magnolia.ui.CloseHandler;
44  import info.magnolia.ui.UIComponent;
45  import info.magnolia.ui.ValueContext;
46  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
47  import info.magnolia.ui.contentapp.JcrPropertySetFactory;
48  import info.magnolia.ui.datasource.DatasourceDefinition;
49  import info.magnolia.ui.datasource.PropertySetFactory;
50  import info.magnolia.ui.datasource.jcr.JcrDatasource;
51  import info.magnolia.ui.datasource.jcr.JcrDatasourceDefinition;
52  import info.magnolia.ui.dialog.DialogDefinitionRegistry;
53  import info.magnolia.ui.dialog.actions.OpenDialogAction;
54  import info.magnolia.ui.editor.LocaleContext;
55  import info.magnolia.ui.observation.DatasourceObservation;
56  import info.magnolia.ui.observation.LocalManualDatasourceObservation;
57  
58  import java.util.Set;
59  import java.util.function.UnaryOperator;
60  import java.util.stream.Collectors;
61  
62  import javax.inject.Inject;
63  
64  import com.google.common.collect.Iterators;
65  import com.machinezoo.noexception.Exceptions;
66  import com.vaadin.ui.Component;
67  import com.vaadin.ui.CssLayout;
68  import com.vaadin.ui.Window;
69  
70  /**
71   * Action for upload and edit from asset chooser.
72   */
73  public class JcrAssetUploadAndEditAction extends OpenDialogAction<Item> {
74  
75      private final Component chooser;
76  
77      @Inject
78      JcrAssetUploadAndEditAction(JcrAssetUploadAndEditActionDefinition definition, LocaleContext localeContext, ValueContext<Item> valueContext, UIComponent parentView,
79                                  I18NAuthoringSupport<Item> i18NAuthoringSupport, DialogDefinitionRegistry dialogDefinitionRegistry, I18nizer i18nizer) {
80          super(definition, localeContext, valueContext, parentView.create(View.class), i18NAuthoringSupport, dialogDefinitionRegistry, i18nizer);
81          chooser = getWindow(parentView.asVaadinComponent());
82          chooser.setVisible(false);
83      }
84  
85      private Component getWindow(Component component) {
86          return component instanceof Window ? component : getWindow(component.getParent());
87      }
88  
89      @Override
90      protected CloseHandler getCloseHandler(Window dialog) {
91          return () -> {
92              super.getCloseHandler(dialog).close();
93              chooser.setVisible(true);
94          };
95      }
96  
97      /**
98       * View used to open upload&edit dialog above chooser.
99       */
100     public static class View extends CssLayout implements UIComponent {
101 
102         @Inject
103         View(ValueContext<Item> chooserValueContext, NodeNameHelper nodeNameHelper) {
104             final JcrDatasourceDefinition jcrDatasourceDefinition = new JcrDatasourceDefinition();
105             jcrDatasourceDefinition.setWorkspace(DamConstants.WORKSPACE);
106             bindInstance(DatasourceDefinition.class, jcrDatasourceDefinition);
107             bindInstance(PropertySetFactory.class, create(JcrPropertySetFactory.class));
108             final JcrDatasource datasource = create(JcrDatasource.class);
109             final DatasourceObservation datasourceObservation = create(LocalManualDatasourceObservation.class);
110             bindInstance(DatasourceObservation.class, datasourceObservation);
111             datasourceObservation.register(() -> chooserValueContext.current().update((UnaryOperator<Set<Item>>) items -> items.stream()
112                     .map(Folder.class::cast)
113                     .map(Folder::getChildren)
114                     .map(Iterators::getLast) //select newly added asset
115                     .collect(Collectors.toSet())
116             ));
117             chooserValueContext.getSingle()
118                     .map(JcrFolder.class::cast)
119                     .map(JcrFolder::getNode)
120                     .map(datasource::wrapNode)
121                     .map(Exceptions.wrap().function(folder -> folder.addNode(nodeNameHelper.getUniqueName(folder, "untitled"), AssetNodeTypes.Asset.NAME)))
122                     .ifPresent(bindContext(ValueContext.class)::set);
123         }
124     }
125 }