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.systemevent;
016    
017    import com.liferay.counter.service.CounterLocalServiceUtil;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.AutoResetThreadLocal;
020    import com.liferay.portal.model.SystemEventConstants;
021    import com.liferay.portal.util.PortalUtil;
022    
023    import java.util.Stack;
024    
025    /**
026     * @author Zsolt Berentey
027     */
028    public class SystemEventHierarchyEntryThreadLocal {
029    
030            public static void clear() {
031                    Stack<SystemEventHierarchyEntry> systemEventHierarchyEntries =
032                            _systemEventHierarchyEntries.get();
033    
034                    systemEventHierarchyEntries.clear();
035            }
036    
037            public static SystemEventHierarchyEntry peek() {
038                    Stack<SystemEventHierarchyEntry> systemEventHierarchyEntries =
039                            _systemEventHierarchyEntries.get();
040    
041                    if (systemEventHierarchyEntries.isEmpty()) {
042                            return null;
043                    }
044    
045                    return systemEventHierarchyEntries.peek();
046            }
047    
048            public static SystemEventHierarchyEntry pop() {
049                    Stack<SystemEventHierarchyEntry> systemEventHierarchyEntries =
050                            _systemEventHierarchyEntries.get();
051    
052                    if (systemEventHierarchyEntries.isEmpty()) {
053                            return null;
054                    }
055    
056                    return systemEventHierarchyEntries.pop();
057            }
058    
059            public static SystemEventHierarchyEntry push() throws SystemException {
060                    return push(SystemEventConstants.ACTION_SKIP);
061            }
062    
063            public static SystemEventHierarchyEntry push(Class<?> clazz, long classPK)
064                    throws SystemException {
065    
066                    return push(
067                            PortalUtil.getClassNameId(clazz), classPK,
068                            SystemEventConstants.ACTION_SKIP);
069            }
070    
071            public static SystemEventHierarchyEntry push(
072                            Class<?> clazz, long classPK, int action)
073                    throws SystemException {
074    
075                    return push(PortalUtil.getClassNameId(clazz), classPK, action);
076            }
077    
078            public static SystemEventHierarchyEntry push(int action)
079                    throws SystemException {
080    
081                    return push(0, 0, action);
082            }
083    
084            public static SystemEventHierarchyEntry push(
085                            long classNameId, long classPK, int action)
086                    throws SystemException {
087    
088                    long parentSystemEventId = 0;
089                    long systemEventSetKey = 0;
090    
091                    Stack<SystemEventHierarchyEntry> systemEventHierarchyEntries =
092                            _systemEventHierarchyEntries.get();
093    
094                    SystemEventHierarchyEntry parentSystemEventHierarchyEntry = null;
095    
096                    if (!systemEventHierarchyEntries.isEmpty()) {
097                            parentSystemEventHierarchyEntry =
098                                    systemEventHierarchyEntries.peek();
099                    }
100    
101                    if (parentSystemEventHierarchyEntry == null) {
102                            systemEventSetKey = CounterLocalServiceUtil.increment();
103                    }
104                    else if (parentSystemEventHierarchyEntry.getAction() ==
105                                            SystemEventConstants.ACTION_SKIP) {
106    
107                            return null;
108                    }
109                    else {
110                            parentSystemEventId =
111                                    parentSystemEventHierarchyEntry.getSystemEventId();
112                            systemEventSetKey =
113                                    parentSystemEventHierarchyEntry.getSystemEventSetKey();
114                    }
115    
116                    SystemEventHierarchyEntry systemEventHierarchyEntry =
117                            new SystemEventHierarchyEntry(
118                                    CounterLocalServiceUtil.increment(), classNameId, classPK,
119                                    parentSystemEventId, systemEventSetKey, action);
120    
121                    return systemEventHierarchyEntries.push(systemEventHierarchyEntry);
122            }
123    
124            public static SystemEventHierarchyEntry push(
125                            String className, long classPK, int action)
126                    throws SystemException {
127    
128                    return push(PortalUtil.getClassNameId(className), classPK, action);
129            }
130    
131            private static ThreadLocal<Stack<SystemEventHierarchyEntry>>
132                    _systemEventHierarchyEntries =
133                            new AutoResetThreadLocal<Stack<SystemEventHierarchyEntry>>(
134                                    SystemEventHierarchyEntryThreadLocal.class +
135                                            "._systemEventHierarchyEntries",
136                                    new Stack<SystemEventHierarchyEntry>());
137    
138    }