001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.fetchByPrimaryKey(userId);
057    
058                            if (user != null) {
059                                    companyId = user.getCompanyId();
060                                    userName = user.getFullName();
061                            }
062                    }
063                    else if (groupId > 0) {
064                            Group group = groupPersistence.findByPrimaryKey(groupId);
065    
066                            companyId = group.getCompanyId();
067                    }
068    
069                    return addSystemEvent(
070                            userId, companyId, groupId, className, classPK, classUuid,
071                            referrerClassName, type, extraData, userName);
072            }
073    
074            @Override
075            public SystemEvent addSystemEvent(
076                            long companyId, String className, long classPK, String classUuid,
077                            String referrerClassName, int type, String extraData)
078                    throws PortalException, SystemException {
079    
080                    return addSystemEvent(
081                            0, companyId, 0, className, classPK, classUuid, referrerClassName,
082                            type, extraData, StringPool.BLANK);
083            }
084    
085            @Override
086            public void deleteSystemEvents(long groupId) throws SystemException {
087                    systemEventPersistence.removeByGroupId(groupId);
088            }
089    
090            @Override
091            public void deleteSystemEvents(long groupId, long systemEventSetKey)
092                    throws SystemException {
093    
094                    systemEventPersistence.removeByG_S(groupId, systemEventSetKey);
095            }
096    
097            @Override
098            public SystemEvent fetchSystemEvent(
099                            long groupId, long classNameId, long classPK, int type)
100                    throws SystemException {
101    
102                    return systemEventPersistence.fetchByG_C_C_T_First(
103                            groupId, classNameId, classPK, type, null);
104            }
105    
106            @Override
107            public List<SystemEvent> getSystemEvents(
108                            long groupId, long classNameId, long classPK)
109                    throws SystemException {
110    
111                    return systemEventPersistence.findByG_C_C(
112                            groupId, classNameId, classPK);
113            }
114    
115            @Override
116            public List<SystemEvent> getSystemEvents(
117                            long groupId, long classNameId, long classPK, int type)
118                    throws SystemException {
119    
120                    return systemEventPersistence.findByG_C_C_T(
121                            groupId, classNameId, classPK, type);
122            }
123    
124            protected SystemEvent addSystemEvent(
125                            long userId, long companyId, long groupId, String className,
126                            long classPK, String classUuid, String referrerClassName, int type,
127                            String extraData, String userName)
128                    throws PortalException, SystemException {
129    
130                    SystemEventHierarchyEntry systemEventHierarchyEntry =
131                            SystemEventHierarchyEntryThreadLocal.peek();
132    
133                    int action = SystemEventConstants.ACTION_NONE;
134    
135                    if (systemEventHierarchyEntry != null) {
136                            action = systemEventHierarchyEntry.getAction();
137    
138                            if ((action == SystemEventConstants.ACTION_SKIP) &&
139                                    !systemEventHierarchyEntry.hasTypedModel(
140                                            className, classPK)) {
141    
142                                    return null;
143                            }
144                    }
145    
146                    if (!CompanyThreadLocal.isDeleteInProcess()) {
147                            Company company = companyPersistence.findByPrimaryKey(companyId);
148    
149                            Group companyGroup = company.getGroup();
150    
151                            if (companyGroup.getGroupId() == groupId) {
152                                    groupId = 0;
153                            }
154                    }
155    
156                    if (Validator.isNotNull(referrerClassName) &&
157                            referrerClassName.equals(className)) {
158    
159                            referrerClassName = null;
160                    }
161    
162                    long systemEventId = 0;
163    
164                    if ((systemEventHierarchyEntry != null) &&
165                            systemEventHierarchyEntry.hasTypedModel(className, classPK)) {
166    
167                            systemEventId = systemEventHierarchyEntry.getSystemEventId();
168                    }
169                    else {
170                            systemEventId = counterLocalService.increment();
171                    }
172    
173                    SystemEvent systemEvent = systemEventPersistence.create(systemEventId);
174    
175                    systemEvent.setGroupId(groupId);
176                    systemEvent.setCompanyId(companyId);
177                    systemEvent.setUserId(userId);
178                    systemEvent.setUserName(userName);
179                    systemEvent.setCreateDate(new Date());
180                    systemEvent.setClassName(className);
181                    systemEvent.setClassPK(classPK);
182                    systemEvent.setClassUuid(classUuid);
183                    systemEvent.setReferrerClassName(referrerClassName);
184    
185                    long parentSystemEventId = 0;
186    
187                    if (action == SystemEventConstants.ACTION_HIERARCHY) {
188                            if (systemEventHierarchyEntry.hasTypedModel(className, classPK)) {
189                                    parentSystemEventId =
190                                            systemEventHierarchyEntry.getParentSystemEventId();
191                            }
192                            else {
193                                    parentSystemEventId =
194                                            systemEventHierarchyEntry.getSystemEventId();
195                            }
196                    }
197    
198                    systemEvent.setParentSystemEventId(parentSystemEventId);
199    
200                    long systemEventSetKey = 0;
201    
202                    if ((action == SystemEventConstants.ACTION_GROUP) ||
203                            (action == SystemEventConstants.ACTION_HIERARCHY)) {
204    
205                            systemEventSetKey =
206                                    systemEventHierarchyEntry.getSystemEventSetKey();
207                    }
208                    else {
209                            systemEventSetKey = counterLocalService.increment();
210                    }
211    
212                    systemEvent.setSystemEventSetKey(systemEventSetKey);
213    
214                    systemEvent.setType(type);
215                    systemEvent.setExtraData(extraData);
216    
217                    return systemEventPersistence.update(systemEvent);
218            }
219    
220    }