View Javadoc

1   /*
2    * Copyright (C) 2012 Atlassian
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  
17  package com.atlassian.jira.rest.client.api.domain.input;
18  
19  import com.atlassian.jira.rest.client.api.IdentifiableEntity;
20  import com.atlassian.jira.rest.client.api.NamedEntity;
21  import com.atlassian.jira.rest.client.api.domain.BasicProject;
22  import com.atlassian.jira.rest.client.api.domain.CustomFieldOption;
23  import com.atlassian.jira.rest.client.api.domain.TimeTracking;
24  import com.google.common.collect.ImmutableMap;
25  import com.google.common.collect.Maps;
26  
27  import java.util.Map;
28  
29  /**
30   * Transforms most of standard fields values into form understandable by input generator.
31   *
32   * @since v1.0
33   */
34  public class BaseValueTransformer implements ValueTransformer {
35  
36      public Object apply(Object rawValue) {
37          if (rawValue == null) {
38              return null;
39          } else if (rawValue instanceof String || rawValue instanceof Number || rawValue instanceof ComplexIssueInputFieldValue) {
40              return rawValue;
41          } else if (rawValue instanceof BasicProject) {
42              return new ComplexIssueInputFieldValue(ImmutableMap.<String, Object>of("key", ((BasicProject) rawValue).getKey()));
43          } else if (rawValue instanceof CustomFieldOption) {
44              return transformCustomFieldOption((CustomFieldOption) rawValue);
45          } else if (rawValue instanceof TimeTracking) {
46              return transformTimeTracking((TimeTracking) rawValue);
47          } else if (rawValue instanceof IdentifiableEntity) {
48              final IdentifiableEntity identifiableEntity = (IdentifiableEntity) rawValue;
49              return new ComplexIssueInputFieldValue(ImmutableMap.<String, Object>of("id", identifiableEntity.getId().toString()));
50          } else if (rawValue instanceof NamedEntity) {
51              final NamedEntity namedEntity = (NamedEntity) rawValue;
52              return new ComplexIssueInputFieldValue(ImmutableMap.<String, Object>of("name", namedEntity.getName()));
53          }
54  
55          return CANNOT_HANDLE;
56      }
57  
58      private ComplexIssueInputFieldValue transformCustomFieldOption(CustomFieldOption cfo) {
59          if (cfo.getChild() != null) {
60              return new ComplexIssueInputFieldValue(ImmutableMap.<String, Object>of(
61                      "id", cfo.getId().toString(),
62                      "value", cfo.getValue(),
63                      "child", this.apply(cfo.getChild())));
64          } else {
65              return new ComplexIssueInputFieldValue(ImmutableMap.<String, Object>of("id", cfo.getId().toString(), "value", cfo
66                      .getValue()));
67          }
68      }
69  
70      private ComplexIssueInputFieldValue transformTimeTracking(TimeTracking timeTracking) {
71          final Map<String, Object> fields = Maps.newHashMap();
72  
73          final Integer originalEstimateMinutes = timeTracking.getOriginalEstimateMinutes();
74          if (originalEstimateMinutes != null) {
75              fields.put("originalEstimate", originalEstimateMinutes + "m");
76          }
77  
78          final Integer remainingEstimateMinutes = timeTracking.getRemainingEstimateMinutes();
79          if (remainingEstimateMinutes != null) {
80              fields.put("remainingEstimate", remainingEstimateMinutes + "m");
81          }
82  
83          // Don't use time spent as JIRA says: "Setting the Time Spent directly is not supported."
84  
85          return new ComplexIssueInputFieldValue(fields);
86      }
87  
88  }