#16 -- Add JPA. Create entity classes, repositories and database changelogs.

This commit is contained in:
Anton Romanov 2021-03-29 11:38:27 +04:00
parent 736ee1b3d8
commit 99415a1a22
21 changed files with 437 additions and 78 deletions

View File

@ -1,6 +1,6 @@
buildscript {
ext {
versionSpringBoot = '2.3.8.RELEASE'
versionSpringBoot = '2.3.9.RELEASE'
}
repositories {
@ -47,11 +47,10 @@ dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-jetty'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'
compile group: 'nz.net.ultraq.thymeleaf', name: 'thymeleaf-layout-dialect'
compile group: 'org.springframework.data', name: 'spring-data-commons'
compile group: 'com.fasterxml.jackson.module', name: 'jackson-module-afterburner'
compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc'
compile group: 'org.postgresql', name: 'postgresql', version: '9.4.1212'
compile group: 'org.liquibase', name: 'liquibase-core', version: '4.3.1'

View File

@ -38,7 +38,7 @@ public class GitFilteringController {
@PostMapping("/sendFilter")
public String sendFilter(@ModelAttribute FilterForm filterForm, Model model) throws GitAPIException, IOException {
List<Commit> list = gitRepositoryService.getCommits(filterForm.getUrl());
List<Commit> list = gitRepositoryService.getCommits(filterForm.getUrl(), filterForm.getBranch());
model.addAttribute("commits", list);
if (filterForm.getFilter() == null || filterForm.getFilter().isEmpty()) {
model.addAttribute("error", "'Строка' не должно быть пустым");
@ -53,11 +53,11 @@ public class GitFilteringController {
@ModelAttribute FilterForm filterForm,
@RequestParam("page") Optional<Integer> page,
@RequestParam("size") Optional<Integer> size,
@RequestParam String url,
@RequestParam String branch) throws GitAPIException, IOException {
@RequestParam String repositoryUrl,
@RequestParam String branchName) throws GitAPIException, IOException {
int currentPage = page.orElse(1);
int pageSize = size.orElse(5);
Page<Commit> commitPage = commitService.findPaginated(PageRequest.of(currentPage - 1, pageSize), url);
Page<Commit> commitPage = commitService.findPaginated(PageRequest.of(currentPage - 1, pageSize), repositoryUrl, branchName);
model.addAttribute("commitPage", commitPage);
int totalPages = commitPage.getTotalPages();
if (totalPages > 0) {

View File

@ -11,15 +11,19 @@ import ru.ulstu.extractor.model.Branch;
import ru.ulstu.extractor.model.mvc.FilterForm;
import ru.ulstu.extractor.model.mvc.RepoForm;
import ru.ulstu.extractor.service.GitRepositoryService;
import ru.ulstu.extractor.service.IndexService;
import java.util.List;
@Controller
public class GitIndexingController {
private final GitRepositoryService gitRepositoryService;
private final IndexService indexService;
public GitIndexingController(GitRepositoryService gitRepositoryService) {
public GitIndexingController(GitRepositoryService gitRepositoryService,
IndexService indexService) {
this.gitRepositoryService = gitRepositoryService;
this.indexService = indexService;
}
@GetMapping("/newRepo")
@ -47,6 +51,12 @@ public class GitIndexingController {
if (repoForm.getBranch() == null) {
return "newRepo";
} else {
try {
indexService.index(repoForm.getRepo(), repoForm.getBranch());
} catch (Exception ex) {
model.addAttribute("error", ex.getMessage());
return "newRepo";
}
redirectAttributes.addAttribute("url", repoForm.getRepo());
redirectAttributes.addAttribute("branch", repoForm.getBranch());
return "redirect:/filtering";

View File

@ -29,8 +29,9 @@ public class RepoController {
}
@GetMapping("commits")
public List<Commit> getCommits(@RequestParam("url") String url) throws GitAPIException, IOException {
return gitRepositoryService.getCommits(url);
public List<Commit> getCommits(@RequestParam("repositoryUrl") String repositoryUrl,
@RequestParam("branchName") String branchName) throws GitAPIException, IOException {
return gitRepositoryService.getCommits(repositoryUrl, branchName);
}
}

View File

@ -0,0 +1,23 @@
package ru.ulstu.extractor.model;
import javax.persistence.Entity;
@Entity
public class Author extends BaseEntity {
private String name;
public Author() {
}
public Author(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,86 @@
package ru.ulstu.extractor.model;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;
import java.io.Serializable;
@MappedSuperclass
public abstract class BaseEntity implements Serializable, Comparable {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Integer id;
@Version
private Integer version;
public BaseEntity() {
}
public BaseEntity(Integer id, Integer version) {
this.id = id;
this.version = version;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getVersion() {
return version;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!getClass().isAssignableFrom(obj.getClass())) {
return false;
}
BaseEntity other = (BaseEntity) obj;
if (id == null) {
if (other.id != null) {
return false;
}
} else if (!id.equals(other.id)) {
return false;
}
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (id == null ? 0 : id.hashCode());
return result;
}
@Override
public String toString() {
return getClass().getSimpleName() + "{" +
"id=" + id +
", version=" + version +
'}';
}
@Override
public int compareTo(Object o) {
return id != null ? id.compareTo(((BaseEntity) o).getId()) : -1;
}
public void reset() {
this.id = null;
this.version = null;
}
}

View File

@ -1,14 +1,61 @@
package ru.ulstu.extractor.model;
public class Branch {
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
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.List;
@Entity
public class Branch extends BaseEntity {
private String name;
@ManyToOne
private Repository repository;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "branch_id", unique = true)
@Fetch(FetchMode.SUBSELECT)
private List<Commit> commits;
public Branch() {
}
public Branch(String name) {
this.name = name;
}
public Branch(Repository repository, String branchName) {
this.repository = repository;
this.name = branchName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Repository getRepository() {
return repository;
}
public void setRepository(Repository repository) {
this.repository = repository;
}
public List<Commit> getCommits() {
return commits;
}
public void setCommits(List<Commit> commits) {
this.commits = commits;
}
}

View File

@ -1,18 +1,37 @@
package ru.ulstu.extractor.model;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
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;
import java.util.Date;
import java.util.List;
public class Commit {
private String message;
@Entity
public class Commit extends BaseEntity {
private String hash;
private Date date;
private String author;
private Changes changes;
private String message;
@ManyToOne
private Author author;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "commit_id", unique = true)
@Fetch(FetchMode.SUBSELECT)
private List<FileChange> fileChanges = new ArrayList<>();
public Commit(String message, String author, Date date) {
public Commit() {
}
public Commit(String message, Author author, Date date) {
this.message = message;
this.author = author;
this.date = date;
}
public String getMessage() {
@ -23,15 +42,35 @@ public class Commit {
return date;
}
public String getAuthor() {
public String getHash() {
return hash;
}
public void setHash(String hash) {
this.hash = hash;
}
public void setDate(Date date) {
this.date = date;
}
public void setMessage(String message) {
this.message = message;
}
public void setAuthor(Author author) {
this.author = author;
}
public List<FileChange> getFileChanges() {
return fileChanges;
}
public void setFileChanges(List<FileChange> fileChanges) {
this.fileChanges = fileChanges;
}
public Author getAuthor() {
return author;
}
public Changes getChanges() {
return changes;
}
public void setChanges(Changes changes) {
this.changes = changes;
}
}

View File

@ -1,12 +1,28 @@
package ru.ulstu.extractor.model;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import javax.persistence.CascadeType;
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;
public class FileChange {
@Entity
public class FileChange extends BaseEntity {
private String file;
@Transient
private boolean removed;
@Transient
private boolean added;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "file_change_id", unique = true)
@Fetch(FetchMode.SUBSELECT)
private List<LineChange> lineChanges = new ArrayList<>();
public FileChange() {
@ -47,4 +63,12 @@ public class FileChange {
public void setLineChanges(List<LineChange> lineChanges) {
this.lineChanges = lineChanges;
}
public boolean isRemoved() {
return removed;
}
public boolean isAdded() {
return added;
}
}

View File

@ -1,7 +1,13 @@
package ru.ulstu.extractor.model;
public class LineChange {
import javax.persistence.Entity;
import javax.persistence.Transient;
@Entity
public class LineChange extends BaseEntity {
@Transient
private boolean added;
@Transient
private boolean removed;
private String lineFrom;
private String lineTo;
@ -42,4 +48,19 @@ public class LineChange {
return lineTo;
}
public boolean isAdded() {
return added;
}
public void setAdded(boolean added) {
this.added = added;
}
public boolean isRemoved() {
return removed;
}
public void setRemoved(boolean removed) {
this.removed = removed;
}
}

View File

@ -0,0 +1,23 @@
package ru.ulstu.extractor.model;
import javax.persistence.Entity;
@Entity
public class Repository extends BaseEntity {
private String url;
public Repository() {
}
public Repository(String repositoryUrl) {
url = repositoryUrl;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}

View File

@ -3,6 +3,7 @@ package ru.ulstu.extractor.model.mvc;
public class FilterForm {
private String filter;
private String url;
private String branch;
public FilterForm() {
}
@ -27,6 +28,14 @@ public class FilterForm {
this.url = url;
}
public String getBranch() {
return branch;
}
public void setBranch(String branch) {
this.branch = branch;
}
@Override
public String toString() {
return "FilterForm{" +

View File

@ -0,0 +1,11 @@
package ru.ulstu.extractor.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.extractor.model.Branch;
import ru.ulstu.extractor.model.Repository;
import java.util.Optional;
public interface BranchRepository extends JpaRepository<Branch, Integer> {
Optional<Branch> findByRepositoryAndName(Repository repository, String name);
}

View File

@ -1,42 +1,7 @@
package ru.ulstu.extractor.repository;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.extractor.model.Commit;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@Repository
public class CommitRepository {
private final static String SQL_SELECT_COMMITS = "SELECT * FROM commit";
private final static String SQL_INSERT_COMMITS = "INSERT INTO commit (hash, date, author, message) " +
"VALUES (?, ?, ?, ?)";
private final JdbcTemplate jdbcTemplate;
public CommitRepository(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
private static class CommitRowMapper implements RowMapper<Commit> {
@Override
public Commit mapRow(ResultSet rs, int rowNum) throws SQLException {
return new Commit(rs.getString("message"),
rs.getString("author"),
rs.getDate("date"));
}
}
public List<Commit> getCommits() {
return jdbcTemplate.query(SQL_SELECT_COMMITS, new CommitRowMapper());
}
public void saveCommit(Commit commit) {
jdbcTemplate.update(SQL_INSERT_COMMITS, "", commit.getDate(),
commit.getAuthor(),
commit.getMessage());
}
public interface CommitRepository extends JpaRepository<Commit, Integer> {
}

View File

@ -0,0 +1,10 @@
package ru.ulstu.extractor.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.extractor.model.Repository;
import java.util.Optional;
public interface RepositoryRepository extends JpaRepository<Repository, Integer> {
Optional<Repository> findByUrl(String url);
}

View File

@ -20,11 +20,11 @@ public class CommitService {
this.gitRepositoryService = gitRepositoryService;
}
public Page<Commit> findPaginated(Pageable pageable, String url) throws GitAPIException, IOException {
public Page<Commit> findPaginated(Pageable pageable, String repositoryUrl, String branchName) throws GitAPIException, IOException {
int pageSize = pageable.getPageSize();
int currentPage = pageable.getPageNumber();
int startItem = currentPage * pageSize;
List<Commit> commits = gitRepositoryService.getCommits(url);
List<Commit> commits = gitRepositoryService.getCommits(repositoryUrl, branchName);
if (commits.size() < startItem) {
commits = Collections.emptyList();

View File

@ -1,16 +1,18 @@
package ru.ulstu.extractor.service;
import org.eclipse.jgit.api.CreateBranchCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ListBranchCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import ru.ulstu.extractor.model.Author;
import ru.ulstu.extractor.model.Branch;
import ru.ulstu.extractor.model.Changes;
import ru.ulstu.extractor.model.Commit;
import ru.ulstu.extractor.model.FileChange;
import ru.ulstu.extractor.model.LineChange;
@ -68,13 +70,19 @@ public class GitRepositoryService {
return file.exists();
}
public List<Commit> getCommits(String url) throws GitAPIException, IOException {
cloneOrUpdateRepo(url);
Repository localRepo = new FileRepository(getProjectGitDirectory(url));
public List<Commit> getCommits(String repositoryUrl, String branchName) throws GitAPIException, IOException {
cloneOrUpdateRepo(repositoryUrl);
Repository localRepo = new FileRepository(getProjectGitDirectory(repositoryUrl));
Git git = new Git(localRepo);
git.pull().call();
Ref ref = git.checkout().
setCreateBranch(true).
setName(branchName).
setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).
setStartPoint("origin/" + branchName).
call();
List<RevCommit> commits = new ArrayList<>();
//TODO: сделать преобразование в коллекцию "наших объектов"
git.log().call().forEach(commits::add);
List<Commit> list = new ArrayList<>();
@ -82,10 +90,10 @@ public class GitRepositoryService {
for (RevCommit revCommit : commits) {
Commit commit = new Commit(
revCommit.getFullMessage(),
revCommit.getAuthorIdent().getName(),
new Author(revCommit.getAuthorIdent().getName()),
Date.from(Instant.ofEpochSecond(revCommit.getCommitTime())));
if (prevCommit != null) {
commit.setChanges(findDiffBetweenTwoRevisions(revCommit, prevCommit, localRepo));
commit.setFileChanges(findDiffBetweenTwoRevisions(revCommit, prevCommit, localRepo));
}
list.add(commit);
prevCommit = revCommit;
@ -93,7 +101,7 @@ public class GitRepositoryService {
return list;
}
public Changes findDiffBetweenTwoRevisions(RevCommit laterCommit, RevCommit earlierCommit, Repository localRepo) {
public List<FileChange> findDiffBetweenTwoRevisions(RevCommit laterCommit, RevCommit earlierCommit, Repository localRepo) {
if (laterCommit == null || earlierCommit == null) {
return null;
}
@ -109,8 +117,8 @@ public class GitRepositoryService {
return parseOutputDiff(output);
}
private Changes parseOutputDiff(String output) {
Changes changes = new Changes();
private List<FileChange> parseOutputDiff(String output) {
List<FileChange> changes = new ArrayList<>();
String[] strings = output.split("\n");
FileChange fileChange = new FileChange();
int stringsLength = strings.length - 1;
@ -121,7 +129,7 @@ public class GitRepositoryService {
fileChange = new FileChange();
fileChange.setFile(maybeFileName.get());
/// вытащить другие изменения из коммита
changes.getFileChanges().add(fileChange);
changes.add(fileChange);
}
LineChange lineChange = new LineChange();
if (strings[i].startsWith("-")) {

View File

@ -0,0 +1,41 @@
package ru.ulstu.extractor.service;
import com.sun.istack.NotNull;
import org.eclipse.jgit.api.errors.GitAPIException;
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 ru.ulstu.extractor.repository.CommitRepository;
import ru.ulstu.extractor.repository.RepositoryRepository;
import java.io.IOException;
import java.util.List;
@Service
public class IndexService {
private final GitRepositoryService gitRepositoryService;
private final RepositoryRepository repositoryRepository;
private final BranchRepository branchRepository;
private final CommitRepository commitRepository;
public IndexService(GitRepositoryService gitRepositoryService,
RepositoryRepository repositoryRepository,
BranchRepository branchRepository,
CommitRepository commitRepository) {
this.gitRepositoryService = gitRepositoryService;
this.repositoryRepository = repositoryRepository;
this.branchRepository = branchRepository;
this.commitRepository = commitRepository;
}
public void index(@NotNull String repositoryUrl, @NotNull String branchName) throws GitAPIException, IOException {
Repository repository = repositoryRepository.findByUrl(repositoryUrl)
.orElse(repositoryRepository.save(new Repository(repositoryUrl)));
Branch branch = branchRepository.findByRepositoryAndName(repository, branchName)
.orElse(branchRepository.save(new Branch(repository, branchName)));
List<Commit> commits = gitRepositoryService.getCommits(repositoryUrl, branchName);
branch.setCommits(commits);
}
}

View File

@ -11,6 +11,9 @@ spring.datasource.url=jdbc:postgresql://localhost:5432/repo
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.driverclassName=org.postgresql.Driver
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false
# Liquibase Settings
spring.liquibase.drop-first=false
spring.liquibase.enabled=true

View File

@ -0,0 +1,38 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<changeSet author="orion" id="20210329-120000-1">
<addColumn tableName="repository">
<column name="version" type="integer"/>
</addColumn>
<addColumn tableName="branch">
<column name="version" type="integer"/>
</addColumn>
<addColumn tableName="commit">
<column name="version" type="integer"/>
</addColumn>
<addColumn tableName="author">
<column name="version" type="integer"/>
</addColumn>
</changeSet>
<changeSet author="orion" id="20210329-120000-2">
<addColumn tableName="file_change">
<column name="version" type="integer"/>
</addColumn>
<addColumn tableName="line_change">
<column name="version" type="integer"/>
</addColumn>
</changeSet>
<changeSet author="orion" id="20210329-120000-3" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
<createTable tableName="hibernate_sequences">
<column name="sequence_name" type="VARCHAR(255)">
<constraints nullable="false"/>
</column>
<column name="sequence_next_hi_value" type="BIGINT"/>
<column name="next_val" type="bigint"/>
</createTable>
<addPrimaryKey columnNames="sequence_name" constraintName="hibernate_sequences_pkey"
tableName="hibernate_sequences"/>
</changeSet>
</databaseChangeLog>

View File

@ -5,4 +5,5 @@
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<include file="db/changelog-20210317_140000-schema.xml"/>
<include file="db/changelog-20210326_170000-schema.xml"/>
<include file="db/changelog-20210329_120000-schema.xml"/>
</databaseChangeLog>