1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
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
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 }