001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(
120                            userId, thread.getThreadId());
121            }
122    
123            public boolean hasThreadFlag(long userId, MBThread thread)
124                    throws PortalException, SystemException {
125    
126                    User user = userPersistence.findByPrimaryKey(userId);
127    
128                    if (user.isDefaultUser()) {
129                            return true;
130                    }
131    
132                    MBThreadFlag threadFlag = mbThreadFlagPersistence.fetchByU_T(
133                            userId, thread.getThreadId());
134    
135                    if ((threadFlag != null) &&
136                            (DateUtil.equals(
137                                    threadFlag.getModifiedDate(), thread.getLastPostDate(),
138                                    true))) {
139    
140                            return true;
141                    }
142                    else {
143                            return false;
144                    }
145            }
146    
147            private static Log _log = LogFactoryUtil.getLog(
148                    MBThreadFlagLocalServiceImpl.class);
149    
150    }