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.domain.BasicUser;
20  import com.atlassian.jira.rest.client.api.domain.Visibility;
21  import com.atlassian.jira.rest.client.api.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.api.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  	 *
77  	 * @param newEstimate new estimate value to set.<br/>
78  	 *                    You can specify a time unit after a time value 'X', such as Xw, Xd, Xh or Xm,
79  	 *                    to represent weeks (w), days (d), hours (h) and minutes (m), respectively.<br/>
80  	 *                    If you do not specify a time unit, minute will be assumed.
81  	 * @return this worklog input builder object
82  	 */
83  	public WorklogInputBuilder setAdjustEstimateNew(String newEstimate) {
84  		return setAdjustEstimate(WorklogInput.AdjustEstimate.NEW, newEstimate);
85  	}
86  
87  	/**
88  	 * Sets AdjustEstimate to NEW - sets estimate to specified value.
89  	 *
90  	 * @param newEstimateMinutes new estimate value to set, in minutes.
91  	 * @return this worklog input builder object
92  	 */
93  	public WorklogInputBuilder setAdjustEstimateNew(int newEstimateMinutes) {
94  		return setAdjustEstimate(WorklogInput.AdjustEstimate.NEW, newEstimateMinutes + ESTIMATE_UNIT_MINUTES);
95  	}
96  
97  	/**
98  	 * Sets AdjustEstimate to LEAVE - leaves estimate as is.
99  	 *
100 	 * @return this worklog input builder object
101 	 */
102 	public WorklogInputBuilder setAdjustEstimateLeave() {
103 		return setAdjustEstimate(WorklogInput.AdjustEstimate.LEAVE, null);
104 	}
105 
106 	/**
107 	 * Sets AdjustEstimate to MANUAL - reduces remaining estimate by given value.
108 	 *
109 	 * @param reduceEstimateBy the amount to reduce the remaining estimate by<br/>
110 	 *                         You can specify a time unit after a time value 'X', such as Xw, Xd, Xh or Xm,
111 	 *                         to represent weeks (w), days (d), hours (h) and minutes (m), respectively.<br/>
112 	 *                         If you do not specify a time unit, minute will be assumed.
113 	 * @return this worklog input builder object
114 	 */
115 	public WorklogInputBuilder setAdjustEstimateManual(String reduceEstimateBy) {
116 		return setAdjustEstimate(WorklogInput.AdjustEstimate.MANUAL, reduceEstimateBy);
117 	}
118 
119 	/**
120 	 * Sets AdjustEstimate to MANUAL - reduces remaining estimate by given value.
121 	 *
122 	 * @param reduceEstimateByMinutes the amount to reduce the remaining estimate by, in minutes.
123 	 * @return this worklog input builder object
124 	 */
125 	public WorklogInputBuilder setAdjustEstimateManual(int reduceEstimateByMinutes) {
126 		return setAdjustEstimate(WorklogInput.AdjustEstimate.MANUAL, reduceEstimateByMinutes + ESTIMATE_UNIT_MINUTES);
127 	}
128 
129 	/**
130 	 * Sets AdjustEstimate to AUTO - will automatically adjust the value
131 	 * based on the minutes spend specified on the worklog input.<br/>
132 	 * This is the default option.
133 	 *
134 	 * @return this worklog input builder object
135 	 */
136 	@SuppressWarnings("UnusedDeclaration")
137 	public WorklogInputBuilder setAdjustEstimateAuto() {
138 		return setAdjustEstimate(WorklogInput.AdjustEstimate.AUTO, null);
139 	}
140 
141 	public WorklogInputBuilder setSelf(URI self) {
142 		this.self = self;
143 		return this;
144 	}
145 
146 	public WorklogInputBuilder setIssueUri(URI issueUri) {
147 		this.issueUri = issueUri;
148 		return this;
149 	}
150 
151 	public WorklogInputBuilder setAuthor(BasicUser author) {
152 		this.author = author;
153 		return this;
154 	}
155 
156 	public WorklogInputBuilder setUpdateAuthor(BasicUser updateAuthor) {
157 		this.updateAuthor = updateAuthor;
158 		return this;
159 	}
160 
161 	public WorklogInputBuilder setComment(String comment) {
162 		this.comment = comment;
163 		return this;
164 	}
165 
166 	public WorklogInputBuilder setStartDate(DateTime startDate) {
167 		this.startDate = startDate;
168 		return this;
169 	}
170 
171 	public WorklogInputBuilder setMinutesSpent(int minutesSpent) {
172 		this.minutesSpent = minutesSpent;
173 		return this;
174 	}
175 
176 	public WorklogInputBuilder setVisibility(Visibility visibility) {
177 		this.visibility = visibility;
178 		return this;
179 	}
180 
181 	public WorklogInput build() {
182 		return new WorklogInput(self, issueUri, author, updateAuthor, comment, startDate, minutesSpent, visibility, adjustEstimate, adjustEstimateValue);
183 	}
184 }