Liran Zelkha has uploaded a new change for review.

Change subject: core: WIP move Job, Step to JPA
......................................................................

core: WIP move Job, Step to JPA

Moving the Job and Step entities to JPA

Change-Id: Ifcfda7b055d37c92c1346b100101c27d594d21fb
Signed-off-by: lzel...@redhat.com <lzel...@redhat.com>
---
M backend/manager/modules/bll/src/main/resources/META-INF/persistence.xml
M 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/job/Job.java
M 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/job/Step.java
M 
backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/GuidMapper.java
M 
backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/JobDaoDbFacadeImpl.java
M 
backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/StepDaoDbFacadeImpl.java
M 
backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/JobDaoTest.java
M 
backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/StepDaoTest.java
M backend/manager/modules/dal/src/test/resources/META-INF/persistence.xml
M packaging/dbscripts/job_sp.sql
10 files changed, 218 insertions(+), 289 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/53/34553/1

diff --git 
a/backend/manager/modules/bll/src/main/resources/META-INF/persistence.xml 
b/backend/manager/modules/bll/src/main/resources/META-INF/persistence.xml
index c172996..34e004e 100644
--- a/backend/manager/modules/bll/src/main/resources/META-INF/persistence.xml
+++ b/backend/manager/modules/bll/src/main/resources/META-INF/persistence.xml
@@ -6,6 +6,8 @@
         <provider>org.hibernate.ejb.HibernatePersistence</provider>
         <non-jta-data-source>java:/ENGINEDataSource</non-jta-data-source>
         <class>org.ovirt.engine.core.common.businessentities.Bookmark</class>
+        <class>org.ovirt.engine.core.common.job.Job</class>
+        <class>org.ovirt.engine.core.common.job.Step</class>        
         <properties>
             <property name="hibernate.dialect" 
value="org.hibernate.dialect.PostgreSQLDialect" />
             <property name="jboss.entity.manager.jndi.name" 
value="java:/ovirtEM"/>
diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/job/Job.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/job/Job.java
index a15dd1b..568ec62 100644
--- 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/job/Job.java
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/job/Job.java
@@ -5,6 +5,18 @@
 import java.util.List;
 import java.util.Map;
 
+import javax.persistence.CascadeType;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.EnumType;
+import javax.persistence.Enumerated;
+import javax.persistence.Id;
+import javax.persistence.OneToMany;
+import javax.persistence.Table;
+
+import org.hibernate.annotations.LazyCollection;
+import org.hibernate.annotations.LazyCollectionOption;
+import org.hibernate.annotations.Type;
 import org.ovirt.engine.core.common.VdcObjectType;
 import org.ovirt.engine.core.common.action.VdcActionType;
 import org.ovirt.engine.core.common.businessentities.BusinessEntity;
@@ -16,6 +28,8 @@
  * of steps which describe portions of the entire Job. The Job entity is 
capable to produce a descriptive tree of
  * steps, reflecting the action parts.
  */
