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