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