|
34 | 34 | import java.util.Collections; |
35 | 35 | import java.util.Comparator; |
36 | 36 | import java.util.List; |
| 37 | +import java.util.Map; |
37 | 38 | import java.util.Scanner; |
38 | 39 | import java.util.UUID; |
39 | 40 | import java.util.stream.Collectors; |
|
96 | 97 |
|
97 | 98 | public class SysMLInteractive extends SysMLUtil { |
98 | 99 |
|
| 100 | + public static final String HELP_KEY = "help"; |
| 101 | + public static final String PROJECT_ID_KEY = "id"; |
| 102 | + public static final String PROJECT_NAME_KEY = "name"; |
| 103 | + public static final String BRANCH_ID_KEY = "branch-id"; |
| 104 | + public static final String BRANCH_NAME_KEY = "branch"; |
| 105 | + |
99 | 106 | public static final String KERNEL_LIBRARIES_DIRECTORY = "Kernel Libraries"; |
100 | 107 | public static final String SYSTEMS_LIBRARY_DIRECTORY = "Systems Library"; |
101 | 108 | public static final String DOMAIN_LIBRARIES_DIRECTORY = "Domain Libraries"; |
@@ -438,61 +445,74 @@ protected String publish(String name) { |
438 | 445 | publish(name, null, null, false, Collections.emptyList()); |
439 | 446 | } |
440 | 447 |
|
441 | | - public String loadByName(String projectName, String branchName, List<String> help) { |
442 | | - if (Strings.isNullOrEmpty(projectName)) { |
443 | | - return help.isEmpty()? "": SysMLInteractiveHelp.getLoadHelp(); |
444 | | - } |
| 448 | + /** |
| 449 | + * Loads using the supplied parameters<br> |
| 450 | + * Possible parameters are: |
| 451 | + * <li> {@value SysMLInteractive#PROJECT_NAME_KEY}: name of the project</li> |
| 452 | + * <li> {@value SysMLInteractive#PROJECT_ID_KEY}: id of the project</li> |
| 453 | + * <li> {@value SysMLInteractive#BRANCH_NAME_KEY}: name of the project's branch</li> |
| 454 | + * <li> {@value SysMLInteractive#BRANCH_ID_KEY}: id of the project's branch </li> |
| 455 | + * <li>{@value SysMLInteractive#HELP_KEY}: request help string for the operation</li> |
| 456 | + * |
| 457 | + * <br> |
| 458 | + * {@value SysMLInteractive#PROJECT_NAME_KEY} and {@value SysMLInteractive#PROJECT_ID_KEY} are mutually exclusive. It is mandatory to specify one of them. |
| 459 | + * <br> |
| 460 | + * {@value SysMLInteractive#BRANCH_NAME_KEY} and {@value SysMLInteractive#BRANCH_ID_KEY} are mutually exclusive. If neither is given the project's default branch is used. |
| 461 | + * <br> |
| 462 | + * If help {@value SysMLInteractive#HELP_KEY} is passed as a parameter (with any value) the help string is printed. Every other parameters are ignored. |
| 463 | + * |
| 464 | + * <br> |
| 465 | + * <br> |
| 466 | + * @param parameters map containing the parameters and their values |
| 467 | + * @return output of the command |
| 468 | + */ |
| 469 | + public String load(Map<String, String> parameters) { |
445 | 470 |
|
446 | | - ProjectRepository repository = new ProjectRepository(apiBasePath); |
447 | | - RemoteProject repositoryProject = repository.getProjectByName(projectName); |
| 471 | + if (parameters.containsKey(HELP_KEY)) { |
| 472 | + return SysMLInteractiveHelp.getLoadHelp(); |
| 473 | + } |
448 | 474 |
|
449 | | - if (repositoryProject == null) { |
450 | | - return "ERROR:Project doesn't exist."; |
| 475 | + if (parameters.containsKey(PROJECT_ID_KEY) && parameters.containsKey(PROJECT_NAME_KEY)) { |
| 476 | + return "ERROR:Name and id cannot be provided at the same time."; |
451 | 477 | } |
452 | 478 |
|
453 | | - return load(repositoryProject, branchName); |
454 | | - } |
455 | | - |
456 | | - public String loadById(String projectId, String branchId, List<String> help) { |
457 | | - if (Strings.isNullOrEmpty(projectId)) { |
458 | | - return help.isEmpty()? "": SysMLInteractiveHelp.getLoadHelp(); |
| 479 | + if (parameters.containsKey(BRANCH_ID_KEY) && parameters.containsKey(BRANCH_NAME_KEY)) { |
| 480 | + return "ERROR:Branch name and branch id cannot be provided at the same time"; |
459 | 481 | } |
460 | 482 |
|
461 | | - ProjectRepository repository = new ProjectRepository(apiBasePath); |
462 | | - RemoteProject remoteProject = repository.getPRojectById(UUID.fromString(projectId)); |
| 483 | + final ProjectRepository repository = new ProjectRepository(apiBasePath); |
| 484 | + final RemoteProject project; |
463 | 485 |
|
464 | | - if (remoteProject == null) { |
465 | | - return "ERROR:Project doesn't exist."; |
| 486 | + if (parameters.containsKey(PROJECT_ID_KEY)) { |
| 487 | + String projectId = parameters.get(PROJECT_ID_KEY); |
| 488 | + project = repository.getProjectById(projectId); |
| 489 | + } else if (parameters.containsKey(PROJECT_NAME_KEY)) { |
| 490 | + String projectName = parameters.get(PROJECT_NAME_KEY); |
| 491 | + project = repository.getProjectByName(projectName); |
| 492 | + } else { |
| 493 | + return SysMLInteractiveHelp.getLoadHelp(); |
466 | 494 | } |
467 | 495 |
|
468 | | - if (branchId == null) { |
469 | | - return load(remoteProject, (UUID) null); |
470 | | - } else { |
471 | | - UUID branchUUID = UUID.fromString(branchId); |
472 | | - return load(remoteProject, branchUUID); |
| 496 | + if (project == null) { |
| 497 | + return "ERROR:Project doesn't exist."; |
473 | 498 | } |
474 | | - } |
475 | | - |
476 | | - private String load(RemoteProject remoteProject, String branchName) { |
| 499 | + |
477 | 500 | final RemoteBranch branch; |
478 | | - if (branchName == null) { |
479 | | - branch = remoteProject.getDefaultBranch(); |
| 501 | + |
| 502 | + if (parameters.containsKey(BRANCH_NAME_KEY)) { |
| 503 | + String branchName = parameters.get(BRANCH_NAME_KEY); |
| 504 | + branch = project.getBranch(branchName); |
| 505 | + } else if (parameters.containsKey(BRANCH_ID_KEY)) { |
| 506 | + String branchIdString = parameters.get(BRANCH_ID_KEY); |
| 507 | + UUID branchId = UUID.fromString(branchIdString); |
| 508 | + branch = project.getBranch(branchId); |
480 | 509 | } else { |
481 | | - branch = remoteProject.getBranch(branchName); |
| 510 | + branch = project.getDefaultBranch(); |
482 | 511 | } |
| 512 | + |
483 | 513 | return load(branch); |
484 | 514 | } |
485 | 515 |
|
486 | | - private String load(RemoteProject remoteProject, UUID branchId) { |
487 | | - final RemoteBranch branch; |
488 | | - if (branchId == null) { |
489 | | - branch = remoteProject.getDefaultBranch(); |
490 | | - } else { |
491 | | - branch = remoteProject.getBranch(branchId); |
492 | | - } |
493 | | - return load(branch); |
494 | | - } |
495 | | - |
496 | 516 | private String load(RemoteBranch branch) { |
497 | 517 | if (branch == null) { |
498 | 518 | return "ERROR:Branch doesn't exist"; |
@@ -538,8 +558,8 @@ private String load(RemoteBranch branch) { |
538 | 558 |
|
539 | 559 | protected String download(String name) { |
540 | 560 | return "-h".equals(name)? |
541 | | - loadByName(null, null, Collections.singletonList("true")): |
542 | | - loadByName(name, null, Collections.emptyList()); |
| 561 | + load(Map.of(HELP_KEY, "true")): |
| 562 | + load(Map.of(PROJECT_NAME_KEY, name)); |
543 | 563 | } |
544 | 564 |
|
545 | 565 | public String projects(List<String> help) { |
|
0 commit comments