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.portlet.messageboards.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.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.systemevent.SystemEvent;
022    import com.liferay.portal.kernel.util.DateUtil;
023    import com.liferay.portal.model.SystemEventConstants;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portlet.messageboards.model.MBThread;
027    import com.liferay.portlet.messageboards.model.MBThreadFlag;
028    import com.liferay.portlet.messageboards.service.base.MBThreadFlagLocalServiceBaseImpl;
029    
030    import java.util.List;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Shuyang Zhou
035     */
036    public class MBThreadFlagLocalServiceImpl
037            extends MBThreadFlagLocalServiceBaseImpl {
038    
039            @Override
040            public MBThreadFlag addThreadFlag(
041                            long userId, MBThread thread, ServiceContext serviceContext)
042                    throws PortalException {
043    
044                    User user = userPersistence.findByPrimaryKey(userId);
045    
046                    if (user.isDefaultUser()) {
047                            return null;
048                    }
049    
050                    long threadId = thread.getThreadId();
051    
052                    MBThreadFlag threadFlag = mbThreadFlagPersistence.fetchByU_T(
053                            userId, threadId);
054    
055                    if (threadFlag == null) {
056                            long threadFlagId = counterLocalService.increment();
057    
058                            threadFlag = mbThreadFlagPersistence.create(threadFlagId);
059    
060                            threadFlag.setUuid(serviceContext.getUuid());
061                            threadFlag.setGroupId(thread.getGroupId());
062                            threadFlag.setCompanyId(user.getCompanyId());
063                            threadFlag.setUserId(userId);
064                            threadFlag.setUserName(user.getFullName());
065                            threadFlag.setModifiedDate(
066                                    serviceContext.getModifiedDate(thread.getLastPostDate()));
067                            threadFlag.setThreadId(threadId);
068    
069                            try {
070                                    mbThreadFlagPersistence.update(threadFlag);
071                            }
072                            catch (SystemException se) {
073                                    if (_log.isWarnEnabled()) {
074                                            _log.warn(
075                                                    "Add failed, fetch {userId=" + userId + ", threadId=" +
076                                                            threadId + "}");
077                                    }
078    
079                                    threadFlag = mbThreadFlagPersistence.fetchByU_T(
080                                            userId, threadId, false);
081    
082                                    if (threadFlag == null) {
083                                            throw se;
084                                    }
085                            }
086                    }
087                    else if (!DateUtil.equals(
088                                            threadFlag.getModifiedDate(), thread.getLastPostDate())) {
089    
090                            threadFlag.setModifiedDate(thread.getLastPostDate());
091    
092                            mbThreadFlagPersistence.update(threadFlag);
093                    }
094    
095                    return threadFlag;
096            }
097    
098            @Override
099            public void deleteThreadFlag(long threadFlagId) throws PortalException {
100                    MBThreadFlag threadFlag = mbThreadFlagPersistence.findByPrimaryKey(
101                            threadFlagId);
102    
103                    mbThreadFlagLocalService.deleteThreadFlag(threadFlag);
104            }
105    
106            @Override
107            @SystemEvent(type = SystemEventConstants.TYPE_DELETE)
108            public void deleteThreadFlag(MBThreadFlag threadFlag) {
109                    mbThreadFlagPersistence.remove(threadFlag);
110            }
111    
112            @Override
113            public void deleteThreadFlagsByThreadId(long threadId) {
114                    List<MBThreadFlag> threadFlags = mbThreadFlagPersistence.findByThreadId(
115                            threadId);
116    
117                    for (MBThreadFlag threadFlag : threadFlags) {
118                            mbThreadFlagLocalService.deleteThreadFlag(threadFlag);
119                    }
120            }
121    
122            @Override
123            public void deleteThreadFlagsByUserId(long userId) {
124                    List<MBThreadFlag> threadFlags = mbThreadFlagPersistence.findByUserId(
125                            userId);
126    
127                    for (MBThreadFlag threadFlag : threadFlags) {
128                            mbThreadFlagLocalService.deleteThreadFlag(threadFlag);
129                    }
130            }
131    
132            @Override
133            public MBThreadFlag getThreadFlag(long userId, MBThread thread)
134                    throws PortalException {
135    
136                    User user = userPersistence.findByPrimaryKey(userId);
137    
138                    if (user.isDefaultUser()) {
139                            return null;
140                    }
141    
142                    return mbThreadFlagPersistence.fetchByU_T(userId, thread.getThreadId());
143            }
144    
145            @Override
146            public boolean hasThreadFlag(long userId, MBThread thread)
147                    throws PortalException {
148    
149                    User user = userPersistence.findByPrimaryKey(userId);
150    
151                    if (user.isDefaultUser()) {
152                            return true;
153                    }
154    
155                    MBThreadFlag threadFlag = mbThreadFlagPersistence.fetchByU_T(
156                            userId, thread.getThreadId());
157    
158                    if ((threadFlag != null) &&
159                            DateUtil.equals(
160                                    threadFlag.getModifiedDate(), thread.getLastPostDate())) {
161    
162                            return true;
163                    }
164                    else {
165                            return false;
166                    }
167            }
168    
169            private static final Log _log = LogFactoryUtil.getLog(
170                    MBThreadFlagLocalServiceImpl.class);
171    
172    }