diff --git a/src/main/java/ru/ulstu/extractor/controller/BranchController.java b/src/main/java/ru/ulstu/extractor/controller/BranchController.java index 4006edd..5e0f845 100644 --- a/src/main/java/ru/ulstu/extractor/controller/BranchController.java +++ b/src/main/java/ru/ulstu/extractor/controller/BranchController.java @@ -13,6 +13,7 @@ import ru.ulstu.extractor.repository.BranchRepository; import ru.ulstu.extractor.repository.RepositoryRepository; import springfox.documentation.annotations.ApiIgnore; +import static ru.ulstu.extractor.controller.Route.DELETE_BRANCH; import static ru.ulstu.extractor.controller.Route.LIST_REPOSITORY_BRANCHES; @Controller @@ -34,4 +35,14 @@ public class BranchController { model.addAttribute("repository", repositoryRepository.findById(repositoryId).get()); return LIST_REPOSITORY_BRANCHES; } + + @GetMapping(DELETE_BRANCH) + public String deleteBranch(Model model, + @RequestParam int repositoryId, + @RequestParam Integer id) { + branchRepository.deleteById(id); + model.addAttribute("branches", branchRepository.findByRepositoryId(repositoryId)); + model.addAttribute("repository", repositoryRepository.findById(repositoryId).get()); + return LIST_REPOSITORY_BRANCHES; + } } diff --git a/src/main/java/ru/ulstu/extractor/controller/RepoController.java b/src/main/java/ru/ulstu/extractor/controller/RepoController.java index 98fafbf..b7da394 100644 --- a/src/main/java/ru/ulstu/extractor/controller/RepoController.java +++ b/src/main/java/ru/ulstu/extractor/controller/RepoController.java @@ -44,5 +44,4 @@ public class RepoController { indexService.index(repositoryUrl, branchName); return true; } - } diff --git a/src/main/java/ru/ulstu/extractor/controller/RepositoryController.java b/src/main/java/ru/ulstu/extractor/controller/RepositoryController.java index 31befc0..dd58781 100644 --- a/src/main/java/ru/ulstu/extractor/controller/RepositoryController.java +++ b/src/main/java/ru/ulstu/extractor/controller/RepositoryController.java @@ -8,9 +8,11 @@ package ru.ulstu.extractor.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; import ru.ulstu.extractor.repository.RepositoryRepository; import springfox.documentation.annotations.ApiIgnore; +import static ru.ulstu.extractor.controller.Route.DELETE_INDEXED_REPOSITORY; import static ru.ulstu.extractor.controller.Route.LIST_INDEXED_REPOSITORIES; @Controller @@ -27,4 +29,12 @@ public class RepositoryController { model.addAttribute("repositories", repositoryRepository.findAll()); return LIST_INDEXED_REPOSITORIES; } + + @GetMapping(DELETE_INDEXED_REPOSITORY) + public String deleteRepo(Model model, + @RequestParam Integer id) { + repositoryRepository.deleteById(id); + model.addAttribute("repositories", repositoryRepository.findAll()); + return "redirect:/" + LIST_INDEXED_REPOSITORIES; + } } diff --git a/src/main/java/ru/ulstu/extractor/controller/Route.java b/src/main/java/ru/ulstu/extractor/controller/Route.java index 8a0282e..37cc08f 100644 --- a/src/main/java/ru/ulstu/extractor/controller/Route.java +++ b/src/main/java/ru/ulstu/extractor/controller/Route.java @@ -10,7 +10,9 @@ import org.springframework.stereotype.Component; @Component public class Route { public static final String LIST_INDEXED_REPOSITORIES = "listRepositories"; + public static final String DELETE_INDEXED_REPOSITORY = "deleteRepository"; public static final String LIST_REPOSITORY_BRANCHES = "listBranches"; + public static final String DELETE_BRANCH = "deleteBranch"; public static final String INDEXING_NEW_REPOSITORY = "indexNewRepository"; public static final String FILTER_COMMITS = "filterCommits"; public static final String STATISTIC = "statistic"; diff --git a/src/main/java/ru/ulstu/extractor/model/Branch.java b/src/main/java/ru/ulstu/extractor/model/Branch.java index 5c39ce9..a7f720c 100644 --- a/src/main/java/ru/ulstu/extractor/model/Branch.java +++ b/src/main/java/ru/ulstu/extractor/model/Branch.java @@ -1,12 +1,12 @@ -package ru.ulstu.extractor.model; +/* + * Copyright (C) 2021 Anton Romanov - All Rights Reserved + * You may use, distribute and modify this code, please write to: romanov73@gmail.com. + */ -import org.hibernate.annotations.Fetch; -import org.hibernate.annotations.FetchMode; +package ru.ulstu.extractor.model; -import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.FetchType; -import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import java.util.ArrayList; @@ -19,9 +19,7 @@ public class Branch extends BaseEntity { @ManyToOne private Repository repository; - @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) - @JoinColumn(name = "branch_id", unique = true) - @Fetch(FetchMode.SUBSELECT) + @OneToMany(fetch = FetchType.LAZY, mappedBy = "branch") private List commits = new ArrayList<>(); public Branch() { diff --git a/src/main/java/ru/ulstu/extractor/model/Changes.java b/src/main/java/ru/ulstu/extractor/model/Changes.java deleted file mode 100644 index 101dc0b..0000000 --- a/src/main/java/ru/ulstu/extractor/model/Changes.java +++ /dev/null @@ -1,19 +0,0 @@ -package ru.ulstu.extractor.model; - -import java.util.ArrayList; -import java.util.List; - -public class Changes { - private List fileChanges = new ArrayList<>(); - - public Changes() { - } - - public Changes(List fileChanges) { - this.fileChanges = fileChanges; - } - - public List getFileChanges() { - return fileChanges; - } -} diff --git a/src/main/java/ru/ulstu/extractor/model/Commit.java b/src/main/java/ru/ulstu/extractor/model/Commit.java index 821d3d8..f539609 100644 --- a/src/main/java/ru/ulstu/extractor/model/Commit.java +++ b/src/main/java/ru/ulstu/extractor/model/Commit.java @@ -23,13 +23,13 @@ public class Commit extends BaseEntity { private String hash; private Date date; private String message; - @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @ManyToOne(fetch = FetchType.LAZY) private Author author; @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @JoinColumn(name = "commit_id", unique = true) @Fetch(FetchMode.SUBSELECT) private List fileChanges = new ArrayList<>(); - @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @ManyToOne(fetch = FetchType.LAZY) private Branch branch; public Commit() { diff --git a/src/main/java/ru/ulstu/extractor/model/FileChange.java b/src/main/java/ru/ulstu/extractor/model/FileChange.java index 80d1b8f..5f33725 100644 --- a/src/main/java/ru/ulstu/extractor/model/FileChange.java +++ b/src/main/java/ru/ulstu/extractor/model/FileChange.java @@ -13,17 +13,16 @@ import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.JoinColumn; import javax.persistence.OneToMany; -import javax.persistence.Transient; import java.util.ArrayList; import java.util.List; @Entity public class FileChange extends BaseEntity { private String file; - @Transient - private boolean removed; - @Transient - private boolean added; + + private Boolean removed; + + private Boolean added; private Boolean containsEntity; @@ -45,11 +44,11 @@ public class FileChange extends BaseEntity { return file; } - public boolean getRemoved() { + public Boolean getRemoved() { return removed; } - public boolean getAdded() { + public Boolean getAdded() { return added; } @@ -73,11 +72,11 @@ public class FileChange extends BaseEntity { this.lineChanges = lineChanges; } - public boolean isRemoved() { + public Boolean isRemoved() { return removed; } - public boolean isAdded() { + public Boolean isAdded() { return added; } diff --git a/src/main/java/ru/ulstu/extractor/model/LineChange.java b/src/main/java/ru/ulstu/extractor/model/LineChange.java index 7901322..ff237e7 100644 --- a/src/main/java/ru/ulstu/extractor/model/LineChange.java +++ b/src/main/java/ru/ulstu/extractor/model/LineChange.java @@ -1,14 +1,18 @@ +/* + * Copyright (C) 2021 Anton Romanov - All Rights Reserved + * You may use, distribute and modify this code, please write to: romanov73@gmail.com. + */ + package ru.ulstu.extractor.model; import javax.persistence.Entity; -import javax.persistence.Transient; @Entity public class LineChange extends BaseEntity { - @Transient - private boolean added; - @Transient - private boolean removed; + + private Boolean added; + + private Boolean removed; private String lineFrom; private String lineTo; @@ -48,19 +52,19 @@ public class LineChange extends BaseEntity { return lineTo; } - public boolean isAdded() { + public Boolean isAdded() { return added; } - public void setAdded(boolean added) { + public void setAdded(Boolean added) { this.added = added; } - public boolean isRemoved() { + public Boolean isRemoved() { return removed; } - public void setRemoved(boolean removed) { + public void setRemoved(Boolean removed) { this.removed = removed; } } diff --git a/src/main/java/ru/ulstu/extractor/repository/AuthorRepository.java b/src/main/java/ru/ulstu/extractor/repository/AuthorRepository.java index a99a1b1..c2739d0 100644 --- a/src/main/java/ru/ulstu/extractor/repository/AuthorRepository.java +++ b/src/main/java/ru/ulstu/extractor/repository/AuthorRepository.java @@ -16,4 +16,6 @@ import java.util.List; public interface AuthorRepository extends JpaRepository { @Query("SELECT DISTINCT a.name FROM Commit c, Repository r, Branch b, Author a WHERE c.author = a AND c.branch = b AND r = b.repository AND r = :repository AND b.name = :branchName AND a.name IS NOT NULL AND a.name <> '' ORDER BY a.name") List findByRepositoryAndBranch(@Param("repository") Repository repository, @Param("branchName") String branchName); + + List findByName(String name); } diff --git a/src/main/java/ru/ulstu/extractor/service/AuthorService.java b/src/main/java/ru/ulstu/extractor/service/AuthorService.java new file mode 100644 index 0000000..d829740 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/service/AuthorService.java @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2021 Anton Romanov - All Rights Reserved + * You may use, distribute and modify this code, please write to: romanov73@gmail.com. + */ + +package ru.ulstu.extractor.service; + +import org.springframework.stereotype.Service; +import ru.ulstu.extractor.model.Author; +import ru.ulstu.extractor.repository.AuthorRepository; + +import javax.transaction.Transactional; +import java.util.Optional; + +@Service +public class AuthorService { + private final AuthorRepository authorRepository; + + public AuthorService(AuthorRepository authorRepository) { + this.authorRepository = authorRepository; + } + + @Transactional + public Author findOrCreate(Author author) { + Optional newAuthor = authorRepository.findByName(author.getName()).stream().findAny(); + if (newAuthor.isEmpty()) { + return authorRepository.save(author); + } + return newAuthor.get(); + } +} diff --git a/src/main/java/ru/ulstu/extractor/service/BranchService.java b/src/main/java/ru/ulstu/extractor/service/BranchService.java new file mode 100644 index 0000000..a38997f --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/service/BranchService.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2021 Anton Romanov - All Rights Reserved + * You may use, distribute and modify this code, please write to: romanov73@gmail.com. + */ + +package ru.ulstu.extractor.service; + +import org.springframework.stereotype.Service; +import ru.ulstu.extractor.model.Branch; +import ru.ulstu.extractor.model.Commit; +import ru.ulstu.extractor.model.Repository; +import ru.ulstu.extractor.repository.BranchRepository; + +import javax.transaction.Transactional; +import java.util.List; + +@Service +public class BranchService { + private final BranchRepository branchRepository; + private final CommitService commitService; + + public BranchService(BranchRepository branchRepository, + CommitService commitService) { + this.branchRepository = branchRepository; + this.commitService = commitService; + } + + @Transactional + public Branch save(Branch branch, List commits) { + List commitsToRemove = branch.getCommits(); + branch.getCommits().clear(); + commitService.delete(commitsToRemove); + branch.setCommits(commitService.save(commits)); + return branchRepository.save(branch); + } + + public Branch findByRepositoryAndName(Repository repository, String branchName) { + return branchRepository.findByRepositoryAndName(repository, branchName); + } +} diff --git a/src/main/java/ru/ulstu/extractor/service/CommitService.java b/src/main/java/ru/ulstu/extractor/service/CommitService.java index 62073d6..7b76509 100644 --- a/src/main/java/ru/ulstu/extractor/service/CommitService.java +++ b/src/main/java/ru/ulstu/extractor/service/CommitService.java @@ -1,3 +1,8 @@ +/* + * Copyright (C) 2021 Anton Romanov - All Rights Reserved + * You may use, distribute and modify this code, please write to: romanov73@gmail.com. + */ + package ru.ulstu.extractor.service; import org.eclipse.jgit.api.errors.GitAPIException; @@ -7,17 +12,26 @@ import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import ru.ulstu.extractor.model.Commit; +import ru.ulstu.extractor.repository.CommitRepository; +import javax.transaction.Transactional; import java.io.IOException; import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; @Service public class CommitService { private final GitRepositoryService gitRepositoryService; + private final CommitRepository commitRepository; + private final AuthorService authorService; - public CommitService(GitRepositoryService gitRepositoryService) { + public CommitService(GitRepositoryService gitRepositoryService, + CommitRepository commitRepository, + AuthorService authorService) { this.gitRepositoryService = gitRepositoryService; + this.commitRepository = commitRepository; + this.authorService = authorService; } public Page findPaginated(Pageable pageable, String repositoryUrl, String branchName) throws GitAPIException, IOException { @@ -34,6 +48,19 @@ public class CommitService { } return new PageImpl<>(commits, PageRequest.of(currentPage, pageSize), commits.size()); } + + public void delete(List commitsToRemove) { + commitRepository.deleteAll(commitsToRemove); + } + + @Transactional + public List save(List commits) { + return commits.stream() + .map(commit -> { + commit.setAuthor(authorService.findOrCreate(commit.getAuthor())); + return commitRepository.save(commit); + }).collect(Collectors.toList()); + } } diff --git a/src/main/java/ru/ulstu/extractor/service/IndexService.java b/src/main/java/ru/ulstu/extractor/service/IndexService.java index 44b7414..74d1782 100644 --- a/src/main/java/ru/ulstu/extractor/service/IndexService.java +++ b/src/main/java/ru/ulstu/extractor/service/IndexService.java @@ -12,7 +12,6 @@ import org.springframework.transaction.annotation.Transactional; import ru.ulstu.extractor.model.Branch; import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.model.Repository; -import ru.ulstu.extractor.repository.BranchRepository; import ru.ulstu.extractor.repository.CommitRepository; import ru.ulstu.extractor.repository.RepositoryRepository; @@ -23,17 +22,20 @@ import java.util.List; public class IndexService { private final GitRepositoryService gitRepositoryService; private final RepositoryRepository repositoryRepository; - private final BranchRepository branchRepository; + private final BranchService branchService; private final CommitRepository commitRepository; + private final AuthorService authorService; public IndexService(GitRepositoryService gitRepositoryService, RepositoryRepository repositoryRepository, - BranchRepository branchRepository, - CommitRepository commitRepository) { + BranchService branchService, + CommitRepository commitRepository, + AuthorService authorService) { this.gitRepositoryService = gitRepositoryService; this.repositoryRepository = repositoryRepository; - this.branchRepository = branchRepository; + this.branchService = branchService; this.commitRepository = commitRepository; + this.authorService = authorService; } @Transactional @@ -42,15 +44,11 @@ public class IndexService { if (repository == null) { repository = repositoryRepository.save(new Repository(repositoryUrl)); } - Branch branch = branchRepository.findByRepositoryAndName(repository, branchName); + Branch branch = branchService.findByRepositoryAndName(repository, branchName); if (branch == null) { - branch = branchRepository.save(new Branch(repository, branchName)); + branch = new Branch(repository, branchName); } List commits = gitRepositoryService.getCommits(repositoryUrl, branchName); - List commitsToRemove = branch.getCommits(); - branch.getCommits().clear(); - commitRepository.deleteAll(commitsToRemove); - branch.setCommits(commits); - branchRepository.save(branch); + branchService.save(branch, commits); } } diff --git a/src/main/resources/db/changelog-20210412_100000-schema.xml b/src/main/resources/db/changelog-20210412_100000-schema.xml index 83f9844..0711a60 100644 --- a/src/main/resources/db/changelog-20210412_100000-schema.xml +++ b/src/main/resources/db/changelog-20210412_100000-schema.xml @@ -17,4 +17,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/templates/listBranches.html b/src/main/resources/templates/listBranches.html index 4d1b008..596a990 100644 --- a/src/main/resources/templates/listBranches.html +++ b/src/main/resources/templates/listBranches.html @@ -12,13 +12,22 @@ Ветки + + th:text="${branch.name}"/> + + + + + + diff --git a/src/main/resources/templates/listRepositories.html b/src/main/resources/templates/listRepositories.html index b2787df..8e07b46 100644 --- a/src/main/resources/templates/listRepositories.html +++ b/src/main/resources/templates/listRepositories.html @@ -8,18 +8,27 @@ xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
- - - - - - - - - - - -
Репозиторий
+
+ + + + + + + + + + + + + +
Репозиторий
+ + + +
+