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

com.github.lontime.extjobrunr.beans.JobBean Maven / Gradle / Ivy

package com.github.lontime.extjobrunr.beans;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import com.github.lontime.extbeans.BeanHelper;
import com.github.lontime.extjobrunr.beans.states.DeletedStateBean;
import com.github.lontime.extjobrunr.beans.states.EnqueuedStateBean;
import com.github.lontime.extjobrunr.beans.states.FailedStateBean;
import com.github.lontime.extjobrunr.beans.states.JobStateBean;
import com.github.lontime.extjobrunr.beans.states.ProcessingStateBean;
import com.github.lontime.extjobrunr.beans.states.ScheduledStateBean;
import com.github.lontime.extjobrunr.beans.states.SucceededStateBean;
import org.jobrunr.jobs.Job;
import org.jobrunr.jobs.context.JobDashboardLogger.JobDashboardLogLines;
import org.jobrunr.jobs.states.DeletedState;
import org.jobrunr.jobs.states.EnqueuedState;
import org.jobrunr.jobs.states.FailedState;
import org.jobrunr.jobs.states.JobState;
import org.jobrunr.jobs.states.ProcessingState;
import org.jobrunr.jobs.states.ScheduledState;
import org.jobrunr.jobs.states.StateName;
import org.jobrunr.jobs.states.SucceededState;
import org.joda.beans.Bean;
import org.joda.beans.BeanBuilder;
import org.joda.beans.JodaBeanUtils;
import org.joda.beans.MetaBean;
import org.joda.beans.MetaProperty;
import org.joda.beans.Property;
import org.joda.beans.gen.BeanDefinition;
import org.joda.beans.gen.PropertyDefinition;
import org.joda.beans.impl.direct.DirectBeanBuilder;
import org.joda.beans.impl.direct.DirectMetaBean;
import org.joda.beans.impl.direct.DirectMetaProperty;
import org.joda.beans.impl.direct.DirectMetaPropertyMap;

/**
 * JobBean.
 *
 * @author lontime
 * @since 1.0
 */
@BeanDefinition
public class JobBean implements Bean {

    @PropertyDefinition
    private String id;

    //这个必须是 Map类型 因为反序列化后是hashmap
    @PropertyDefinition
    private List> jobHistory;

    @PropertyDefinition
    private Map metadata;

    @PropertyDefinition
    private Integer version;

    @PropertyDefinition
    private String jobSignature;

    @PropertyDefinition
    private String jobName;

    @PropertyDefinition
    private JobDetailsBean jobDetails;

    public JobBean() {

    }

    public JobBean(Job job) {
        this.id = job.getId().toString();
        this.jobHistory = job.getJobStates().stream().map(s -> {
            if (s instanceof SucceededState) {
                return new SucceededStateBean((SucceededState) s);
            }
            if (s instanceof DeletedState) {
                return new DeletedStateBean((DeletedState) s);
            }
            if (s instanceof EnqueuedState) {
                return new EnqueuedStateBean((EnqueuedState) s);
            }
            if (s instanceof FailedState) {
                return new FailedStateBean((FailedState) s);
            }
            if (s instanceof ProcessingState) {
                return new ProcessingStateBean((ProcessingState) s);
            }
            if (s instanceof ScheduledState) {
                return new ScheduledStateBean((ScheduledState) s);
            }
            return null;
        }).filter(Objects::nonNull)
                .map(s -> BeanHelper.beanToMap(s))
                .collect(Collectors.toList());
        this.metadata = convertMetadata(job.getMetadata());

        this.version = job.getVersion();
        this.jobSignature = job.getJobSignature();
        this.jobName = job.getJobName();
        this.jobDetails = new JobDetailsBean(job.getJobDetails());
    }

