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.Date;
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 void 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;
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.setCreateDate(serviceContext.getCreateDate(new Date()));
066                            threadFlag.setModifiedDate(
067                                    serviceContext.getModifiedDate(thread.getLastPostDate()));
068                            threadFlag.setThreadId(threadId);
069    
070                            try {
071                                    mbThreadFlagPersistence.update(threadFlag);
072                            }
073                            catch (SystemException se) {
074                                    if (_log.isWarnEnabled()) {
075                                            _log.warn(
076                                                    "Add failed, fetch {userId=" + userId + ", threadId=" +
077                                                            threadId + "}");
078                                    }
079    
080                                    threadFlag = mbThreadFlagPersistence.fetchByU_T(
081                                            userId, threadId, false);
082    
083                                    if (threadFlag == null) {
084                                            throw se;
085                                    }
086                            }
087                    }
088                    else if (!DateUtil.equals(
089                                            threadFlag.getModifiedDate(), thread.getLastPostDate(),
090                                            true)) {
091    
092                            threadFlag.setModifiedDate(thread.getLastPostDate());
093    
094                            mbThreadFlagPersistence.update(threadFlag);
095                    }
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                    mbThreadFlagPersistence.removeByThreadId(threadId);
115            }
116    
117            @Override
118            public void deleteThreadFlagsByUserId(long userId) {
119                    mbThreadFlagPersistence.removeByUserId(userId);
120            }
121    
122            @Override
123            public MBThreadFlag getThreadFlag(long userId, MBThread thread)
124                    throws PortalException {
125    
126                    User user = userPersistence.findByPrimaryKey(userId);
127    
128                    if (user.isDefaultUser()) {
129                            return null;
130                    }
131    
132                    return mbThreadFlagPersistence.fetchByU_T(userId, thread.getThreadId());
133            }
134    
135            @Override
136            public boolean hasThreadFlag(long userId, MBThread thread)
137                    throws PortalException {
138    
139                    User user = userPersistence.findByPrimaryKey(userId);
140    
141                    if (user.isDefaultUser()) {
142                            return true;
143                    }
144    
145                    MBThreadFlag threadFlag = mbThreadFlagPersistence.fetchByU_T(
146                            userId, thread.getThreadId());
147    
148                    if ((threadFlag != null) &&
149                            DateUtil.equals(
150                                    threadFlag.getModifiedDate(), thread.getLastPostDate(), true)) {
151    
152                            return true;
153                    }
154                    else {
155                            return false;
156                    }
157            }
158    
159            private static final Log _log = LogFactoryUtil.getLog(
160                    MBThreadFlagLocalServiceImpl.class);
161    
162    }