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 import com.vaadin.v7.data.Item;
59
60
61
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
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
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 }