    public Job toJob() {
        final List jobStates = getJobHistory().stream().map(s -> {
            final StateName stateName = StateName.valueOf((String) s.get("state"));
            if (stateName == StateName.DELETED) {
                return BeanHelper.mapToBean(s, DeletedStateBean.meta());
            }
            if (stateName == StateName.ENQUEUED) {
                return BeanHelper.mapToBean(s, EnqueuedStateBean.meta());
            }
            if (stateName == StateName.FAILED) {
                return BeanHelper.mapToBean(s, FailedStateBean.meta());
            }
            if (stateName == StateName.PROCESSING) {
                return BeanHelper.mapToBean(s, ProcessingStateBean.meta());
            }
            if (stateName == StateName.SCHEDULED) {
                return BeanHelper.mapToBean(s, ScheduledStateBean.meta());
            }
            if (stateName == StateName.SUCCEEDED) {
                return BeanHelper.mapToBean(s, SucceededStateBean.meta());
            }
            return null;
        }).filter(Objects::nonNull)
                .map(s -> ((JobStateBean) s).toState()).collect(Collectors.toList());

        final Job job = new Job(UUID.fromString(getId()),
                getVersion(),
                getJobDetails().toJobDetails(),
                jobStates, convertMetadata());
        job.setJobName(getJobName());
        return job;
    }

    private ConcurrentHashMap convertMetadata() {
        final Map metadata = getMetadata();
        final ConcurrentHashMap metadataNew = new ConcurrentHashMap<>();
        for (Map.Entry entry : metadata.entrySet()) {
            final Object value = entry.getValue();
            if (value instanceof JobDashboardLogLinesBean) {
                metadataNew.put(entry.getKey(),
                        ((JobDashboardLogLinesBean) value).toJobDashboardLogLines());
            } else {
                metadataNew.put(entry.getKey(), value);
            }
        }
        return metadataNew;
    }

    private ConcurrentHashMap convertMetadata(Map metadata) {
        final ConcurrentHashMap metadataNew = new ConcurrentHashMap<>();
        for (Map.Entry entry : metadata.entrySet()) {
            final Object value = entry.getValue();
            if (value instanceof JobDashboardLogLines) {
                metadataNew.put(entry.getKey(), new JobDashboardLogLinesBean((JobDashboardLogLines) value));
            } else {
                metadataNew.put(entry.getKey(), value);
            }
        }
        return metadataNew;
    }

//    private ConcurrentHashMap convertMetadata() {
//        final Map metadata = getMetadata();
//        final ConcurrentHashMap metadataNew = new ConcurrentHashMap<>();
//        for (Map.Entry entry : metadata.entrySet()) {
//            final String value = (String) entry.getValue();
//            final char c = new JSONTokener(value).nextClean();
//            if (c == '[') {
//                final JSONArray array = new JSONArray(value);
//                final String s = array.toString();
//                System.out.println(s);
//                metadataNew.put(entry.getKey(), new JSONArray(value));
//            } else if (c == '{') {
//                metadataNew.put(entry.getKey(), new JSONObject(value));
//            } else {
//                metadataNew.put(entry.getKey(), value);
//            }
//        }
//
//        return metadataNew;
//    }

    //------------------------- AUTOGENERATED START -------------------------
    /**
     * The meta-bean for {@code JobBean}.
     * @return the meta-bean, not null
     */
    public static JobBean.Meta meta() {
        return JobBean.Meta.INSTANCE;
    }

    static {
        MetaBean.register(JobBean.Meta.INSTANCE);
    }

    @Override
    public JobBean.Meta metaBean() {
        return JobBean.Meta.INSTANCE;
    }

    //-----------------------------------------------------------------------
    /**
     * Gets the id.
     * @return the value of the property
     */
    public String getId() {
        return id;
    }

    /**
     * Sets the id.
     * @param id  the new value of the property
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * Gets the the {@code id} property.
     * @return the property, not null
     */
    public final Property id() {
        return metaBean().id().createProperty(this);
    }

    //-----------------------------------------------------------------------
    /**
     * Gets the jobHistory.
     * @return the value of the property
     */
    public List> getJobHistory() {
        return jobHistory;
    }

