001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(
125                            _TRIGGER_DATES_FIELD, jobState.getTriggerDates());
126                    jobStateMap.put(
127                            _TRIGGER_STATE_FIELD, jobState.getTriggerState().toString());
128                    jobStateMap.put(_VERSION_FIELD, JobState.VERSION);
129    
130                    return jobStateMap;
131            }
132    
133            private static final String _EXCEPTIONS_FIELD = "exceptions";
134    
135            private static final String _EXCEPTIONS_MAX_SIZE_FIELD =
136                    "exceptionsMaxSize";
137    
138            private static final String _TRIGGER_DATES_FIELD = "triggerDates";
139    
140            private static final String _TRIGGER_STATE_FIELD = "triggerState";
141    
142            private static final String _VERSION_FIELD = "version";
143    
144    }