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