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