Wednesday 27 June 2012

Display journal article in to the custom.jsp

Some time we need to display journal article into our custom jsp not from the web content display portlet.

Following is the code snippet for fetching the journal article and display it into the custom.jsp

1)Code for the controller of the portlet.
JournalArticle article = null;
JournalArticleDisplay display = null;

article = JournalArticleLocalServiceUtil.getArticleByUrlTitle(themedisplay.getScopeGroupId(), formatUrlTitle(name));

Function for getting the formatted url title.

private String formatUrlTitle(String name){
        return name.toLowerCase().replaceAll(" ","-");
    }

Then get the journal article display object  for the journal article

display = JournalArticleLocalServiceUtil.getArticleDisplay(themedisplay.getScopeGroupId(),article.getArticleId(),WebKeys.WINDOW_STATE,themedisplay.getLanguageId(),themedisplay);

set the article display object
request.setAttribute("post_of_the_week", article);



On the jsp side.

<%
JournalArticleDisplay display = (JournalArticleDisplay)renderRequest.getAttribute("post_of_the_week");

%>

<%=display.getContent() %>



Consuming web service in liferay

Following are the steps to Consume the web service in the liferay.

1) Now it's time to verify the web service which we have generated is working or not. For that we need to make a client which is going to consume the web service we have published.


Make a sample portlet called "web-service-Consuming-portlet ".  Hit the url "http://127.0.0.1:8080/web-service-publishing-portlet/api/axis/Plugin_WebServiceDemoSample_SampleService?wsdl" copy the wsdl file from the browser.


2) Copy that file into the WEB-INF folder.


3) Right click on the .wsdl file from eclipse and click on the new and make a web service client.then set assemble client and clisk on thegenerate the classes for accessing the service.

4) Write a class ConsumingWebService and write the follwoing code access the web service.

public void render(RenderRequest request,RenderResponse renderResponse){
        SampleServiceSoapServiceLocator serviceLocator = new SampleServiceSoapServiceLocator();
    //SampleServiceSoapService soapService = (SampleServiceSoapService) serviceLocator.getPlugin_WebServiceDemoSample_SampleService();
    SampleServiceSoap serviceSoap;
    try {
        serviceSoap = serviceLocator.getPlugin_WebServiceDemoSample_SampleService();
        SampleSoap[] sampleSoap =  serviceSoap.getUsers(10180);
        for(int i=0 ;i<sampleSoap.length;i++){
            System.out.println("User Id"+sampleSoap[i].getName());
        }
    } catch (ServiceException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (RemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
   
    }


In the above code we have cnsumed the web service which are published in the previous example.Which is going to print print the resut on the concel. 

Monday 25 June 2012

Publish a Web service In liferay.

Steps to publish a SOAP web-service in liferay.


1) Create a portlet called "web-service-publishing-portlet" in liferay.
2) Put the service.xml in the web-inf folder. In the service.xml remote-service="true".

Code-Snippet:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.1.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_1_0.dtd">
<service-builder package-path="com.aalap.sample.webservice">
    <namespace>WebServiceDemoSample</namespace>
        <entity name="Sample" local-service="true" remote-service="true">
        <column name="id" type="long" primary="true"></column>
        <column name="name" type="String" primary="true"></column>
        <column name="groupId" type="long"></column>
       
        <finder return-type="Collection" name="groupId">
            <finder-column name="groupId"></finder-column>
        </finder>
    </entity>
</service-builder>

3) Now clean and run the command ant clean-build-service command.By this command appropriate methods has been generated.

4) Form the local service impl class write a method

public  List<Sample> getUsers(long groupId) throws SystemException{
        return samplePersistence.findBygroupId(groupId);
    }
again run the build-service task. Make sure your build task run successful.

5) Now put a method in the serviceImpl class.

public class SampleServiceImpl extends SampleServiceBaseImpl {
    public  List<Sample> getUsers(long groupId) throws SystemException{
        return SampleLocalServiceUtil.getUsers(groupId);
    }
}

Again run the command ant clean build-service.Verify the task has been run successfully.

