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.model.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.repository.model.Folder;
020    import com.liferay.portal.model.Lock;
021    import com.liferay.portal.portletfilerepository.PortletFileRepositoryUtil;
022    import com.liferay.portal.service.LockLocalServiceUtil;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
026    import com.liferay.portlet.messageboards.model.MBCategory;
027    import com.liferay.portlet.messageboards.model.MBMessage;
028    import com.liferay.portlet.messageboards.model.MBThread;
029    import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
030    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Mika Koivisto
035     */
036    public class MBThreadImpl extends MBThreadBaseImpl {
037    
038            public MBThreadImpl() {
039            }
040    
041            public long getAttachmentsFolderId()
042                    throws PortalException, SystemException {
043    
044                    ServiceContext serviceContext = new ServiceContext();
045    
046                    serviceContext.setAddGroupPermissions(true);
047                    serviceContext.setAddGuestPermissions(true);
048    
049                    long repositoryId = PortletFileRepositoryUtil.getPortletRepository(
050                            getGroupId(), PortletKeys.MESSAGE_BOARDS, serviceContext);
051    
052                    MBMessage message = MBMessageLocalServiceUtil.getMessage(
053                            getRootMessageId());
054    
055                    Folder folder = PortletFileRepositoryUtil.getPortletFolder(
056                            message.getUserId(), repositoryId,
057                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
058                            String.valueOf(getThreadId()), serviceContext);
059    
060                    return folder.getFolderId();
061            }
062    
063            public Lock getLock() {
064                    try {
065                            return LockLocalServiceUtil.getLock(
066                                    MBThread.class.getName(), getThreadId());
067                    }
068                    catch (Exception e) {
069                    }
070    
071                    return null;
072            }
073    
074            public MBCategory getTrashContainer() {
075                    try {
076                            MBCategory category = MBCategoryLocalServiceUtil.getCategory(
077                                    getCategoryId());
078    
079                            if (category.isInTrash()) {
080                                    return category;
081                            }
082    
083                            return category.getTrashContainer();
084                    }
085                    catch (Exception e) {
086                            return null;
087                    }
088            }
089    
090            public boolean hasLock(long userId) {
091                    try {
092                            return LockLocalServiceUtil.hasLock(
093                                    userId, MBThread.class.getName(), getThreadId());
094                    }
095                    catch (Exception e) {
096                    }
097    
098                    return false;
099            }
100    
101            public boolean isInTrashContainer() {
102                    if (getTrashContainer() != null) {
103                            return true;
104                    }
105                    else {
106                            return false;
107                    }
108            }
109    
110            public boolean isLocked() {
111                    try {
112                            return LockLocalServiceUtil.isLocked(
113                                    MBThread.class.getName(), getThreadId());
114                    }
115                    catch (Exception e) {
116                    }
117    
118                    return false;
119            }
120    
121    }