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