View Javadoc
1   /**
2    * Copyright (c) 2012-2015, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package com.jcabi.odesk;
31  
32  import com.jcabi.aspects.Immutable;
33  import com.jcabi.aspects.Loggable;
34  import com.jcabi.http.Request;
35  import com.jcabi.http.RequestURI;
36  import com.jcabi.http.response.JsonResponse;
37  import com.jcabi.http.response.RestResponse;
38  import java.io.IOException;
39  import java.math.BigDecimal;
40  import java.net.HttpURLConnection;
41  import java.util.ArrayList;
42  import java.util.Collection;
43  import javax.json.JsonString;
44  import javax.validation.constraints.NotNull;
45  import lombok.EqualsAndHashCode;
46  import lombok.ToString;
47  
48  /**
49   * RESTful {@link Adjustments}.
50   *
51   * @author Yegor Bugayenko (yegor@tpc2.com)
52   * @version $Id: b8bb2cf74be022b5ad523accf1c6ed1e2856b7bb $
53   * @since 0.1
54   */
55  @Immutable
56  @ToString
57  @Loggable(Loggable.DEBUG)
58  @EqualsAndHashCode(of = "entry")
59  final class RtAdjustments implements Adjustments {
60  
61      /**
62       * Request to use.
63       */
64      private final transient Request entry;
65  
66      /**
67       * Public ctor.
68       * @param req Request
69       * @param name Team ref/name
70       */
71      RtAdjustments(final Request req, final String name) {
72          this.entry = req.uri()
73              .path("v2/teams")
74              .path(name)
75              .path("adjustments.json")
76              .back();
77      }
78  
79      // @checkstyle ParameterNumber (10 lines)
80      @Override
81      @NotNull(message = "adjustment ID is never NULL")
82      public String add(
83          @NotNull(message = "engagement ref can't be NULL")
84          final String engagement,
85          @NotNull(message = "charge amount can't be NULL")
86          final BigDecimal charge,
87          @NotNull(message = "comments can't be NULL") final String comments,
88          @NotNull(message = "notes can't be NULL") final String notes)
89          throws IOException {
90          RequestURI uri = this.entry.uri()
91              .queryParam("engagement__reference", engagement)
92              .queryParam("comments", comments)
93              .queryParam("notes", notes);
94          if (charge != null && charge.compareTo(BigDecimal.ZERO) != 0) {
95              uri = uri.queryParam("charge_amount", charge.toString());
96          }
97          return uri.back().method(Request.POST).fetch()
98              .as(RestResponse.class)
99              .assertStatus(HttpURLConnection.HTTP_OK)
100             .as(JsonResponse.class)
101             .json().readObject().getJsonObject("adjustment")
102             .getString("reference");
103     }
104 
105     @Override
106     @NotNull(message = "iterable of adjustments is never NULL")
107     public Iterable<String> iterate() throws IOException {
108         final Collection<JsonString> values = this.entry.fetch()
109             .as(RestResponse.class)
110             .assertStatus(HttpURLConnection.HTTP_OK)
111             .as(JsonResponse.class)
112             .json().readObject().getJsonArray("adjustments")
113             .getValuesAs(JsonString.class);
114         final Collection<String> refs = new ArrayList<String>(values.size());
115         for (final JsonString val : values) {
116             refs.add(val.getString());
117         }
118         return refs;
119     }
120 }