6) Now run the task build-wsdd.Verify tha task has run successfully.Then run the task ant clean deploy ant. Check in the concel the message one "web-service-publishing-portlet" is available for use.

7) After this command one wsdd(Web Service Deployment Descriptor) file has been generated.In this file the tag <service name> defines the name of the wsdl(Web Service Description Language). In our example name of the wsdl file is "Plugin_WebServiceDemoSample_SampleService". To access this file hit the url "http://127.0.0.1:8080/web-service-publishing-portlet/api/axis/Plugin_WebServiceDemoSample_SampleService?wsdl" . If some one want to access methods published url hit above mention url.

In the wsdl file the tag <wsdl:operation> define the name of the methods we have published. In our example we have nethod "getUsers" which is in the tag "<wsdl:operation name="getUsers">".




Thursday 14 June 2012

Content For tab Using ajax

In the liferay It is going to render whole page whne we click on the tab which is liferay:ui tabs.Without refershing whole page we can get that content using Ajax call on the click of the tab.


Jsp Code.
<portlet:resourceURL var="getTabContentURL" id="getTabContentURL"></portlet:resourceURL>
<div class="ForumTabsContainer">
            <div class="ForumTabs">
                <ul>
                    <li>
                        <span class="ForumTabSelected TabFirst"><a href="#" onclick="getTabContent('<%=getTabContentURL%>','tabA')">Tab A</a></span></li>
                    <li>
                        <span class="ForumTab"><a href="#" onclick="getTabContent('<%=getTabContentURL%>','tabB')">Tab B</a></span></li>
                    <li>
                        <span class="ForumTab"><a href="#" onclick="getTabContent('<%=getTabContentURL%>','tabC')">Tab C</a></span></li>
                </ul>
            </div>
</div>
<div id="tabContent" style="margin-top: 30px;">
    U have selected Tab tabA
</div>

On clicking of the tab called a js function getTabContent.In this we have passed the resource url and the tabname a the parameter. From the js we have made the ajax call which is going to fetch the data for the tab using the ajax method.


Js Code.

<script src="/ajax-tab-portlet/js/jquery-1.5.2.min.js"></script>
<script type="text/javascript">
function getTabContent(getTabContentURL,selectedTab){
    alert(getTabContentURL);
    alert(selectedTab);
    $.ajax({
        url :getTabContentURL,            
          data: {"selectedTab":selectedTab},
          type: "GET",
          dataType: "text",
           success: function(data) {
               $("#tabContent").html(data);
          }
         });
}

</script>

Controller Code.
In the controller we have return the value we want to put the div below the tab.

