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.server.command;
18  
19  import static com.google.common.base.Preconditions.checkArgument;
20  import static java.util.Objects.requireNonNull;
21  
22  import java.util.Objects;
23  
24  import javax.annotation.Nullable;
25  
26  import com.fasterxml.jackson.annotation.JsonCreator;
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.ToStringHelper;
31  
32  import com.linecorp.centraldogma.common.Author;
33  import com.linecorp.centraldogma.server.storage.encryption.WrappedDekDetails;
34  
35  /**
36   * A {@link Command} which is used for creating a new repository.
37   */
38  @JsonInclude(Include.NON_NULL)
39  public final class CreateRepositoryCommand extends ProjectCommand<Void> {
40  
41      private final String repositoryName;
42      @Nullable
43      private final WrappedDekDetails wdekDetails;
44  
45      @JsonCreator
46      CreateRepositoryCommand(@JsonProperty("timestamp") @Nullable Long timestamp,
47                              @JsonProperty("author") @Nullable Author author,
48                              @JsonProperty("projectName") String projectName,
49                              @JsonProperty("repositoryName") String repositoryName,
50                              @JsonProperty("wdekDetails") @Nullable WrappedDekDetails wdekDetails) {
51          super(CommandType.CREATE_REPOSITORY, timestamp, author, projectName);
52          this.repositoryName = requireNonNull(repositoryName, "repositoryName");
53          this.wdekDetails = wdekDetails;
54          if (wdekDetails != null) {
55              checkArgument(wdekDetails.projectName().equals(projectName) &&
56                            wdekDetails.repoName().equals(repositoryName),
57                            "projectName: %s and repositoryName: %s, " +
58                            "(expected projectName: %s and repositoryName: %s in wdekDetails)",
59                            projectName, repositoryName, wdekDetails.projectName(), wdekDetails.repoName());
60          }
61      }
62  
63      /**
64       * Returns the repository name.
65       */
66      @JsonProperty
67      public String repositoryName() {
68          return repositoryName;
69      }
70  
71      /**
72       * Returns the WDEK of the repository.
73       */
74      @JsonProperty
75      @Nullable
76      public WrappedDekDetails wdekDetails() {
77          return wdekDetails;
78      }
79  
80      @Override
81      public boolean equals(Object obj) {
82          if (this == obj) {
83              return true;
84          }
85  
86          if (!(obj instanceof CreateRepositoryCommand)) {
87              return false;
88          }
89  
90          final CreateRepositoryCommand that = (CreateRepositoryCommand) obj;
91          return super.equals(obj) &&
92                 repositoryName.equals(that.repositoryName) &&
93                 Objects.equals(wdekDetails, that.wdekDetails);
94      }
95  
96      @Override
97      public int hashCode() {
98          return (repositoryName.hashCode() * 31 + Objects.hashCode(wdekDetails)) * 31 + super.hashCode();
99      }
100 
101     @Override
102     ToStringHelper toStringHelper() {
103         return super.toStringHelper().omitNullValues()
104                     .add("repositoryName", repositoryName)
105                     .add("wdekDetails", wdekDetails);
106     }
107 }