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.taglib.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.trash.TrashHandler;
021    import com.liferay.portal.kernel.trash.TrashHandlerRegistryUtil;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portlet.trash.exception.RestoreEntryException;
024    import com.liferay.portlet.trash.model.TrashEntry;
025    import com.liferay.portlet.trash.model.TrashEntryConstants;
026    import com.liferay.portlet.trash.service.TrashEntryLocalServiceUtil;
027    
028    import javax.portlet.PortletRequest;
029    
030    /**
031     * @author Eudaldo Alonso
032     */
033    public class RestoreEntryUtil {
034    
035            public static JSONObject checkEntry(PortletRequest portletRequest)
036                    throws PortalException {
037    
038                    long trashEntryId = ParamUtil.getLong(portletRequest, "trashEntryId");
039    
040                    String newName = ParamUtil.getString(portletRequest, "newName");
041    
042                    TrashEntry entry = TrashEntryLocalServiceUtil.fetchTrashEntry(
043                            trashEntryId);
044    
045                    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
046                            entry.getClassName());
047    
048                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
049    
050                    try {
051                            trashHandler.checkRestorableEntry(
052                                    entry, TrashEntryConstants.DEFAULT_CONTAINER_ID, newName);
053    
054                            jsonObject.put("success", true);
055                    }
056                    catch (RestoreEntryException ree) {
057                            jsonObject.put("duplicateEntryId", ree.getDuplicateEntryId());
058                            jsonObject.put("errorMessage", ree.getErrorMessage());
059                            jsonObject.put("oldName", ree.getOldName());
060                            jsonObject.put("overridable", ree.isOverridable());
061                            jsonObject.put("success", false);
062                            jsonObject.put("trashEntryId", ree.getTrashEntryId());
063                    }
064    
065                    return jsonObject;
066            }
067    
068    }