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.backgroundtask;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.MapUtil;
022    import com.liferay.portal.model.BackgroundTask;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.service.UserLocalServiceUtil;
025    
026    import java.io.Serializable;
027    
028    import java.util.Locale;
029    import java.util.Map;
030    
031    /**
032     * @author Michael C. Han
033     */
034    public abstract class BaseBackgroundTaskExecutor
035            implements BackgroundTaskExecutor {
036    
037            @Override
038            public BackgroundTaskStatusMessageTranslator
039                    getBackgroundTaskStatusMessageTranslator() {
040    
041                    return _backgroundTaskStatusMessageTranslator;
042            }
043    
044            @Override
045            public String handleException(BackgroundTask backgroundTask, Exception e)
046                    throws SystemException {
047    
048                    return "Unable to execute background task: " + e.getMessage();
049            }
050    
051            @Override
052            public boolean isSerial() {
053                    return _serial;
054            }
055    
056            protected Locale getLocale(BackgroundTask backgroundTask) {
057                    Map<String, Serializable> taskContextMap =
058                            backgroundTask.getTaskContextMap();
059    
060                    long userId = MapUtil.getLong(taskContextMap, "userId");
061    
062                    if (userId > 0) {
063                            try {
064                                    User user = UserLocalServiceUtil.fetchUser(userId);
065    
066                                    if (user != null) {
067                                            return user.getLocale();
068                                    }
069                            }
070                            catch (Exception e) {
071                                    if (_log.isDebugEnabled()) {
072                                            _log.debug("Unable to get the user's locale", e);
073                                    }
074                            }
075                    }
076    
077                    return LocaleUtil.getDefault();
078            }
079    
080            protected void setBackgroundTaskStatusMessageTranslator(
081                    BackgroundTaskStatusMessageTranslator
082                            backgroundTaskStatusMessageTranslator) {
083    
084                    _backgroundTaskStatusMessageTranslator =
085                            backgroundTaskStatusMessageTranslator;
086            }
087    
088            protected void setSerial(boolean serial) {
089                    _serial = serial;
090            }
091    
092            private static Log _log = LogFactoryUtil.getLog(
093                    BaseBackgroundTaskExecutor.class);
094    
095            private BackgroundTaskStatusMessageTranslator
096                    _backgroundTaskStatusMessageTranslator;
097            private boolean _serial;
098    
099    }