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.domain.input;
18  
19  import com.atlassian.jira.rest.client.IdentifiableEntity;
20  import com.atlassian.jira.rest.client.NamedEntity;
21  import com.atlassian.jira.rest.client.domain.BasicProject;
22  import com.atlassian.jira.rest.client.domain.CustomFieldOption;
23  import com.atlassian.jira.rest.client.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  		}
40  		else if (rawValue instanceof String || rawValue instanceof Number || rawValue instanceof ComplexIssueInputFieldValue) {
41  			return rawValue;
42  		}
43  		else if (rawValue instanceof BasicProject) {
44  			return new ComplexIssueInputFieldValue(ImmutableMap.<String, Object>of("key", ((BasicProject) rawValue).getKey()));
45  		}
46  		else if (rawValue instanceof CustomFieldOption) {
47  			return transformCustomFieldOption((CustomFieldOption) rawValue);
48  		}
49  		else if (rawValue instanceof TimeTracking) {
50  			return transformTimeTracking((TimeTracking) rawValue);
51  		}
52  		else if (rawValue instanceof IdentifiableEntity) {
53  			final IdentifiableEntity identifiableEntity = (IdentifiableEntity) rawValue;
54  			return new ComplexIssueInputFieldValue(ImmutableMap.<String, Object>of("id", identifiableEntity.getId().toString()));
55  		}
56  		else if (rawValue instanceof NamedEntity) {
57  			final NamedEntity namedEntity = (NamedEntity) rawValue;
58  			return new ComplexIssueInputFieldValue(ImmutableMap.<String, Object>of("name", namedEntity.getName()));
59  		}
60  
61  		return CANNOT_HANDLE;
62  	}
63  
64  	private ComplexIssueInputFieldValue transformCustomFieldOption(CustomFieldOption cfo) {
65  		if (cfo.getChild() != null) {
66  			return new ComplexIssueInputFieldValue(ImmutableMap.<String, Object>of(
67  					"id", cfo.getId().toString(),
68  					"value", cfo.getValue(),
69  					"child", this.apply(cfo.getChild())));
70  		}
71  		else {
72  			return new ComplexIssueInputFieldValue(ImmutableMap.<String, Object>of("id", cfo.getId().toString(), "value", cfo.getValue()));
73  		}
74  	}
75  
76  	private ComplexIssueInputFieldValue transformTimeTracking(TimeTracking timeTracking) {
77  		final Map<String, Object> fields = Maps.newHashMap();
78  
79  		final Integer originalEstimateMinutes = timeTracking.getOriginalEstimateMinutes();
80  		if (originalEstimateMinutes != null) {
81  			fields.put("originalEstimate", originalEstimateMinutes + "m");
82  		}
83  
84  		final Integer remainingEstimateMinutes = timeTracking.getRemainingEstimateMinutes();
85  		if (remainingEstimateMinutes != null) {
86  			fields.put("remainingEstimate", remainingEstimateMinutes + "m");
87  		}
88  
89  		// Don't use time spent as JIRA says: "Setting the Time Spent directly is not supported."
90  
91  		return new ComplexIssueInputFieldValue(fields);
92  	}
93  
94  }