AddWorkspace and AddPrivileges in JCR Explorer
authorMathieu Baudier <mbaudier@argeo.org>
Sat, 8 Dec 2012 14:07:52 +0000 (14:07 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Sat, 8 Dec 2012 14:07:52 +0000 (14:07 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@5918 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

server/plugins/org.argeo.jcr.ui.explorer/META-INF/spring/commands.xml
server/plugins/org.argeo.jcr.ui.explorer/icons/addPrivileges.png [new file with mode: 0644]
server/plugins/org.argeo.jcr.ui.explorer/icons/addWorkspace.png [new file with mode: 0644]
server/plugins/org.argeo.jcr.ui.explorer/plugin.xml
server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/commands/AddPrivileges.java [new file with mode: 0644]
server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/commands/CreateWorkspace.java [new file with mode: 0644]
server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/model/RepositoriesNode.java
server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/model/RepositoryNode.java
server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/wizards/ChangeRightsWizard.java [new file with mode: 0644]
server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/wizards/ChooseRightsPage.java [new file with mode: 0644]

index 5e4a185c73e16b84dbd2d5f9dd53e5f49e65ffdd..ef9f3f1d40361698a50d6c08cf0919da4b996d5d 100644 (file)
        <bean id="addFolderNode" class="org.argeo.jcr.ui.explorer.commands.AddFolderNode"
                scope="prototype" />
 
+       <bean id="addPrivileges" class="org.argeo.jcr.ui.explorer.commands.AddPrivileges"
+               scope="prototype" />
+
+       <bean id="createWorkspace" class="org.argeo.jcr.ui.explorer.commands.CreateWorkspace"
+               scope="prototype" />
+
        <bean id="refresh" class="org.argeo.jcr.ui.explorer.commands.Refresh"
                scope="prototype" />
 
diff --git a/server/plugins/org.argeo.jcr.ui.explorer/icons/addPrivileges.png b/server/plugins/org.argeo.jcr.ui.explorer/icons/addPrivileges.png
new file mode 100644 (file)
index 0000000..a6b251f
Binary files /dev/null and b/server/plugins/org.argeo.jcr.ui.explorer/icons/addPrivileges.png differ
diff --git a/server/plugins/org.argeo.jcr.ui.explorer/icons/addWorkspace.png b/server/plugins/org.argeo.jcr.ui.explorer/icons/addWorkspace.png
new file mode 100644 (file)
index 0000000..bbee775
Binary files /dev/null and b/server/plugins/org.argeo.jcr.ui.explorer/icons/addWorkspace.png differ
index 1ab31901f56e25c292206dd7b499507fcff44aee..9428cbe9805fbec27ce8cec8975098ce7d285ce9 100644 (file)
          <command
                defaultHandler="org.argeo.eclipse.spring.SpringCommandHandler"
                id="org.argeo.jcr.ui.explorer.addFolderNode"
-               name="Create the new folder">
+               name="Create a new folder">
+         </command>
+         <command
+               defaultHandler="org.argeo.eclipse.spring.SpringCommandHandler"
+               id="org.argeo.jcr.ui.explorer.addPrivileges"
+               name="Add Privileges">
+         </command>
+         <command
+               defaultHandler="org.argeo.eclipse.spring.SpringCommandHandler"
+               id="org.argeo.jcr.ui.explorer.createWorkspace"
+               name="Create a new workspace">
          </command>
          <command
                defaultHandler="org.argeo.eclipse.spring.SpringCommandHandler"
                                style="push">
                        </command>
                        <command
-         commandId="org.argeo.jcr.ui.explorer.addFolderNode"
-         icon="icons/addFolder.gif"
-         label="Add folder"
-         style="push">
+                        commandId="org.argeo.jcr.ui.explorer.addFolderNode"
+                        icon="icons/addFolder.gif"
+                        label="Add Folder"
+                        style="push">
+                               <visibleWhen>
+                                       <iterate>
+                                     <and>
+                                        <or>
+                                           <instanceof
+                                                 value="org.argeo.jcr.ui.explorer.model.SingleJcrNode">
+                                           </instanceof>
+                                           <instanceof
+                                                 value="org.argeo.jcr.ui.explorer.model.WorkspaceNode">
+                                           </instanceof>
+                                        </or>
+                               <with variable="activeMenuSelection"><count value="1"/></with>
+                                     </and>
+                                       </iterate>
+                               </visibleWhen>
+                       </command>
+                       <command
+                        commandId="org.argeo.jcr.ui.explorer.addPrivileges"
+                        icon="icons/addPrivileges.png"
+                        label="Add Privileges"
+                        style="push">
                                <visibleWhen>
                                        <iterate>
                                      <and>
                                                  value="org.argeo.jcr.ui.explorer.model.WorkspaceNode">
                                            </instanceof>
                                         </or>
-             <reference
-                   definitionId="expression.onlyOneItemSelected">
-             </reference>
+                               <with variable="activeMenuSelection"><count value="1"/></with>
                                      </and>
                                        </iterate>
                                </visibleWhen>
                        </command>
+                       <command
+                        commandId="org.argeo.jcr.ui.explorer.createWorkspace"
+                        icon="icons/addWorkspace.png"
+                        label="Create Workspace"
+                        style="push">
+                       <visibleWhen>
+                               <iterate>
+                             <and>
+                                <or>
+                                   <instanceof
+                                         value="org.argeo.jcr.ui.explorer.model.RepositoryNode">
+                                   </instanceof>
+                                </or>
+                       <with variable="activeMenuSelection"><count value="1"/></with>
+                             </and>
+                               </iterate>
+                       </visibleWhen>
+                       </command>
      <command
            commandId="org.argeo.jcr.ui.explorer.deleteNodes"
            icon="icons/remove.gif"
+                  label="Delete Nodes"
            style="push">
         <visibleWhen>
            <iterate>
            commandId="org.argeo.jcr.ui.explorer.importFileSystem"
            icon="icons/import_fs.png"
            style="push"
-           tooltip="Import files from the files sytem to the repository">
+           tooltip="Import files from the files sytem">
         <visibleWhen>
            <iterate>
               <and>
                           value="org.argeo.jcr.ui.explorer.model.WorkspaceNode">
                     </instanceof>
                  </or>
-                 <with
-                     variable="activeMenuSelection">
-                  <count
-                        value="1">
-                  </count>
-               </with>
+                 <with variable="activeMenuSelection"><count value="1"/></with>
               </and>
            </iterate>
         </visibleWhen>
diff --git a/server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/commands/AddPrivileges.java b/server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/commands/AddPrivileges.java
new file mode 100644 (file)
index 0000000..c0873eb
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * Copyright (C) 2007-2012 Argeo GmbH
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *         http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.argeo.jcr.ui.explorer.commands;
+
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+
+import org.argeo.ArgeoException;
+import org.argeo.eclipse.ui.ErrorFeedback;
+import org.argeo.eclipse.ui.TreeParent;
+import org.argeo.jcr.ui.explorer.JcrExplorerPlugin;
+import org.argeo.jcr.ui.explorer.model.SingleJcrNode;
+import org.argeo.jcr.ui.explorer.model.WorkspaceNode;
+import org.argeo.jcr.ui.explorer.wizards.ChangeRightsWizard;
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.wizard.WizardDialog;
+import org.eclipse.ui.handlers.HandlerUtil;
+
+/**
+ * Open a dialog to change rights on the selected node.
+ */
+
+public class AddPrivileges extends AbstractHandler {
+       public final static String ID = JcrExplorerPlugin.ID + ".addPrivileges";
+
+       public Object execute(ExecutionEvent event) throws ExecutionException {
+
+               ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
+                               .getActivePage().getSelection();
+               if (selection != null && !selection.isEmpty()
+                               && selection instanceof IStructuredSelection) {
+                       Object obj = ((IStructuredSelection) selection).getFirstElement();
+                       TreeParent treeParentNode = null;
+                       Node jcrParentNode = null;
+
+                       if (obj instanceof SingleJcrNode) {
+                               treeParentNode = (TreeParent) obj;
+                               jcrParentNode = ((SingleJcrNode) treeParentNode).getNode();
+                       } else if (obj instanceof WorkspaceNode) {
+                               treeParentNode = (TreeParent) obj;
+                               jcrParentNode = ((WorkspaceNode) treeParentNode).getRootNode();
+                       } else
+                               return null;
+
+                       try {
+                               ChangeRightsWizard wizard = new ChangeRightsWizard(
+                                               jcrParentNode.getSession(), jcrParentNode.getPath());
+                               WizardDialog dialog = new WizardDialog(
+                                               HandlerUtil.getActiveShell(event), wizard);
+                               dialog.open();
+                               return null;
+                       } catch (RepositoryException re) {
+                               throw new ArgeoException(
+                                               "Unexpected error while creating the new workspace.",
+                                               re);
+                       }
+               } else {
+                       ErrorFeedback.show("Cannot add privileges");
+               }
+               return null;
+       }
+}
\ No newline at end of file
diff --git a/server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/commands/CreateWorkspace.java b/server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/commands/CreateWorkspace.java
new file mode 100644 (file)
index 0000000..f567926
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * Copyright (C) 2007-2012 Argeo GmbH
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *         http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.argeo.jcr.ui.explorer.commands;
+
+import java.util.Arrays;
+
+import org.argeo.eclipse.ui.ErrorFeedback;
+import org.argeo.eclipse.ui.dialogs.SingleValue;
+import org.argeo.jcr.ui.explorer.JcrExplorerPlugin;
+import org.argeo.jcr.ui.explorer.model.RepositoryNode;
+import org.argeo.jcr.ui.explorer.views.GenericJcrBrowser;
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.ui.handlers.HandlerUtil;
+
+/** Creates a new JCR workspace */
+public class CreateWorkspace extends AbstractHandler {
+
+       public final static String ID = JcrExplorerPlugin.ID + ".addFolderNode";
+
+       public Object execute(ExecutionEvent event) throws ExecutionException {
+
+               ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
+                               .getActivePage().getSelection();
+
+               GenericJcrBrowser view = (GenericJcrBrowser) HandlerUtil
+                               .getActiveWorkbenchWindow(event).getActivePage()
+                               .findView(HandlerUtil.getActivePartId(event));
+
+               if (selection != null && !selection.isEmpty()
+                               && selection instanceof IStructuredSelection) {
+                       Object obj = ((IStructuredSelection) selection).getFirstElement();
+                       if (!(obj instanceof RepositoryNode))
+                               return null;
+
+                       RepositoryNode repositoryNode = (RepositoryNode) obj;
+                       String workspaceName = SingleValue.ask("Workspace name",
+                                       "Enter workspace name");
+                       if (workspaceName != null) {
+                               if (Arrays.asList(repositoryNode.getAccessibleWorkspaceNames())
+                                               .contains(workspaceName)) {
+                                       ErrorFeedback.show("Workspace " + workspaceName
+                                                       + " already exists.");
+                               } else {
+                                       repositoryNode.createWorkspace(workspaceName);
+                                       view.nodeAdded(repositoryNode);
+                               }
+                       }
+               } else {
+                       ErrorFeedback.show("Cannot create workspace");
+               }
+               return null;
+       }
+}
index 92e9c313cb60c4d9ab55e2b42530baba39e2bb3e..df9d5b94314224ea578e83acdf9d8a6727d5a20a 100644 (file)
@@ -15,7 +15,6 @@
  */
 package org.argeo.jcr.ui.explorer.model;
 
-import java.util.Hashtable;
 import java.util.Map;
 
 import javax.jcr.Node;
@@ -28,7 +27,6 @@ import javax.jcr.Session;
 import org.argeo.ArgeoException;
 import org.argeo.eclipse.ui.ErrorFeedback;
 import org.argeo.eclipse.ui.TreeParent;
-import org.argeo.jcr.ArgeoJcrConstants;
 import org.argeo.jcr.ArgeoNames;
 import org.argeo.jcr.RepositoryRegister;
 import org.argeo.jcr.UserJcrUtils;
index 8adb99ac6908c4967cb3a462a33a2a34c6bdc89c..a8b64afff8a457161d387c50f4b49d9f0ecf981e 100644 (file)
@@ -81,6 +81,16 @@ public class RepositoryNode extends TreeParent implements UiNode {
                }
        }
 
+       public void createWorkspace(String workspaceName) {
+               if (!isConnected())
+                       login();
+               try {
+                       defaultSession.getWorkspace().createWorkspace(workspaceName);
+               } catch (RepositoryException e) {
+                       throw new ArgeoException("Cannot create workspace", e);
+               }
+       }
+
        /** returns the {@link Repository} referenced by the current UI Node */
        public Repository getRepository() {
                return repository;
diff --git a/server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/wizards/ChangeRightsWizard.java b/server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/wizards/ChangeRightsWizard.java
new file mode 100644 (file)
index 0000000..4381837
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2007-2012 Argeo GmbH
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *         http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.argeo.jcr.ui.explorer.wizards;
+
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+import org.argeo.ArgeoException;
+import org.argeo.jcr.JcrUtils;
+import org.eclipse.jface.wizard.Wizard;
+
+/**
+ * Small wizard to manage authorizations on the root node of the current
+ * workspace
+ */
+public class ChangeRightsWizard extends Wizard {
+
+       private Session currentSession;
+       private String path;
+
+       // This page widget
+       private ChooseRightsPage page;
+
+       public ChangeRightsWizard(Session currentSession, String path) {
+               super();
+               this.currentSession = currentSession;
+               this.path = path;
+       }
+
+       @Override
+       public void addPages() {
+               try {
+                       page = new ChooseRightsPage(path);
+                       addPage(page);
+               } catch (Exception e) {
+                       throw new ArgeoException("Cannot add page to wizard ", e);
+               }
+       }
+
+       @Override
+       public boolean performFinish() {
+               if (!canFinish())
+                       return false;
+               try {
+                       JcrUtils.addPrivilege(currentSession, path, page.getGroupName(),
+                                       page.getAuthTypeStr());
+               } catch (RepositoryException re) {
+                       throw new ArgeoException(
+                                       "Unexpected error while setting privileges", re);
+               }
+               return true;
+       }
+}
diff --git a/server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/wizards/ChooseRightsPage.java b/server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/wizards/ChooseRightsPage.java
new file mode 100644 (file)
index 0000000..69948ce
--- /dev/null
@@ -0,0 +1,101 @@
+/*
+ * Copyright (C) 2007-2012 Argeo GmbH
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *         http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.argeo.jcr.ui.explorer.wizards;
+
+import javax.jcr.security.Privilege;
+
+import org.eclipse.jface.wizard.WizardPage;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Combo;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Text;
+
+public class ChooseRightsPage extends WizardPage implements ModifyListener {
+
+       // This page widget
+       private Text groupNameTxt;
+       private Combo authorizationCmb;
+
+       // USABLE SHORTCUTS
+       protected final static String[] validAuthType = { Privilege.JCR_READ,
+                       Privilege.JCR_WRITE, Privilege.JCR_ALL };
+
+       public ChooseRightsPage(String path) {
+               super("Main");
+               setTitle("Add privilege to " + path);
+       }
+
+       public void createControl(Composite parent) {
+               // specify subject
+               Composite composite = new Composite(parent, SWT.NONE);
+               composite.setLayout(new GridLayout(2, false));
+               Label lbl = new Label(composite, SWT.LEAD);
+               lbl.setText("Group or user name (no blank, no special chars)");
+               lbl.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
+               groupNameTxt = new Text(composite, SWT.LEAD | SWT.BORDER);
+               groupNameTxt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
+                               false));
+               if (groupNameTxt != null)
+                       groupNameTxt.addModifyListener(this);
+
+               // Choose rigths
+               new Label(composite, SWT.NONE).setText("Choose corresponding rights");
+               authorizationCmb = new Combo(composite, SWT.BORDER | SWT.V_SCROLL);
+               authorizationCmb.setItems(validAuthType);
+               GridData gd = new GridData(GridData.FILL_HORIZONTAL);
+               authorizationCmb.setLayoutData(gd);
+
+               authorizationCmb.select(0);
+
+               // Compulsory
+               setControl(composite);
+       }
+
+       protected String getGroupName() {
+               return groupNameTxt.getText();
+       }
+
+       protected String getAuthTypeStr() {
+               return authorizationCmb.getItem(authorizationCmb.getSelectionIndex());
+       }
+
+       public void modifyText(ModifyEvent event) {
+               String message = checkComplete();
+               if (message != null)
+                       setMessage(message, WizardPage.ERROR);
+               else {
+                       setMessage("Complete", WizardPage.INFORMATION);
+                       setPageComplete(true);
+               }
+       }
+
+       /** @return error message or null if complete */
+       protected String checkComplete() {
+               String groupStr = groupNameTxt.getText();
+               if (groupStr == null || "".equals(groupStr))
+                       return "Please enter the name of the corresponding group.";
+               // Remove regexp check for the time being.
+               // else if (!match(groupStr))
+               // return
+               // "Please use only alphanumerical chars for the short technical name.";
+               return null;
+       }
+}