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