syncope-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject [3/5] syncope git commit: Fixed SYNCOPE-586
Date Wed, 14 Oct 2015 13:11:04 GMT
Fixed SYNCOPE-586


Project: http://git-wip-us.apache.org/repos/asf/syncope/repo
Commit: http://git-wip-us.apache.org/repos/asf/syncope/commit/1483986e
Tree: http://git-wip-us.apache.org/repos/asf/syncope/tree/1483986e
Diff: http://git-wip-us.apache.org/repos/asf/syncope/diff/1483986e

Branch: refs/heads/master
Commit: 1483986e37ba33d62be19f8fc3370cd5b79a0d34
Parents: 237f90f
Author: massi <massimiliano.perrone@tirasa.net>
Authored: Wed Oct 14 15:10:16 2015 +0200
Committer: massi <massimiliano.perrone@tirasa.net>
Committed: Wed Oct 14 15:10:16 2015 +0200

----------------------------------------------------------------------
 .../client/cli/commands/PolicyCommand.java      | 193 +++++++++++++------
 1 file changed, 134 insertions(+), 59 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/syncope/blob/1483986e/client/cli/src/main/java/org/apache/syncope/client/cli/commands/PolicyCommand.java
----------------------------------------------------------------------
diff --git a/client/cli/src/main/java/org/apache/syncope/client/cli/commands/PolicyCommand.java
b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/PolicyCommand.java
index ae34f53..d17419f 100644
--- a/client/cli/src/main/java/org/apache/syncope/client/cli/commands/PolicyCommand.java
+++ b/client/cli/src/main/java/org/apache/syncope/client/cli/commands/PolicyCommand.java
@@ -18,10 +18,12 @@
  */
 package org.apache.syncope.client.cli.commands;
 
-import com.beust.jcommander.Parameter;
-import com.beust.jcommander.Parameters;
+import javax.xml.ws.WebServiceException;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.syncope.client.cli.Command;
+import org.apache.syncope.client.cli.Input;
 import org.apache.syncope.client.cli.SyncopeServices;
+import org.apache.syncope.client.cli.messages.UsageMessages;
 import org.apache.syncope.common.lib.SyncopeClientException;
 import org.apache.syncope.common.lib.policy.AbstractPolicyTO;
 import org.apache.syncope.common.lib.types.PolicyType;
