PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/src/main/java/com/davidehringer/atlassian/bamboo/maven/MavenVariableTaskConfigurator.java

https://bitbucket.org/ccalvo/bamboo-maven-pom-extractor-plugin
Java | 144 lines | 107 code | 19 blank | 18 comment | 5 complexity | 10f6dab75a9360bc2929b374c965dbe0 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Copyright 2012 the original author or authors.
  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 com.davidehringer.atlassian.bamboo.maven;
  17. import static com.davidehringer.atlassian.bamboo.maven.TaskConfiguration.CUSTOM_ELEMENT;
  18. import static com.davidehringer.atlassian.bamboo.maven.TaskConfiguration.CUSTOM_VARIABLE_NAME;
  19. import static com.davidehringer.atlassian.bamboo.maven.TaskConfiguration.EXTRACT_MODE;
  20. import static com.davidehringer.atlassian.bamboo.maven.TaskConfiguration.EXTRACT_MODE_CUSTOM;
  21. import static com.davidehringer.atlassian.bamboo.maven.TaskConfiguration.EXTRACT_MODE_GAV;
  22. import static com.davidehringer.atlassian.bamboo.maven.TaskConfiguration.PREFIX_OPTION;
  23. import static com.davidehringer.atlassian.bamboo.maven.TaskConfiguration.PREFIX_OPTION_CUSTOM;
  24. import static com.davidehringer.atlassian.bamboo.maven.TaskConfiguration.PREFIX_OPTION_CUSTOM_VALUE;
  25. import static com.davidehringer.atlassian.bamboo.maven.TaskConfiguration.PREFIX_OPTION_DEFAULT;
  26. import static com.davidehringer.atlassian.bamboo.maven.TaskConfiguration.PROJECT_FILE;
  27. import static com.davidehringer.atlassian.bamboo.maven.TaskConfiguration.REMOVE_VERSION_SNAPSHOT;
  28. import static com.davidehringer.atlassian.bamboo.maven.TaskConfiguration.VARIABLE_TYPE;
  29. import static com.davidehringer.atlassian.bamboo.maven.TaskConfiguration.VARIABLE_TYPE_JOB;
  30. import static com.davidehringer.atlassian.bamboo.maven.TaskConfiguration.VARIABLE_TYPE_PLAN;
  31. import java.util.List;
  32. import java.util.Map;
  33. import org.apache.commons.lang.StringUtils;
  34. import org.apache.commons.logging.Log;
  35. import org.apache.commons.logging.LogFactory;
  36. import org.jetbrains.annotations.NotNull;
  37. import org.jetbrains.annotations.Nullable;
  38. import com.atlassian.bamboo.collections.ActionParametersMap;
  39. import com.atlassian.bamboo.task.AbstractTaskConfigurator;
  40. import com.atlassian.bamboo.task.TaskConfiguratorHelper;
  41. import com.atlassian.bamboo.task.TaskDefinition;
  42. import com.atlassian.bamboo.utils.error.ErrorCollection;
  43. import com.google.common.collect.ImmutableList;
  44. import com.google.common.collect.Maps;
  45. import com.opensymphony.xwork.TextProvider;
  46. /**
  47. * @author David Ehringer
  48. */
  49. public class MavenVariableTaskConfigurator extends AbstractTaskConfigurator {
  50. private static final Log LOG = LogFactory.getLog(MavenVariableTaskConfigurator.class);
  51. private static final List<String> FIELDS_TO_COPY = ImmutableList.of(PROJECT_FILE, EXTRACT_MODE, VARIABLE_TYPE,
  52. PREFIX_OPTION, PREFIX_OPTION_CUSTOM_VALUE, CUSTOM_VARIABLE_NAME, CUSTOM_ELEMENT, REMOVE_VERSION_SNAPSHOT);
  53. private TextProvider textProvider;
  54. public MavenVariableTaskConfigurator(TextProvider textProvider, TaskConfiguratorHelper taskConfiguratorHelper) {
  55. this.textProvider = textProvider;
  56. this.taskConfiguratorHelper = taskConfiguratorHelper;
  57. }
  58. @NotNull
  59. @Override
  60. public Map<String, String> generateTaskConfigMap(@NotNull final ActionParametersMap params,
  61. @Nullable final TaskDefinition previousTaskDefinition) {
  62. final Map<String, String> config = super.generateTaskConfigMap(params, previousTaskDefinition);
  63. taskConfiguratorHelper.populateTaskConfigMapWithActionParameters(config, params, FIELDS_TO_COPY);
  64. return config;
  65. }
  66. @Override
  67. public void populateContextForCreate(@NotNull final Map<String, Object> context) {
  68. super.populateContextForCreate(context);
  69. context.put(EXTRACT_MODE, EXTRACT_MODE_GAV);
  70. context.put(VARIABLE_TYPE, VARIABLE_TYPE_JOB);
  71. context.put(PREFIX_OPTION, PREFIX_OPTION_DEFAULT);
  72. populateContextForAll(context);
  73. }
  74. @Override
  75. public void populateContextForEdit(@NotNull final Map<String, Object> context,
  76. @NotNull final TaskDefinition taskDefinition) {
  77. super.populateContextForEdit(context, taskDefinition);
  78. taskConfiguratorHelper.populateContextWithConfiguration(context, taskDefinition, FIELDS_TO_COPY);
  79. populateContextForAll(context);
  80. }
  81. private void populateContextForAll(@NotNull final Map<String, Object> context) {
  82. Map<String, String> servers = Maps.newHashMap();
  83. servers.put(EXTRACT_MODE_CUSTOM, textProvider.getText("maven.extractor.config.option.extract.custom"));
  84. servers.put(EXTRACT_MODE_GAV, textProvider.getText("maven.extractor.config.option.extract.gav"));
  85. context.put("options", servers);
  86. Map<String, String> prefixOptions = Maps.newHashMap();
  87. prefixOptions.put(PREFIX_OPTION_DEFAULT, textProvider.getText("maven.extractor.config.option.prefix.maven"));
  88. prefixOptions.put(PREFIX_OPTION_CUSTOM, textProvider.getText("maven.extractor.config.option.prefix.custom"));
  89. context.put("prefixOptions", prefixOptions);
  90. Map<String, String> variableTypeOptions = Maps.newHashMap();
  91. variableTypeOptions.put(VARIABLE_TYPE_JOB,
  92. textProvider.getText("maven.extractor.config.option.variableType.job"));
  93. variableTypeOptions.put(VARIABLE_TYPE_PLAN,
  94. textProvider.getText("maven.extractor.config.option.variableType.plan"));
  95. context.put("variableTypeOptions", variableTypeOptions);
  96. }
  97. @Override
  98. public void populateContextForView(@NotNull final Map<String, Object> context,
  99. @NotNull final TaskDefinition taskDefinition) {
  100. super.populateContextForView(context, taskDefinition);
  101. taskConfiguratorHelper.populateContextWithConfiguration(context, taskDefinition, FIELDS_TO_COPY);
  102. }
  103. @Override
  104. public void validate(@NotNull final ActionParametersMap params, @NotNull final ErrorCollection errorCollection) {
  105. super.validate(params, errorCollection);
  106. String gavOrCustom = params.getString(EXTRACT_MODE);
  107. if (EXTRACT_MODE_CUSTOM.equals(gavOrCustom)) {
  108. String variableName = params.getString(CUSTOM_VARIABLE_NAME);
  109. String element = params.getString(CUSTOM_ELEMENT);
  110. if (StringUtils.isEmpty(variableName)) {
  111. errorCollection.addError(CUSTOM_VARIABLE_NAME,
  112. textProvider.getText("maven.extractor.config.custom.variable.name.error"));
  113. }
  114. if (StringUtils.isEmpty(element)) {
  115. errorCollection.addError(CUSTOM_ELEMENT,
  116. textProvider.getText("maven.extractor.config.custom.element.error"));
  117. }
  118. }
  119. if (LOG.isDebugEnabled()) {
  120. if (errorCollection.hasAnyErrors()) {
  121. LOG.debug("Submitted configuration has validation errors.");
  122. }
  123. }
  124. }
  125. }