public class AjaxTabPortlet extends MVCPortlet{
    public void serveResource(ResourceRequest request, ResourceResponse response){
        String selectedTab = request.getParameter("selectedTab");
        String responseValue = "U have selected Tab "+selectedTab;
        System.out.println("selectedTab--->>>"+selectedTab);
        System.out.println("responseValue--->>>"+responseValue);
        try {
            response.getWriter().write(responseValue);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Css Code.

<style>
.ForumTabs span {
    border-right: 1px solid #FFFFFF;
    font-size: 14px;
    padding: 7px 20px;
}

.ForumTab {
    background: url("/ajax-tab-portlet/images/tabBack.png") repeat-x scroll 0 0 transparent;
}

.TabFirst {
    border-top-left-radius: 7px;
}

.ForumTabSelected {
    background: url("/ajax-tab-portlet/images/tabBack.png") repeat-x scroll 0 0 transparent;
}

.ForumTabsContainer {
    padding-top: 20px;
}

.ForumTabs ul {
    margin: 0;
}

.ForumTabs li {
    float: left;
    list-style: none outside none;
}

.ForumTabs span {
    border-right: 1px solid #FFFFFF;
    font-size: 14px;
    padding: 7px 20px;
}

.TabFirst {
    border-top-left-radius: 7px;
}

.ForumTabSelected a {
    font-weight: bold;
}

.ForumTabs a, .ForumTabs a:visited {
    color: #FFFFFF;
    text-decoration: none;
}


</style>

Thursday 7 June 2012

Build number exception In liferay

Error :Build namespace has build number 29 which is newer than 7.

So many time got the message build number is newer than some number.
Solution:
go to service.properties into the src and just change the buld number

build.number=30 

Make a ajax call using serve resource method in liferay

In liferay the ajax call has been done using the serve resource.

Example to add a Data on button call with the help of the ajax in liferay.

In the jsp

Created a  resource url and a form.
<portlet:resourceURL var="addToDo" id="addToDo"></portlet:resourceURL>
<form>
<input type="text" name="toDo" id="toDo">
<button name="Add" type="button" onclick="addToDo('<%=addToDo%>')">Add</button>
<div id="toDoList">


</div>
</form>

In the above code when click on the add button we have called  a java script method "addToDo" with the resource url as the parameter.

Js Code :
<script type="text/javascript">
function addToDo(addToDo){
    var todo =document.getElementById('toDo').value;
    $.ajax({
        url :addToDo,            
          data: {"todo":todo,"CMD":"addToDo"},
          type: "GET",
          dataType: "text",
        success: function(data) {              
              $("#toDoList").html(data);
        }
    });
}
 </script>
when click on the add button it calls the js function  addToDo in which we have passes resource url as the parameter.Fetch the value put into the text box .

In the controller.
By default it is going to call serve resource mthod of the controller.

public void serveResource(ResourceRequest request, ResourceResponse response){
        if(request.getParameter("CMD").equals("addToDo")){
            user_todo userToDo = new user_todoImpl();
            userToDo.setUserId(userId);
            try {
                userToDo.setTodoId(CounterLocalServiceUtil.increment());
                userToDo.setToDoName(request.getParameter("todo"));
                user_todoLocalServiceUtil.adduser_todo(userToDo);
                       
            }
            catch (SystemException e) {
                e.printStackTrace();
            }
   
        }
    }

It's going to add the todo list into the custom table made for making the todo entries.



Make tabs view in liferay

Make a tabs view in liferay using <liferay-ui:tabs>

String tabNames = "Posts,Replied To,Following";

portletURL.setParameter("tabs1", tabs1);
                       portletURL.setWindowState(WindowState.NORMAL);
                    portletURL.setParameter("userId",
                            ((Long) renderRequest.getAttribute("userId")).toString());

    <liferay-ui:tabs names="<%=tabNames%>"
                    url="<%=portletURL.toString()%>" />


<c:if test="<%tabs1.equalsIgnoreCase("Posts")%>">
 <div class="ForumProfile" style="padding-top: 0px;">
                    <%@ include file="posts.jsp"%>
                </div>
</c:if>

<c:if test="<%tabs1.equalsIgnoreCase("Replied To")%>">
            <div class="ForumProfile" style="padding-top: 0px;">
                <%@ include file="replied.jsp"%>
            </div>

</c:if>


<c:if test="<%tabs1.equalsIgnoreCase("Following")%>">
           <div class="ForumProfile" style="padding-top: 0px;">
                <%@ include file="following.jsp"%>
            </div>

</c:if>


Access portal impl classes.

 Access portal impl classes.

If we want to access the portal-impl class we are not able to use it directly. 

For example if we want to use the RSSUtil class which is in portal-impl we are not able to access it.

To Over come this liferay comes with a solution which is portalclassinvoker.

Example: want to access getFeed() of the RSSUtil in the portal-impl which has String as parameter.

For this we are defining Method key which is used to access the method.

Here we passed the class name with the package,method name and the type of the parameter.In this accessing the getFeed method of the RSSUtil which has the String as a parameter.


final MethodKey GETFEEDS = new MethodKey("com.liferay.portlet.rss.util.RSSUtil", "getFeed",String.class);

 Now called the getFeed by using the PortalClassInvoker, in which we have passed three parameters,first is the if we want to create new instance  , second is method key and third is the parameter for the method we want to pass.Here the getFeed returns the ObjectValuePair<String, SyndFeed> type of the object.


ovp = (ObjectValuePair<String, SyndFeed>) PortalClassInvoker.invoke(false, GETFEEDS, "http://rss.news.yahoo.com/rss/tech"); 

That's  All Now u are able to access the portal-impl class.