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 com.fasterxml.jackson.annotation.JsonCreator;
22  import com.fasterxml.jackson.annotation.JsonProperty;
23  import com.google.common.base.MoreObjects;
24  
25  import com.linecorp.centraldogma.common.Author;
26  import com.linecorp.centraldogma.internal.Util;
27  import com.linecorp.centraldogma.server.storage.project.Project;
28  
29  /**
30   * Specifies details of a member who belongs to the {@link Project}.
31   */
32  public class Member implements Identifiable {
33  
34      /**
35       * A login name of a member.
36       */
37      private final String login;
38  
39      /**
40       * A role of a member in a project.
41       */
42      private final ProjectRole role;
43  
44      /**
45       * Specifies when this member is added to the project by whom.
46       */
47      private final UserAndTimestamp creation;
48  
49      /**
50       * Creates a new instance.
51       */
52      public Member(User user, ProjectRole role, UserAndTimestamp creation) {
53          this(requireNonNull(user, "user").id(), role, creation);
54      }
55  
56      /**
57       * Creates a new instance.
58       */
59      public Member(Author author, ProjectRole role, UserAndTimestamp creation) {
60          this(requireNonNull(author, "author").email(), role, creation);
61      }
62  
63      /**
64       * Creates a new instance.
65       */
66      @JsonCreator
67      public Member(@JsonProperty("login") String login,
68                    @JsonProperty("role") ProjectRole role,
69                    @JsonProperty("creation") UserAndTimestamp creation) {
70          this.login = Util.toEmailAddress(login, "login");
71          this.role = requireNonNull(role, "role");
72          this.creation = requireNonNull(creation, "creation");
73      }
74  
75      @Override
76      public String id() {
77          return login;
78      }
79  
80      /**
81       * Returns the login name.
82       */
83      @JsonProperty
84      public String login() {
85          return login;
86      }
87  
88      /**
89       * Returns the role in a certain project.
90       */
91      @JsonProperty
92      public ProjectRole role() {
93          return role;
94      }
95  
96      /**
97       * Returns who added this member to a project when.
98       */
99      @JsonProperty
100     public UserAndTimestamp creation() {
101         return creation;
102     }
103 
104     @Override
105     public String toString() {
106         return MoreObjects.toStringHelper(this)
107                           .add("login", login())
108                           .add("role", role())
109                           .add("creation", creation())
110                           .toString();
111     }
112 }