@@ -29,76 +31,149 @@ import org.apache.syncope.common.rest.api.service.PolicyService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-@Parameters(
-        commandNames = "policy",
-        optionPrefixes = "-",
-        separators = "=",
-        commandDescription = "Apache Syncope policy service")
+@Command(name = "policy")
 public class PolicyCommand extends AbstractCommand {
 
     private static final Logger LOG = LoggerFactory.getLogger(PolicyCommand.class);
 
-    private final String helpMessage = "Usage: policy [options]\n"
+    private static final String HELP_MESSAGE = "Usage: policy [options]\n"
             + "  Options:\n"
-            + "    -h, --help \n"
-            + "    -l, --list \n"
-            + "    -ll, --list-policy \n"
-            + "       Syntax: -ll={POLICY-TYPE} \n"
-            + "    -r, --read \n"
-            + "       Syntax: -r={POLICY-ID} \n"
-            + "    -d, --delete \n"
-            + "       Syntax: -d={POLICY-ID}";
+            + "    --help \n"
+            + "    --list-policy \n"
+            + "       Syntax: --list-policy {POLICY-TYPE} \n"
+            + "          Policy type: ACCOUNT / PASSWORD / SYNC / PUSH\n"
+            + "    --read \n"
+            + "       Syntax: --read {POLICY-ID} {POLICY-ID} [...]\n"
+            + "    --delete \n"
+            + "       Syntax: --delete {POLICY-ID} {POLICY-ID} [...]";
 
-    @Parameter(names = { "-ll", "--list-policy" })
-    private String policyType;
+    @Override
+    public void execute(final Input input) {
+        LOG.debug("Option: {}", input.getOption());
+        LOG.debug("Parameters:");
+        for (final String parameter : input.getParameters()) {
+            LOG.debug("   > " + parameter);
+        }
 
-    @Parameter(names = { "-r", "--read" })
-    private Long policyIdToRead = -1L;
+        final String[] parameters = input.getParameters();
 
-    @Parameter(names = { "-d", "--delete" })
-    private Long policyIdToDelete = -1L;
+        if (StringUtils.isBlank(input.getOption())) {
+            input.setOption(Options.HELP.getOptionName());
+        }
 
-    @Override
-    public void execute() {
         final PolicyService policyService = SyncopeServices.get(PolicyService.class);
-        LOG.debug("Policy service successfully created");
-
-        if (help) {
-            LOG.debug("- policy help command");
-            System.out.println(helpMessage);
-        } else if (list) {
-            // do nothing for the moment...
-        } else if (StringUtils.isNotBlank(policyType)) {
-            LOG.debug("- policy list command for type {}", policyType);
-            try {
-                for (final AbstractPolicyTO policyTO : policyService.list(PolicyType.valueOf(policyType)))
{
-                    System.out.println(policyTO);
+        switch (Options.fromName(input.getOption())) {
+            case LIST_POLICY:
+                final String listPolicyErrorMessage = UsageMessages.optionCommandMessage(
+                        "Usage: policy --list-policy {POLICY-TYPE}\n"
+                        + "   Policy type: ACCOUNT / PASSWORD / SYNC / PUSH");
+                if (parameters.length == 1) {
+                    try {
+                        for (final AbstractPolicyTO policyTO : policyService.list(PolicyType.valueOf(parameters[0])))
{
+                            System.out.println(policyTO);
+                        }
+                    } catch (final SyncopeClientException ex) {
+                        UsageMessages.printErrorMessage(ex.getMessage());
+                    } catch (final IllegalArgumentException ex) {
+                        UsageMessages.printErrorMessage(
+                                "Error: " + parameters[0] + " isn't a valid policy type,
try with:");
+                        for (final PolicyType type : PolicyType.values()) {
+                            System.out.println("  *** " + type.name());
+                        }
+                        System.out.println("");
+                    }
+                } else {
+                    System.out.println(listPolicyErrorMessage);
                 }
-            } catch (final SyncopeClientException ex) {
-                System.out.println(" - Error: " + ex.getMessage());
-            } catch (final IllegalArgumentException ex) {
-                System.out.println(" - Error: " + policyType + " isn't a valid policy type,
try with:");
-                for (final PolicyType type : PolicyType.values()) {
-                    System.out.println("  *** " + type.name());
+                break;
+            case READ:
+                final String readErrorMessage = UsageMessages.optionCommandMessage(
+                        "Usage: policy --read {POLICY-ID} {POLICY-ID} [...]");
+                if (parameters.length >= 1) {
+                    for (final String parameter : parameters) {
+                        try {
+                            System.out.println(policyService.read(Long.valueOf(parameter)));
+                        } catch (final NumberFormatException ex) {
+                            UsageMessages.printErrorMessage(
+                                    "Error reading " + parameter + ". It isn't a valid policy
id");
+                        } catch (final WebServiceException | SyncopeClientException ex) {
+                            if (ex.getMessage().startsWith("NotFound")) {
+                                UsageMessages.printErrorMessage("Policy " + parameter + "
doesn't exists!");
+                            } else {
+                                UsageMessages.printErrorMessage(ex.getMessage());
+                            }
+                        }
+                    }
+                } else {
+                    System.out.println(readErrorMessage);
+                }
+                break;
+            case DELETE:
+                final String deleteErrorMessage = UsageMessages.optionCommandMessage(
+                        "Usage: policy --delete {POLICY-ID} {POLICY-ID} [...]");
+
+                if (parameters.length >= 1) {
+                    for (final String parameter : parameters) {
+                        try {
+                            policyService.delete(Long.valueOf(parameter));
+                            System.out.println(" - Policy " + parameter + " deleted!");
+                        } catch (final WebServiceException | SyncopeClientException ex) {
+                            System.out.println("Error:");
+                            if (ex.getMessage().startsWith("NotFound")) {
+                                UsageMessages.printErrorMessage("Policy " + parameter + "
doesn't exists!");
+                            } else if (ex.getMessage().startsWith("DataIntegrityViolation"))
{
+                                UsageMessages.printErrorMessage("You cannot delete policy
" + parameter);
+                            } else {
+                                UsageMessages.printErrorMessage(ex.getMessage());
+                            }
+                        } catch (final NumberFormatException ex) {
+                            UsageMessages.printErrorMessage(
+                                    "Error reading " + parameter + ". It isn't a valid policy
id");
+                        }
+                    }
+                } else {
+                    System.out.println(deleteErrorMessage);
+                }
+                break;
+            case HELP:
+                System.out.println(HELP_MESSAGE);
+                break;
+            default:
+                System.out.println(input.getOption() + " is not a valid option.");
+                System.out.println("");
+                System.out.println(HELP_MESSAGE);
+        }
+    }
+
+    private enum Options {
+
+        HELP("--help"),
+        LIST_POLICY("--list-policy"),
+        READ("--read"),
+        DELETE("--delete");
+
+        private final String optionName;
+
+        private Options(final String optionName) {
+            this.optionName = optionName;
+        }
+
+        public String getOptionName() {
+            return optionName;
+        }
+
+        public boolean equalsOptionName(final String otherName) {
+            return (otherName == null) ? false : optionName.equals(otherName);
+        }
+
+        public static Options fromName(final String name) {
+            Options optionToReturn = HELP;
+            for (final Options option : Options.values()) {
+                if (option.equalsOptionName(name)) {
+                    optionToReturn = option;
                 }
             }
-        } else if (policyIdToRead > -1L) {
-            LOG.debug("- policy read {} command", policyIdToRead);
-            try {
-                System.out.println(policyService.read(policyIdToRead));
-            } catch (final SyncopeClientException ex) {
-                System.out.println(" - Error: " + ex.getMessage());
-            }
-        } else if (policyIdToDelete > -1L) {
-            try {
-                LOG.debug("- policy delete {} command", policyIdToDelete);
-                policyService.delete(policyIdToDelete);
-                System.out.println(" - Report " + policyIdToDelete + " deleted!");
-            } catch (final SyncopeClientException ex) {
-                System.out.println(" - Error: " + ex.getMessage());
-            }
-        } else {
-            System.out.println(helpMessage);
+            return optionToReturn;
         }
     }
 


Mime
View raw message