001    /**
002     * Copyright (c) 2000-2012 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.trash.action;
016    
017    import com.liferay.portal.kernel.json.JSONFactoryUtil;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.trash.TrashHandler;
020    import com.liferay.portal.kernel.trash.TrashHandlerRegistryUtil;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portlet.trash.DuplicateEntryException;
023    import com.liferay.portlet.trash.TrashEntryConstants;
024    import com.liferay.portlet.trash.model.TrashEntry;
025    import com.liferay.portlet.trash.service.TrashEntryLocalServiceUtil;
026    
027    import javax.portlet.ActionRequest;
028    
029    /**
030     * @author Eudaldo Alonso
031     */
032    public class ActionUtil {
033    
034            public static JSONObject checkEntry(ActionRequest actionRequest)
035                    throws Exception {
036    
037                    long entryId = ParamUtil.getLong(actionRequest, "entryId");
038    
039                    String newName = ParamUtil.getString(actionRequest, "newName");
040    
041                    TrashEntry entry = TrashEntryLocalServiceUtil.getTrashEntry(entryId);
042    
043                    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
044                            entry.getClassName());
045    
046                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
047    
048                    try {
049                            trashHandler.checkDuplicateTrashEntry(
050                                    entry, TrashEntryConstants.DEFAULT_CONTAINER_ID, newName);
051    
052                            jsonObject.put("success", true);
053                    }
054                    catch (DuplicateEntryException dee) {
055                            jsonObject.put("duplicateEntryId", dee.getDuplicateEntryId());
056                            jsonObject.put("oldName", dee.getOldName());
057                            jsonObject.put("success", false);
058                            jsonObject.put("trashEntryId", dee.getTrashEntryId());
059                    }
060    
061                    return jsonObject;
062            }
063    
064    }