PageRenderTime 510ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/rest-api/src/main/java/com/cloudbees/workflow/rest/external/InputParameterDefExt.java

https://gitlab.com/vectorci/pipeline-stage-view-plugin
Java | 92 lines | 53 code | 12 blank | 27 comment | 9 complexity | 2c180d2eb83e5d15a415649a06e789a5 MD5 | raw file
  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2015, CloudBees, Inc.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package com.cloudbees.workflow.rest.external;
  25. import com.fasterxml.jackson.annotation.JsonInclude;
  26. import hudson.model.BooleanParameterDefinition;
  27. import hudson.model.ChoiceParameterDefinition;
  28. import hudson.model.ParameterDefinition;
  29. import hudson.model.PasswordParameterDefinition;
  30. import hudson.model.StringParameterDefinition;
  31. import hudson.model.StringParameterValue;
  32. import javax.annotation.Nonnull;
  33. import java.util.HashMap;
  34. import java.util.Map;
  35. /**
  36. * Input Parameter Definition.
  37. * @author <a href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
  38. */
  39. public class InputParameterDefExt {
  40. private final String type;
  41. private final String name;
  42. @JsonInclude(JsonInclude.Include.NON_NULL)
  43. private final String description;
  44. private final Map<String, Object> definition;
  45. public InputParameterDefExt(@Nonnull ParameterDefinition definition) {
  46. this.type = definition.getType();
  47. this.name = definition.getName();
  48. this.description = definition.getDescription();
  49. this.definition = toDefinitionMap(definition);
  50. }
  51. public String getType() {
  52. return type;
  53. }
  54. public String getName() {
  55. return name;
  56. }
  57. public String getDescription() {
  58. return description;
  59. }
  60. public Map<String, Object> getDefinition() {
  61. return definition;
  62. }
  63. static Map<String, Object> toDefinitionMap(@Nonnull ParameterDefinition definition) {
  64. Map<String, Object> definitionMap = new HashMap<String, Object>();
  65. if (definition instanceof BooleanParameterDefinition) {
  66. definitionMap.put("defaultVal", ((BooleanParameterDefinition) definition).isDefaultValue());
  67. } else if (definition instanceof StringParameterDefinition) {
  68. definitionMap.put("defaultVal", ((StringParameterDefinition) definition).getDefaultValue());
  69. } else if (definition instanceof PasswordParameterDefinition) {
  70. definitionMap.put("defaultVal", ((PasswordParameterDefinition) definition).getDefaultValue());
  71. } else if (definition instanceof ChoiceParameterDefinition) {
  72. StringParameterValue defaultParameterValue = ((ChoiceParameterDefinition) definition).getDefaultParameterValue();
  73. if (defaultParameterValue != null) {
  74. definitionMap.put("defaultVal", defaultParameterValue.getValue());
  75. }
  76. definitionMap.put("choices", ((ChoiceParameterDefinition) definition).getChoices());
  77. }
  78. return definitionMap;
  79. }
  80. }