+@Entity
+@Table(name = "job")
 public class Job extends IVdcQueryable implements BusinessEntity<Guid> {
 
     /**
@@ -26,71 +40,93 @@
     /**
      * The Job ID uniquely identifies a disk in the system.
      */
+
+    @Id
+    @Column(name = "job_id")
+    @Type(type = "org.ovirt.engine.core.dao.GuidMapper")
     private Guid id;
 
     /**
      * The action type which the Job describes
      */
+    @Column(name = "action_type")
+    @Enumerated(EnumType.STRING)
     private VdcActionType actionType;
 
     /**
      * The description of the job
      */
+    @Column(name = "description")
     private String description;
 
     /**
      * The status of the job
      */
+    @Column(name = "status")
+    @Enumerated(EnumType.STRING)
     private JobExecutionStatus status;
 
     /**
      * The user id which invoked the job
      */
+    @Column(name = "owner_id")
+    @Type(type = "org.ovirt.engine.core.dao.GuidMapper")
     private Guid ownerId;
 
     /**
      * Determines whether the Job should be presented
      */
+    @Column(name = "visible")
     private boolean isVisible;
 
     /**
      * The start time of the Job
      */
+    @Column(name = "start_time")
     private Date startTime;
 
     /**
      * The end time of the Job
      */
+    @Column(name = "end_time")
     private Date endTime;
 
     /**
      * Describes when the Job was last updated
      */
+    @Column(name = "last_update_time")
     private Date lastUpdateTime;
 
     /**
      * A pass-thru string to identify one or more Jobs cross-layer
      */
+    @Column(name = "correlation_id")
     private String correlationId;
 
     /**
      * A flag defining if this Job were invoked from external plug-in
      */
+    @Column(name = "is_external")
     private boolean external;
 
     /**
      * A flag indicating if the Job is auto cleared from the table after the 
configured time for succeeded/failed jobs
      */
+    @Column(name = "is_auto_cleared")
     private boolean autoCleared;
 
     /**
      * A collection which holds the entities associated with the Job
      */
-    private Map<Guid, VdcObjectType> jobSubjectEntities;
+    //TODO: FIX
+    private transient Map<Guid, VdcObjectType> jobSubjectEntities;
 
     /**
      * A collection which stores the steps of the Job
      */
+
+    @LazyCollection(LazyCollectionOption.FALSE)
+    @OneToMany(cascade = { CascadeType.ALL }, mappedBy = "jobId", 
orphanRemoval = true)
     private List<Step> steps;
 
     /**
diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/job/Step.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/job/Step.java
index b9bb1bf..c4f4d6f 100644
--- 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/job/Step.java
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/job/Step.java
@@ -4,6 +4,19 @@
 import java.util.Date;
 import java.util.List;
 
+import javax.persistence.CascadeType;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.EnumType;
+import javax.persistence.Enumerated;
+import javax.persistence.Id;
+import javax.persistence.OneToMany;
+import javax.persistence.OrderBy;
+import javax.persistence.Table;
+
+import org.hibernate.annotations.LazyCollection;
+import org.hibernate.annotations.LazyCollectionOption;
+import org.hibernate.annotations.Type;
 import org.ovirt.engine.core.common.businessentities.BusinessEntity;
 import org.ovirt.engine.core.common.businessentities.IVdcQueryable;
 import org.ovirt.engine.core.compat.Guid;
@@ -12,6 +25,8 @@
  * represents a meaningful phase of the Job. A Step could be a parent of other 
steps (e.g. step named EXECUTION could
  * have a list of steps beneath it which are also part of the job)
  */
+@Entity
+@Table(name = "step")
 public class Step extends IVdcQueryable implements BusinessEntity<Guid> {
 
     /**
@@ -22,66 +37,98 @@
     /**
      * The Step ID uniquely identifies a disk in the system.
      */
+    @Id
+    @Column(name = "step_id")
+    @Type(type = "org.ovirt.engine.core.dao.GuidMapper")
     private Guid id;
 
     /**
      * The job which the step comprises
      */
+    @Column(name = "job_id")
+    @Type(type = "org.ovirt.engine.core.dao.GuidMapper")
     private Guid jobId;
 
     /**
      * The direct parent step of the current step
      */
+    @Column(name = "parent_step_id")
+    @Type(type = "org.ovirt.engine.core.dao.GuidMapper")
     private Guid parentStepId;
 
     /**
      * The step type
      */
+    @Column(name = "step_type")
+    @Enumerated(EnumType.STRING)
     private StepEnum stepType;
 
     /**
      * The description of the step
      */
+    @Column(name = "description")
     private String description;
 
     /**
      * The order of the step in current hierarchy level
      */
+    @Column(name = "step_number")
     private int stepNumber;
 
     /**
      * The status of the step
      */
+    @Column(name = "status")
+    @Enumerated(EnumType.STRING)
     private JobExecutionStatus status;
 
     /**
      * The start time of the step
      */
+    @Column(name = "start_time")
     private Date startTime;
 
     /**
      * The end time of the step
      */
+    @Column(name = "end_time")
     private Date endTime;
 
     /**
      * A pass-thru string to identify this step as part of a wider action
      */
+    @Column(name = "correlation_id")
     private String correlationId;
 
     /**
      * A flag defining if this step were invoked from external plug-in
      */
+    @Column(name = "is_external")
     private boolean external;
 
     /**
      * An external system referenced by the step (e.g. VDSM)
      */
-    private ExternalSystem externalSystem;
+    @Column(name = "external_system_type")
+    @Enumerated(EnumType.STRING)
+    private ExternalSystemType externalSystemType;
+
+    // TODO: FIX
+    private transient ExternalSystem externalSystem;
+
+    /**
+     * The ID of the external system
+     */
+    @Column(name = "external_id")
+    @Type(type = "org.ovirt.engine.core.dao.GuidMapper")
+    private Guid externalId;
 
     /**
      * The successors steps
      */
+    @LazyCollection(LazyCollectionOption.FALSE)
+    @OneToMany(cascade = { CascadeType.ALL }, mappedBy = "parentStepId", 
orphanRemoval = true)
+    @OrderBy("stepNumber")
     private List<Step> steps;
 
     public Step() {
@@ -198,6 +245,14 @@
         this.external = isExternal;
     }
 
+    public Guid getExternalId() {
+        return externalId;
+    }
+
+    public void setExternalId(Guid externalId) {
+        this.externalId = externalId;
+    }
+
     public Step addStep(StepEnum childStepType, String description) {
         Step childStep = new Step(childStepType);
         childStep.setParentStepId(id);
@@ -292,6 +347,7 @@
         result = prime * result + stepNumber;
         result = prime * result + ((stepType == null) ? 0 : 
stepType.hashCode());
         result = prime * result + ((steps == null) ? 0 : steps.hashCode());
+        result = prime * result + ((externalId == null) ? 0 : 
externalId.hashCode());
         result = prime * result + (external ? 1231 : 1237);
         return result;
     }
@@ -383,6 +439,9 @@
         if (external != other.external) {
             return false;
         }
+        if (externalId != other.externalId) {
+            return false;
+        }
         return true;
     }
 
diff --git 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/GuidMapper.java
 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/GuidMapper.java
index 808db61..e43dba0 100644
--- 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/GuidMapper.java
+++ 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/GuidMapper.java
@@ -51,6 +51,12 @@
 
     @Override
     public boolean equals(Object x, Object y) throws HibernateException {
+        if (x == null && y == null) {
+            return true;
+        }
+        if (x == null) {
+            return false;
+        }
         return x.equals(y);
     }
 
diff --git 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/JobDaoDbFacadeImpl.java
 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/JobDaoDbFacadeImpl.java
index 70cecfa..e501bfc 100644
--- 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/JobDaoDbFacadeImpl.java
+++ 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/JobDaoDbFacadeImpl.java
@@ -1,55 +1,18 @@
 package org.ovirt.engine.core.dao;
 
-import java.sql.ResultSet;
-import java.sql.SQLException;
+import java.util.ArrayList;
 import java.util.Date;
+import java.util.EnumSet;
 import java.util.List;
 
 import org.apache.commons.lang.StringUtils;
-import org.ovirt.engine.core.common.action.VdcActionType;
 import org.ovirt.engine.core.common.job.Job;
 import org.ovirt.engine.core.common.job.JobExecutionStatus;
-import org.ovirt.engine.core.common.utils.EnumUtils;
+import org.ovirt.engine.core.common.job.Step;
 import org.ovirt.engine.core.compat.Guid;
-import org.ovirt.engine.core.dal.dbbroker.DbFacadeUtils;
-import org.springframework.jdbc.core.RowMapper;
 import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
 
-public class JobDaoDbFacadeImpl extends DefaultGenericDaoDbFacade<Job, Guid> 
implements JobDao {
-
-    private static JobRowMapper jobRowMapper = new JobRowMapper();
-
-    public JobDaoDbFacadeImpl() {
-        super("Job");
-        setProcedureNameForGetAll("GetAllJobs");
-    }
-
-    @Override
-    protected MapSqlParameterSource createIdParameterMapper(Guid id) {
-        return getCustomMapSqlParameterSource().addValue("job_id", id);
-    }
-
-    @Override
-    protected MapSqlParameterSource createFullParametersMapper(Job entity) {
-        return createIdParameterMapper(entity.getId())
-                .addValue("action_type", 
EnumUtils.nameOrNull(entity.getActionType()))
-                .addValue("description", entity.getDescription())
-                .addValue("status", EnumUtils.nameOrNull(entity.getStatus()))
-                .addValue("owner_id", entity.getOwnerId())
-                .addValue("visible", entity.isVisible())
-                .addValue("start_time", entity.getStartTime())
-                .addValue("end_time", entity.getEndTime())
-                .addValue("last_update_time", entity.getLastUpdateTime())
-                .addValue("correlation_id", entity.getCorrelationId())
-                .addValue("is_external", entity.isExternal())
-                .addValue("is_auto_cleared", entity.isAutoCleared());
-    }
-
-    @Override
-    protected RowMapper<Job> createEntityRowMapper() {
-        return jobRowMapper;
-    }
-
+public class JobDaoDbFacadeImpl extends HibernateFacade<Job, Guid> implements 
JobDao {
     @Override
     public boolean exists(Guid id) {
         return get(id) != null;
@@ -57,27 +20,24 @@
 
     @Override
     public List<Job> getJobsByOffsetAndPageSize(int offset, int pageSize) {
-        MapSqlParameterSource parameterSource = 
getCustomMapSqlParameterSource()
-                .addValue("position", offset)
-                .addValue("page_size", pageSize);
+        return super.multiResults(getEntityManager().createNativeQuery("call 
GetJobsByOffsetAndPageSize(:offset,:pageSize)",
+                Job.class)
+                .setParameter("offset", offset)
+                .setParameter("pageSize", pageSize));
 
-        return getCallsHandler().executeReadList("GetJobsByOffsetAndPageSize", 
createEntityRowMapper(), parameterSource);
     }
 
     @Override
     public List<Job> getJobsByCorrelationId(String correlationId) {
-        MapSqlParameterSource parameterSource = 
getCustomMapSqlParameterSource()
-                .addValue("correlation_id", correlationId);
-
-        return getCallsHandler().executeReadList("GetJobsByCorrelationId", 
createEntityRowMapper(), parameterSource);
+        return super.multiResults(getEntityManager().createQuery("select j 
from Job j where j.correlationId = :correlationId")
+                .setParameter("correlationId", correlationId));
     }
 
     @Override
     public void updateJobLastUpdateTime(Guid jobId, Date lastUpdateTime) {
-        MapSqlParameterSource parameterSource = 
getCustomMapSqlParameterSource()
-                .addValue("job_id", jobId)
-                .addValue("last_update_time", lastUpdateTime);
-        getCallsHandler().executeModification("UpdateJobLastUpdateTime", 
parameterSource);
+        Job job = get(jobId);
+        job.setLastUpdateTime(lastUpdateTime);
+        update(job);
     }
 
     @Override
@@ -102,39 +62,26 @@
 
     @Override
     public void deleteCompletedJobs(Date succeededJobs, Date failedJobs) {
-        MapSqlParameterSource parameterSource = 
getCustomMapSqlParameterSource()
-                .addValue("succeeded_end_time", succeededJobs)
-                .addValue("failed_end_time", failedJobs);
-        
getCallsHandler().executeModification("DeleteCompletedJobsOlderThanDate", 
parameterSource);
-
+        getEntityManager().createQuery("delete from job j where j.autoCleared 
and ((endTime < :failedEndTime and status = :failStatus) or (endTime < 
:successEndTime and status in :successStatus)")
+                .setParameter("successEndTime", succeededJobs)
+                .setParameter("failedEndTime", failedJobs)
+                .setParameter("failStatus",
+                        EnumSet.of(JobExecutionStatus.FAILED, 
JobExecutionStatus.ABORTED, JobExecutionStatus.UNKNOWN))
+                .setParameter("successStatus", JobExecutionStatus.FINISHED)
+                .executeUpdate();
     }
 
     @Override
     public boolean checkIfJobHasTasks(Guid jobId) {
-        MapSqlParameterSource parameterSource = 
getCustomMapSqlParameterSource().addValue("job_id", jobId);
-        return getCallsHandler().executeRead("CheckIfJobHasTasks", 
createBooleanMapper(), parameterSource);
+        List<Step> steps = new ArrayList<Step>();
+        // super.multiResults(getEntityManager().createQuery("select s from 
Step s where s.jobId = :jobId and s.externalId is not null and s.externalSystem 
in :systemType")
+        // .setParameter("jobId", jobId)
+        // .setParameter("systemType", EnumSet.of(ExternalSystemType.VDSM, 
ExternalSystemType.GLUSTER)));
+        return steps.size() > 0;
     }
 
-    private static class JobRowMapper implements RowMapper<Job> {
-
-        @Override
-        public Job mapRow(ResultSet rs, int rowNum) throws SQLException {
-            Job job = new Job();
-
-            job.setId(getGuidDefaultEmpty(rs, "job_id"));
-            
job.setActionType(VdcActionType.valueOf(rs.getString("action_type")));
-            job.setDescription(rs.getString("description"));
-            job.setStatus(JobExecutionStatus.valueOf(rs.getString("status")));
-            job.setOwnerId(getGuid(rs, "owner_id"));
-            job.setVisible(rs.getBoolean("visible"));
-            
job.setStartTime(DbFacadeUtils.fromDate(rs.getTimestamp("start_time")));
-            
job.setEndTime(DbFacadeUtils.fromDate(rs.getTimestamp("end_time")));
-            
job.setLastUpdateTime(DbFacadeUtils.fromDate(rs.getTimestamp("last_update_time")));
-            job.setCorrelationId(rs.getString("correlation_id"));
-            job.setExternal(rs.getBoolean("is_external"));
-            job.setAutoCleared(rs.getBoolean("is_auto_cleared"));
-            return job;
-        }
+    @Override
+    public List<Job> getAll() {
+        return super.multiResults(getEntityManager().createQuery("select j 
from Job j"));
     }
-
 }
diff --git 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/StepDaoDbFacadeImpl.java
 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/StepDaoDbFacadeImpl.java
index 8c52eb0..c1094e1 100644
--- 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/StepDaoDbFacadeImpl.java
+++ 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/StepDaoDbFacadeImpl.java
@@ -11,107 +11,57 @@
 import org.ovirt.engine.core.common.job.StepEnum;
 import org.ovirt.engine.core.common.utils.EnumUtils;
 import org.ovirt.engine.core.compat.Guid;
+import org.ovirt.engine.core.dal.dbbroker.DbFacade;
 import org.ovirt.engine.core.dal.dbbroker.DbFacadeUtils;
 import org.springframework.jdbc.core.RowMapper;
 import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
 
-public class StepDaoDbFacadeImpl extends DefaultGenericDaoDbFacade<Step, Guid> 
implements StepDao {
-
-    private static StepRowMapper stepRowMapper = new StepRowMapper();
-
-    public StepDaoDbFacadeImpl() {
-        super("Step");
-        setProcedureNameForGetAll("GetAllSteps");
-    }
-
+public class StepDaoDbFacadeImpl extends HibernateFacade<Step, Guid> 
implements StepDao {
     @Override
     public boolean exists(Guid id) {
         return get(id) != null;
     }
 
     @Override
-    protected MapSqlParameterSource createIdParameterMapper(Guid id) {
-        return getCustomMapSqlParameterSource().addValue("step_id", id);
-    }
-
-    @Override
-    protected MapSqlParameterSource createFullParametersMapper(Step entity) {
-        return createIdParameterMapper(entity.getId())
-                .addValue("parent_step_id", entity.getParentStepId())
-                .addValue("job_id", entity.getJobId())
-                .addValue("step_type", 
EnumUtils.nameOrNull(entity.getStepType()))
-                .addValue("description", entity.getDescription())
-                .addValue("step_number", entity.getStepNumber())
-                .addValue("status", EnumUtils.nameOrNull(entity.getStatus()))
-                .addValue("start_time", entity.getStartTime())
-                .addValue("end_time", entity.getEndTime())
-                .addValue("correlation_id", entity.getCorrelationId())
-                .addValue("external_id", entity.getExternalSystem().getId())
-                .addValue("external_system_type", 
EnumUtils.nameOrNull(entity.getExternalSystem().getType()))
-                .addValue("is_external", entity.isExternal());
-    }
-
-    @Override
-    protected RowMapper<Step> createEntityRowMapper() {
-        return stepRowMapper;
-    }
-
-    @Override
     public List<Step> getStepsByJobId(Guid jobId) {
-        MapSqlParameterSource parameterSource = 
getCustomMapSqlParameterSource().addValue("job_id", jobId);
-        return getCallsHandler().executeReadList("GetStepsByJobId", 
createEntityRowMapper(), parameterSource);
+        return DbFacade.getInstance().getJobDao().get(jobId).getSteps();
     }
 
     @Override
     public List<Step> getStepsByParentStepId(Guid parentStepId) {
-        MapSqlParameterSource parameterSource =
-                getCustomMapSqlParameterSource().addValue("parent_step_id", 
parentStepId);
-        return getCallsHandler().executeReadList("GetStepsByParentStepId", 
createEntityRowMapper(), parameterSource);
+        return get(parentStepId).getSteps();
     }
 
     @Override
     public void updateJobStepsCompleted(Guid jobId, JobExecutionStatus status, 
Date endTime) {
-        MapSqlParameterSource parameterSource =
-                getCustomMapSqlParameterSource().addValue("job_id", jobId)
-                        .addValue("status", status.name())
-                        .addValue("end_time", endTime);
-        getCallsHandler().executeModification("updateJobStepsCompleted", 
parameterSource);
-
-    }
-
-    private static class StepRowMapper implements RowMapper<Step> {
-
-        @Override
-        public Step mapRow(ResultSet rs, int rowNum) throws SQLException {
-            Step step = new Step();
-            step.setId(getGuidDefaultEmpty(rs, "step_id"));
-            step.setParentStepId(getGuid(rs, "parent_step_id"));
-            step.setJobId(getGuidDefaultEmpty(rs, "job_id"));
-            step.setStepType(StepEnum.valueOf(rs.getString("step_type")));
-            step.setDescription(rs.getString("description"));
-            step.setStepNumber(rs.getInt("step_number"));
-            step.setStatus(JobExecutionStatus.valueOf(rs.getString("status")));
-            
step.setStartTime(DbFacadeUtils.fromDate(rs.getTimestamp("start_time")));
-            
step.setEndTime(DbFacadeUtils.fromDate(rs.getTimestamp("end_time")));
-            step.setCorrelationId(rs.getString("correlation_id"));
-            step.getExternalSystem().setId(getGuid(rs, "external_id"));
-            
step.getExternalSystem().setType(ExternalSystemType.safeValueOf(rs.getString("external_system_type")));
-            step.setExternal(rs.getBoolean("is_external"));
-            return step;
+        if (status != JobExecutionStatus.STARTED) {
+            getEntityManager().createQuery("update Step s set s.status = 
:status, s.endTime = :endTime where s.status = :startedStatus and s.jobId = 
:jobId")
+                    .setParameter("jobId", jobId)
+                    .setParameter("status", status)
+                    .setParameter("endTime", endTime)
+                    .setParameter("startedStatus", JobExecutionStatus.STARTED)
+                    .executeUpdate();
         }
     }
 
     @Override
     public List<Step> getStepsByExternalId(Guid externalId) {
-        MapSqlParameterSource parameterSource = 
getCustomMapSqlParameterSource().addValue("external_id", externalId);
-        return getCallsHandler().executeReadList("GetStepsByExternalTaskId", 
createEntityRowMapper(), parameterSource);
+        return super.multiResults(getEntityManager().createQuery("select s 
from Step s where s.externalId = :externalId order by s.parentStepId, 
s.stepNumber")
+                .setParameter("externalId", externalId));
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public List<Guid> getExternalIdsForRunningSteps(ExternalSystemType 
systemType) {
+        return getEntityManager().createNativeQuery("call 
GetExternalIdsFromSteps(:external_system_type,:status",
+                Guid.class)
+                .setParameter(1, systemType.name())
+                .setParameter(2, JobExecutionStatus.STARTED.name())
+                .getResultList();
     }
 
     @Override
-    public List<Guid> getExternalIdsForRunningSteps(ExternalSystemType 
systemType) {
-        MapSqlParameterSource parameterSource = 
getCustomMapSqlParameterSource().addValue("external_system_type", 
systemType.name())
-                                                .addValue("status", 
JobExecutionStatus.STARTED.name());
-        return getCallsHandler().executeReadList("GetExternalIdsFromSteps", 
createGuidMapper(), parameterSource);
+    public List<Step> getAll() {
+        return super.multiResults(getEntityManager().createQuery("select s 
from Step s"));
     }
-
 }
diff --git 
a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/JobDaoTest.java
 
b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/JobDaoTest.java
index b098489..d7e8485 100644
--- 
a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/JobDaoTest.java
+++ 
b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/JobDaoTest.java
@@ -4,14 +4,22 @@
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
+import java.io.InputStream;
 import java.text.DateFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Arrays;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.Persistence;
 
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
 import org.ovirt.engine.core.common.action.VdcActionType;
 import org.ovirt.engine.core.common.job.Job;
@@ -26,6 +34,27 @@
     private static final int NUMBER_OF_JOBS_FOR_EXISTING_CORRELATION_ID = 1;
     private static final int TOTAL_JOBS = 6;
 
+    @BeforeClass
+    public static void initTestCase() throws Exception {
+        BaseDAOTestCase.initTestCase();
+
+        Properties properties = new Properties();
+
+        InputStream is = null;
+        is = 
BaseDAOTestCase.class.getResourceAsStream("/test-database.properties");
+        properties.load(is);
+
+        Map<String, String> props = new HashMap<>();
+        props.put("javax.persistence.jdbc.driver", 
properties.getProperty("database.driver"));
+        props.put("javax.persistence.jdbc.user", 
properties.getProperty("database.username"));
+        props.put("javax.persistence.jdbc.password", 
properties.getProperty("database.password"));
+        props.put("javax.persistence.jdbc.url", 
properties.getProperty("database.url"));
+
+        EntityManagerFactory emf = 
Persistence.createEntityManagerFactory("ovirt-test", props);
+
+        HibernateFacade.setEntityManagerFactory(emf);
+    }
+
     @Override
     @Before
     public void setUp() throws Exception {
diff --git 
a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/StepDaoTest.java
 
b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/StepDaoTest.java
index 6008347..d47c373 100644
--- 
a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/StepDaoTest.java
+++ 
b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/StepDaoTest.java
@@ -5,10 +5,18 @@
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
+import java.io.InputStream;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.Persistence;
 
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
 import org.ovirt.engine.core.common.job.ExternalSystemType;
 import org.ovirt.engine.core.common.job.JobExecutionStatus;
@@ -31,6 +39,27 @@
     private static final Guid REBALANCING_GLUSTER_VOLUME_STEP_ID = new 
Guid("cd75984e-1fd4-48fb-baf8-e45800a61a66");
     private static final int TOTAL_STEPS_OF_REBALANCING_GLUSTER_VOLUME = 1;
 
+    @BeforeClass
+    public static void initTestCase() throws Exception {
+        BaseDAOTestCase.initTestCase();
+
+        Properties properties = new Properties();
+
+        InputStream is = null;
+        is = 
BaseDAOTestCase.class.getResourceAsStream("/test-database.properties");
+        properties.load(is);
+
+        Map<String, String> props = new HashMap<>();
+        props.put("javax.persistence.jdbc.driver", 
properties.getProperty("database.driver"));
+        props.put("javax.persistence.jdbc.user", 
properties.getProperty("database.username"));
+        props.put("javax.persistence.jdbc.password", 
properties.getProperty("database.password"));
+        props.put("javax.persistence.jdbc.url", 
properties.getProperty("database.url"));
+
+        EntityManagerFactory emf = 
Persistence.createEntityManagerFactory("ovirt-test", props);
+
+        HibernateFacade.setEntityManagerFactory(emf);
+    }
+
     @Override
     @Before
     public void setUp() throws Exception {
diff --git 
a/backend/manager/modules/dal/src/test/resources/META-INF/persistence.xml 
b/backend/manager/modules/dal/src/test/resources/META-INF/persistence.xml
index 22bacfc..910b18f 100644
--- a/backend/manager/modules/dal/src/test/resources/META-INF/persistence.xml
+++ b/backend/manager/modules/dal/src/test/resources/META-INF/persistence.xml
@@ -2,6 +2,8 @@
     <persistence-unit name="ovirt-test">
         <provider>org.hibernate.ejb.HibernatePersistence</provider>
         <class>org.ovirt.engine.core.common.businessentities.Bookmark</class>
+        <class>org.ovirt.engine.core.common.job.Job</class>
+        <class>org.ovirt.engine.core.common.job.Step</class>        
         <properties>
             <property name="javax.persistence.jdbc.driver" 
value="${engine.db.driver}" />
             <property name="javax.persistence.jdbc.user" 
value="${engine.db.username}" />
diff --git a/packaging/dbscripts/job_sp.sql b/packaging/dbscripts/job_sp.sql
index 9856d77..1968ac0 100644
--- a/packaging/dbscripts/job_sp.sql
+++ b/packaging/dbscripts/job_sp.sql
@@ -94,65 +94,6 @@
 END; $procedure$
 LANGUAGE plpgsql;
 
------------------------------------------------
--- Retrieves All Job entities by Correlation-ID
------------------------------------------------
-Create or replace FUNCTION GetJobsByCorrelationId(v_correlation_id VARCHAR(50))
-RETURNS SETOF job STABLE
-AS $procedure$
-BEGIN
-    RETURN QUERY SELECT job.*
-    FROM JOB
-    WHERE correlation_id = v_correlation_id;
-END; $procedure$
-LANGUAGE plpgsql;
-
-----------------------------------
--- Updates Job entity in Job table
-----------------------------------
-Create or replace FUNCTION UpdateJob(
-    v_job_id UUID,
-    v_action_type VARCHAR(50),
-    v_description TEXT,
-    v_status VARCHAR(32),
-    v_owner_id UUID,
-    v_visible BOOLEAN,
-    v_start_time TIMESTAMP WITH TIME ZONE,
-    v_end_time TIMESTAMP WITH TIME ZONE,
-    v_last_update_time TIMESTAMP WITH TIME ZONE,
-    v_correlation_id VARCHAR(50))
-RETURNS VOID
-AS $procedure$
-BEGIN
-    update job
-    SET action_type = v_action_type,
-        description = v_description,
-        status = v_status,
-        owner_id = v_owner_id,
-        visible = v_visible,
-        start_time = v_start_time,
-        end_time = v_end_time,
-        last_update_time = v_last_update_time,
-        correlation_id = v_correlation_id
-    WHERE job_id = v_job_id;
-END; $procedure$
-LANGUAGE plpgsql;
-
--------------------------------------------------------
--- Updates Job entity for last update time in Job table
--------------------------------------------------------
-Create or replace FUNCTION UpdateJobLastUpdateTime(
-    v_job_id UUID,
-    v_last_update_time TIMESTAMP WITH TIME ZONE)
-RETURNS VOID
-AS $procedure$
-BEGIN
-    update job
-    SET last_update_time = v_last_update_time
-    WHERE job_id = v_job_id;
-END; $procedure$
-LANGUAGE plpgsql;
-
 --------------------------------------------
 -- Deletes Job entity by status and end time
 --------------------------------------------
@@ -378,20 +319,6 @@
 END; $procedure$
 LANGUAGE plpgsql;
 
-------------------------------------------------------------
--- Gets Step entities list from Step table by parent-step-id
-------------------------------------------------------------
-Create or replace FUNCTION GetStepsByParentStepId(v_parent_step_id UUID)
-RETURNS SETOF step STABLE
-AS $procedure$
-BEGIN
-    RETURN QUERY SELECT step.*
-    FROM step
-    WHERE parent_step_id = v_parent_step_id
-    ORDER BY step_number;
-END; $procedure$
-LANGUAGE plpgsql;
-
 ---------------------------------------------
 -- Gets Step entity from Step table by Job-Id
 ---------------------------------------------
@@ -416,27 +343,6 @@
     WHERE step_id = v_step_id;
 END; $procedure$
 LANGUAGE plpgsql;
-
-----------------------------------------------
--- Updates steps related to a Job as completed
-----------------------------------------------
-Create or replace FUNCTION updateJobStepsCompleted(
-    v_job_id UUID,
-    v_status VARCHAR(32),
-    v_end_time TIMESTAMP WITH TIME ZONE
-    )
-RETURNS VOID
-AS $procedure$
-BEGIN
-    UPDATE step
-    SET status = v_status,
-        end_time = v_end_time
-    WHERE job_id = v_job_id
-    AND   status = 'STARTED'
-    AND   STATUS != v_status;
-END; $procedure$
-LANGUAGE plpgsql;
-
 
 -------------------------------------------
 -- Updates Job and Step statuses to UNKNOWN
@@ -483,43 +389,6 @@
     DELETE FROM job
     WHERE job_id IN
           (SELECT job_id from step WHERE (status = 'STARTED' AND 
step_type='MIGRATE_VM'));
-END; $procedure$
-LANGUAGE plpgsql;
-
---------------------------------------------
--- Cleanup Jobs entities by end time
---------------------------------------------
-Create or replace FUNCTION DeleteCompletedJobsOlderThanDate(
-    v_succeeded_end_time TIMESTAMP WITH TIME ZONE,
-    v_failed_end_time TIMESTAMP WITH TIME ZONE
-    )
-RETURNS VOID
-AS $procedure$
-BEGIN
-    DELETE FROM job
-    WHERE (is_auto_cleared
-    AND ((end_time < v_succeeded_end_time
-    AND    status = 'FINISHED')
-    OR    (end_time < v_failed_end_time
-    AND    status IN ('FAILED', 'ABORTED', 'UNKNOWN'))));
-END; $procedure$
-LANGUAGE plpgsql;
-
--------------------------------------
--- Checks if a Job has step for tasks
--------------------------------------
-Create or replace FUNCTION CheckIfJobHasTasks(
-    v_job_id UUID)
-RETURNS SETOF booleanResultType STABLE
-AS $procedure$
-BEGIN
-    RETURN QUERY
-    SELECT EXISTS(
-        SELECT *
-        FROM   step
-        WHERE  job_id = v_job_id
-        AND    external_id is not null
-        AND    external_system_type in ('VDSM','GLUSTER'));
 END; $procedure$
 LANGUAGE plpgsql;
 


-- 
To view, visit http://gerrit.ovirt.org/34553
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ifcfda7b055d37c92c1346b100101c27d594d21fb
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Liran Zelkha <lzel...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to