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.internal.api;
18  
19  import static java.util.Objects.requireNonNull;
20  
21  import java.util.Map;
22  
23  import javax.annotation.Nullable;
24  
25  import com.linecorp.centraldogma.common.Author;
26  import com.linecorp.centraldogma.common.Change;
27  import com.linecorp.centraldogma.common.Commit;
28  import com.linecorp.centraldogma.common.Entry;
29  import com.linecorp.centraldogma.common.EntryType;
30  import com.linecorp.centraldogma.common.MergedEntry;
31  import com.linecorp.centraldogma.common.ProjectRole;
32  import com.linecorp.centraldogma.common.RepositoryStatus;
33  import com.linecorp.centraldogma.common.Revision;
34  import com.linecorp.centraldogma.internal.api.v1.ChangeDto;
35  import com.linecorp.centraldogma.internal.api.v1.CommitDto;
36  import com.linecorp.centraldogma.internal.api.v1.CommitMessageDto;
37  import com.linecorp.centraldogma.internal.api.v1.EntryDto;
38  import com.linecorp.centraldogma.internal.api.v1.MergedEntryDto;
39  import com.linecorp.centraldogma.internal.api.v1.ProjectDto;
40  import com.linecorp.centraldogma.internal.api.v1.PushResultDto;
41  import com.linecorp.centraldogma.internal.api.v1.RepositoryDto;
42  import com.linecorp.centraldogma.server.metadata.RepositoryMetadata;
43  import com.linecorp.centraldogma.server.storage.project.Project;
44  import com.linecorp.centraldogma.server.storage.repository.Repository;
45  
46  /**
47   * A utility class to convert domain objects to DTO objects.
48   */
49  final class DtoConverter {
50  
51      public static ProjectDto convert(Project project, ProjectRole userRole) {
52          requireNonNull(project, "project");
53          return new ProjectDto(project.name(), project.author(), userRole, project.creationTimeMillis());
54      }
55  
56      public static RepositoryDto convert(Repository repository,
57                                          Map<String, RepositoryMetadata> metadataMap) {
58          requireNonNull(repository, "repository");
59          final RepositoryStatus status;
60          if (metadataMap == null) {
61              status = RepositoryStatus.ACTIVE;
62          } else {
63              final RepositoryMetadata metadata = metadataMap.get(repository.name());
64              if (metadata == null) {
65                  status = RepositoryStatus.ACTIVE;
66              } else {
67                  status = metadata.status();
68              }
69          }
70          return convert(repository, status);
71      }
72  
73      public static RepositoryDto convert(Repository repository, RepositoryStatus status) {
74          final Revision headRevision = repository.normalizeNow(Revision.HEAD);
75          final String projectName = repository.parent().name();
76          return new RepositoryDto(projectName, repository.name(), repository.author(), headRevision,
77                                   repository.creationTimeMillis(), status);
78      }
79  
80      public static <T> EntryDto<T> convert(Repository repository, Revision revision,
81                                            Entry<T> entry, boolean withContent) {
82          requireNonNull(entry, "entry");
83          if (withContent && entry.hasContent()) {
84              return convert(repository, revision, entry.path(), entry.type(), entry.content());
85          }
86          return convert(repository, revision, entry.path(), entry.type());
87      }
88  
89      private static <T> EntryDto<T> convert(Repository repository, Revision revision,
90                                             String path, EntryType type) {
91          return convert(repository, revision, path, type, null);
92      }
93  
94      private static <T> EntryDto<T> convert(Repository repository, Revision revision, String path,
95                                             EntryType type, @Nullable T content) {
96          requireNonNull(repository, "repository");
97          return new EntryDto<>(requireNonNull(revision, "revision"),
98                                requireNonNull(path, "path"),
99                                requireNonNull(type, "type"),
100                               repository.parent().name(),
101                               repository.name(),
102                               content);
103     }
104 
105     public static PushResultDto convert(Revision revision, long commitTimeMillis) {
106         return new PushResultDto(revision, commitTimeMillis);
107     }
108 
109     public static CommitDto convert(Commit commit) {
110         requireNonNull(commit, "commit");
111 
112         return convert(commit.revision(), commit.author(),
113                        new CommitMessageDto(commit.summary(), commit.detail(), commit.markup()),
114                        commit.when());
115     }
116 
117     public static CommitDto convert(Revision revision, Author author, CommitMessageDto commitMessage,
118                                     long commitTimeMillis) {
119         return new CommitDto(revision, author, commitMessage, commitTimeMillis);
120     }
121 
122     public static <T> ChangeDto<T> convert(Change<T> change) {
123         requireNonNull(change, "change");
124         return new ChangeDto<>(change.path(), change.type(), change.content());
125     }
126 
127     public static <T> MergedEntryDto<T> convert(MergedEntry<T> mergedEntry) {
128         requireNonNull(mergedEntry, "mergedEntry");
129         return new MergedEntryDto<>(mergedEntry.revision(), mergedEntry.type(),
130                                     mergedEntry.content(), mergedEntry.paths());
131     }
132 
133     private DtoConverter() {}
134 }