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