0
votes

I am using liferay and i have created my own portlet which have just add functionality.but some how i am having error in adding books

this is my view.jsp

<%@ include file="/init.jsp"%>


<%
    Restaurant resto = (Restaurant) request.getAttribute("Restaurant");
    if (resto == null) {
        resto = new RestaurantImpl();

        resto.setTable_Count(1);
    }
%>
<liferay-portlet:actionURL name="addreRestaurant" var="addreRestaurantURL"></liferay-portlet:actionURL>

<aui:form action="<%=addreRestaurantURL.toString() %>" method="post" name="fm">

    <aui:fieldset>
        <%-- <liferay-ui:error key="title-required" message="title-required" /> --%>
        <p>
            <aui:input name="Name" label="Name" type="text"
                value="<%=resto.getName()%>"></aui:input>
        </p>
        <%-- <liferay-ui:error key="author-required" message="author-required" /> --%>
        <p>
            <aui:input name="Location" label="Location" type="text"
                value="<%=resto.getLocation() %>"></aui:input>
        </p>
        <%-- <liferay-ui:error key="pages-required" message="pages-required" />
        <liferay-ui:error key="pages-cannot-be-zero"
            message="pages-cannot-be-zero" /> --%>
        <p>
            <aui:input name="table_count" label="Table_count" type="text"
                value="<%=String.valueOf(resto.getTable_Count()) %>"></aui:input>
        </p>
        <p>
            <aui:input name="room_count" label="Room_count" type="text"
                value="<%=String.valueOf(resto.getRoom_Count()) %>"></aui:input>

        </p>
        <p>
            <aui:input name="reseller_ID" label="Reseller_ID" type="text"
                value="<%=String.valueOf(resto.getReseller_ID()) %>"></aui:input>

        </p>

        <p>
                <aui:button class="aui-button-input" type="submit" value="Submit" />
                <aui:button class="aui-button-input" type="reset" value="Reset" />
            </p>

            </aui:fieldset>
</aui:form>

<liferay-ui:search-container emptyResultsMessage="no-restaurant" delta="5">
</liferay-ui:search-container>

And following is my service implementation class

/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.test.service.impl;

import java.util.Collections;
import java.util.List;

import com.test.model.Restaurant;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;

import com.test.service.base.RestaurantServiceBaseImpl;

/**
 * The implementation of the restaurant remote service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link com.test.service.RestaurantService} interface.
 *
 * <p>
 * This is a remote service. Methods of this service are expected to have security checks based on the propagated JAAS credentials because this service can be accessed remotely.
 * </p>
 *
 * @author bhavik.kama
 * @see com.test.service.base.RestaurantServiceBaseImpl
 * @see com.test.service.RestaurantServiceUtil
 */
public class RestaurantServiceImpl extends RestaurantServiceBaseImpl {
    public Restaurant addreRestaurant(Restaurant restoParam) {
        Restaurant restoVar;

        try {
            restoVar = restaurantPersistence.create((int) counterLocalService
                    .increment(Restaurant.class.toString()));
        } catch (SystemException e) {
            e.printStackTrace();
            return restoVar = null;
        }

        try {
            resourceLocalService.addResources(restoParam.getCompanyId(),
                    restoParam.getGroupId(), restoParam.getUserId(),
                    Restaurant.class.getName(), restoParam.getPrimaryKey(),false,
                    true, true);
        } catch (PortalException e) {
            e.printStackTrace();
            return restoVar = null;
        } catch (SystemException e) {
            e.printStackTrace();
            return restoVar = null;
        }

        restoVar.setLocation(restoParam.getLocation());
        restoVar.setCompanyId(restoParam.getCompanyId());
        restoVar.setResto_ID(restoParam.getResto_ID());
        restoVar.setUserId(restoParam.getUserId());
        restoVar.setGroupId(restoParam.getGroupId());
        restoVar.setName(restoParam.getName());
        restoVar.setRoom_Count(restoParam.getRoom_Count());
        restoVar.setTable_Count(restoParam.getRoom_Count());
        restoVar.setUserId(restoParam.getUserId());

        try {
            return restaurantPersistence.update(restoVar, false);
        } catch (SystemException e) {
            e.printStackTrace();
            return restoVar = null;
        }
    }
    public List<Restaurant> getAllerRestaurants() {
        try {
            return restaurantPersistence.findAll();
        } catch (SystemException e) {
            e.printStackTrace();
            return Collections.emptyList();
        }
    }

    public List<Restaurant> getAllreRestaurants(long groupId, String title) {
        try {
            return restaurantPersistence.findByGroupId(groupId);
        } catch (SystemException e) {
            e.printStackTrace();
            return Collections.emptyList();
        }
    }
}

And following error i am facing its hwoing that its cant find method of addreRestaurant but i have that method.so where am doing mistake?

Error is as follows:

