#16 -- Add JPA. Create entity classes, repositories and database changelogs.
parent
736ee1b3d8
commit
99415a1a22
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
@ -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> {
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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>
|
Loading…
Reference in New Issue