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