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.JournalFolderAssetRenderer;
027    import com.liferay.portlet.journal.model.JournalFolder;
028    import com.liferay.portlet.journal.service.JournalFolderLocalServiceUtil;
029    import com.liferay.portlet.journal.service.permission.JournalFolderPermission;
030    import com.liferay.portlet.journal.util.JournalUtil;
031    import com.liferay.portlet.trash.DuplicateEntryException;
032    import com.liferay.portlet.trash.model.TrashEntry;
033    
034    import javax.portlet.PortletRequest;
035    
036    /**
037     * Implements trash handling for the journal folder entity.
038     *
039     * @author Eudaldo Alonso
040     */
041    public class JournalFolderTrashHandler extends JournalBaseTrashHandler {
042    
043            @Override
044            public void checkDuplicateTrashEntry(
045                            TrashEntry trashEntry, long containerModelId, String newName)
046                    throws PortalException, SystemException {
047    
048                    JournalFolder folder = JournalFolderLocalServiceUtil.getFolder(
049                            trashEntry.getClassPK());
050    
051                    String originalTitle = trashEntry.getTypeSettingsProperty("title");
052    
053                    if (Validator.isNotNull(newName)) {
054                            originalTitle = newName;
055                    }
056    
057                    JournalFolder duplicateFolder =
058                            JournalFolderLocalServiceUtil.fetchFolder(
059                                    folder.getGroupId(), folder.getParentFolderId(), originalTitle);
060    
061                    if (duplicateFolder != null) {
062                            DuplicateEntryException dee = new DuplicateEntryException();
063    
064                            dee.setDuplicateEntryId(duplicateFolder.getFolderId());
065                            dee.setOldName(duplicateFolder.getName());
066                            dee.setTrashEntryId(trashEntry.getEntryId());
067    
068                            throw dee;
069                    }
070            }
071    
072            public void deleteTrashEntry(long classPK)
073                    throws PortalException, SystemException {
074    
075                    JournalFolderLocalServiceUtil.deleteFolder(classPK, false);
076            }
077    
078            public String getClassName() {
079                    return JournalFolder.class.getName();
080            }
081    
082            @Override
083            public String getDeleteMessage() {
084                    return "found-in-deleted-folder-x";
085            }
086    
087            @Override
088            public ContainerModel getParentContainerModel(long classPK)
089                    throws PortalException, SystemException {
090    
091                    JournalFolder folder = getJournalFolder(classPK);
092    
093                    long parentFolderId = folder.getParentFolderId();
094    
095                    if (parentFolderId <= 0) {
096                            return null;
097                    }
098    
099                    return getContainerModel(parentFolderId);
100            }
101    
102            @Override
103            public String getRestoreLink(PortletRequest portletRequest, long classPK)
104                    throws PortalException, SystemException {
105    
106                    JournalFolder folder = JournalFolderLocalServiceUtil.getFolder(classPK);
107    
108                    return JournalUtil.getJournalControlPanelLink(
109                            portletRequest, folder.getParentFolderId());
110            }
111    
112            @Override
113            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
114                    throws PortalException, SystemException {
115    
116                    JournalFolder folder = JournalFolderLocalServiceUtil.getFolder(classPK);
117    
118                    return JournalUtil.getAbsolutePath(
119                            portletRequest, folder.getParentFolderId());
120            }
121    
122            @Override
123            public ContainerModel getTrashContainer(long classPK)
124                    throws PortalException, SystemException {
125    
126                    JournalFolder folder = getJournalFolder(classPK);
127    
128                    return folder.getTrashContainer();
129            }
130    
131            @Override
132            public TrashRenderer getTrashRenderer(long classPK)
133                    throws PortalException, SystemException {
134    
135                    JournalFolder folder = JournalFolderLocalServiceUtil.getFolder(classPK);
136    
137                    return new JournalFolderAssetRenderer(folder);
138            }
139    
140            @Override
141            public boolean hasTrashPermission(
142                            PermissionChecker permissionChecker, long groupId, long classPK,
143                            String trashActionId)
144                    throws PortalException, SystemException {
145    
146                    if (trashActionId.equals(TrashActionKeys.MOVE)) {
147                            return JournalFolderPermission.contains(
148                                    permissionChecker, groupId, classPK, ActionKeys.ADD_FOLDER);
149                    }
150    
151                    return super.hasTrashPermission(
152                            permissionChecker, groupId, classPK, trashActionId);
153            }
154    
155            @Override
156            public boolean isContainerModel() {
157                    return true;
158            }
159    
160            public boolean isInTrash(long classPK)
161                    throws PortalException, SystemException {
162    
163                    JournalFolder folder = JournalFolderLocalServiceUtil.getFolder(classPK);
164    
165                    return folder.isInTrash();
166            }
167    
168            @Override
169            public boolean isInTrashContainer(long classPK)
170                    throws PortalException, SystemException {
171    
172                    JournalFolder folder = getJournalFolder(classPK);
173    
174                    return folder.isInTrashContainer();
175            }
176    
177            @Override
178            public boolean isRestorable(long classPK)
179                    throws PortalException, SystemException {
180    
181                    JournalFolder folder = getJournalFolder(classPK);
182    
183                    return !folder.isInTrashContainer();
184            }
185    
186            @Override
187            public void moveEntry(
188                            long userId, long classPK, long containerModelId,
189                            ServiceContext serviceContext)
190                    throws PortalException, SystemException {
191    
192                    JournalFolderLocalServiceUtil.moveFolder(
193                            classPK, containerModelId, serviceContext);
194            }
195    
196            @Override
197            public void moveTrashEntry(
198                            long userId, long classPK, long containerModelId,
199                            ServiceContext serviceContext)
200                    throws PortalException, SystemException {
201    
202                    JournalFolderLocalServiceUtil.moveFolderFromTrash(
203                            userId, classPK, containerModelId, serviceContext);
204            }
205    
206            public void restoreTrashEntry(long userId, long classPK)
207                    throws PortalException, SystemException {
208    
209                    JournalFolderLocalServiceUtil.restoreFolderFromTrash(userId, classPK);
210            }
211    
212            @Override
213            public void updateTitle(long classPK, String name)
214                    throws PortalException, SystemException {
215    
216                    JournalFolder folder = JournalFolderLocalServiceUtil.getFolder(classPK);
217    
218                    folder.setName(name);
219    
220                    JournalFolderLocalServiceUtil.updateJournalFolder(folder);
221            }
222    
223            @Override
224            protected JournalFolder getJournalFolder(long classPK)
225                    throws PortalException, SystemException {
226    
227                    JournalFolder folder = JournalFolderLocalServiceUtil.getFolder(classPK);
228    
229                    return folder;
230            }
231    
232            @Override
233            protected boolean hasPermission(
234                            PermissionChecker permissionChecker, long classPK, String actionId)
235                    throws PortalException, SystemException {
236    
237                    JournalFolder folder = JournalFolderLocalServiceUtil.getFolder(classPK);
238    
239                    return JournalFolderPermission.contains(
240                            permissionChecker, folder, actionId);
241            }
242    
243    }