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