]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui.workbench/src/org/argeo/cms/ui/workbench/internal/jcr/commands/UploadFiles.java
Fix char array comparison
[lgpl/argeo-commons.git] / org.argeo.cms.ui.workbench / src / org / argeo / cms / ui / workbench / internal / jcr / commands / UploadFiles.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.cms.ui.workbench.internal.jcr.commands;
17
18 import java.io.InputStream;
19 import java.lang.reflect.Method;
20 import java.net.URI;
21 import java.nio.file.Files;
22 import java.nio.file.Path;
23 import java.nio.file.Paths;
24
25 import javax.jcr.Binary;
26 import javax.jcr.Node;
27 import javax.jcr.Property;
28 import javax.jcr.nodetype.NodeType;
29
30 import org.argeo.cms.ui.workbench.WorkbenchUiPlugin;
31 import org.argeo.cms.ui.workbench.internal.jcr.model.SingleJcrNodeElem;
32 import org.argeo.cms.ui.workbench.internal.jcr.model.WorkspaceElem;
33 import org.argeo.cms.ui.workbench.jcr.JcrBrowserView;
34 import org.argeo.eclipse.ui.EclipseUiUtils;
35 import org.argeo.eclipse.ui.TreeParent;
36 import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
37 import org.eclipse.core.commands.AbstractHandler;
38 import org.eclipse.core.commands.ExecutionEvent;
39 import org.eclipse.core.commands.ExecutionException;
40 import org.eclipse.jface.dialogs.MessageDialog;
41 import org.eclipse.jface.viewers.ISelection;
42 import org.eclipse.jface.viewers.IStructuredSelection;
43 import org.eclipse.swt.SWT;
44 import org.eclipse.swt.widgets.FileDialog;
45 import org.eclipse.ui.handlers.HandlerUtil;
46
47 /** Upload local file(s) under the currently selected node */
48 public class UploadFiles extends AbstractHandler {
49 // private final static Log log = LogFactory.getLog(ImportFileSystem.class);
50
51 public Object execute(ExecutionEvent event) throws ExecutionException {
52
53 ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getSelection();
54 JcrBrowserView view = (JcrBrowserView) HandlerUtil.getActiveWorkbenchWindow(event).getActivePage()
55 .findView(HandlerUtil.getActivePartId(event));
56 if (selection != null && !selection.isEmpty() && selection instanceof IStructuredSelection) {
57 Object obj = ((IStructuredSelection) selection).getFirstElement();
58 try {
59 Node folder = null;
60 if (obj instanceof SingleJcrNodeElem) {
61 folder = ((SingleJcrNodeElem) obj).getNode();
62 } else if (obj instanceof WorkspaceElem) {
63 folder = ((WorkspaceElem) obj).getRootNode();
64 } else {
65 ErrorFeedback.show(WorkbenchUiPlugin.getMessage("warningInvalidNodeToImport"));
66 }
67 if (folder != null) {
68 FileDialog dialog = new FileDialog(HandlerUtil.getActiveShell(event), SWT.MULTI);
69 dialog.setText("Choose one or more files to upload");
70
71 if (EclipseUiUtils.notEmpty(dialog.open())) {
72 String[] names = dialog.getFileNames();
73 // Workaround small differences between RAP and RCP
74 // 1. returned names are absolute path on RAP and
75 // relative in RCP
76 // 2. in RCP we must use getFilterPath that does not
77 // exists on RAP
78 Method filterMethod = null;
79 Path parPath = null;
80
81 try {
82 filterMethod = dialog.getClass().getDeclaredMethod("getFilterPath");
83 String filterPath = (String) filterMethod.invoke(dialog);
84 parPath = Paths.get(filterPath);
85 } catch (NoSuchMethodException nsme) { // RAP
86 }
87 if (names.length == 0)
88 return null;
89 else {
90 loop: for (String name : names) {
91 Path path = Paths.get(name);
92 if (parPath != null)
93 path = parPath.resolve(path);
94 if (Files.exists(path)) {
95 URI uri = path.toUri();
96 String uriStr = uri.toString();
97 System.out.println(uriStr);
98
99 if (Files.isDirectory(path)) {
100 MessageDialog.openError(HandlerUtil.getActiveShell(event),
101 "Unimplemented directory import",
102 "Upload of directories in the system is not yet implemented");
103 continue loop;
104 }
105 Node fileNode = folder.addNode(path.getFileName().toString(), NodeType.NT_FILE);
106 Node resNode = fileNode.addNode(Property.JCR_CONTENT, NodeType.NT_RESOURCE);
107 Binary binary = null;
108 try (InputStream is = Files.newInputStream(path)) {
109 binary = folder.getSession().getValueFactory().createBinary(is);
110 resNode.setProperty(Property.JCR_DATA, binary);
111 }
112 folder.getSession().save();
113 } else {
114 String msg = "Cannot upload file at " + path.toString();
115 if (parPath != null)
116 msg += "\nPlease remember that file upload fails when choosing files from the \"Recently Used\" bookmarks on some OS";
117 MessageDialog.openError(HandlerUtil.getActiveShell(event), "Missing file", msg);
118 continue loop;
119 }
120 }
121 view.nodeAdded((TreeParent) obj);
122 return true;
123 }
124 }
125 }
126 } catch (Exception e) {
127 ErrorFeedback.show("Cannot import files to " + obj, e);
128 }
129 }
130 return null;
131 }
132 }