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(
083                    String className, long classPK) {
084    
085                    return pop(PortalUtil.getClassNameId(className), classPK);
086            }
087    
088            public static SystemEventHierarchyEntry push() throws SystemException {
089                    return push(SystemEventConstants.ACTION_SKIP);
090            }
091    
092            public static SystemEventHierarchyEntry push(Class<?> clazz)
093                    throws SystemException {
094    
095                    return push(
096                            PortalUtil.getClassNameId(clazz), 0,
097                            SystemEventConstants.ACTION_SKIP);
098            }
099    
100            public static SystemEventHierarchyEntry push(Class<?> clazz, long classPK)
101                    throws SystemException {
102    
103                    return push(
104                            PortalUtil.getClassNameId(clazz), classPK,
105                            SystemEventConstants.ACTION_SKIP);
106            }
107    
108            public static SystemEventHierarchyEntry push(
109                            Class<?> clazz, long classPK, int action)
110                    throws SystemException {
111    
112                    return push(PortalUtil.getClassNameId(clazz), classPK, action);
113            }
114    
115            public static SystemEventHierarchyEntry push(int action)
116                    throws SystemException {
117    
118                    return push(0, 0, action);
119            }
120    
121            public static SystemEventHierarchyEntry push(
122                            long classNameId, long classPK, int action)
123                    throws SystemException {
124    
125                    long parentSystemEventId = 0;
126                    long systemEventSetKey = 0;
127    
128                    Stack<SystemEventHierarchyEntry> systemEventHierarchyEntries =
129                            _systemEventHierarchyEntries.get();
130    
131                    SystemEventHierarchyEntry parentSystemEventHierarchyEntry = null;
132    
133                    if (!systemEventHierarchyEntries.isEmpty()) {
134                            parentSystemEventHierarchyEntry =
135                                    systemEventHierarchyEntries.peek();
136                    }
137    
138                    if (parentSystemEventHierarchyEntry == null) {
139                            systemEventSetKey = CounterLocalServiceUtil.increment();
140                    }
141                    else if (parentSystemEventHierarchyEntry.getAction() ==
142                                            SystemEventConstants.ACTION_SKIP) {
143    
144                            return null;
145                    }
146                    else {
147                            parentSystemEventId =
148                                    parentSystemEventHierarchyEntry.getSystemEventId();
149                            systemEventSetKey =
150                                    parentSystemEventHierarchyEntry.getSystemEventSetKey();
151                    }
152    
153                    SystemEventHierarchyEntry systemEventHierarchyEntry =
154                            new SystemEventHierarchyEntry(
155                                    CounterLocalServiceUtil.increment(), classNameId, classPK,
156                                    parentSystemEventId, systemEventSetKey, action);
157    
158                    return systemEventHierarchyEntries.push(systemEventHierarchyEntry);
159            }
160    
161            public static SystemEventHierarchyEntry push(
162                            String className, long classPK, int action)
163                    throws SystemException {
164    
165                    return push(PortalUtil.getClassNameId(className), classPK, action);
166            }
167    
168            private static ThreadLocal<Stack<SystemEventHierarchyEntry>>
169                    _systemEventHierarchyEntries =
170                            new AutoResetThreadLocal<Stack<SystemEventHierarchyEntry>>(
171                                    SystemEventHierarchyEntryThreadLocal.class +
172                                            "._systemEventHierarchyEntries",
173                                    new Stack<SystemEventHierarchyEntry>());
174    
175    }