com.liferay.portlet.bookmarks.action.EditEntryAction.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.portlet.bookmarks.action.EditEntryAction.java

Source

/**
 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.liferay.portlet.bookmarks.action;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.model.LayoutTypePortlet;
import com.liferay.portal.security.auth.PrincipalException;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.ServiceContextFactory;
import com.liferay.portal.struts.PortletAction;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.WebKeys;
import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
import com.liferay.portlet.bookmarks.EntryURLException;
import com.liferay.portlet.bookmarks.NoSuchEntryException;
import com.liferay.portlet.bookmarks.NoSuchFolderException;
import com.liferay.portlet.bookmarks.model.BookmarksEntry;
import com.liferay.portlet.bookmarks.service.BookmarksEntryServiceUtil;
import com.liferay.portlet.tags.TagsEntryException;
import com.liferay.util.MetadataActionUtil;

/**
 * Extended this class in updateEntry method. After create/update new resource
 * also new metadata entry would be create. After delete also metadata will be
 * delete.
 * 
 * @author Daniel
 * 
 */
public class EditEntryAction extends PortletAction {

    public void processAction(ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
            ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

        String cmd = ParamUtil.getString(actionRequest, Constants.CMD);

        try {
            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
                updateEntry(actionRequest);
            } else if (cmd.equals(Constants.DELETE)) {
                deleteEntry(actionRequest);
            }

            ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

            LayoutTypePortlet layoutTypePortlet = themeDisplay.getLayoutTypePortlet();

            if (layoutTypePortlet.hasPortletId(portletConfig.getPortletName())) {

                sendRedirect(actionRequest, actionResponse);
            } else {
                String redirect = ParamUtil.getString(actionRequest, "redirect");

                actionResponse.sendRedirect(redirect);
            }
        } catch (Exception e) {
            if (e instanceof NoSuchEntryException || e instanceof PrincipalException) {

                SessionErrors.add(actionRequest, e.getClass().getName());

                setForward(actionRequest, "portlet.bookmarks.error");
            } else if (e instanceof EntryURLException || e instanceof NoSuchFolderException) {

                SessionErrors.add(actionRequest, e.getClass().getName());
            } else if (e instanceof TagsEntryException) {
                SessionErrors.add(actionRequest, e.getClass().getName(), e);
            } else {
                throw e;
            }
        }
    }

    public ActionForward render(ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
            RenderRequest renderRequest, RenderResponse renderResponse) throws Exception {

        try {
            ActionUtil.getEntry(renderRequest);
        } catch (Exception e) {
            if (e instanceof NoSuchEntryException || e instanceof PrincipalException) {

                SessionErrors.add(renderRequest, e.getClass().getName());

                return mapping.findForward("portlet.bookmarks.error");
            } else {
                throw e;
            }
        }

        return mapping.findForward(getForward(renderRequest, "portlet.bookmarks.edit_entry"));
    }

    protected void deleteEntry(ActionRequest actionRequest) throws Exception {
        long entryId = ParamUtil.getLong(actionRequest, "entryId");

        BookmarksEntryServiceUtil.deleteEntry(entryId);
        MetadataActionUtil.deleteMetadata(entryId);
    }

    protected void updateEntry(ActionRequest actionRequest) throws Exception {
        long entryId = ParamUtil.getLong(actionRequest, "entryId");

        long folderId = ParamUtil.getLong(actionRequest, "folderId");
        String name = ParamUtil.getString(actionRequest, "name");
        String url = ParamUtil.getString(actionRequest, "url");
        String comments = ParamUtil.getString(actionRequest, "comments");

        ServiceContext serviceContext = ServiceContextFactory.getInstance(BookmarksEntry.class.getName(),
                actionRequest);

        if (entryId <= 0) {

            // Add entry

            BookmarksEntry entry = BookmarksEntryServiceUtil.addEntry(folderId, name, url, comments,
                    serviceContext);

            MetadataActionUtil.addMetadata(BookmarksEntry.class.getName(), entry.getEntryId(), actionRequest);

            AssetPublisherUtil.addAndStoreSelection(actionRequest, BookmarksEntry.class.getName(),
                    entry.getEntryId(), -1);
        } else {

            // Update entry

            BookmarksEntryServiceUtil.updateEntry(entryId, folderId, name, url, comments, serviceContext);
        }

        AssetPublisherUtil.addRecentFolderId(actionRequest, BookmarksEntry.class.getName(), folderId);
    }

}