001    /**
002     * Copyright (c) 2000-present 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 int getIsolationLevel() {
045                    return _isolationLevel;
046            }
047    
048            @Override
049            public String handleException(BackgroundTask backgroundTask, Exception e) {
050                    return "Unable to execute background task: " + e.getMessage();
051            }
052    
053            @Override
054            public boolean isSerial() {
055                    if (_isolationLevel ==
056                                    BackgroundTaskConstants.ISOLATION_LEVEL_NOT_ISOLATED) {
057    
058                            return false;
059                    }
060    
061                    return true;
062            }
063    
064            protected Locale getLocale(BackgroundTask backgroundTask) {
065                    Map<String, Serializable> taskContextMap =
066                            backgroundTask.getTaskContextMap();
067    
068                    long userId = MapUtil.getLong(taskContextMap, "userId");
069    
070                    if (userId > 0) {
071                            try {
072                                    User user = UserLocalServiceUtil.fetchUser(userId);
073    
074                                    if (user != null) {
075                                            return user.getLocale();
076                                    }
077                            }
078                            catch (Exception e) {
079                                    if (_log.isDebugEnabled()) {
080                                            _log.debug("Unable to get the user's locale", e);
081                                    }
082                            }
083                    }
084    
085                    return LocaleUtil.getDefault();
086            }
087    
088            protected void setBackgroundTaskStatusMessageTranslator(
089                    BackgroundTaskStatusMessageTranslator
090                            backgroundTaskStatusMessageTranslator) {
091    
092                    _backgroundTaskStatusMessageTranslator =
093                            backgroundTaskStatusMessageTranslator;
094            }
095    
096            protected void setIsolationLevel(int isolationLevel) {
097                    _isolationLevel = isolationLevel;
098            }
099    
100            private static final Log _log = LogFactoryUtil.getLog(
101                    BaseBackgroundTaskExecutor.class);
102    
103            private BackgroundTaskStatusMessageTranslator
104                    _backgroundTaskStatusMessageTranslator;
105            private int _isolationLevel =
106                    BackgroundTaskConstants.ISOLATION_LEVEL_NOT_ISOLATED;
107    
108    }