From 00e809a06369df3249a5f89c7e998b184618dbd2 Mon Sep 17 00:00:00 2001
From: Zoltan Runyo <zoltan.runyo@ess.eu>
Date: Thu, 25 Aug 2022 15:37:25 +0200
Subject: [PATCH] ICSHWI-10137: Implement IOC instance repository

---
 .../api/IIocInstanceRepository.java           |  19 +++
 .../repository/entity/IocInstanceEntity.java  | 144 ++++++++++++++++++
 .../impl/IocInstanceRepository.java           |  71 +++++++++
 3 files changed, 234 insertions(+)
 create mode 100644 src/main/java/eu/ess/ics/ce/template/repository/api/IIocInstanceRepository.java
 create mode 100644 src/main/java/eu/ess/ics/ce/template/repository/entity/IocInstanceEntity.java
 create mode 100644 src/main/java/eu/ess/ics/ce/template/repository/impl/IocInstanceRepository.java

diff --git a/src/main/java/eu/ess/ics/ce/template/repository/api/IIocInstanceRepository.java b/src/main/java/eu/ess/ics/ce/template/repository/api/IIocInstanceRepository.java
new file mode 100644
index 00000000..a3fe2a68
--- /dev/null
+++ b/src/main/java/eu/ess/ics/ce/template/repository/api/IIocInstanceRepository.java
@@ -0,0 +1,19 @@
+package eu.ess.ics.ce.template.repository.api;
+
+import eu.ess.ics.ce.template.repository.entity.IocInstanceEntity;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.List;
+
+/**
+ * @author <a href="mailto:zoltan.runyo@ess.eu">Zoltan Runyo</a>
+ **/
+public interface IIocInstanceRepository {
+
+    @Transactional
+    void create(IocInstanceEntity iocInstance);
+
+    List<IocInstanceEntity> findAll(Long templateId, int page, int limit);
+
+    long countForPaging(Long templateId);
+}
diff --git a/src/main/java/eu/ess/ics/ce/template/repository/entity/IocInstanceEntity.java b/src/main/java/eu/ess/ics/ce/template/repository/entity/IocInstanceEntity.java
new file mode 100644
index 00000000..53ae4464
--- /dev/null
+++ b/src/main/java/eu/ess/ics/ce/template/repository/entity/IocInstanceEntity.java
@@ -0,0 +1,144 @@
+package eu.ess.ics.ce.template.repository.entity;
+
+import org.hibernate.annotations.CreationTimestamp;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.ManyToOne;
+import javax.persistence.Table;
+import java.util.Date;
+
+/**
+ * @author <a href="mailto:zoltan.runyo@ess.eu">Zoltan Runyo</a>
+ **/
+@Entity
+@Table(name = "ioc_instance")
+public class IocInstanceEntity {
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    private Long id;
+
+    @ManyToOne
+    @JoinColumn(name = "template_id")
+    private TemplateEntity template;
+
+    @Column(name = "template_git_project_id")
+    private Long templateGitProjectId;
+
+    @Column(name = "template_revision")
+    private String templateRevision;
+
+    @Column(name = "configuration_git_project_id")
+    private Long configurationGitProjectId;
+
+    @Column(name = "configuration_revision")
+    private String configurationRevision;
+
+    @Column(name = "created_by")
+    private String createdBy;
+
+    @CreationTimestamp
+    @Column(name = "created_at")
+    private Date createdAt;
+
+    @Column(name = "git_project_id")
+    private Long gitProjectId;
+
+    @Column(name = "git_revision")
+    private String gitRevision;
+
+    @Column(name = "git_tag")
+    private String gitTag;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public TemplateEntity getTemplate() {
+        return template;
+    }
+
+    public void setTemplate(TemplateEntity template) {
+        this.template = template;
+    }
+
+    public Long getTemplateGitProjectId() {
+        return templateGitProjectId;
+    }
+
+    public void setTemplateGitProjectId(Long templateGitProjectId) {
+        this.templateGitProjectId = templateGitProjectId;
+    }
+
+    public String getTemplateRevision() {
+        return templateRevision;
+    }
+
+    public void setTemplateRevision(String templateRevision) {
+        this.templateRevision = templateRevision;
+    }
+
+    public Long getConfigurationGitProjectId() {
+        return configurationGitProjectId;
+    }
+
+    public void setConfigurationGitProjectId(Long configurationGitProjectId) {
+        this.configurationGitProjectId = configurationGitProjectId;
+    }
+
+    public String getConfigurationRevision() {
+        return configurationRevision;
+    }
+
+    public void setConfigurationRevision(String configurationRevision) {
+        this.configurationRevision = configurationRevision;
+    }
+
+    public String getCreatedBy() {
+        return createdBy;
+    }
+
+    public void setCreatedBy(String createdBy) {
+        this.createdBy = createdBy;
+    }
+
+    public Date getCreatedAt() {
+        return createdAt;
+    }
+
+    public void setCreatedAt(Date createdAt) {
+        this.createdAt = createdAt;
+    }
+
+    public Long getGitProjectId() {
+        return gitProjectId;
+    }
+
+    public void setGitProjectId(Long gitProjectId) {
+        this.gitProjectId = gitProjectId;
+    }
+
+    public String getGitRevision() {
+        return gitRevision;
+    }
+
+    public void setGitRevision(String gitRevision) {
+        this.gitRevision = gitRevision;
+    }
+
+    public String getGitTag() {
+        return gitTag;
+    }
+
+    public void setGitTag(String gitTag) {
+        this.gitTag = gitTag;
+    }
+}
diff --git a/src/main/java/eu/ess/ics/ce/template/repository/impl/IocInstanceRepository.java b/src/main/java/eu/ess/ics/ce/template/repository/impl/IocInstanceRepository.java
new file mode 100644
index 00000000..aa02d489
--- /dev/null
+++ b/src/main/java/eu/ess/ics/ce/template/repository/impl/IocInstanceRepository.java
@@ -0,0 +1,71 @@
+package eu.ess.ics.ce.template.repository.impl;
+
+import eu.ess.ics.ce.template.repository.api.IIocInstanceRepository;
+import eu.ess.ics.ce.template.repository.entity.IocInstanceEntity;
+import org.springframework.stereotype.Repository;
+
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.persistence.TypedQuery;
+import javax.persistence.criteria.CriteriaBuilder;
+import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Expression;
+import javax.persistence.criteria.Predicate;
+import javax.persistence.criteria.Root;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author <a href="mailto:zoltan.runyo@ess.eu">Zoltan Runyo</a>
+ **/
+@Repository
+public class IocInstanceRepository implements IIocInstanceRepository {
+    @PersistenceContext
+    private EntityManager em;
+
+
+    @Override
+    public void create(IocInstanceEntity iocInstance) {
+        em.persist(iocInstance);
+    }
+
+    @Override
+    public List<IocInstanceEntity> findAll(Long templateId, int page, int limit) {
+        CriteriaBuilder cb = em.getCriteriaBuilder();
+
+        CriteriaQuery<IocInstanceEntity> cq = cb.createQuery(IocInstanceEntity.class);
+        Root<IocInstanceEntity> from = cq.from(IocInstanceEntity.class);
+        CriteriaQuery<IocInstanceEntity> select = cq.select(from);
+
+        TypedQuery<IocInstanceEntity> q = pagingQuery(cb, select, from, cq, templateId);
+        q.setFirstResult(page * limit);
+        q.setMaxResults(limit);
+
+        return q.getResultList();
+    }
+
+    @Override
+    public long countForPaging(Long templateId) {
+        CriteriaBuilder cb = em.getCriteriaBuilder();
+
+        CriteriaQuery<Long> cq = cb.createQuery(Long.class);
+        Root<IocInstanceEntity> from = cq.from(IocInstanceEntity.class);
+        Expression<Long> count1 = cb.count(from);
+        CriteriaQuery<Long> select = cq.select(count1);
+
+        return pagingQuery(cb, select, from, cq, templateId)
+                .getSingleResult();
+    }
+
+    private <T, R> TypedQuery<T> pagingQuery(CriteriaBuilder cb, CriteriaQuery<T> select, Root<R> from,
+                                             CriteriaQuery<T> cq, Long templateId) {
+        List<Predicate> predicates = new ArrayList<>();
+
+        if (templateId != null) {
+            predicates.add(cb.equal(from.get("template_id"), templateId));
+        }
+
+        select.where(cb.and(predicates.toArray(new Predicate[0])));
+        return em.createQuery(cq);
+    }
+}
-- 
GitLab