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