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.domain.BasicUser;
20  import com.atlassian.jira.rest.client.domain.Visibility;
21  import com.atlassian.jira.rest.client.domain.Worklog;
22  import com.google.common.base.Preconditions;
23  import org.joda.time.DateTime;
24  
25  import java.net.URI;
26  
27  /**
28   * Builder class for WorklogInput. Allows to create new worklogInput instance by using convenient setters.
29   * Especially useful are methods to set estimate adjustment options:
30   * {@link WorklogInputBuilder#setAdjustEstimateAuto()}, {@link WorklogInputBuilder#setAdjustEstimateLeave()},
31   * {@link WorklogInputBuilder#setAdjustEstimateManual(String)} and {@link WorklogInputBuilder#setAdjustEstimateNew(String)}.
32   * <br/>
33   * If you want ot create new WorklogInput from existing Worklog entity then use
34   * {@link WorklogInputBuilder#copyFromWorklog(com.atlassian.jira.rest.client.domain.Worklog)} method.
35   */
36  public class WorklogInputBuilder {
37  	public static final String ESTIMATE_UNIT_MINUTES = "m";
38  	private URI self;
39  	private URI issueUri;
40  	private BasicUser author;
41  	private BasicUser updateAuthor;
42  	private String comment;
43  	private DateTime startDate;
44  	private int minutesSpent;
45  	private Visibility visibility;
46  	private WorklogInput.AdjustEstimate adjustEstimate = WorklogInput.AdjustEstimate.AUTO;
47  	private String adjustEstimateValue;
48  
49  	public WorklogInputBuilder(URI issueUri) {
50  		Preconditions.checkNotNull(issueUri, "The issueUri cannot be null");
51  		this.issueUri = issueUri;
52  	}
53  
54  	@SuppressWarnings("UnusedDeclaration")
55  	public WorklogInputBuilder copyFromWorklog(Worklog worklog) {
56  		return this
57  				.setSelf(worklog.getSelf())
58  				.setIssueUri(worklog.getIssueUri())
59  				.setAuthor(worklog.getAuthor())
60  				.setUpdateAuthor(worklog.getUpdateAuthor())
61  				.setComment(worklog.getComment())
62  				.setStartDate(worklog.getStartDate())
63  				.setMinutesSpent(worklog.getMinutesSpent())
64  				.setVisibility(worklog.getVisibility());
65  	}
66  
67  
68  	private WorklogInputBuilder setAdjustEstimate(WorklogInput.AdjustEstimate adjustEstimate, String estimateValue) {
69  		this.adjustEstimate = adjustEstimate;
70  		this.adjustEstimateValue = estimateValue;
71  		return this;
72  	}
73  
74  	/**
75  	 * Sets AdjustEstimate to NEW - sets estimate to specified value.
76  	 * @param newEstimate new estimate value to set.<br/>
77  	 *                       You can specify a time unit after a time value 'X', such as Xw, Xd, Xh or Xm,
78  	 *                       to represent weeks (w), days (d), hours (h) and minutes (m), respectively.<br/>
79  	 *                       If you do not specify a time unit, minute will be assumed.
80  	 * @return this worklog input builder object
81  	 */
82  	public WorklogInputBuilder setAdjustEstimateNew(String newEstimate) {
83  		return setAdjustEstimate(WorklogInput.AdjustEstimate.NEW, newEstimate);
84  	}
85  
86  	/**
87  	 * Sets AdjustEstimate to NEW - sets estimate to specified value.
88  	 * @param newEstimateMinutes new estimate value to set, in minutes.
89  	 * @return this worklog input builder object
90  	 */
91  	public WorklogInputBuilder setAdjustEstimateNew(int newEstimateMinutes) {
92  		return setAdjustEstimate(WorklogInput.AdjustEstimate.NEW, newEstimateMinutes + ESTIMATE_UNIT_MINUTES);
93  	}
94  
95  	/**
96  	 * Sets AdjustEstimate to LEAVE - leaves estimate as is.
97  	 * @return this worklog input builder object
98  	 */
99  	public WorklogInputBuilder setAdjustEstimateLeave() {
100 		return setAdjustEstimate(WorklogInput.AdjustEstimate.LEAVE, null);
101 	}
102 
103 	/**
104 	 * Sets AdjustEstimate to MANUAL - reduces remaining estimate by given value.
105 	 * @param reduceEstimateBy the amount to reduce the remaining estimate by<br/>
106 	 *                            You can specify a time unit after a time value 'X', such as Xw, Xd, Xh or Xm,
107 	 *                            to represent weeks (w), days (d), hours (h) and minutes (m), respectively.<br/>
108 	 *                            If you do not specify a time unit, minute will be assumed.
109 	 * @return this worklog input builder object
110 	 */
111 	public WorklogInputBuilder setAdjustEstimateManual(String reduceEstimateBy) {
112 		return setAdjustEstimate(WorklogInput.AdjustEstimate.MANUAL, reduceEstimateBy);
113 	}
114 
115 	/**
116 	 * Sets AdjustEstimate to MANUAL - reduces remaining estimate by given value.
117 	 * @param reduceEstimateByMinutes the amount to reduce the remaining estimate by, in minutes.
118 	 * @return this worklog input builder object
119 	 */
120 	public WorklogInputBuilder setAdjustEstimateManual(int reduceEstimateByMinutes) {
121 		return setAdjustEstimate(WorklogInput.AdjustEstimate.MANUAL, reduceEstimateByMinutes + ESTIMATE_UNIT_MINUTES);
122 	}
123 
124 	/**
125 	 * Sets AdjustEstimate to AUTO - will automatically adjust the value
126 	 * based on the minutes spend specified on the worklog input.<br/>
127 	 * This is the default option.
128 	 * @return this worklog input builder object
129 	 */
130 	@SuppressWarnings("UnusedDeclaration")
131 	public WorklogInputBuilder setAdjustEstimateAuto() {
132 		return setAdjustEstimate(WorklogInput.AdjustEstimate.AUTO, null);
133 	}
134 
135 	public WorklogInputBuilder setSelf(URI self) {
136 		this.self = self;
137 		return this;
138 	}
139 
140 	public WorklogInputBuilder setIssueUri(URI issueUri) {
141 		this.issueUri = issueUri;
142 		return this;
143 	}
144 
145 	public WorklogInputBuilder setAuthor(BasicUser author) {
146 		this.author = author;
147 		return this;
148 	}
149 
150 	public WorklogInputBuilder setUpdateAuthor(BasicUser updateAuthor) {
151 		this.updateAuthor = updateAuthor;
152 		return this;
153 	}
154 
155 	public WorklogInputBuilder setComment(String comment) {
156 		this.comment = comment;
157 		return this;
158 	}
159 
160 	public WorklogInputBuilder setStartDate(DateTime startDate) {
161 		this.startDate = startDate;
162 		return this;
163 	}
164 
165 	public WorklogInputBuilder setMinutesSpent(int minutesSpent) {
166 		this.minutesSpent = minutesSpent;
167 		return this;
168 	}
169 
170 	public WorklogInputBuilder setVisibility(Visibility visibility) {
171 		this.visibility = visibility;
172 		return this;
173 	}
174 
175 	public WorklogInput build() {
176 		return new WorklogInput(self, issueUri, author, updateAuthor, comment, startDate, minutesSpent, visibility, adjustEstimate, adjustEstimateValue);
177 	}
178 }