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.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, SystemException {
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, SystemException {
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) throws SystemException {
084                    systemEventPersistence.removeByGroupId(groupId);
085            }
086    
087            @Override
088            public SystemEvent fetchSystemEvent(
089                            long groupId, long classNameId, long classPK, int type)
090                    throws SystemException {
091    
092                    return systemEventPersistence.fetchByG_C_C_T_First(
093                            groupId, classNameId, classPK, type, null);
094            }
095    
096            @Override
097            public List<SystemEvent> getSystemEvents(
098                            long groupId, long classNameId, long classPK)
099                    throws SystemException {
100    
101                    return systemEventPersistence.findByG_C_C(
102                            groupId, classNameId, classPK);
103            }
104    
105            @Override
106            public List<SystemEvent> getSystemEvents(
107                            long groupId, long classNameId, long classPK, int type)
108                    throws SystemException {
109    
110                    return systemEventPersistence.findByG_C_C_T(
111                            groupId, classNameId, classPK, type);
112            }
113    
114            protected SystemEvent addSystemEvent(
115                            long userId, long companyId, long groupId, String className,
116                            long classPK, String classUuid, String referrerClassName, int type,
117                            String extraData, String userName)
118                    throws PortalException, SystemException {
119    
120                    SystemEventHierarchyEntry systemEventHierarchyEntry =
121                            SystemEventHierarchyEntryThreadLocal.peek();
122    
123                    int action = SystemEventConstants.ACTION_NONE;
124    
125                    if (systemEventHierarchyEntry != null) {
126                            action = systemEventHierarchyEntry.getAction();
127    
128                            if ((action == SystemEventConstants.ACTION_SKIP) &&
129                                    !systemEventHierarchyEntry.hasTypedModel(
130                                            className, classPK)) {
131    
132                                    return null;
133                            }
134                    }
135    
136                    Company company = companyPersistence.findByPrimaryKey(companyId);
137    
138                    Group companyGroup = company.getGroup();
139    
140                    if (companyGroup.getGroupId() == groupId) {
141                            groupId = 0;
142                    }
143    
144                    if (Validator.isNotNull(referrerClassName) &&
145                            referrerClassName.equals(className)) {
146    
147                            referrerClassName = null;
148                    }
149    
150                    long systemEventId = 0;
151    
152                    if ((systemEventHierarchyEntry != null) &&
153                            systemEventHierarchyEntry.hasTypedModel(className, classPK)) {
154    
155                            systemEventId = systemEventHierarchyEntry.getSystemEventId();
156                    }
157                    else {
158                            systemEventId = counterLocalService.increment();
159                    }
160    
161                    SystemEvent systemEvent = systemEventPersistence.create(systemEventId);
162    
163                    systemEvent.setGroupId(groupId);
164                    systemEvent.setCompanyId(companyId);
165                    systemEvent.setUserId(userId);
166                    systemEvent.setUserName(userName);
167                    systemEvent.setCreateDate(new Date());
168                    systemEvent.setClassName(className);
169                    systemEvent.setClassPK(classPK);
170                    systemEvent.setClassUuid(classUuid);
171                    systemEvent.setReferrerClassName(referrerClassName);
172    
173                    long parentSystemEventId = 0;
174    
175                    if (action == SystemEventConstants.ACTION_HIERARCHY) {
176                            if (systemEventHierarchyEntry.hasTypedModel(className, classPK)) {
177                                    parentSystemEventId =
178                                            systemEventHierarchyEntry.getParentSystemEventId();
179                            }
180                            else {
181                                    parentSystemEventId =
182                                            systemEventHierarchyEntry.getSystemEventId();
183                            }
184                    }
185    
186                    systemEvent.setParentSystemEventId(parentSystemEventId);
187    
188                    long systemEventSetKey = 0;
189    
190                    if ((action == SystemEventConstants.ACTION_GROUP) ||
191                            (action == SystemEventConstants.ACTION_HIERARCHY)) {
192    
193                            systemEventSetKey =
194                                    systemEventHierarchyEntry.getSystemEventSetKey();
195                    }
196                    else {
197                            systemEventSetKey = counterLocalService.increment();
198                    }
199    
200                    systemEvent.setSystemEventSetKey(systemEventSetKey);
201    
202                    systemEvent.setType(type);
203                    systemEvent.setExtraData(extraData);
204    
205                    return systemEventPersistence.update(systemEvent);
206            }
207    
208    }