    /**
     * Sets the jobHistory.
     * @param jobHistory  the new value of the property
     */
    public void setJobHistory(List> jobHistory) {
        this.jobHistory = jobHistory;
    }

    /**
     * Gets the the {@code jobHistory} property.
     * @return the property, not null
     */
    public final Property>> jobHistory() {
        return metaBean().jobHistory().createProperty(this);
    }

    //-----------------------------------------------------------------------
    /**
     * Gets the metadata.
     * @return the value of the property
     */
    public Map getMetadata() {
        return metadata;
    }

    /**
     * Sets the metadata.
     * @param metadata  the new value of the property
     */
    public void setMetadata(Map metadata) {
        this.metadata = metadata;
    }

    /**
     * Gets the the {@code metadata} property.
     * @return the property, not null
     */
    public final Property> metadata() {
        return metaBean().metadata().createProperty(this);
    }

    //-----------------------------------------------------------------------
    /**
     * Gets the version.
     * @return the value of the property
     */
    public Integer getVersion() {
        return version;
    }

    /**
     * Sets the version.
     * @param version  the new value of the property
     */
    public void setVersion(Integer version) {
        this.version = version;
    }

    /**
     * Gets the the {@code version} property.
     * @return the property, not null
     */
    public final Property version() {
        return metaBean().version().createProperty(this);
    }

    //-----------------------------------------------------------------------
    /**
     * Gets the jobSignature.
     * @return the value of the property
     */
    public String getJobSignature() {
        return jobSignature;
    }

    /**
     * Sets the jobSignature.
     * @param jobSignature  the new value of the property
     */
    public void setJobSignature(String jobSignature) {
        this.jobSignature = jobSignature;
    }

    /**
     * Gets the the {@code jobSignature} property.
     * @return the property, not null
     */
    public final Property jobSignature() {
        return metaBean().jobSignature().createProperty(this);
    }

    //-----------------------------------------------------------------------
    /**
     * Gets the jobName.
     * @return the value of the property
     */
    public String getJobName() {
        return jobName;
    }

    /**
     * Sets the jobName.
     * @param jobName  the new value of the property
     */
    public void setJobName(String jobName) {
        this.jobName = jobName;
    }

    /**
     * Gets the the {@code jobName} property.
     * @return the property, not null
     */
    public final Property jobName() {
        return metaBean().jobName().createProperty(this);
    }

    //-----------------------------------------------------------------------
    /**
     * Gets the jobDetails.
     * @return the value of the property
     */
    public JobDetailsBean getJobDetails() {
        return jobDetails;
    }

    /**
     * Sets the jobDetails.
     * @param jobDetails  the new value of the property
     */
    public void setJobDetails(JobDetailsBean jobDetails) {
        this.jobDetails = jobDetails;
    }

    /**
     * Gets the the {@code jobDetails} property.
     * @return the property, not null
     */
    public final Property jobDetails() {
        return metaBean().jobDetails().createProperty(this);
    }

    //-----------------------------------------------------------------------
    @Override
    public JobBean clone() {
        return JodaBeanUtils.cloneAlways(this);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (obj != null && obj.getClass() == this.getClass()) {
            JobBean other = (JobBean) obj;
            return JodaBeanUtils.equal(getId(), other.getId()) &&
                    JodaBeanUtils.equal(getJobHistory(), other.getJobHistory()) &&
                    JodaBeanUtils.equal(getMetadata(), other.getMetadata()) &&
                    JodaBeanUtils.equal(getVersion(), other.getVersion()) &&
                    JodaBeanUtils.equal(getJobSignature(), other.getJobSignature()) &&
                    JodaBeanUtils.equal(getJobName(), other.getJobName()) &&
                    JodaBeanUtils.equal(getJobDetails(), other.getJobDetails());
        }
        return false;
    }

