Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Blaine Boule
Adding Ratings to portles - classPK?
April 1, 2010 1:55 PM
Answer

Blaine Boule

Rank: New Member

Posts: 15

Join Date: December 2, 2009

Recent Posts

I have added ratings to custom portlets according to this article http://www.liferay.com/community/wiki/-/wiki/Main/Adding+Rating+to+a+portlet

I have it working using arbitrary integers for the classPK - or supplying the themes PLID - but for my custom portlets I have developed, how do i get the classPK for it?

Meaning, I have deployed a custom JSP/Flex portlet, i need the classPK so that the rating mapped to that portlet class - but I cannot seem to get the PK. The article says to use yourportlet.getResourcePrimKey() - but this doesnt exist on my portlet object.

Any suggestions?

Lastly, say I write a specific wrapper portlet that holds flex content, it uses a generic portlet class that I have written - but allows me to define multiple portlets in the portlet.xml all using the same class, but different view.jsp's and flex app embedded within. I want to have the users rate the portlet based on the flex app inside, but if it only ties to the classPK - wont all the portlets share the ratings? (being that it ties back to the class and not the portlet configuration)

Thanks
Sandeep Nair
RE: Adding Ratings to portles - classPK?
April 1, 2010 9:29 PM
Answer

Sandeep Nair

Rank: Liferay Legend

Posts: 1693

Join Date: November 5, 2008

Recent Posts

What is it that you want to give rating. It might be a unique content of some user or community, that someone might be trying to give a rating to. classPK and className are just a unique id that it need.

Regards,
Sandeep
Blaine Boule
RE: Adding Ratings to portles - classPK?
April 2, 2010 11:13 AM
Answer

Blaine Boule

Rank: New Member

Posts: 15

Join Date: December 2, 2009

Recent Posts

We want to be able to allow users to rate the content of the portlet. From my understanding the classPK ties to the specific java class that renders the portlet. In this instance, the java class is shared among many configurations of that portlet.

For example, we have a flex portlet, that allows the user to configure it to display a flex application, we want the rating to tie to the configuration NOT the portlet wrapper. In this example, I can configure the flex wrapper to display flexApp1, and then add another instance of flex wrapper to the page, configure it to display flexApp2 - but when rating these - if we use classPK, will the ratings for flexApp1 show up on flexApp2 as well (because behind the scenes they both use the same java class to render)

I hope I am expressing this clearly enough to understand.

Regarding my initial question, How do I get classPK for my portlet? I cannot get any method to return the actual PK.

Thanks
Samir Bhatt
RE: Adding Ratings to portles - classPK?
April 2, 2010 9:40 PM
Answer

Samir Bhatt

Rank: New Member

Posts: 12

Join Date: August 26, 2009

Recent Posts

Hi,

For your requirement you need ratings for content and not for the Portlet. So for example in Wiki Portlet, when we give rating they are stored against WikiPage class name and WikiPage instances primary key. So this ratings are per content and not for the portlet.

To achieve same thing you need to have a model class representing your content like WikiPage. If you have such class then you can use its classname and its resource pk in ratings tag.

Now if you dont have any such class then you can probably use PortletPreferences class for your requirement. Issue with this approach will be that if you have 2 portlet instances representing same flex applicatio1 then also it will not present the same ratings. But if it is not a requirement to have 2 portlet instances with same flex application in the configuration then you can use portlet preferences for your requirement in following
 1
 2<%
 3    com.liferay.portal.model.PortletPreferences prefs =
 4    com.liferay.portal.service.PortletPreferencesLocalServiceUtil.getPortletPreferences
 5    (com.liferay.portal.util.PortletKeys.PREFS_OWNER_ID_DEFAULT ,
 6    com.liferay.portal.util.PortletKeys.PREFS_OWNER_TYPE_LAYOUT,
 7    themeDisplay.getPlid(), themeDisplay.getPortletDisplay().getId());
 8   
 9%>
10
11
12<liferay-ui:ratings
13className="<%= com.liferay.portal.model.PortletPreferences.class.getName() %>"
14classPK="<%= wikiPage.getPrimaryKey() %>"
15url='<%= themeDisplay.getPathMain() + "/relativepathtoaction" %>'
16/>



Regards,

Samir Bhatt
CIGNEX Technologies
Blaine Boule
RE: Adding Ratings to portles - classPK?
April 5, 2010 12:45 PM
Answer

Blaine Boule

Rank: New Member

Posts: 15

Join Date: December 2, 2009

Recent Posts

Samir,

This makes sense, but preference wont work because of the issue you mentioned. We dont however, have a model class representing the content, but from my understanding this shouldn't be a difficult task because even though we are using the same JAVA class, we have defined several portlets in the portlet.xml (using same class) - when deployed these get entered into the PORTLET table of the database. Now, in that table the PORTLETID (that I have specified - mapping to portlet-name) get entered as a new row for every portlet defined in my Portlet.xml, correct? So I should be able to use PORTLET table's PK field to link back to my ratings (ie using Liferays Portlet Model and PK to establish ratings).

