View Javadoc
1   /**
2    * This file Copyright (c) 2010-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.dam.app.assets.action;
35  
36  import info.magnolia.context.MgnlContext;
37  import info.magnolia.dam.app.DamAppConfiguration;
38  import info.magnolia.dam.app.commands.ImportAssetZipCommand;
39  import info.magnolia.dam.app.ui.field.upload.AssetUploadReceiver;
40  import info.magnolia.i18nsystem.SimpleTranslator;
41  import info.magnolia.ui.api.action.AbstractAction;
42  import info.magnolia.ui.api.action.ActionExecutionException;
43  import info.magnolia.ui.api.app.AppContext;
44  import info.magnolia.ui.api.message.Message;
45  import info.magnolia.ui.api.message.MessageType;
46  import info.magnolia.ui.contentapp.field.WorkbenchField;
47  import info.magnolia.ui.vaadin.integration.jcr.JcrItemId;
48  import info.magnolia.ui.vaadin.integration.jcr.JcrItemUtil;
49  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
50  
51  import javax.inject.Inject;
52  import javax.jcr.Node;
53  import javax.jcr.RepositoryException;
54  
55  import org.slf4j.Logger;
56  import org.slf4j.LoggerFactory;
57  
58  import com.vaadin.v7.data.Item;
59  
60  /**
61   * Adds an asset form the received upload to the workspace.
62   */
63  public class DirectUploadAction extends AbstractAction<DirectUploadActionDefinition> {
64  
65      private Logger log = LoggerFactory.getLogger(getClass());
66  
67      private Item currentSelection;
68  
69      private AssetUploadReceiver receiver;
70  
71      private WorkbenchField field;
72  
73      private AppContext appContext;
74  
75      private final SimpleTranslator i18n;
76  
77      @Inject
78      public DirectUploadAction(
79              DirectUploadActionDefinition definition,
80              Item currentSelection,
81              AssetUploadReceiver receiver,
82              WorkbenchField field,
83              AppContext appContext,SimpleTranslator i18n) {
84          super(definition);
85          this.currentSelection = currentSelection;
86          this.receiver = receiver;
87          this.field = field;
88          this.appContext = appContext;
89          this.i18n = i18n;
90      }
91  
92      /**
93       * @deprecated since 2.0.2
94       */
95      @Deprecated
96      public DirectUploadAction(
97              DirectUploadActionDefinition definition,
98              Item currentSelection,
99              AssetUploadReceiver receiver,
100             WorkbenchField field,
101             DamAppConfiguration damAppConfig,
102             AppContext appContext,SimpleTranslator i18n) {
103         this(definition, currentSelection, receiver, field, appContext, i18n);
104     }
105 
106     @Override
107     public void execute() throws ActionExecutionException {
108         try {
109             Node parentNode;
110             // Get parent (Node and Item)
111             if (currentSelection instanceof JcrNodeAdapter) {
112                 parentNode = ((JcrNodeAdapter)currentSelection).getJcrItem();
113             } else {
114                 Object workbenchRoot = field.getPresenter().resolveWorkbenchRoot();
115                 if (workbenchRoot instanceof JcrItemId) {
116                     JcrItemId jcrItemId = (JcrItemId) workbenchRoot;
117                     parentNode = MgnlContext.getJCRSession(jcrItemId.getWorkspace()).getNodeByIdentifier(jcrItemId.getUuid());
118                 } else if (workbenchRoot instanceof Node) {
119                     parentNode = (Node) workbenchRoot;
120                 } else if (workbenchRoot instanceof JcrNodeAdapter) {
121                     parentNode = ((JcrNodeAdapter)workbenchRoot).getJcrItem();
122                 } else {
123                     parentNode = MgnlContext.getJCRSession(getDefinition().getWorkspaceName()).getRootNode();
124                 }
125             }
126 
127             Node assetNode = ImportAssetZipCommand.createAsset(parentNode, receiver);
128             assetNode.getSession().save();
129 
130             field.getPresenter().refresh();
131             field.getPresenter().select(JcrItemUtil.getItemId(assetNode));
132 
133         } catch (RepositoryException e) {
134             String subject = i18n.translate("dam.assets.directUpload.error");
135             Message error = new Message(MessageType.ERROR, subject, e.getMessage());
136             appContext.sendLocalMessage(error);
137             log.error(subject, e);
138         }
139     }
140 }