SEVERE: Servlet.service() for servlet TabsTest Servlet threw exception
java.lang.NoSuchMethodException: com.liferay.util.bridges.mvc.MVCPortlet.addreRestaurant(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
    at java.lang.Class.getMethod(Class.java:1605).....

As follows is my Portlet Class Method to addRestaurant..

package com.test.portlet;

import java.util.ArrayList;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.test.model.Restaurant;
import com.test.service.RestaurantServiceUtil;
import com.test.util.RestaurantActionUtil;
import com.test.util.RestaurantValidator;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 * Portlet implementation class BooksPortlet
 */
public class RestaurantPortlet extends MVCPortlet {

    private static Log log = LogFactory.getLog(RestaurantPortlet.class);
    private static String errorJSP="/jsps/error.jsp" ;

    public void addRestaurant(ActionRequest request, ActionResponse response) {

        log.info("Inside addRegistration");
        List<String> errors=new ArrayList<String>();
        Restaurant resto=RestaurantActionUtil.getRestaurantFromRequest(request);
        boolean bookValid=RestaurantValidator.validateBook(resto, errors);
        if(bookValid) {
            log.info(resto);
            Restaurant test=RestaurantServiceUtil.addreRestaurant(resto);
            if(test==null) {
                log.error("REsto was Found Null");
                //response.setRenderParameter("jspPage", errorJSP);
                return ;
            }
            SessionMessages.add(request,"book-added");
            return ;
        }
        else {
             for (String error : errors) {
                    SessionErrors.add(request, error);
                }
                SessionErrors.add(request, "error-while-adding");
                request.setAttribute("Restaurant",resto);
                return ;
        }

    }

    /*
    public void deleteBooks(ActionRequest request, ActionResponse response) {

        long bookId = ParamUtil.getLong(request, "bookId");
        ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(
                WebKeys.THEME_DISPLAY);

        if (Validator.isNotNull(bookId)) {
            BooksLocalServiceUtil.deleteBooks(bookId, themeDisplay.getCompanyId());
            SessionMessages.add(request, "book-deleted");

        } else {
            SessionErrors.add(request, "error-deleting");

        }

    }

*/
}

This is my new error trace...

14:16:50,870 INFO  [RestaurantPortlet:35] Inside addRegistration
14:16:50,872 INFO  [RestaurantPortlet:40] {Resto_ID=0, Name=dasd, Location=ads, Room_Count=2, Table_Count=1, userId=10196, companyId=10154, groupId=10180, Reseller_ID=55}
com.liferay.portal.ResourceActionsException: There are no actions associated with the resource com.test.model.Restaurant
    at com.liferay.portal.service.impl.ResourceLocalServiceImpl.validate(ResourceLocalServiceImpl.java:1348)
    at com.liferay.portal.service.impl.ResourceLocalServiceImpl.addResources(ResourceLocalServiceImpl.java:845)
    at com.liferay.portal.service.impl.ResourceLocalServiceImpl.addResources(ResourceLocalServiceImpl.java:147)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

........... ............. 14:16:50,878 ERROR [RestaurantPortlet:43] REsto was Found Null

My Action Resource mapping xml..

<?xml version="1.0" encoding="UTF-8"?>

<resource-action-mapping>

    <portlet-resource>
        <portlet-name>RestaurantPortlet</portlet-name>
        <permissions>
            <supports>
                <action-key>addRestaurant</action-key>
                <action-key>VIEW</action-key>
            </supports>
            <community-defaults>
                <action-key>VIEW</action-key>
            </community-defaults>
            <guest-defaults>
                <action-key>VIEW</action-key>
            </guest-defaults>

            <guest-unsupported>
                <action-key>addRestaurant</action-key>
            </guest-unsupported>
        </permissions>
    </portlet-resource>

    <model-resource>
        <model-name>com.test.Restaurant</model-name>
        <portlet-ref>
            <portlet-name>RestaurantPortlet</portlet-name>
        </portlet-ref>
        <permissions>
            <supports>
                <action-key>addRestaurant</action-key>

                <action-key>VIEW</action-key>
            </supports>
            <community-defaults>
                <action-key>VIEW</action-key>
            </community-defaults>
            <guest-defaults>
                <action-key>VIEW</action-key>
            </guest-defaults>
            <guest-unsupported>

            </guest-unsupported>
        </permissions>
    </model-resource>

</resource-action-mapping>
5

5 Answers

1
votes

Your method : public Restaurant addreRestaurant(Restaurant restoParam)
The method that is required by the portlet: addreRestaurant(javax.portlet.ActionRequest,javax.portlet.ActionResponse)

1
votes

your mvc class method name is addRestaurant(), while your jsp requires:

name="addreRestaurant"

method names are differents :)

1
votes

Your new stacktrace is related to a different error. The problem is inside the RestauranteServiceImpl.addreRestaurant(), more specifically the snippet:

resourceLocalService.addResources(restoParam.getCompanyId(),
                    restoParam.getGroupId(), restoParam.getUserId(),
                    Restaurant.class.getName(), restoParam.getPrimaryKey(),false,
                    true, true);

this means you have no resource action related to your class or your portlet. Have you defined your resource-action-mapping ?

1
votes

I got the things done at last...in database there is no entry about my addRestaurant action in actionrescources table.

After lots of R&D have found that.. I have to just manually add the addRestaurant entry in that table and it just worked.

I wiil now be searching how its not adding entry to resourceaction automatically.

0
votes

Running

service-builder

fixed the problem for me.