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.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.systemevent.SystemEventHierarchyEntry;
019    import com.liferay.portal.kernel.systemevent.SystemEventHierarchyEntryThreadLocal;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.Company;
023    import com.liferay.portal.model.Group;
024    import com.liferay.portal.model.SystemEvent;
025    import com.liferay.portal.model.SystemEventConstants;
026    import com.liferay.portal.model.User;
027    import com.liferay.portal.security.auth.CompanyThreadLocal;
028    import com.liferay.portal.security.auth.PrincipalThreadLocal;
029    import com.liferay.portal.service.base.SystemEventLocalServiceBaseImpl;
030    
031    import java.util.Date;
032    import java.util.List;
033    
034    /**
035     * @author Zsolt Berentey
036     */
037    public class SystemEventLocalServiceImpl
038            extends SystemEventLocalServiceBaseImpl {
039    
040            @Override
041            public SystemEvent addSystemEvent(
042                            long userId, long groupId, String className, long classPK,
043                            String classUuid, String referrerClassName, int type,
044                            String extraData)
045                    throws PortalException {
046    
047                    if (userId == 0) {
048                            userId = PrincipalThreadLocal.getUserId();
049                    }
050    
051                    long companyId = 0;
052                    String userName = StringPool.BLANK;
053    
054                    if (userId > 0) {
055                            User user = userPersistence.findByPrimaryKey(userId);
056    
057                            companyId = user.getCompanyId();
058                            userName = user.getFullName();
059                    }
060                    else if (groupId > 0) {
061                            Group group = groupPersistence.findByPrimaryKey(groupId);
062    
063                            companyId = group.getCompanyId();
064                    }
065    
066                    return addSystemEvent(
067                            userId, companyId, groupId, className, classPK, classUuid,
068                            referrerClassName, type, extraData, userName);
069            }
070    
071            @Override
072            public SystemEvent addSystemEvent(
073                            long companyId, String className, long classPK, String classUuid,
074                            String referrerClassName, int type, String extraData)
075                    throws PortalException {
076    
077                    return addSystemEvent(
078                            0, companyId, 0, className, classPK, classUuid, referrerClassName,
079                            type, extraData, StringPool.BLANK);
080            }
081    
082            @Override
083            public void deleteSystemEvents(long groupId) {
084                    systemEventPersistence.removeByGroupId(groupId);
085            }
086    
087            @Override
088            public void deleteSystemEvents(long groupId, long systemEventSetKey) {
089                    systemEventPersistence.removeByG_S(groupId, systemEventSetKey);
090            }
091    
092            @Override
093            public SystemEvent fetchSystemEvent(
094                    long groupId, long classNameId, long classPK, int type) {
095    
096                    return systemEventPersistence.fetchByG_C_C_T_First(
097                            groupId, classNameId, classPK, type, null);
098            }
099    
100            @Override
101            public List<SystemEvent> getSystemEvents(
102                    long groupId, long classNameId, long classPK) {
103    
104                    return systemEventPersistence.findByG_C_C(
105                            groupId, classNameId, classPK);
106            }
107    
108            @Override
109            public List<SystemEvent> getSystemEvents(
110                    long groupId, long classNameId, long classPK, int type) {
111    
112                    return systemEventPersistence.findByG_C_C_T(
113                            groupId, classNameId, classPK, type);
114            }
115    
116            protected SystemEvent addSystemEvent(
117                            long userId, long companyId, long groupId, String className,
118                            long classPK, String classUuid, String referrerClassName, int type,
119                            String extraData, String userName)
120                    throws PortalException {
121    
122                    SystemEventHierarchyEntry systemEventHierarchyEntry =
123                            SystemEventHierarchyEntryThreadLocal.peek();
124    
125                    int action = SystemEventConstants.ACTION_NONE;
126    
127                    if (systemEventHierarchyEntry != null) {
128                            action = systemEventHierarchyEntry.getAction();
129    
130                            if ((action == SystemEventConstants.ACTION_SKIP) &&
131                                    !systemEventHierarchyEntry.hasTypedModel(
132                                            className, classPK)) {
133    
134                                    return null;
135                            }
136                    }
137    
138                    if (!CompanyThreadLocal.isDeleteInProcess()) {
139                            Company company = companyPersistence.findByPrimaryKey(companyId);
140    
141                            Group companyGroup = company.getGroup();
142    
143                            if (companyGroup.getGroupId() == groupId) {
144                                    groupId = 0;
145                            }
146                    }
147    
148                    if (Validator.isNotNull(referrerClassName) &&
149                            referrerClassName.equals(className)) {
150    
151                            referrerClassName = null;
152                    }
153    
154                    long systemEventId = 0;
155    
156                    if ((systemEventHierarchyEntry != null) &&
157                            systemEventHierarchyEntry.hasTypedModel(className, classPK)) {
158    
159                            systemEventId = systemEventHierarchyEntry.getSystemEventId();
160                    }
161                    else {
162                            systemEventId = counterLocalService.increment();
163                    }
164    
165                    SystemEvent systemEvent = systemEventPersistence.create(systemEventId);
166    
167                    systemEvent.setGroupId(groupId);
168                    systemEvent.setCompanyId(companyId);
169                    systemEvent.setUserId(userId);
170                    systemEvent.setUserName(userName);
171                    systemEvent.setCreateDate(new Date());
172                    systemEvent.setClassName(className);
173                    systemEvent.setClassPK(classPK);
174                    systemEvent.setClassUuid(classUuid);
175                    systemEvent.setReferrerClassName(referrerClassName);
176    
177                    long parentSystemEventId = 0;
178    
179                    if (action == SystemEventConstants.ACTION_HIERARCHY) {
180                            if (systemEventHierarchyEntry.hasTypedModel(className, classPK)) {
181                                    parentSystemEventId =
182                                            systemEventHierarchyEntry.getParentSystemEventId();
183                            }
184                            else {
185                                    parentSystemEventId =
186                                            systemEventHierarchyEntry.getSystemEventId();
187                            }
188                    }
189    
190                    systemEvent.setParentSystemEventId(parentSystemEventId);
191    
192                    long systemEventSetKey = 0;
193    
194                    if ((action == SystemEventConstants.ACTION_GROUP) ||
195                            (action == SystemEventConstants.ACTION_HIERARCHY)) {
196    
197                            systemEventSetKey =
198                                    systemEventHierarchyEntry.getSystemEventSetKey();
199                    }
200                    else {
201                            systemEventSetKey = counterLocalService.increment();
202                    }
203    
204                    systemEvent.setSystemEventSetKey(systemEventSetKey);
205    
206                    systemEvent.setType(type);
207                    systemEvent.setExtraData(extraData);
208    
209                    return systemEventPersistence.update(systemEvent);
210            }
211    
212    }