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