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                                            true)) {
090    
091                            threadFlag.setModifiedDate(thread.getLastPostDate());
092    
093                            mbThreadFlagPersistence.update(threadFlag);
094                    }
095    
096                    return threadFlag;
097            }
098    
099            @Override
100            public void deleteThreadFlag(long threadFlagId) throws PortalException {
101                    MBThreadFlag threadFlag = mbThreadFlagPersistence.findByPrimaryKey(
102                            threadFlagId);
103    
104                    mbThreadFlagLocalService.deleteThreadFlag(threadFlag);
105            }
106    
107            @Override
108            @SystemEvent(type = SystemEventConstants.TYPE_DELETE)
109            public void deleteThreadFlag(MBThreadFlag threadFlag) {
110                    mbThreadFlagPersistence.remove(threadFlag);
111            }
112    
113            @Override
114            public void deleteThreadFlagsByThreadId(long threadId) {
115                    List<MBThreadFlag> threadFlags = mbThreadFlagPersistence.findByThreadId(
116                            threadId);
117    
118                    for (MBThreadFlag threadFlag : threadFlags) {
119                            mbThreadFlagLocalService.deleteThreadFlag(threadFlag);
120                    }
121            }
122    
123            @Override
124            public void deleteThreadFlagsByUserId(long userId) {
125                    List<MBThreadFlag> threadFlags = mbThreadFlagPersistence.findByUserId(
126                            userId);
127    
128                    for (MBThreadFlag threadFlag : threadFlags) {
129                            mbThreadFlagLocalService.deleteThreadFlag(threadFlag);
130                    }
131            }
132    
133            @Override
134            public MBThreadFlag getThreadFlag(long userId, MBThread thread)
135                    throws PortalException {
136    
137                    User user = userPersistence.findByPrimaryKey(userId);
138    
139                    if (user.isDefaultUser()) {
140                            return null;
141                    }
142    
143                    return mbThreadFlagPersistence.fetchByU_T(userId, thread.getThreadId());
144            }
145    
146            @Override
147            public boolean hasThreadFlag(long userId, MBThread thread)
148                    throws PortalException {
149    
150                    User user = userPersistence.findByPrimaryKey(userId);
151    
152                    if (user.isDefaultUser()) {
153                            return true;
154                    }
155    
156                    MBThreadFlag threadFlag = mbThreadFlagPersistence.fetchByU_T(
157                            userId, thread.getThreadId());
158    
159                    if ((threadFlag != null) &&
160                            DateUtil.equals(
161                                    threadFlag.getModifiedDate(), thread.getLastPostDate(), true)) {
162    
163                            return true;
164                    }
165                    else {
166                            return false;
167                    }
168            }
169    
170            private static final Log _log = LogFactoryUtil.getLog(
171                    MBThreadFlagLocalServiceImpl.class);
172    
173    }