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

com.liferay.portal.scheduler.JobStateSerializeUtil Maven / Gradle / Ivy

There is a newer version: 5.0.2
Show newest version
/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.portal.scheduler;

import com.liferay.portal.kernel.scheduler.JobState;
import com.liferay.portal.kernel.scheduler.TriggerState;
import com.liferay.portal.kernel.util.ObjectValuePair;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Tina Tian
 */
public class JobStateSerializeUtil {

	public static JobState deserialize(Map jobStateMap) {
		Object object = jobStateMap.get(_VERSION_FIELD);

		if (!(object instanceof Integer)) {
			throw new IllegalStateException(
				"Unable to find JobState version number");
		}

		int version = (Integer)object;

		if (version == 1) {
			return _deserialize_1(jobStateMap);
		}
		else {
			throw new IllegalStateException(
				"Unable to deserialize field for job state with version " +
					version);
		}
	}

	public static Map serialize(JobState jobState) {
		switch (JobState.VERSION) {
			case 1:
				return _serialize_1(jobState);

			default:
				throw new IllegalStateException(
					"Unable to serialize field for job state with version " +
						JobState.VERSION);
		}
	}

	private static JobState _deserialize_1(Map jobStateMap) {
		TriggerState triggerState = null;

		String triggerStateString = (String)jobStateMap.get(
			_TRIGGER_STATE_FIELD);

		try {
			triggerState = TriggerState.valueOf(triggerStateString);
		}
		catch (IllegalArgumentException iae) {
			throw new IllegalStateException(
				"Invalid value " + triggerStateString, iae);
		}

		int exceptionsMaxSize = (Integer)jobStateMap.get(
			_EXCEPTIONS_MAX_SIZE_FIELD);
		Map triggerDates = (Map)jobStateMap.get(
			_TRIGGER_DATES_FIELD);

		JobState jobState = null;

		if (triggerDates != null) {
			jobState = new JobState(
				triggerState, exceptionsMaxSize, triggerDates);
		}
		else {
			jobState = new JobState(triggerState, exceptionsMaxSize);
		}

		ArrayList exceptionsList =
			(ArrayList)jobStateMap.get(_EXCEPTIONS_FIELD);

		if (exceptionsList != null) {
			for (Object[] exceptions : exceptionsList) {
				jobState.addException(
					(Exception)exceptions[0], (Date)exceptions[1]);
			}
		}

		return jobState;
	}

	private static Map _serialize_1(JobState jobState) {
		Map jobStateMap = new HashMap<>();

		ObjectValuePair[] exceptions =
			jobState.getExceptions();

		if (exceptions != null) {
			ArrayList exceptionsList = new ArrayList<>();

			for (ObjectValuePair exception : exceptions) {
				exceptionsList.add(
					new Object[] {exception.getKey(), exception.getValue()});
			}

			exceptionsList.trimToSize();

			jobStateMap.put(_EXCEPTIONS_FIELD, exceptionsList);
		}

		jobStateMap.put(
			_EXCEPTIONS_MAX_SIZE_FIELD, jobState.getExceptionsMaxSize());
		jobStateMap.put(_TRIGGER_DATES_FIELD, jobState.getTriggerDates());
		jobStateMap.put(
			_TRIGGER_STATE_FIELD, jobState.getTriggerState().toString());
		jobStateMap.put(_VERSION_FIELD, JobState.VERSION);

		return jobStateMap;
	}

	private static final String _EXCEPTIONS_FIELD = "exceptions";

	private static final String _EXCEPTIONS_MAX_SIZE_FIELD =
		"exceptionsMaxSize";

	private static final String _TRIGGER_DATES_FIELD = "triggerDates";

	private static final String _TRIGGER_STATE_FIELD = "triggerState";

	private static final String _VERSION_FIELD = "version";

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy