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