    @Override
    public int hashCode() {
        int hash = getClass().hashCode();
        hash = hash * 31 + JodaBeanUtils.hashCode(getId());
        hash = hash * 31 + JodaBeanUtils.hashCode(getJobHistory());
        hash = hash * 31 + JodaBeanUtils.hashCode(getMetadata());
        hash = hash * 31 + JodaBeanUtils.hashCode(getVersion());
        hash = hash * 31 + JodaBeanUtils.hashCode(getJobSignature());
        hash = hash * 31 + JodaBeanUtils.hashCode(getJobName());
        hash = hash * 31 + JodaBeanUtils.hashCode(getJobDetails());
        return hash;
    }

    @Override
    public String toString() {
        StringBuilder buf = new StringBuilder(256);
        buf.append("JobBean{");
        int len = buf.length();
        toString(buf);
        if (buf.length() > len) {
            buf.setLength(buf.length() - 2);
        }
        buf.append('}');
        return buf.toString();
    }

    protected void toString(StringBuilder buf) {
        buf.append("id").append('=').append(JodaBeanUtils.toString(getId())).append(',').append(' ');
        buf.append("jobHistory").append('=').append(JodaBeanUtils.toString(getJobHistory())).append(',').append(' ');
        buf.append("metadata").append('=').append(JodaBeanUtils.toString(getMetadata())).append(',').append(' ');
        buf.append("version").append('=').append(JodaBeanUtils.toString(getVersion())).append(',').append(' ');
        buf.append("jobSignature").append('=').append(JodaBeanUtils.toString(getJobSignature())).append(',').append(' ');
        buf.append("jobName").append('=').append(JodaBeanUtils.toString(getJobName())).append(',').append(' ');
        buf.append("jobDetails").append('=').append(JodaBeanUtils.toString(getJobDetails())).append(',').append(' ');
    }

    //-----------------------------------------------------------------------
    /**
     * The meta-bean for {@code JobBean}.
     */
    public static class Meta extends DirectMetaBean {
        /**
         * The singleton instance of the meta-bean.
         */
        static final Meta INSTANCE = new Meta();

        /**
         * The meta-property for the {@code id} property.
         */
        private final MetaProperty id = DirectMetaProperty.ofReadWrite(
                this, "id", JobBean.class, String.class);
        /**
         * The meta-property for the {@code jobHistory} property.
         */
        @SuppressWarnings({"unchecked", "rawtypes" })
        private final MetaProperty>> jobHistory = DirectMetaProperty.ofReadWrite(
                this, "jobHistory", JobBean.class, (Class) List.class);
        /**
         * The meta-property for the {@code metadata} property.
         */
        @SuppressWarnings({"unchecked", "rawtypes" })
        private final MetaProperty> metadata = DirectMetaProperty.ofReadWrite(
                this, "metadata", JobBean.class, (Class) Map.class);
        /**
         * The meta-property for the {@code version} property.
         */
        private final MetaProperty version = DirectMetaProperty.ofReadWrite(
                this, "version", JobBean.class, Integer.class);
        /**
         * The meta-property for the {@code jobSignature} property.
         */
        private final MetaProperty jobSignature = DirectMetaProperty.ofReadWrite(
                this, "jobSignature", JobBean.class, String.class);
        /**
         * The meta-property for the {@code jobName} property.
         */
        private final MetaProperty jobName = DirectMetaProperty.ofReadWrite(
                this, "jobName", JobBean.class, String.class);
        /**
         * The meta-property for the {@code jobDetails} property.
         */
        private final MetaProperty jobDetails = DirectMetaProperty.ofReadWrite(
                this, "jobDetails", JobBean.class, JobDetailsBean.class);
        /**
         * The meta-properties.
         */
        private final Map> metaPropertyMap$ = new DirectMetaPropertyMap(
                this, null,
                "id",
                "jobHistory",
                "metadata",
                "version",
                "jobSignature",
                "jobName",
                "jobDetails");

        /**
         * Restricted constructor.
         */
        protected Meta() {
        }