Example Portlet.xml:

 1
 2<portlet-app version="1.0" xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd">
 3    <portlet>
 4        <portlet-name>samples1</portlet-name>
 5        <portlet-class>samples.flex.FlexPortlet</portlet-class>
 6        <portlet-info>
 7            <title>Sample Flex Portlet (Publisher)</title>
 8            <short-title></short-title>
 9        </portlet-info>
10        <init-param>
11            <name>view-jsp</name>
12            <value>/WEB-INF/pages/flex/publisher/view.jsp</value>
13        </init-param>
14        <supports>
15            <mime-type>text/html</mime-type>
16            <portlet-mode>VIEW</portlet-mode>
17        </supports>
18        <supported-locale>en</supported-locale>
19        <portlet-preferences>
20            <preference>
21                <name>eventNamePref</name>
22                <value>sampleEvent</value>
23            </preference>
24            <preference>
25                <name>title</name>
26                <value>Sample Flex Portlet (Publisher)</value>
27            </preference>
28        </portlet-preferences>
29    </portlet>
30    <portlet>
31        <portlet-name>samples2</portlet-name>
32        <portlet-class>samples.flex.FlexPortlet</portlet-class>
33        <portlet-info>
34            <title>Sample Flex Portlet (Subscriber)</title>
35            <short-title></short-title>
36        </portlet-info>
37        <init-param>
38            <name>view-jsp</name>
39            <value>/WEB-INF/pages/flex/subscriber/view.jsp</value>
40        </init-param>
41        <supports>
42            <mime-type>text/html</mime-type>
43            <portlet-mode>VIEW</portlet-mode>
44        </supports>
45        <supported-locale>en</supported-locale>
46        <portlet-preferences>
47            <preference>
48                <name>eventNamePref</name>
49                <value>sampleEvent</value>
50            </preference>
51            <preference>
52                <name>title</name>
53                <value>Sample Flex Portlet (Subscriber)</value>
54            </preference>
55        </portlet-preferences>
56    </portlet>
57</portlet-app>


So in this example, I am defining two portlets (simple flex wrappers). They use the same JAVA class that I have written. When I deploy this to Liferay, it will add two records to the Portlet Table, one for samples1 and one for samples2, I should then be able to use the PK of the PORTLET Table for my ratings classPK.

I am trying to leverage functionality that is there, without having to customize anything. In essence, to solve my issue I need to get the "ID_" field (PrimaryKey) from the PORTLET Table for each portlet defined in my Portlet.xml. I would think this is available, but I cant seem obtain this data.

Thanks
Blaine
Blaine Boule
RE: Adding Ratings to portles - classPK?
April 5, 2010 3:29 PM
Answer

Blaine Boule

Rank: New Member

Posts: 15

Join Date: December 2, 2009

Recent Posts

Update, I can get a reference to the Portlet Object, but I cant get the PK off of it:

 1
 2Portlet portlet = PortletLocalServiceUtil.getPortletById((String)request.getAttribute(WebKeys.PORTLET_ID));   
 3            
 4if(portlet !=null){
 5    System.out.println("portlet id:"+portlet.getId());
 6    System.out.println("plugin id:"+portlet.getPluginId());
 7    System.out.println("primary key:"+portlet.getPrimaryKey());
 8}else{
 9    System.out.println("Temp is null ");
10}


In this snippet, I can get the PortletID, the PluginID, but the PrimaryKey call returns 0. Why is this occurring?

Thanks

Blaine
Samir Bhatt
RE: Adding Ratings to portles - classPK?
April 5, 2010 9:01 PM
Answer

Samir Bhatt

Rank: New Member

Posts: 12

Join Date: August 26, 2009

Recent Posts

Portlet model is normally not persisted in database until you modify something through plugin configuration functionality in control panel.

So by defaulte Portlet mode is polulated from portlet.xml and then if you override or save the model from plugin configuration tab then a new entry is created in portlet table in the databse.

So from control panel go to Plugin Configuration and then from Portlet Plugins tab locate your portlets and click on it. After that without changing anything click on save. A new entry will be created in portlet table. Then try your code.

Though this is not the right approach, right approach would be to have one model class storing ratings against it.

Regards,

Samir Bhatt
CIGNEX Technologies
Blaine Boule
RE: Adding Ratings to portles - classPK?
April 6, 2010 10:30 AM
Answer

Blaine Boule

Rank: New Member

Posts: 15

Join Date: December 2, 2009

Recent Posts

Ahh, makes sense now. So in reality I need to implement a model for these wrapper portlets and use that PK for ratings.

Thank you for the clarification.

Blaine