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