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