diff --git a/build.gradle b/build.gradle index 9ef6c17..c356675 100644 --- a/build.gradle +++ b/build.gradle @@ -53,6 +53,7 @@ dependencies { compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5' compile group: 'org.postgresql', name: 'postgresql', version: '9.4.1212' compile group: 'org.liquibase', name: 'liquibase-core', version: '4.3.1' + implementation group: 'commons-io', name: 'commons-io', version: '2.6' compile group: 'io.springfox', name: 'springfox-boot-starter', version: '3.0.0' diff --git a/src/main/java/ru/ulstu/extractor/controller/GitFilteringController.java b/src/main/java/ru/ulstu/extractor/controller/GitFilteringController.java index f4dcde8..85ef6aa 100644 --- a/src/main/java/ru/ulstu/extractor/controller/GitFilteringController.java +++ b/src/main/java/ru/ulstu/extractor/controller/GitFilteringController.java @@ -1,22 +1,18 @@ package ru.ulstu.extractor.controller; -import org.eclipse.jgit.api.errors.GitAPIException; import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import ru.ulstu.extractor.model.Commit; +import ru.ulstu.extractor.model.OffsetablePageRequest; import ru.ulstu.extractor.model.mvc.FilterForm; -import ru.ulstu.extractor.service.CommitService; import ru.ulstu.extractor.service.FilteringService; -import ru.ulstu.extractor.service.GitRepositoryService; +import ru.ulstu.extractor.service.IndexService; -import java.io.IOException; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @@ -25,18 +21,15 @@ import java.util.stream.IntStream; @Controller public class GitFilteringController { private final FilteringService filteringService; - private final GitRepositoryService gitRepositoryService; - private final CommitService commitService; + private final IndexService indexService; public GitFilteringController(FilteringService filteringService, - GitRepositoryService gitRepositoryService, - CommitService commitService) { + IndexService indexService) { this.filteringService = filteringService; - this.gitRepositoryService = gitRepositoryService; - this.commitService = commitService; + this.indexService = indexService; } - @PostMapping("/sendFilter") + /* @PostMapping("/sendFilter") public String sendFilter(@ModelAttribute FilterForm filterForm, Model model) throws GitAPIException, IOException { List list = gitRepositoryService.getCommits(filterForm.getUrl(), filterForm.getBranch()); model.addAttribute("commits", list); @@ -45,7 +38,7 @@ public class GitFilteringController { return "filtering"; } return "resultRepo"; - } + }*/ @RequestMapping(value = "/filtering", method = RequestMethod.GET) public String listCommits( @@ -54,18 +47,23 @@ public class GitFilteringController { @RequestParam("page") Optional page, @RequestParam("size") Optional size, @RequestParam String repositoryUrl, - @RequestParam String branchName) throws GitAPIException, IOException { + @RequestParam String branchName) { int currentPage = page.orElse(1); int pageSize = size.orElse(5); - Page commitPage = commitService.findPaginated(PageRequest.of(currentPage - 1, pageSize), repositoryUrl, branchName); - model.addAttribute("commitPage", commitPage); - int totalPages = commitPage.getTotalPages(); + + Page commitsPage = indexService.getCommits(repositoryUrl, branchName, new OffsetablePageRequest(currentPage, pageSize)); + int totalPages = commitsPage.getTotalPages(); if (totalPages > 0) { List pageNumbers = IntStream.rangeClosed(1, totalPages) .boxed() .collect(Collectors.toList()); model.addAttribute("pageNumbers", pageNumbers); } + filterForm = new FilterForm(); + filterForm.setCommitsPage(commitsPage); + filterForm.setBranch(branchName); + filterForm.setUrl(repositoryUrl); + model.addAttribute("filterForm", filterForm); return "filtering"; } } diff --git a/src/main/java/ru/ulstu/extractor/model/Commit.java b/src/main/java/ru/ulstu/extractor/model/Commit.java index 916f81f..1bbe22f 100644 --- a/src/main/java/ru/ulstu/extractor/model/Commit.java +++ b/src/main/java/ru/ulstu/extractor/model/Commit.java @@ -24,6 +24,8 @@ public class Commit extends BaseEntity { @JoinColumn(name = "commit_id", unique = true) @Fetch(FetchMode.SUBSELECT) private List fileChanges = new ArrayList<>(); + @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + private Branch branch; public Commit() { } @@ -74,4 +76,12 @@ public class Commit extends BaseEntity { public Author getAuthor() { return author; } + + public Branch getBranch() { + return branch; + } + + public void setBranch(Branch branch) { + this.branch = branch; + } } diff --git a/src/main/java/ru/ulstu/extractor/model/OffsetablePageRequest.java b/src/main/java/ru/ulstu/extractor/model/OffsetablePageRequest.java new file mode 100644 index 0000000..e96673b --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/model/OffsetablePageRequest.java @@ -0,0 +1,93 @@ +package ru.ulstu.extractor.model; + +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; + +import java.io.Serializable; + +public class OffsetablePageRequest implements Pageable, Serializable { + private final int offset; + private final int count; + private final Sort sort; + + public OffsetablePageRequest(int page, long pageSize) { + this(pageSize * page, pageSize, Sort.by("id")); + } + + public OffsetablePageRequest(long offset, long count, Sort sort) { + if (offset < 0) { + throw new IllegalArgumentException("Offset value must not be less than zero!"); + } + if (count < 1) { + throw new IllegalArgumentException("Count value must not be less than one!"); + } + this.offset = (int) offset; + this.count = (int) count; + this.sort = sort; + } + + @Override + public Sort getSort() { + return sort; + } + + @Override + public int getPageSize() { + return count; + } + + @Override + public int getPageNumber() { + return offset / count; + } + + @Override + public long getOffset() { + return offset; + } + + @Override + public boolean hasPrevious() { + return offset > 0; + } + + @Override + public Pageable next() { + return new OffsetablePageRequest(getOffset() + getPageSize(), getPageSize(), getSort()); + } + + @Override + public Pageable previousOrFirst() { + return hasPrevious() ? previous() : first(); + } + + public Pageable previous() { + return getOffset() == 0 ? this : new OffsetablePageRequest(getOffset() - getPageSize(), getPageSize(), getSort()); + } + + @Override + public Pageable first() { + return new OffsetablePageRequest(0, getPageSize(), getSort()); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null || getClass() != obj.getClass()) { + return false; + } + final OffsetablePageRequest other = (OffsetablePageRequest) obj; + return this.offset == other.offset && this.count == other.count; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + offset; + result = prime * result + count; + return result; + } +} diff --git a/src/main/java/ru/ulstu/extractor/model/mvc/FilterForm.java b/src/main/java/ru/ulstu/extractor/model/mvc/FilterForm.java index 4946871..6180b13 100644 --- a/src/main/java/ru/ulstu/extractor/model/mvc/FilterForm.java +++ b/src/main/java/ru/ulstu/extractor/model/mvc/FilterForm.java @@ -1,9 +1,13 @@ package ru.ulstu.extractor.model.mvc; +import org.springframework.data.domain.Page; +import ru.ulstu.extractor.model.Commit; + public class FilterForm { private String filter; private String url; private String branch; + private Page commitsPage; public FilterForm() { } @@ -36,6 +40,14 @@ public class FilterForm { this.branch = branch; } + public Page getCommitsPage() { + return commitsPage; + } + + public void setCommitsPage(Page commitsPage) { + this.commitsPage = commitsPage; + } + @Override public String toString() { return "FilterForm{" + diff --git a/src/main/java/ru/ulstu/extractor/repository/AuthorRepository.java b/src/main/java/ru/ulstu/extractor/repository/AuthorRepository.java new file mode 100644 index 0000000..504aaf2 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/repository/AuthorRepository.java @@ -0,0 +1,14 @@ +package ru.ulstu.extractor.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import ru.ulstu.extractor.model.Author; +import ru.ulstu.extractor.model.Repository; + +import java.util.List; + +public interface AuthorRepository extends JpaRepository { + @Query("SELECT a 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") + List findByRepositoryAndBranch(@Param("repository") Repository repository, @Param("branchName") String branchName); +} diff --git a/src/main/java/ru/ulstu/extractor/repository/CommitRepository.java b/src/main/java/ru/ulstu/extractor/repository/CommitRepository.java index fd386d4..3c32439 100644 --- a/src/main/java/ru/ulstu/extractor/repository/CommitRepository.java +++ b/src/main/java/ru/ulstu/extractor/repository/CommitRepository.java @@ -1,7 +1,14 @@ package ru.ulstu.extractor.repository; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import ru.ulstu.extractor.model.Commit; +import ru.ulstu.extractor.model.Repository; public interface CommitRepository extends JpaRepository { + @Query("SELECT c FROM Commit c, Repository r, Branch b WHERE c.branch = b AND r = b.repository AND r = :repository AND b.name = :branchName") + Page findByRepositoryAndBranch(Pageable pageable, @Param("repository") Repository repository, @Param("branchName") String branchName); } diff --git a/src/main/java/ru/ulstu/extractor/service/FilteringService.java b/src/main/java/ru/ulstu/extractor/service/FilteringService.java index 254437d..b6b7cf7 100644 --- a/src/main/java/ru/ulstu/extractor/service/FilteringService.java +++ b/src/main/java/ru/ulstu/extractor/service/FilteringService.java @@ -1,32 +1,13 @@ package ru.ulstu.extractor.service; import org.springframework.stereotype.Service; -import ru.ulstu.extractor.model.Commit; - -import java.util.List; -import java.util.stream.Collectors; @Service public class FilteringService { - private final GitRepositoryService gitRepositoryService; + private final IndexService indexService; - public FilteringService(GitRepositoryService gitRepositoryService) { - this.gitRepositoryService = gitRepositoryService; - } - - public List getCommits(String urlRepo) { - return getCommits(urlRepo, null); - } - - public List getCommits(String urlRepo, String filterCommitMessage) { - if (filterCommitMessage != null) { - return getCommits(urlRepo) - .stream() - .filter(commit -> commit.getMessage().contains(filterCommitMessage)) - .collect(Collectors.toList()); - } else { - return getCommits(urlRepo); - } + public FilteringService(IndexService indexService) { + this.indexService = indexService; } } diff --git a/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java b/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java index ef26349..63082d0 100644 --- a/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java +++ b/src/main/java/ru/ulstu/extractor/service/GitRepositoryService.java @@ -213,4 +213,8 @@ public class GitRepositoryService { .map(r -> new Branch(r.getName().replace(BRANCH_PREFIX, ""))) .collect(Collectors.toList()); } + + public void remove(String repositoryUrl) throws IOException { + //FileUtils.deleteDirectory(getProjectDirectoryFile(repositoryUrl)); + } } diff --git a/src/main/java/ru/ulstu/extractor/service/IndexService.java b/src/main/java/ru/ulstu/extractor/service/IndexService.java index 413685f..581921e 100644 --- a/src/main/java/ru/ulstu/extractor/service/IndexService.java +++ b/src/main/java/ru/ulstu/extractor/service/IndexService.java @@ -2,10 +2,14 @@ package ru.ulstu.extractor.service; import com.sun.istack.NotNull; import org.eclipse.jgit.api.errors.GitAPIException; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; +import ru.ulstu.extractor.model.Author; import ru.ulstu.extractor.model.Branch; import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.model.Repository; +import ru.ulstu.extractor.repository.AuthorRepository; import ru.ulstu.extractor.repository.BranchRepository; import ru.ulstu.extractor.repository.CommitRepository; import ru.ulstu.extractor.repository.RepositoryRepository; @@ -19,15 +23,19 @@ public class IndexService { private final RepositoryRepository repositoryRepository; private final BranchRepository branchRepository; private final CommitRepository commitRepository; + private final AuthorRepository authorRepository; + public IndexService(GitRepositoryService gitRepositoryService, RepositoryRepository repositoryRepository, BranchRepository branchRepository, - CommitRepository commitRepository) { + CommitRepository commitRepository, + AuthorRepository authorRepository) { this.gitRepositoryService = gitRepositoryService; this.repositoryRepository = repositoryRepository; this.branchRepository = branchRepository; this.commitRepository = commitRepository; + this.authorRepository = authorRepository; } public void index(@NotNull String repositoryUrl, @NotNull String branchName) throws GitAPIException, IOException { @@ -42,5 +50,14 @@ public class IndexService { List commits = gitRepositoryService.getCommits(repositoryUrl, branchName); branch.setCommits(commits); branchRepository.save(branch); + gitRepositoryService.remove(repositoryUrl); + } + + public List getRepositoryAuthors(@NotNull String repositoryUrl, @NotNull String branchName) { + return authorRepository.findByRepositoryAndBranch(repositoryRepository.findByUrl(repositoryUrl), branchName); + } + + public Page getCommits(@NotNull String repositoryUrl, @NotNull String branchName, Pageable pageable) { + return commitRepository.findByRepositoryAndBranch(pageable, repositoryRepository.findByUrl(repositoryUrl), branchName); } } diff --git a/src/main/resources/templates/filtering.html b/src/main/resources/templates/filtering.html index 8a8bca1..365ac17 100644 --- a/src/main/resources/templates/filtering.html +++ b/src/main/resources/templates/filtering.html @@ -38,8 +38,8 @@ - - + + @@ -50,7 +50,7 @@

-