001    /**
002     * Copyright (c) 2000-present 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.repository.model.Folder;
019    import com.liferay.portal.kernel.util.ArrayUtil;
020    import com.liferay.portal.kernel.workflow.WorkflowConstants;
021    import com.liferay.portal.model.Lock;
022    import com.liferay.portal.model.Repository;
023    import com.liferay.portal.portletfilerepository.PortletFileRepositoryUtil;
024    import com.liferay.portal.service.LockLocalServiceUtil;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.util.PortletKeys;
027    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
028    import com.liferay.portlet.messageboards.model.MBCategory;
029    import com.liferay.portlet.messageboards.model.MBCategoryConstants;
030    import com.liferay.portlet.messageboards.model.MBMessage;
031    import com.liferay.portlet.messageboards.model.MBThread;
032    import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
033    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
034    
035    import java.util.HashSet;
036    import java.util.List;
037    import java.util.Set;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     * @author Mika Koivisto
042     */
043    public class MBThreadImpl extends MBThreadBaseImpl {
044    
045            public MBThreadImpl() {
046            }
047    
048            @Override
049            public Folder addAttachmentsFolder() throws PortalException {
050                    if (_attachmentsFolderId !=
051                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
052    
053                            return PortletFileRepositoryUtil.getPortletFolder(
054                                    _attachmentsFolderId);
055                    }
056    
057                    ServiceContext serviceContext = new ServiceContext();
058    
059                    serviceContext.setAddGroupPermissions(true);
060                    serviceContext.setAddGuestPermissions(true);
061    
062                    Repository repository = PortletFileRepositoryUtil.addPortletRepository(
063                            getGroupId(), PortletKeys.MESSAGE_BOARDS, serviceContext);
064    
065                    MBMessage message = MBMessageLocalServiceUtil.getMessage(
066                            getRootMessageId());
067    
068                    Folder folder = PortletFileRepositoryUtil.addPortletFolder(
069                            message.getUserId(), repository.getRepositoryId(),
070                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
071                            String.valueOf(getThreadId()), serviceContext);
072    
073                    _attachmentsFolderId = folder.getFolderId();
074    
075                    return folder;
076            }
077    
078            @Override
079            public long getAttachmentsFolderId() {
080                    if (_attachmentsFolderId !=
081                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
082    
083                            return _attachmentsFolderId;
084                    }
085    
086                    ServiceContext serviceContext = new ServiceContext();
087    
088                    serviceContext.setAddGroupPermissions(true);
089                    serviceContext.setAddGuestPermissions(true);
090    
091                    Repository repository =
092                            PortletFileRepositoryUtil.fetchPortletRepository(
093                                    getGroupId(), PortletKeys.MESSAGE_BOARDS);
094    
095                    if (repository == null) {
096                            return DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
097                    }
098    
099                    try {
100                            Folder folder = PortletFileRepositoryUtil.getPortletFolder(
101                                    getUserId(), repository.getRepositoryId(),
102                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
103                                    String.valueOf(getThreadId()), serviceContext);
104    
105                            _attachmentsFolderId = folder.getFolderId();
106                    }
107                    catch (Exception e) {
108                    }
109    
110                    return _attachmentsFolderId;
111            }
112    
113            @Override
114            public MBCategory getCategory() throws PortalException {
115                    long parentCategoryId = getCategoryId();
116    
117                    if ((parentCategoryId ==
118                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
119                            (parentCategoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
120    
121                            return null;
122                    }
123    
124                    return MBCategoryLocalServiceUtil.getCategory(getCategoryId());
125            }
126    
127            @Override
128            public Lock getLock() {
129                    try {
130                            return LockLocalServiceUtil.getLock(
131                                    MBThread.class.getName(), getThreadId());
132                    }
133                    catch (Exception e) {
134                    }
135    
136                    return null;
137            }
138    
139            @Override
140            public long[] getParticipantUserIds() {
141                    Set<Long> participantUserIds = new HashSet<Long>();
142    
143                    List<MBMessage> messages = MBMessageLocalServiceUtil.getThreadMessages(
144                            getThreadId(), WorkflowConstants.STATUS_ANY);
145    
146                    for (MBMessage message : messages) {
147                            participantUserIds.add(message.getUserId());
148                    }
149    
150                    return ArrayUtil.toLongArray(participantUserIds);
151            }
152    
153            @Override
154            public boolean hasLock(long userId) {
155                    try {
156                            return LockLocalServiceUtil.hasLock(
157                                    userId, MBThread.class.getName(), getThreadId());
158                    }
159                    catch (Exception e) {
160                    }
161    
162                    return false;
163            }
164    
165            @Override
166            public boolean isLocked() {
167                    try {
168                            if (isInTrash()) {
169                                    return true;
170                            }
171    
172                            return LockLocalServiceUtil.isLocked(
173                                    MBThread.class.getName(), getThreadId());
174                    }
175                    catch (Exception e) {
176                    }
177    
178                    return false;
179            }
180    
181            private long _attachmentsFolderId;
182    
183    }