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                    return pop(-1, -1);
050            }
051    
052            public static SystemEventHierarchyEntry pop(Class<?> clazz) {
053                    return pop(PortalUtil.getClassNameId(clazz), 0);
054            }
055    
056            public static SystemEventHierarchyEntry pop(Class<?> clazz, long classPK) {
057                    return pop(PortalUtil.getClassNameId(clazz), classPK);
058            }
059    
060            public static SystemEventHierarchyEntry pop(
061                    long classNameId, long classPK) {
062    
063                    Stack<SystemEventHierarchyEntry> systemEventHierarchyEntries =
064                            _systemEventHierarchyEntries.get();
065    
066                    if (systemEventHierarchyEntries.isEmpty()) {
067                            return null;
068                    }
069    
070                    SystemEventHierarchyEntry systemEventHierarchyEntry =
071                            systemEventHierarchyEntries.peek();
072    
073                    if (((classNameId < 0) && (classPK < 0)) ||
074                            systemEventHierarchyEntry.hasTypedModel(classNameId, classPK)) {
075    
076                            return systemEventHierarchyEntries.pop();
077                    }
078    
079                    return null;
080            }
081    
082            public static SystemEventHierarchyEntry pop(String className) {
083                    return pop(PortalUtil.getClassNameId(className), 0);
084            }
085    
086            public static SystemEventHierarchyEntry pop(
087                    String className, long classPK) {
088    
089                    return pop(PortalUtil.getClassNameId(className), classPK);
090            }
091    
092            public static SystemEventHierarchyEntry push() throws SystemException {
093                    return push(SystemEventConstants.ACTION_SKIP);
094            }
095    
096            public static SystemEventHierarchyEntry push(Class<?> clazz)
097                    throws SystemException {
098    
099                    return push(
100                            PortalUtil.getClassNameId(clazz), 0,
101                            SystemEventConstants.ACTION_SKIP);
102            }
103    
104            public static SystemEventHierarchyEntry push(Class<?> clazz, long classPK)
105                    throws SystemException {
106    
107                    return push(
108                            PortalUtil.getClassNameId(clazz), classPK,
109                            SystemEventConstants.ACTION_SKIP);
110            }
111    
112            public static SystemEventHierarchyEntry push(
113                            Class<?> clazz, long classPK, int action)
114                    throws SystemException {
115    
116                    return push(PortalUtil.getClassNameId(clazz), classPK, action);
117            }
118    
119            public static SystemEventHierarchyEntry push(int action)
120                    throws SystemException {
121    
122                    return push(0, 0, action);
123            }
124    
125            public static SystemEventHierarchyEntry push(
126                            long classNameId, long classPK, int action)
127                    throws SystemException {
128    
129                    long parentSystemEventId = 0;
130                    long systemEventSetKey = 0;
131    
132                    Stack<SystemEventHierarchyEntry> systemEventHierarchyEntries =
133                            _systemEventHierarchyEntries.get();
134    
135                    SystemEventHierarchyEntry parentSystemEventHierarchyEntry = null;
136    
137                    if (!systemEventHierarchyEntries.isEmpty()) {
138                            parentSystemEventHierarchyEntry =
139                                    systemEventHierarchyEntries.peek();
140                    }
141    
142                    if (parentSystemEventHierarchyEntry == null) {
143                            systemEventSetKey = CounterLocalServiceUtil.increment();
144                    }
145                    else if (parentSystemEventHierarchyEntry.getAction() ==
146                                            SystemEventConstants.ACTION_SKIP) {
147    
148                            return null;
149                    }
150                    else {
151                            parentSystemEventId =
152                                    parentSystemEventHierarchyEntry.getSystemEventId();
153                            systemEventSetKey =
154                                    parentSystemEventHierarchyEntry.getSystemEventSetKey();
155                    }
156    
157                    SystemEventHierarchyEntry systemEventHierarchyEntry =
158                            new SystemEventHierarchyEntry(
159                                    CounterLocalServiceUtil.increment(), classNameId, classPK,
160                                    parentSystemEventId, systemEventSetKey, action);
161    
162                    return systemEventHierarchyEntries.push(systemEventHierarchyEntry);
163            }
164    
165            public static SystemEventHierarchyEntry push(String className)
166                    throws SystemException {
167    
168                    return push(className, 0, SystemEventConstants.ACTION_SKIP);
169            }
170    
171            public static SystemEventHierarchyEntry push(
172                            String className, long classPK, int action)
173                    throws SystemException {
174    
175                    return push(PortalUtil.getClassNameId(className), classPK, action);
176            }
177    
178            private static ThreadLocal<Stack<SystemEventHierarchyEntry>>
179                    _systemEventHierarchyEntries =
180                            new AutoResetThreadLocal<Stack<SystemEventHierarchyEntry>>(
181                                    SystemEventHierarchyEntryThreadLocal.class +
182                                            "._systemEventHierarchyEntries",
183                                    new Stack<SystemEventHierarchyEntry>());
184    
185    }