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 getRestoreLink(PortletRequest portletRequest, long classPK)
105                    throws PortalException, SystemException {
106    
107                    JournalArticle article =
108                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
109    
110                    return JournalUtil.getJournalControlPanelLink(
111                            portletRequest, article.getFolderId());
112            }
113    
114            @Override
115            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
116                    throws PortalException, SystemException {
117    
118                    JournalArticle article =
119                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
120    
121                    return JournalUtil.getAbsolutePath(
122                            portletRequest, article.getFolderId());
123            }
124    
125            @Override
126            public ContainerModel getTrashContainer(long classPK)
127                    throws PortalException, SystemException {
128    
129                    JournalArticle article =
130                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
131    
132                    return article.getTrashContainer();
133            }
134    
135            @Override
136            public TrashRenderer getTrashRenderer(long classPK)
137                    throws PortalException, SystemException {
138    
139                    JournalArticle article =
140                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
141    
142                    return new JournalArticleAssetRenderer(article);
143            }
144    
145            @Override
146            public boolean hasTrashPermission(
147                            PermissionChecker permissionChecker, long groupId, long classPK,
148                            String trashActionId)
149                    throws PortalException, SystemException {
150    
151                    if (trashActionId.equals(TrashActionKeys.MOVE)) {
152                            return JournalFolderPermission.contains(
153                                    permissionChecker, groupId, classPK, ActionKeys.ADD_ARTICLE);
154                    }
155    
156                    return super.hasTrashPermission(
157                            permissionChecker, groupId, classPK, trashActionId);
158            }
159    
160            @Override
161            public boolean isInTrash(long classPK)
162                    throws PortalException, SystemException {
163    
164                    JournalArticle article =
165                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
166    
167                    return article.isInTrash();
168            }
169    
170            @Override
171            public boolean isInTrashContainer(long classPK)
172                    throws PortalException, SystemException {
173    
174                    JournalArticle article =
175                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
176    
177                    return article.isInTrashContainer();
178            }
179    
180            @Override
181            public boolean isRestorable(long classPK)
182                    throws PortalException, SystemException {
183    
184                    JournalArticle article =
185                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
186    
187                    if ((article.getFolderId() > 0) &&
188                            (JournalFolderLocalServiceUtil.fetchFolder(
189                                    article.getFolderId()) == null)) {
190    
191                            return false;
192                    }
193    
194                    return !article.isInTrashContainer();
195            }
196    
197            @Override
198            public void moveEntry(
199                            long userId, long classPK, long containerModelId,
200                            ServiceContext serviceContext)
201                    throws PortalException, SystemException {
202    
203                    JournalArticle article =
204                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
205    
206                    JournalArticleLocalServiceUtil.moveArticle(
207                            article.getGroupId(), article.getArticleId(), containerModelId);
208            }
209    
210            @Override
211            public void moveTrashEntry(
212                            long userId, long classPK, long containerId,
213                            ServiceContext serviceContext)
214                    throws PortalException, SystemException {
215    
216                    JournalArticle article =
217                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
218    
219                    JournalArticleLocalServiceUtil.moveArticleFromTrash(
220                            userId, article.getGroupId(), article, containerId, serviceContext);
221            }
222    
223            @Override
224            public void restoreTrashEntry(long userId, long classPK)
225                    throws PortalException, SystemException {
226    
227                    JournalArticle article =
228                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
229    
230                    JournalArticleLocalServiceUtil.restoreArticleFromTrash(userId, article);
231            }
232    
233            @Override
234            public void updateTitle(long classPK, String name)
235                    throws PortalException, SystemException {
236    
237                    JournalArticle article =
238                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
239    
240                    article.setArticleId(name);
241    
242                    JournalArticleLocalServiceUtil.updateJournalArticle(article);
243    
244                    JournalArticleResource articleResource =
245                            JournalArticleResourceLocalServiceUtil.getArticleResource(
246                                    article.getResourcePrimKey());
247    
248                    articleResource.setArticleId(name);
249    
250                    JournalArticleResourceLocalServiceUtil.updateJournalArticleResource(
251                            articleResource);
252            }
253    
254            protected void checkDuplicateEntry(
255                            long classPK, long trashEntryId, long containerModelId,
256                            String originalTitle, String newName)
257                    throws PortalException, SystemException {
258    
259                    JournalArticle article =
260                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
261    
262                    if (Validator.isNotNull(newName)) {
263                            originalTitle = newName;
264                    }
265    
266                    JournalArticleResource articleResource =
267                            JournalArticleResourceLocalServiceUtil.fetchArticleResource(
268                                    article.getGroupId(), originalTitle);
269    
270                    if (articleResource != null) {
271                            DuplicateEntryException dee = new DuplicateEntryException();
272    
273                            JournalArticle duplicateArticle =
274                                    JournalArticleLocalServiceUtil.getArticle(
275                                            articleResource.getGroupId(), originalTitle);
276    
277                            dee.setDuplicateEntryId(duplicateArticle.getResourcePrimKey());
278                            dee.setOldName(duplicateArticle.getArticleId());
279                            dee.setTrashEntryId(trashEntryId);
280    
281                            throw dee;
282                    }
283            }
284    
285            @Override
286            protected long getGroupId(long classPK)
287                    throws PortalException, SystemException {
288    
289                    JournalArticle article =
290                            JournalArticleLocalServiceUtil.getLatestArticle(classPK);
291    
292                    return article.getGroupId();
293            }
294    
295            @Override
296            protected boolean hasPermission(
297                            PermissionChecker permissionChecker, long classPK, String actionId)
298                    throws PortalException, SystemException {
299    
300                    return JournalArticlePermission.contains(
301                            permissionChecker, classPK, actionId);
302            }
303    
304    }