        @Override
        protected MetaProperty metaPropertyGet(String propertyName) {
            switch (propertyName.hashCode()) {
                case 3355:  // id
                    return id;
                case -826236393:  // jobHistory
                    return jobHistory;
                case -450004177:  // metadata
                    return metadata;
                case 351608024:  // version
                    return version;
                case -96140933:  // jobSignature
                    return jobSignature;
                case -1438096408:  // jobName
                    return jobName;
                case -195448891:  // jobDetails
                    return jobDetails;
            }
            return super.metaPropertyGet(propertyName);
        }

        @Override
        public BeanBuilder builder() {
            return new DirectBeanBuilder<>(new JobBean());
        }

        @Override
        public Class beanType() {
            return JobBean.class;
        }

        @Override
        public Map> metaPropertyMap() {
            return metaPropertyMap$;
        }

        //-----------------------------------------------------------------------
        /**
         * The meta-property for the {@code id} property.
         * @return the meta-property, not null
         */
        public final MetaProperty id() {
            return id;
        }

        /**
         * The meta-property for the {@code jobHistory} property.
         * @return the meta-property, not null
         */
        public final MetaProperty>> jobHistory() {
            return jobHistory;
        }

        /**
         * The meta-property for the {@code metadata} property.
         * @return the meta-property, not null
         */
        public final MetaProperty> metadata() {
            return metadata;
        }

        /**
         * The meta-property for the {@code version} property.
         * @return the meta-property, not null
         */
        public final MetaProperty version() {
            return version;
        }

        /**
         * The meta-property for the {@code jobSignature} property.
         * @return the meta-property, not null
         */
        public final MetaProperty jobSignature() {
            return jobSignature;
        }

        /**
         * The meta-property for the {@code jobName} property.
         * @return the meta-property, not null
         */
        public final MetaProperty jobName() {
            return jobName;
        }

        /**
         * The meta-property for the {@code jobDetails} property.
         * @return the meta-property, not null
         */
        public final MetaProperty jobDetails() {
            return jobDetails;
        }

        //-----------------------------------------------------------------------
        @Override
        protected Object propertyGet(Bean bean, String propertyName, boolean quiet) {
            switch (propertyName.hashCode()) {
                case 3355:  // id
                    return ((JobBean) bean).getId();
                case -826236393:  // jobHistory
                    return ((JobBean) bean).getJobHistory();
                case -450004177:  // metadata
                    return ((JobBean) bean).getMetadata();
                case 351608024:  // version
                    return ((JobBean) bean).getVersion();
                case -96140933:  // jobSignature
                    return ((JobBean) bean).getJobSignature();
                case -1438096408:  // jobName
                    return ((JobBean) bean).getJobName();
                case -195448891:  // jobDetails
                    return ((JobBean) bean).getJobDetails();
            }
            return super.propertyGet(bean, propertyName, quiet);
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void propertySet(Bean bean, String propertyName, Object newValue, boolean quiet) {
            switch (propertyName.hashCode()) {
                case 3355:  // id
                    ((JobBean) bean).setId((String) newValue);
                    return;
                case -826236393:  // jobHistory
                    ((JobBean) bean).setJobHistory((List>) newValue);
                    return;
                case -450004177:  // metadata
                    ((JobBean) bean).setMetadata((Map) newValue);
                    return;
                case 351608024:  // version
                    ((JobBean) bean).setVersion((Integer) newValue);
                    return;
                case -96140933:  // jobSignature
                    ((JobBean) bean).setJobSignature((String) newValue);
                    return;
                case -1438096408:  // jobName
                    ((JobBean) bean).setJobName((String) newValue);
                    return;
                case -195448891:  // jobDetails
                    ((JobBean) bean).setJobDetails((JobDetailsBean) newValue);
                    return;
            }
            super.propertySet(bean, propertyName, newValue, quiet);
        }

    }

    //-------------------------- AUTOGENERATED END --------------------------
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy