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.journal.trash;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.trash.TrashActionKeys;
020    import com.liferay.portal.kernel.trash.TrashRenderer;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.ContainerModel;
023    import com.liferay.portal.security.permission.ActionKeys;
024    import com.liferay.portal.security.permission.PermissionChecker;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portlet.journal.asset.JournalArticleAssetRenderer;
027    import com.liferay.portlet.journal.model.JournalArticle;
028    import com.liferay.portlet.journal.model.JournalArticleResource;
029    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
030    import com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil;
031    import com.liferay.portlet.journal.service.JournalFolderLocalServiceUtil;
032    import com.liferay.portlet.journal.service.permission.JournalArticlePermission;
033    import com.liferay.portlet.journal.service.permission.JournalFolderPermission;
034    import com.liferay.portlet.journal.util.JournalUtil;
035    import com.liferay.portlet.trash.DuplicateEntryException;
036    import com.liferay.portlet.trash.model.TrashEntry;
037    
038    import javax.portlet.PortletRequest;
039    
040    /**
041     * Implements trash handling for the journal article entity.
042     *
043     * @author Levente Hud??k
044     * @author Sergio Gonz??lez
045     * @author Zsolt Berentey
046     */
047    public class JournalArticleTrashHandler extends JournalBaseTrashHandler {
048    
049            @Override
050            public void checkDuplicateEntry(
051                            long classPK, long containerModelId, String newName)
052                    throws PortalException, SystemException {
053    
054                    JournalArticle article =
055                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
056    
057                    checkDuplicateEntry(
058                            classPK, 0, containerModelId, article.getArticleId(), newName);
059            }
060    
061            @Override
062            public void checkDuplicateTrashEntry(
063                            TrashEntry trashEntry, long containerModelId, String newName)
064                    throws PortalException, SystemException {
065    
066                    checkDuplicateEntry(
067                            trashEntry.getClassPK(), trashEntry.getEntryId(), containerModelId,
068                            trashEntry.getTypeSettingsProperty("title"), newName);
069            }
070    
071            @Override
072            public void deleteTrashEntry(long classPK)
073                    throws PortalException, SystemException {
074    
075                    JournalArticle article =
076                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
077    
078                    JournalArticleLocalServiceUtil.deleteArticle(
079                            article.getGroupId(), article.getArticleId(), null);
080            }
081    
082            @Override
083            public String getClassName() {
084                    return JournalArticle.class.getName();
085            }
086    
087            @Override
088            public ContainerModel getParentContainerModel(long classPK)
089                    throws PortalException, SystemException {
090    
091                    JournalArticle article =
092                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
093    
094                    long parentFolderId = article.getFolderId();
095    
096                    if (parentFolderId <= 0) {
097                            return null;
098                    }
099    
100                    return getContainerModel(parentFolderId);
101            }
102    
103            @Override
104            public String getRestoreContainerModelLink(
105                            PortletRequest portletRequest, long classPK)
106                    throws PortalException, SystemException {
107    
108                    JournalArticle article =
109                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
110    
111                    return JournalUtil.getJournalControlPanelLink(
112                            portletRequest, article.getFolderId());
113            }
114    
115            @Override
116            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
117                    throws PortalException, SystemException {
118    
119                    JournalArticle article =
120                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
121    
122                    return JournalUtil.getAbsolutePath(
123                            portletRequest, article.getFolderId());
124            }
125    
126            @Override
127            public ContainerModel getTrashContainer(long classPK)
128                    throws PortalException, SystemException {
129    
130                    JournalArticle article =
131                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
132    
133                    return article.getTrashContainer();
134            }
135    
136            @Override
137            public TrashRenderer getTrashRenderer(long classPK)
138                    throws PortalException, SystemException {
139    
140                    JournalArticle article =
141                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
142    
143                    return new JournalArticleAssetRenderer(article);
144            }
145    
146            @Override
147            public boolean hasTrashPermission(
148                            PermissionChecker permissionChecker, long groupId, long classPK,
149                            String trashActionId)
150                    throws PortalException, SystemException {
151    
152                    if (trashActionId.equals(TrashActionKeys.MOVE)) {
153                            return JournalFolderPermission.contains(
154                                    permissionChecker, groupId, classPK, ActionKeys.ADD_ARTICLE);
155                    }
156    
157                    return super.hasTrashPermission(
158                            permissionChecker, groupId, classPK, trashActionId);
159            }
160    
161            @Override
162            public boolean isInTrash(long classPK)
163                    throws PortalException, SystemException {
164    
165                    JournalArticle article =
166                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
167    
168                    return article.isInTrash();
169            }
170    
171            @Override
172            public boolean isInTrashContainer(long classPK)
173                    throws PortalException, SystemException {
174    
175                    JournalArticle article =
176                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
177    
178                    return article.isInTrashContainer();
179            }
180    
181            @Override
182            public boolean isRestorable(long classPK)
183                    throws PortalException, SystemException {
184    
185                    JournalArticle article =
186                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
187    
188                    if ((article.getFolderId() > 0) &&
189                            (JournalFolderLocalServiceUtil.fetchFolder(
190                                    article.getFolderId()) == null)) {
191    
192                            return false;
193                    }
194    
195                    return !article.isInTrashContainer();
196            }
197    
198            @Override
199            public void moveEntry(
200                            long userId, long classPK, long containerModelId,
201                            ServiceContext serviceContext)
202                    throws PortalException, SystemException {
203    
204                    JournalArticle article =
205                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
206    
207                    JournalArticleLocalServiceUtil.moveArticle(
208                            article.getGroupId(), article.getArticleId(), containerModelId);
209            }
210    
211            @Override
212            public void moveTrashEntry(
213                            long userId, long classPK, long containerId,
214                            ServiceContext serviceContext)
215                    throws PortalException, SystemException {
216    
217                    JournalArticle article =
218                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
219    
220                    JournalArticleLocalServiceUtil.moveArticleFromTrash(
221                            userId, article.getGroupId(), article, containerId, serviceContext);
222            }
223    
224            @Override
225            public void restoreTrashEntry(long userId, long classPK)
226                    throws PortalException, SystemException {
227    
228                    JournalArticle article =
229                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
230    
231                    JournalArticleLocalServiceUtil.restoreArticleFromTrash(userId, article);
232            }
233    
234            @Override
235            public void updateTitle(long classPK, String name)
236                    throws PortalException, SystemException {
237    
238                    JournalArticle article =
239                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
240    
241                    article.setArticleId(name);
242    
243                    JournalArticleLocalServiceUtil.updateJournalArticle(article);
244    
245                    JournalArticleResource articleResource =
246                            JournalArticleResourceLocalServiceUtil.getArticleResource(
247                                    article.getResourcePrimKey());
248    
249                    articleResource.setArticleId(name);
250    
251                    JournalArticleResourceLocalServiceUtil.updateJournalArticleResource(
252                            articleResource);
253            }
254    
255            protected void checkDuplicateEntry(
256                            long classPK, long trashEntryId, long containerModelId,
257                            String originalTitle, String newName)
258                    throws PortalException, SystemException {
259    
260                    JournalArticle article =
261                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
262    
263                    if (Validator.isNotNull(newName)) {
264                            originalTitle = newName;
265                    }
266    
267                    JournalArticleResource articleResource =
268                            JournalArticleResourceLocalServiceUtil.fetchArticleResource(
269                                    article.getGroupId(), originalTitle);
270    
271                    if (articleResource != null) {
272                            DuplicateEntryException dee = new DuplicateEntryException();
273    
274                            JournalArticle duplicateArticle =
275                                    JournalArticleLocalServiceUtil.getArticle(
276                                            articleResource.getGroupId(), originalTitle);
277    
278                            dee.setDuplicateEntryId(duplicateArticle.getResourcePrimKey());
279                            dee.setOldName(duplicateArticle.getArticleId());
280                            dee.setTrashEntryId(trashEntryId);
281    
282                            throw dee;
283                    }
284            }
285    
286            @Override
287            protected long getGroupId(long classPK)
288                    throws PortalException, SystemException {
289    
290                    JournalArticle article =
291                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
292    
293                    return article.getGroupId();
294            }
295    
296            @Override
297            protected boolean hasPermission(
298                            PermissionChecker permissionChecker, long classPK, String actionId)
299                    throws PortalException, SystemException {
300    
301                    return JournalArticlePermission.contains(
302                            permissionChecker, classPK, actionId);
303            }
304    
305    }