1   /*
2    * Copyright 2019 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.server.metadata;
18  
19  import static java.util.Objects.requireNonNull;
20  
21  import java.util.Map;
22  
23  import javax.annotation.Nullable;
24  
25  import com.fasterxml.jackson.annotation.JsonCreator;
26  import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
27  import com.fasterxml.jackson.annotation.JsonInclude;
28  import com.fasterxml.jackson.annotation.JsonInclude.Include;
29  import com.fasterxml.jackson.annotation.JsonProperty;
30  import com.google.common.base.MoreObjects;
31  
32  import com.linecorp.centraldogma.common.EntryNotFoundException;
33  import com.linecorp.centraldogma.common.RepositoryNotFoundException;
34  import com.linecorp.centraldogma.server.storage.project.Project;
35  
36  /**
37   * Specifies details of a {@link Project}.
38   */
39  @JsonIgnoreProperties(ignoreUnknown = true)
40  @JsonInclude(Include.NON_NULL)
41  public class ProjectMetadata implements Identifiable {
42  
43      /**
44       * A project name.
45       */
46      private final String name;
47  
48      /**
49       * Repositories of this project.
50       */
51      private final Map<String, RepositoryMetadata> repos;
52  
53      /**
54       * Members of this project.
55       */
56      private final Map<String, Member> members;
57  
58      /**
59       * Tokens which belong to this project.
60       */
61      private final Map<String, TokenRegistration> tokens;
62  
63      /**
64       * Specifies when this project is created by whom.
65       */
66      private final UserAndTimestamp creation;
67  
68      /**
69       * Specifies when this project is removed by whom.
70       */
71      @Nullable
72      private final UserAndTimestamp removal;
73  
74      /**
75       * Creates a new instance.
76       */
77      @JsonCreator
78      public ProjectMetadata(@JsonProperty("name") String name,
79                             @JsonProperty("repos") Map<String, RepositoryMetadata> repos,
80                             @JsonProperty("members") Map<String, Member> members,
81                             @JsonProperty("tokens") Map<String, TokenRegistration> tokens,
82                             @JsonProperty("creation") UserAndTimestamp creation,
83                             @JsonProperty("removal") @Nullable UserAndTimestamp removal) {
84          this.name = requireNonNull(name, "name");
85          this.repos = requireNonNull(repos, "repos");
86          this.members = requireNonNull(members, "members");
87          this.tokens = requireNonNull(tokens, "tokens");
88          this.creation = requireNonNull(creation, "creation");
89          this.removal = removal;
90      }
91  
92      @Override
93      public String id() {
94          return name;
95      }
96  
97      /**
98       * Returns the project name.
99       */
100     @JsonProperty
101     public String name() {
102         return name;
103     }
104 
105     /**
106      * Returns the metadata of the repositories in this project.
107      */
108     @JsonProperty
109     public Map<String, RepositoryMetadata> repos() {
110         return repos;
111     }
112 
113     /**
114      * Returns the {@link Member}s of this project.
115      */
116     @JsonProperty
117     public Map<String, Member> members() {
118         return members;
119     }
120 
121     /**
122      * Returns the {@link TokenRegistration}s of this project.
123      */
124     @JsonProperty
125     public Map<String, TokenRegistration> tokens() {
126         return tokens;
127     }
128 
129     /**
130      * Returns who created this project when.
131      */
132     @JsonProperty
133     public UserAndTimestamp creation() {
134         return creation;
135     }
136 
137     /**
138      * Returns who removed this project when.
139      */
140     @Nullable
141     @JsonProperty
142     public UserAndTimestamp removal() {
143         return removal;
144     }
145 
146     /**
147      * Returns the {@link RepositoryMetadata} of the specified repository in this project.
148      */
149     public RepositoryMetadata repo(String repoName) {
150         final RepositoryMetadata repositoryMetadata =
151                 repos.get(requireNonNull(repoName, "repoName"));
152         if (repositoryMetadata != null) {
153             return repositoryMetadata;
154         }
155         throw new RepositoryNotFoundException(repoName);
156     }
157 
158     /**
159      * Returns the {@link Member} of the specified ID in this project.
160      */
161     public Member member(String memberId) {
162         final Member member = memberOrDefault(memberId, null);
163         if (member != null) {
164             return member;
165         }
166         throw new EntryNotFoundException(memberId);
167     }
168 
169     /**
170      * Returns the {@link Member} of the specified ID in this project.
171      * {@code defaultMember} is returned if there is no such member.
172      */
173     @Nullable
174     public Member memberOrDefault(String memberId, @Nullable Member defaultMember) {
175         final Member member = members.get(requireNonNull(memberId, "memberId"));
176         if (member != null) {
177             return member;
178         }
179         return defaultMember;
180     }
181 
182     @Override
183     public String toString() {
184         return MoreObjects.toStringHelper(this)
185                           .add("name", name())
186                           .add("repos", repos())
187                           .add("members", members())
188                           .add("tokens", tokens())
189                           .add("creation", creation())
190                           .add("removal", removal())
191                           .toString();
192     }
193 }