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   * <p>
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.
132      * <p>
133      * This is the default option.
134      *
135      * @return this worklog input builder object
136      */
137     @SuppressWarnings("UnusedDeclaration")
138     public WorklogInputBuilder setAdjustEstimateAuto() {
139         return setAdjustEstimate(WorklogInput.AdjustEstimate.AUTO, null);
140     }
141 
142     public WorklogInputBuilder setSelf(URI self) {
143         this.self = self;
144         return this;
145     }
146 
147     public WorklogInputBuilder setIssueUri(URI issueUri) {
148         this.issueUri = issueUri;
149         return this;
150     }
151 
152     public WorklogInputBuilder setAuthor(BasicUser author) {
153         this.author = author;
154         return this;
155     }
156 
157     public WorklogInputBuilder setUpdateAuthor(BasicUser updateAuthor) {
158         this.updateAuthor = updateAuthor;
159         return this;
160     }
161 
162     public WorklogInputBuilder setComment(String comment) {
163         this.comment = comment;
164         return this;
165     }
166 
167     public WorklogInputBuilder setStartDate(DateTime startDate) {
168         this.startDate = startDate;
169         return this;
170     }
171 
172     public WorklogInputBuilder setMinutesSpent(int minutesSpent) {
173         this.minutesSpent = minutesSpent;
174         return this;
175     }
176 
177     public WorklogInputBuilder setVisibility(Visibility visibility) {
178         this.visibility = visibility;
179         return this;
180     }
181 
182     public WorklogInput build() {
183         return new WorklogInput(self, issueUri, author, updateAuthor, comment, startDate, minutesSpent, visibility, adjustEstimate, adjustEstimateValue);
184     }
185 }