001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.scheduler;
016    
017    import com.liferay.portal.kernel.util.ObjectValuePair;
018    
019    import java.util.ArrayList;
020    import java.util.Date;
021    import java.util.HashMap;
022    import java.util.Map;
023    
024    /**
025     * @author Tina Tian
026     */
027    public class JobStateSerializeUtil {
028    
029            public static JobState deserialize(Map<String, Object> jobStateMap) {
030                    Object object = jobStateMap.get(_VERSION_FIELD);
031    
032                    if (!(object instanceof Integer)) {
033                            throw new IllegalStateException(
034                                    "Unable to find JobState version number");
035                    }
036    
037                    int version = (Integer)object;
038    
039                    if (version == 1) {
040                            return _deserialize_1(jobStateMap);
041                    }
042                    else {
043                            throw new IllegalStateException(
044                                    "Unable to deserialize field for job state with version " +
045                                            version);
046                    }
047            }
048    
049            public static Map<String, Object> serialize(JobState jobState) {
050                    switch (JobState.VERSION) {
051                            case 1:
052                                    return _serialize_1(jobState);
053    
054                            default:
055                                    throw new IllegalStateException(
056                                            "Unable to serialize field for job state with version " +
057                                                    JobState.VERSION);
058                    }
059            }
060    
061            private static JobState _deserialize_1(Map<String, Object> jobStateMap) {
062                    TriggerState triggerState = null;
063    
064                    String triggerStateString = (String)jobStateMap.get(
065                            _TRIGGER_STATE_FIELD);
066    
067                    try {
068                            triggerState = TriggerState.valueOf(triggerStateString);
069                    }
070                    catch (IllegalArgumentException iae) {
071                            throw new IllegalStateException(
072                                    "Invalid value " + triggerStateString, iae);
073                    }
074    
075                    int exceptionsMaxSize = (Integer)jobStateMap.get(
076                            _EXCEPTIONS_MAX_SIZE_FIELD);
077                    Map<String, Date> triggerDates = (Map<String, Date>)jobStateMap.get(
078                            _TRIGGER_DATES_FIELD);
079    
080                    JobState jobState = null;
081    
082                    if (triggerDates != null) {
083                            jobState = new JobState(
084                                    triggerState, exceptionsMaxSize, triggerDates);
085                    }
086                    else {
087                            jobState = new JobState(triggerState, exceptionsMaxSize);
088                    }
089    
090                    ArrayList<Object[]> exceptionsList =
091                            (ArrayList<Object[]>)jobStateMap.get(_EXCEPTIONS_FIELD);
092    
093                    if (exceptionsList != null) {
094                            for (Object[] exceptions : exceptionsList) {
095                                    jobState.addException(
096                                            (Exception)exceptions[0], (Date)exceptions[1]);
097                            }
098                    }
099    
100                    return jobState;
101            }
102    
103            private static Map<String, Object> _serialize_1(JobState jobState) {
104                    Map<String, Object> jobStateMap = new HashMap<String, Object>();
105    
106                    ObjectValuePair<Exception, Date>[] exceptions =
107                            jobState.getExceptions();
108    
109                    if (exceptions != null) {
110                            ArrayList<Object[]> exceptionsList = new ArrayList<Object[]>();
111    
112                            for (ObjectValuePair<Exception, Date> exception : exceptions) {
113                                    exceptionsList.add(
114                                            new Object[] {exception.getKey(), exception.getValue()});
115                            }
116    
117                            exceptionsList.trimToSize();
118    
119                            jobStateMap.put(_EXCEPTIONS_FIELD, exceptionsList);
120                    }
121    
122                    jobStateMap.put(
123                            _EXCEPTIONS_MAX_SIZE_FIELD, jobState.getExceptionsMaxSize());
124                    jobStateMap.put(_TRIGGER_DATES_FIELD, jobState.getTriggerDates());
125                    jobStateMap.put(
126                            _TRIGGER_STATE_FIELD, jobState.getTriggerState().toString());
127                    jobStateMap.put(_VERSION_FIELD, JobState.VERSION);
128    
129                    return jobStateMap;
130            }
131    
132            private static final String _EXCEPTIONS_FIELD = "exceptions";
133    
134            private static final String _EXCEPTIONS_MAX_SIZE_FIELD =
135                    "exceptionsMaxSize";
136    
137            private static final String _TRIGGER_DATES_FIELD = "triggerDates";
138    
139            private static final String _TRIGGER_STATE_FIELD = "triggerState";
140    
141            private static final String _VERSION_FIELD = "version";
142    
143    }