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