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.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 trashEntryId = ParamUtil.getLong(actionRequest, "trashEntryId");
038    
039                    String newName = ParamUtil.getString(actionRequest, "newName");
040    
041                    TrashEntry entry = TrashEntryLocalServiceUtil.getTrashEntry(
042                            trashEntryId);
043    
044                    TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
045                            entry.getClassName());
046    
047                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
048    
049                    try {
050                            trashHandler.checkDuplicateTrashEntry(
051                                    entry, TrashEntryConstants.DEFAULT_CONTAINER_ID, newName);
052    
053                            jsonObject.put("success", true);
054                    }
055                    catch (DuplicateEntryException dee) {
056                            jsonObject.put("duplicateEntryId", dee.getDuplicateEntryId());
057                            jsonObject.put("oldName", dee.getOldName());
058                            jsonObject.put("success", false);
059                            jsonObject.put("trashEntryId", dee.getTrashEntryId());
060                    }
061    
062                    return jsonObject;
063            }
064    
065    }