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 com.linecorp.centraldogma.internal.api.v1.HttpApiV1Constants.REPOS;
21  import static java.time.format.DateTimeFormatter.ISO_INSTANT;
22  import static java.util.Objects.requireNonNull;
23  
24  import java.time.Instant;
25  
26  import javax.annotation.Nullable;
27  
28  import com.fasterxml.jackson.annotation.JsonCreator;
29  import com.fasterxml.jackson.annotation.JsonInclude;
30  import com.fasterxml.jackson.annotation.JsonInclude.Include;
31  import com.fasterxml.jackson.annotation.JsonProperty;
32  import com.google.common.base.MoreObjects;
33  
34  import com.linecorp.centraldogma.common.Author;
35  import com.linecorp.centraldogma.common.RepositoryStatus;
36  import com.linecorp.centraldogma.common.Revision;
37  
38  @JsonInclude(Include.NON_NULL)
39  public class RepositoryDto {
40  
41      public static RepositoryDto removed(String name) {
42          requireNonNull(name, "name");
43          return new RepositoryDto(name);
44      }
45  
46      private final String name;
47  
48      @Nullable
49      private final Author creator;
50  
51      @Nullable
52      private final Revision headRevision;
53  
54      @Nullable
55      private final String url;
56  
57      @Nullable
58      private final String createdAt;
59  
60      @Nullable
61      private final RepositoryStatus status;
62  
63      RepositoryDto(String name) {
64          this.name = requireNonNull(name, "name");
65          creator = null;
66          headRevision = null;
67          url = null;
68          createdAt = null;
69          status = null;
70      }
71  
72      public RepositoryDto(String projectName, String repoName, Author creator, Revision headRevision,
73                           long creationTimeMillis, RepositoryStatus status) {
74          this(requireNonNull(repoName, "repoName"), requireNonNull(creator, "creator"),
75               requireNonNull(headRevision, "headRevision"),
76               PROJECTS_PREFIX + '/' + requireNonNull(projectName, "projectName") + REPOS + '/' + repoName,
77               ISO_INSTANT.format(Instant.ofEpochMilli(creationTimeMillis)), requireNonNull(status, "status"));
78      }
79  
80      @JsonCreator
81      public RepositoryDto(@JsonProperty("name") String name,
82                           @JsonProperty("creator") @Nullable Author creator,
83                           @JsonProperty("headRevision") @Nullable Revision headRevision,
84                           @JsonProperty("url") @Nullable String url,
85                           @JsonProperty("createdAt") @Nullable String createdAt,
86                           @JsonProperty("status") @Nullable RepositoryStatus status) {
87          this.name = requireNonNull(name, "name");
88          this.creator = creator;
89          this.headRevision = headRevision;
90          this.url = url;
91          this.createdAt = createdAt;
92          this.status = status;
93      }
94  
95      @JsonProperty("name")
96      public String name() {
97          return name;
98      }
99  
100     @Nullable
101     @JsonProperty("creator")
102     public Author creator() {
103         return creator;
104     }
105 
106     @Nullable
107     @JsonProperty("headRevision")
108     public Revision headRevision() {
109         return headRevision;
110     }
111 
112     @Nullable
113     @JsonProperty("url")
114     public String url() {
115         return url;
116     }
117 
118     @Nullable
119     @JsonProperty("createdAt")
120     public String createdAt() {
121         return createdAt;
122     }
123 
124     @Nullable
125     @JsonProperty("status")
126     public RepositoryStatus status() {
127         return status;
128     }
129 
130     @Override
131     public String toString() {
132         return MoreObjects.toStringHelper(this).omitNullValues()
133                           .add("name", name())
134                           .add("creator", creator())
135                           .add("headRevision", headRevision())
136                           .add("url", url())
137                           .add("createdAt", createdAt())
138                           .add("status", status())
139                           .toString();
140     }
141 }