1   /*
2    * Copyright 2017 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package com.linecorp.centraldogma.internal.api.v1;
18  
19  import static com.linecorp.centraldogma.internal.api.v1.HttpApiV1Constants.PROJECTS_PREFIX;
20  import static java.time.format.DateTimeFormatter.ISO_INSTANT;
21  import static java.util.Objects.requireNonNull;
22  
23  import java.time.Instant;
24  
25  import javax.annotation.Nullable;
26  
27  import com.fasterxml.jackson.annotation.JsonCreator;
28  import com.fasterxml.jackson.annotation.JsonInclude;
29  import com.fasterxml.jackson.annotation.JsonInclude.Include;
30  import com.fasterxml.jackson.annotation.JsonProperty;
31  import com.google.common.annotations.VisibleForTesting;
32  import com.google.common.base.MoreObjects;
33  import com.google.common.base.MoreObjects.ToStringHelper;
34  
35  import com.linecorp.centraldogma.common.Author;
36  import com.linecorp.centraldogma.common.ProjectRole;
37  
38  @JsonInclude(Include.NON_NULL)
39  public class ProjectDto {
40  
41      private final String name;
42  
43      @Nullable
44      private Author creator;
45  
46      @Nullable
47      private String url;
48  
49      @Nullable
50      private ProjectRole userRole;
51  
52      @Nullable
53      private String createdAt;
54  
55      public ProjectDto(String name) {
56          this.name = requireNonNull(name, "name");
57      }
58  
59      @VisibleForTesting
60      @JsonCreator
61      public ProjectDto(@JsonProperty("name") String name,
62                        @JsonProperty("creator") @Nullable Author creator,
63                        @JsonProperty("url") @Nullable String url,
64                        @JsonProperty("userRole") @Nullable ProjectRole userRole,
65                        @JsonProperty("createdAt") @Nullable String createdAt) {
66          this.name = requireNonNull(name, "name");
67          this.creator = creator;
68          this.url = url;
69          this.userRole = userRole;
70          this.createdAt = createdAt;
71      }
72  
73      public ProjectDto(String name, Author creator, ProjectRole userRole, long creationTimeMillis) {
74          this.name = requireNonNull(name, "name");
75          this.creator = requireNonNull(creator, "creator");
76          createdAt = ISO_INSTANT.format(Instant.ofEpochMilli(creationTimeMillis));
77          url = PROJECTS_PREFIX + '/' + name;
78          this.userRole = requireNonNull(userRole, "userRole");
79      }
80  
81      @JsonProperty("name")
82      public String name() {
83          return name;
84      }
85  
86      @Nullable
87      @JsonProperty("creator")
88      public Author creator() {
89          return creator;
90      }
91  
92      @Nullable
93      @JsonProperty("url")
94      public String url() {
95          return url;
96      }
97  
98      @Nullable
99      @JsonProperty("userRole")
100     public ProjectRole userRole() {
101         return userRole;
102     }
103 
104     @Nullable
105     @JsonProperty("createdAt")
106     public String createdAt() {
107         return createdAt;
108     }
109 
110     @Override
111     public String toString() {
112         final ToStringHelper stringHelper = MoreObjects.toStringHelper(this)
113                                                        .add("name", name());
114         if (creator() != null) {
115             stringHelper.add("creator", creator());
116         }
117 
118         if (createdAt() != null) {
119             stringHelper.add("createdAt", createdAt());
120         }
121         return stringHelper.toString();
122     }
123 }
124