All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openfact.models.jpa.JpaJobReportProvider Maven / Gradle / Ivy

There is a newer version: 1.1.3.Final
Show newest version
package org.openfact.models.jpa;

import org.jboss.logging.Logger;
import org.openfact.models.*;
import org.openfact.models.jpa.entities.JobReportEntity;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.List;
import java.util.stream.Collectors;

@Stateless
public class JpaJobReportProvider implements JobReportProvider {

    protected static final Logger logger = Logger.getLogger(JpaJobReportProvider.class);

    @PersistenceContext
    protected EntityManager em;

    static AdminJobReport convertAdminJobReport(JobReportEntity jobReportEntity) {
        AdminJobReport adminJobReport = new AdminJobReport();

        adminJobReport.setId(jobReportEntity.getId());
        adminJobReport.setJobName(jobReportEntity.getJobName());
        adminJobReport.setDuration(jobReportEntity.getDuration());
        adminJobReport.setStartTime(jobReportEntity.getStartTime());
        adminJobReport.setEndTime(jobReportEntity.getEndTime());
        adminJobReport.setErrorCount(jobReportEntity.getErrorCount());
        adminJobReport.setReadCount(jobReportEntity.getReadCount());
        adminJobReport.setWriteCount(jobReportEntity.getWriteCount());

        return adminJobReport;
    }

    @Override
    public JobReportQuery createQuery(OrganizationModel organization) {
        return new JpaJobReportQuery(em, organization);
    }

    @Override
    public JobReportModel createJobReport(OrganizationModel organization, String jobReportName) throws ModelException {
        JobReportEntity entity = new JobReportEntity();
        entity.setJobName(jobReportName);
        entity.setOrganizationId(organization.getId());
        em.persist(entity);
        em.flush();
        return new JobReportAdapter(organization, em, entity);
    }

    @Override
    public JobReportModel getJobReportById(OrganizationModel organization, String id) {
        JobReportEntity entity = em.find(JobReportEntity.class, id);
        if (entity != null) {
            return new JobReportAdapter(organization, em, entity);
        }
        return null;
    }

    @Override
    public List getJobReports(OrganizationModel organization) {
        return getJobReports(organization, -1, -1);
    }

    @Override
    public List getJobReports(OrganizationModel organization, Integer firstResult, Integer maxResults) {
        TypedQuery query = em.createNamedQuery("getAllJobReportsByOrganization", JobReportEntity.class);
        query.setParameter("organizationId", organization.getId());
        if (firstResult != -1) {
            query.setFirstResult(firstResult);
        }
        if (maxResults != -1) {
            query.setMaxResults(maxResults);
        }
        List results = query.getResultList();
        return results.stream().map(f -> new JobReportAdapter(organization, em, f)).collect(Collectors.toList());
    }

    @Override
    public boolean removeJobReport(OrganizationModel organization, String id) {
        JobReportEntity entity = em.find(JobReportEntity.class, id);
        if (entity != null) {
            em.remove(entity);
            em.flush();
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean removeJobReport(OrganizationModel organization, JobReportModel jobReport) {
        return removeJobReport(organization, jobReport.getId());
    }

    @Override
    public void preRemove(OrganizationModel organization) {
        em.createNamedQuery("deleteJobReportsByOrganization").setParameter("organizationId", organization.getId()).executeUpdate();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy