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