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.admin.service;
18  
19  import java.util.List;
20  import java.util.concurrent.CompletableFuture;
21  
22  import com.linecorp.centraldogma.common.Author;
23  import com.linecorp.centraldogma.common.Change;
24  import com.linecorp.centraldogma.common.Commit;
25  import com.linecorp.centraldogma.common.Entry;
26  import com.linecorp.centraldogma.common.Markup;
27  import com.linecorp.centraldogma.common.Revision;
28  import com.linecorp.centraldogma.server.internal.admin.dto.AuthorDto;
29  import com.linecorp.centraldogma.server.internal.admin.dto.ChangeDto;
30  import com.linecorp.centraldogma.server.internal.admin.dto.CommentDto;
31  import com.linecorp.centraldogma.server.internal.admin.dto.CommitDto;
32  import com.linecorp.centraldogma.server.internal.admin.dto.EntryDto;
33  import com.linecorp.centraldogma.server.internal.admin.dto.ProjectDto;
34  import com.linecorp.centraldogma.server.internal.admin.dto.RepositoryDto;
35  import com.linecorp.centraldogma.server.internal.admin.dto.RevisionDto;
36  import com.linecorp.centraldogma.server.storage.project.Project;
37  import com.linecorp.centraldogma.server.storage.repository.Repository;
38  
39  /**
40   * A utility class to convert domain objects to DTO objects.
41   */
42  final class DtoConverter {
43  
44      static CompletableFuture<RepositoryDto> convert(Repository repository) {
45          return repository.history(Revision.HEAD, Revision.HEAD, Repository.ALL_PATH, 1)
46                           .thenApply(history -> fromCommit(repository.name(), history));
47      }
48  
49      static ProjectDto convert(Project project) {
50          final ProjectDto dto = new ProjectDto();
51          dto.setName(project.name());
52          return dto;
53      }
54  
55      static CommitDto convert(Commit commit) {
56          final CommitDto dto = new CommitDto();
57          dto.setAuthor(convert(commit.author()));
58          dto.setRevision(convert(commit.revision()));
59          dto.setTimestamp(commit.whenAsText());
60          dto.setSummary(commit.summary());
61          dto.setDetail(convert(commit.detail(), commit.markup()));
62          return dto;
63      }
64  
65      static RevisionDto convert(Revision revision) {
66          return new RevisionDto(revision.major(), 0, revision.text());
67      }
68  
69      static AuthorDto convert(Author author) {
70          final AuthorDto dto = new AuthorDto();
71          dto.setName(author.name());
72          dto.setEmail(author.email());
73          return dto;
74      }
75  
76      static CommentDto convert(String content, Markup markup) {
77          final CommentDto dto = new CommentDto();
78          dto.setContent(content);
79          dto.setMarkup(markup.name());
80          return dto;
81      }
82  
83      static ChangeDto convert(Change<?> change) {
84          final ChangeDto dto = new ChangeDto();
85          dto.setPath(change.path());
86          dto.setType(change.type().name());
87          dto.setContent(change.contentAsText());
88          return dto;
89      }
90  
91      static EntryDto convert(Entry<?> entry) {
92          final EntryDto dto = new EntryDto();
93          dto.setRevision(entry.revision().text());
94          dto.setPath(entry.path());
95          dto.setType(entry.type().name());
96          dto.setContent(entry.contentAsText());
97          return dto;
98      }
99  
100     static RepositoryDto fromCommit(String name, List<Commit> history) {
101         final RepositoryDto dto = new RepositoryDto();
102         dto.setName(name);
103         dto.setHead(convert(history.get(0)));
104         return dto;
105     }
106 
107     private DtoConverter() {}
108 }
109