1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
38
39 @JsonIgnoreProperties(ignoreUnknown = true)
40 @JsonInclude(Include.NON_NULL)
41 public class ProjectMetadata implements Identifiable {
42
43
44
45
46 private final String name;
47
48
49
50
51 private final Map<String, RepositoryMetadata> repos;
52
53
54
55
56 private final Map<String, Member> members;
57
58
59
60
61 private final Map<String, TokenRegistration> tokens;
62
63
64
65
66 private final UserAndTimestamp creation;
67
68
69
70
71 @Nullable
72 private final UserAndTimestamp removal;
73
74
75
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
99
100 @JsonProperty
101 public String name() {
102 return name;
103 }
104
105
106
107
108 @JsonProperty
109 public Map<String, RepositoryMetadata> repos() {
110 return repos;
111 }
112
113
114
115
116 @JsonProperty
117 public Map<String, Member> members() {
118 return members;
119 }
120
121
122
123
124 @JsonProperty
125 public Map<String, TokenRegistration> tokens() {
126 return tokens;
127 }
128
129
130
131
132 @JsonProperty
133 public UserAndTimestamp creation() {
134 return creation;
135 }
136
137
138
139
140 @Nullable
141 @JsonProperty
142 public UserAndTimestamp removal() {
143 return removal;
144 }
145
146
147
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
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
171
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 }