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

com.github.lontime.extjobrunr.common.JodaJsonMapper Maven / Gradle / Ivy

package com.github.lontime.extjobrunr.common;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.List;
import java.util.StringJoiner;

import com.github.lontime.extjobrunr.beans.BackgroundJobServerStatusBean;
import com.github.lontime.extjobrunr.beans.JobBean;
import com.github.lontime.extjobrunr.beans.JobStatsExtendedBean;
import com.github.lontime.extjobrunr.beans.PageBean;
import com.github.lontime.extjobrunr.beans.RecurringJobBean;
import com.github.lontime.extjobrunr.beans.RecurringJobUIModelBean;
import com.github.lontime.extjobrunr.beans.VersionUIModelBean;
import com.github.lontime.extjobrunr.beans.problems.CpuAllocationIrregularityProblemBean;
import com.github.lontime.extjobrunr.beans.problems.NewJobRunrVersionProblemBean;
import com.github.lontime.extjobrunr.beans.problems.ScheduledJobsNotFoundProblemBean;
import com.github.lontime.extjobrunr.beans.problems.SevereJobRunrExceptionProblemBean;
import lombok.extern.jbosslog.JBossLog;
import org.jobrunr.JobRunrException;
import org.jobrunr.dashboard.ui.model.RecurringJobUIModel;
import org.jobrunr.dashboard.ui.model.VersionUIModel;
import org.jobrunr.dashboard.ui.model.problems.CpuAllocationIrregularityProblem;
import org.jobrunr.dashboard.ui.model.problems.NewJobRunrVersionProblem;
import org.jobrunr.dashboard.ui.model.problems.Problems;
import org.jobrunr.dashboard.ui.model.problems.ScheduledJobsNotFoundProblem;
import org.jobrunr.dashboard.ui.model.problems.SevereJobRunrExceptionProblem;
import org.jobrunr.jobs.Job;
import org.jobrunr.jobs.RecurringJob;
import org.jobrunr.storage.BackgroundJobServerStatus;
import org.jobrunr.storage.JobStatsExtended;
import org.jobrunr.storage.Page;
import org.jobrunr.utils.mapper.JsonMapper;
import org.joda.beans.ser.JodaBeanSer;
import org.joda.beans.ser.json.JodaBeanSimpleJsonReader;
import org.joda.beans.ser.json.JodaBeanSimpleJsonWriter;
import org.joda.convert.StringConvert;

/**
 * JodaJsonMapper.
 *
 * @author lontime
 * @since 1.0
 */
@JBossLog
public class JodaJsonMapper implements JsonMapper {

    private JodaBeanSer jodaBeanSer = JodaBeanSer.COMPACT
            .withConverter(StringConvert.INSTANCE);

    @Override
    public String serialize(Object object) {

        final JodaBeanSimpleJsonWriter jsonWriter = jodaBeanSer.simpleJsonWriter();

        if (object instanceof Job) {
            return jsonWriter.write(new JobBean((Job) object));
        }

        if (object instanceof RecurringJobUIModel) {
            return jsonWriter.write(new RecurringJobUIModelBean((RecurringJobUIModel) object));
        }

        if (object instanceof RecurringJob) {
            return jsonWriter.write(new RecurringJobBean((RecurringJob) object));
        }

        if (object instanceof JobStatsExtended) {
            return jsonWriter.write(new JobStatsExtendedBean((JobStatsExtended) object));
        }

        if (object instanceof BackgroundJobServerStatus) {
            return jsonWriter.write(new BackgroundJobServerStatusBean((BackgroundJobServerStatus) object));
        }

        if (object instanceof CpuAllocationIrregularityProblem) {
            return jsonWriter.write(new CpuAllocationIrregularityProblemBean((CpuAllocationIrregularityProblem) object));
        }

        if (object instanceof NewJobRunrVersionProblem) {
            return jsonWriter.write(new NewJobRunrVersionProblemBean((NewJobRunrVersionProblem) object));
        }

        if (object instanceof ScheduledJobsNotFoundProblem) {
            return jsonWriter.write(new ScheduledJobsNotFoundProblemBean((ScheduledJobsNotFoundProblem) object));
        }

        if (object instanceof SevereJobRunrExceptionProblem) {
            return jsonWriter.write(new SevereJobRunrExceptionProblemBean((SevereJobRunrExceptionProblem) object));
        }

        if (object instanceof VersionUIModel) {
            return jsonWriter.write(new VersionUIModelBean((VersionUIModel) object));
        }

        if (object instanceof List) {
            return processList((List) object);
        } else if (object instanceof Problems) {
            return processProblems((Problems) object);
        } else if (object instanceof Page) {
            return jsonWriter.write(new PageBean((Page) object));
        }

        log.warnv("Data's type->{0} must is Bean!", object.getClass());
        throw JobRunrException.shouldNotHappenException("Data's type must is Bean!");
    }

    private String processProblems(Problems problems) {
        final StringJoiner joiner = new StringJoiner(",", "[", "]");
        for (int i = 0; i < problems.size(); i++) {
            final Object ele = problems.poll();
            joiner.add(serialize(ele));
        }
        return joiner.toString();
    }

    private String processList(List list) {
        final StringJoiner joiner = new StringJoiner(",", "[", "]");
        for (int i = 0; i < list.size(); i++) {
            final Object ele = list.get(i);
            joiner.add(serialize(ele));
        }
        return joiner.toString();
    }

    @Override
    public void serialize(OutputStream outputStream, Object object) {
        try (final OutputStreamWriter writer = new OutputStreamWriter(outputStream)) {
            writer.write(serialize(object));
            writer.flush();
        } catch (IOException e) {
            throw JobRunrException.shouldNotHappenException(e);
        }
    }

    @Override
    public  T deserialize(String serializedObjectAsString, Class clazz) {

        final JodaBeanSimpleJsonReader jsonReader = jodaBeanSer.simpleJsonReader();
        if (clazz.isAssignableFrom(Job.class)) {
            final JobBean bean = jsonReader.read(serializedObjectAsString, JobBean.class);
            return (T) bean.toJob();
        }

        if (clazz.isAssignableFrom(RecurringJobUIModel.class)) {
            final RecurringJobUIModelBean bean = jsonReader.read(serializedObjectAsString, RecurringJobUIModelBean.class);
            return (T) bean.toRecurringJobUIModel();
        }

        if (clazz.isAssignableFrom(RecurringJob.class)) {
            final RecurringJobBean bean = jsonReader.read(serializedObjectAsString, RecurringJobBean.class);
            return (T) bean.toRecurringJob();
        }

        if (clazz.isAssignableFrom(JobStatsExtended.class)) {
            final JobStatsExtendedBean bean = jsonReader.read(serializedObjectAsString, JobStatsExtendedBean.class);
            return (T) bean.toJobStatsExtended();
        }

        log.warnv("Data's type from:{0} must is Bean!",
                serializedObjectAsString);
        throw JobRunrException.shouldNotHappenException("Data's type must is Bean!");
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy