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