Friday, 21 April 2017

Liferay 7 Site Creation in SpringMvc

Creating Spring Mvc Project in liferay 7.
        First we need to download
         àLiferay 7 Bundle consist following things

àLiferay 7 Eclipse

Required software’s
        ->MSql 5.6+
        ->JDK1.8
       
Creating Project Step’s:
1.Open eclipse
2.Open Perspective and change to Liferay Workspace
3.Create the Workspace for your project.
        àOpen Package Explorer.
        àRight Click New-LiferayWorkspace Project.
        àEnter Workspace Name -next-Finish.
4.In create Package Explorer
        àRight Click New- Liferay Module Project.
        àType Project Name
        àSelect Build Type
               * gradle-module
        àSelect Project Template Name-(mvc-Portlet)
        àFinsh
5.In create Package Explorer(Creating Service)
        àRight Click New- Liferay Module Project.
        àType Project Name
        àSelect Build Type
               * gradle-module
        àSelect Project Template Name-(Service-builder)
        àFinsh
After clicking on finish the project structure will be as follow.
Example: Workspace-sample
               Potlet name – Admin
As shown in below image.


The above project gives the default structure of Liferay Mvc with build tool gradel
Now we need to convert the liferayMvc to Spring Mvc with gradel.
To convert from Liferay Mvc to SpringMvc we need all xml files with web.xml and other Required Folder Structure like Webapps,jsp and context file.


        We have To create a folder structure for spring mvc as folder structure should have its own webapp folder inside that css ,js and WEB-INF folder as to be created.
        Inside WEB-INF folder we need to declare all the xml files.
Like as follows.
1.Liferay-display.xml
2.liferay-plugin-package.properties
3.liferay-portlet.xml
4.portlet.xml
5.web.xml
With xml files we need to have some more folder structure also which are important.
        1.Jspà Where we should create all the jsp pages which are required for our project.
        2.Spring-ContextàThis folder we will be using to declared the Spring configuration details. I will be containg 2 files as flows.


In this Two class we will be declaring Spring Configuration and Bean tag.
SpringMvcWithGradel-portlet.xml:
<?xmlversion="1.0"?>

<beans
     xmlns="http://www.springframework.org/schema/beans"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
> 
     <!-- <context:component-scan base-package="SpringMVC.portlet" /> -->
     <beanclass="SpringMVC.portlet.SpringMVCPortlet"/>
    
    
    
</beans>

portlet-application-context.xml:
<?xmlversion="1.0"?>

<beans
     xmlns="http://www.springframework.org/schema/beans"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
> 
     <context:annotation-config/>

     <beanid="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    
<propertyname="contentType"value="text/html;charset=UTF-8"/>
           <propertyname="prefix"value="/WEB-INF/jsp/"/>
           <propertyname="suffix"value=".jsp"/>
          
          
           <!-- <property name="prefix" value="/META-INF/resources/" />
           <property name="suffix" value=".jsp" /> -->
<propertyname="viewClass"value="org.springframework.web.servlet.view.JstlView"/>
     </bean>
    
</beans>

liferay-display.xml:
<?xmlversion="1.0"?>
<!DOCTYPEdisplayPUBLIC"-//Liferay//DTD Display 6.2.0//EN""http://www.liferay.com/dtd/liferay-display_6_2_0.dtd">
<display>
<categoryname="category.sample">
<portletid="SpringMVCPortlet"/>
</category>
</display>

liferay-plugin-package.properties:
author=Liferay,Inc.
change-log=
licenses=LGPL
liferay-versions=7.0.0+
long-description=
module-group-id=liferay
module-incremental-version=1
name=SpringMVC
page-url=http://www.liferay.com
short-description=
12required-deployment-contexts = my-hook
tags=

liferay-portlet.xml:
<?xmlversion="1.0"?>
<!DOCTYPEliferay-portlet-appPUBLIC"-//Liferay//DTD Portlet Application 6.2.0//EN""http://www.liferay.com/dtd/liferay-portlet-app_6_2_0.dtd">

<liferay-portlet-app>
     <portlet>
           <portlet-name>SpringMVCPortlet</portlet-name>
           <icon>/icon.png</icon>
           <requires-namespaced-parameters>false</requires-namespaced-parameters>
           <header-portlet-css>/css/main.css</header-portlet-css>
           <footer-portlet-javascript>/js/main.js</footer-portlet-javascript>
           <css-class-wrapper>SpringMVCPortlet</css-class-wrapper>
     </portlet>
     <role-mapper>
           <role-name>administrator</role-name>
           <role-link>Administrator</role-link>
     </role-mapper>
     <role-mapper>
           <role-name>guest</role-name>
           <role-link>Guest</role-link>
     </role-mapper>
     <role-mapper>
           <role-name>power-user</role-name>
           <role-link>Power User</role-link>
     </role-mapper>
     <role-mapper>
           <role-name>user</role-name>
           <role-link>User</role-link>
     </role-mapper>
</liferay-portlet-app>



portlet.xml:
<?xmlversion="1.0"?>

<portlet-appxmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
     version="2.0">
     <portlet>
           <portlet-name>SpringMVCPortlet</portlet-name>
           <display-name>SpringMVCPortlet</display-name>
           <portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
           <init-param>
                <name>contextConfigLocation</name>
                <value>/WEB-INF/spring-context/portlet/SpringMvcWithGradle-portlet.xml</value>
           </init-param>
           <expiration-cache>0</expiration-cache>
           <supports>
                <mime-type>text/html</mime-type>
           </supports>
           <resource-bundle>content/Language</resource-bundle>
           <portlet-info>
                <title>SpringMVCPortlet</title>
                <short-title>SpringMVCPortlet</short-title>
                <keywords>SpringMVCPortlet</keywords>
           </portlet-info>
           <security-role-ref>
                <role-name>administrator</role-name>
           </security-role-ref>
           <security-role-ref>
                <role-name>guest</role-name>
           </security-role-ref>
           <security-role-ref>
                <role-name>power-user</role-name>
           </security-role-ref>
           <security-role-ref>
                <role-name>user</role-name>
           </security-role-ref>
     </portlet>
</portlet-app>
web.xml:
<?xmlversion="1.0"?>

<web-app
     version="2.5"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
> 
     <context-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>/WEB-INF/spring-context/portlet-application-context.xml</param-value>
     </context-param>
     <servlet>
           <servlet-name>ViewRendererServlet</servlet-name>
           <servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class>
           <load-on-startup>1</load-on-startup>
     </servlet>
     <servlet-mapping>
           <servlet-name>ViewRendererServlet</servlet-name>
           <url-pattern>/WEB-INF/servlet/view</url-pattern>
</servlet-mapping>
     <listener>
           <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>
</web-app>






build.gradle:
Gradle is an open source build automation system that builds upon the concepts of Apache Ant and Apache Maven and introduces a Groovy-based domain-specific language (DSL) instead of the XML form used by Apache Maven for declaring the project configuration.
In our Project we use WAR file.

buildscript {

           repositories {
           mavenLocal()
           maven {
                url "https://cdn.lfrs.sl/repository.liferay.com/nexus/content/groups/public"
           }
     }

    
     dependencies {
           classpath group: "com.liferay", name: "com.liferay.gradle.plugins.css.builder", version: "latest.release"
           classpath group: "com.liferay", name: "com.liferay.css.builder", version: "latest.release"
     }

  }

apply plugin: 'war'

war {
     dependsOn buildCSS

     exclude('**/*.scss')

     filesMatching("**/.sass-cache/") {
           it.path = it.path.replace(".sass-cache/", "")
     }

     includeEmptyDirs = false
}
dependencies {
     compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.0.0"
     compileOnly group: "com.liferay.portal", name: "com.liferay.util.taglib", version: "2.0.0"
     compileOnly group: "javax.portlet", name: "portlet-api", version: "2.0"
     compileOnly group: "javax.servlet", name: "javax.servlet-api", version: "3.0.1"
     compileOnly group: "jstl", name: "jstl", version: "1.2"
     compileOnly group: "org.osgi", name: "osgi.cmpn", version: "6.0.0"
     compileOnly group: 'org.osgi', name: 'osgi.core', version: '6.0.0'
    
     compile group: 'aopalliance', name: 'aopalliance', version: '1.0'
     compile group: 'commons-logging', name: 'commons-logging', version: '1.2'
     compile group: 'commons-beanutils', name: 'commons-beanutils', version: '1.2'
     compile group: 'commons-fileupload', name: 'commons-fileupload', version: '1.2'
     compile group: 'commons-io', name: 'commons-io', version: '1.0'
    
     compile group: 'org.springframework', name: 'spring-aop', version: '4.1.9.RELEASE'
     compile group: 'org.springframework', name: 'spring-beans', version: '4.1.9.RELEASE'
     compile group: 'org.springframework', name: 'spring-context', version: '4.1.9.RELEASE'
     compile group: 'org.springframework', name: 'spring-core', version: '4.1.9.RELEASE'
     compile group: 'org.springframework', name: 'spring-expression', version: '4.1.9.RELEASE'
     compile group: 'org.springframework', name: 'spring-webmvc-portlet', version: '4.1.9.RELEASE'
     compile group: 'org.springframework', name: 'spring-webmvc', version: '4.1.9.RELEASE'
     compile group: 'org.springframework', name: 'spring-web', version: '4.1.9.RELEASE'
     compileOnly project (":modules:DemoService:DemoService-api")
     compileOnly project (":modules:DemoService:DemoService-service")
    
    
    
}

Download all the library files which is mentioned above.


First Page navigation:
         In controller we need to specify that when the porlet start which page has to be visible. This thing can be specified in Controller inside Giving @RenderMapping. Before that we need configure the controller with Annotation’s @Controller(value = "SpringMVCPortlet")
@RequestMapping("view").

Loading 1st view.jsp page we need to specify in RenderMapping.
Calling Action Method:
In jsp we need to give actionUrl Tag as shown below.



Handling in controller
In Liferay SpringMvc if we need to create Render Url 1st step is we need to specify that Render method in jsp page before calling that page. After that we need to set that in   
                        “response.setParameter(“actiontype”,”MethodName”)”
After that we need to create the method.
Step1:
Step2:
Step3:
Creating a Site Programatically:

We can create the site programmatically by using following code.
View.jsp:
<%@includefile="init.jsp"%>


<%
     ThemeDisplay td = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
%>
<c:iftest="<%=td.isSignedIn()%>">

<portlet:defineObjects/>
<theme:defineObjects/>
<%

PortletPreferences commonpref = renderRequest.getPreferences();
String commonvalue = (String)commonpref.getValue("commonvalue", "Hello! Welcome to our portal.");
//Group group = (Group)request.getAttribute("site.group");
//Group liveGroup = (Group)request.getAttribute("site.liveGroup");
LayoutSetPrototype layoutSetPrototype = (LayoutSetPrototype)request.getAttribute("site.layoutSetPrototype");
//boolean showPrototypes = GetterUtil.getBoolean(request.getAttribute("site.showPrototypes"));

long companyid=company.getCompanyId();
String languageid=user.getLanguageId();
long liveGroupId = 0;
//Group group = (Group)request.getAttribute(WebKeys.);
List<Group> objgroup=GroupLocalServiceUtil.getGroups(0, GroupLocalServiceUtil.getGroupsCount());
Group group=objgroup.get(0);
long groupId = BeanParamUtil.getLong(group, request, "groupId");
//System.out.println("Here are group :"+group);
Group stagingGroup = null;

Group liveGroup = null;

if (group != null) {
if (group.isStagingGroup()) {
        liveGroup = group.getLiveGroup();

        stagingGroup = group;
    }
else {
        liveGroup = group;

if (group.hasStagingGroup()) {
            stagingGroup = group.getStagingGroup();
        }
    }
}
    liveGroupId = liveGroup.getGroupId();
/*  System.out.println("Here are livegroup id :"+liveGroupId);
    System.out.println("Here are companyid :"+companyid);
    System.out.println("Here are liveGroup :"+liveGroup);
    System.out.println("Here are groupId :"+groupId); */
    request.setAttribute("group", group);
%>
<%=commonvalue %>

<portlet:renderURLvar="test">
     <portlet:paramname="action"value="test"/>
</portlet:renderURL>

<portlet:actionURLvar="creatURL">
     <portlet:paramname="action"value="Create"/>
</portlet:actionURL>



<aui:formaction="<%=creatURL %>"method="post">

<liferay-ui:errorkey="sitename"message="The site you has enter already eits"/>
<aui:inputname="commonvalue"type="hidden"value="<%=commonvalue %>"/>
<aui:inputname="liveGroupId"type="hidden"value="<%=liveGroupId%>"/>
<aui:inputname="companyid"type="hidden"value="<%=companyid%>"/>
<aui:inputname="languageid"type="hidden"value="<%=languageid%>"/>
<aui:inputname="groupId"type="hidden"value="<%= groupId %>"/>
<aui:inputname="group"type="hidden"value="<%= group %>"/>
<aui:inputname="sitename"label="Enter Site"type="text"value="">
<aui:validatorname="required"></aui:validator></aui:input>
<aui:inputname="discription"label="Enter site discription"type="textarea"value="">
</aui:input>
<aui:buttontype="submit"value="Create site"></aui:button>

</aui:form>

</c:if>





Contoller:
@ActionMapping(params="action=Create")
     publicvoid Create(ActionRequest po_actionRequest ,
                ActionResponse po_actionResponse) throws Exception
    {
          
          
           /*Service Tracker Methods calling*/
          
           group = new GroupServiceTracker(this);
           group.open();
           layoutSet = new LayoutSetServiceTracker(this);
           layoutSet.open();
          
           LayoutSetLocalService layoutSetLocalService = layoutSet.getService();
           GroupLocalService groupLocalService = group.getService();
          
try {
            String lc_siteName=po_actionRequest.getParameter("sitename");
            String lc_findSiteName="";


          
           List<Group>lo_siteObj=groupLocalService.getGroups(0, groupLocalService.getGroupsCount());
// List<Group> lo_siteObj=GroupLocalServiceUtil.getGroups(0, GroupLocalServiceUtil.getGroupsCount());
          
for(intk=0;k<lo_siteObj.size();k++)
                {

if(lc_siteName.equals(lo_siteObj.get(k).getName()))
                    {
     lc_findSiteName=lo_siteObj.get(k).getName();
                    }

                }
//String lc_commonValue=po_actionRequest.getParameter("commonvalue");
//long ln_liveGroupId = Long.parseLong(po_actionRequest.getParameter("liveGroupId"));
longln_companyId=Long.parseLong(po_actionRequest.getParameter("companyid"));
                String lc_languageId=po_actionRequest.getParameter("languageid");
                String lc_discription=po_actionRequest.getParameter("discription");
intln_type = ParamUtil.getInteger(po_actionRequest, "type");
                String lc_friendlyURL = ParamUtil.getString(po_actionRequest, "friendlyURL");
                ServiceContext lo_serviceContext = ServiceContextFactory.getInstance(Group.class.getName(), po_actionRequest);
//long ln_privateLayoutSetPrototypeId = ParamUtil.getLong(po_actionRequest, "privateLayoutSetPrototypeId");
longln_publicLayoutSetPrototypeId = ParamUtil.getLong(po_actionRequest, "publicLayoutSetPrototypeId");
//ServiceContext lo_serviceContexta = new ServiceContext();
                Group lo_liveGroups = null;

               List<LayoutSetPrototype>lo_layoutSetPrototypes = LayoutSetPrototypeServiceUtil.search(ln_companyId, Boolean.TRUE, null);
               String uuid=null;
for (LayoutSetPrototype curLayoutSetPrototype : lo_layoutSetPrototypes)
                        {
                        Stringname=HtmlUtil.escape(curLayoutSetPrototype.getName(lc_languageId));

uuidcurLayoutSetPrototype.getUuid();
name="";
                        }

if(!uuid.equals(""))
                          {
     if(lc_findSiteName.equals(""))       

                             {                                   
           lo_liveGroups = GroupServiceUtil.addGroup(ln_publicLayoutSetPrototypeId, ln_publicLayoutSetPrototypeId, lc_siteName,lc_discription, 1, false, ln_type, lc_friendlyURL, true, true ,lo_serviceContext);
            System.out.println("liveGroups"+lo_liveGroups);
                                    Group groupobj=groupLocalService.fetchGroup(ln_companyId, lc_siteName);
           longgroupid=groupobj.getGroupId();
                                    System.out.println("Site is creates"+lo_liveGroups.getCreatorUserUuid() );
                                    LayoutSet layoutsets=layoutSetLocalService.getLayoutSet(groupid,false);
    
     layoutsets.setLayoutSetId(layoutsets.getLayoutSetId());
     layoutsets.setGroupId(layoutsets.getGroupId());
     layoutsets.setCompanyId(layoutsets.getCompanyId());
     layoutsets.setCreateDate(layoutsets.getCreateDate());
     layoutsets.setModifiedDate(layoutsets.getModifiedDate());
     layoutsets.setPrivateLayout(layoutsets.getPrivateLayout());
           layoutsets.setLogoId(layoutsets.getLogoId());
     layoutsets.setThemeId(layoutsets.getThemeId());
     layoutsets.setColorSchemeId(layoutsets.getColorSchemeId());
    
     layoutsets.setCss(layoutsets.getCss());
     layoutsets.setPageCount(1);
     layoutsets.setSettings(layoutsets.getSettings());
     layoutsets.setLayoutSetPrototypeUuid(uuid);
     layoutsets.setLayoutSetPrototypeLinkEnabled(true);
    
     layoutsets = layoutSetLocalService.updateLayoutSet(layoutsets);


     booleanprivateLayout = ParamUtil.getBoolean(po_actionRequest, "privateLayout");
     longparentLayoutId = ParamUtil.getLong(po_actionRequest, "parentLayoutId");
                                String name1 = ParamUtil.getString(po_actionRequest, "name", "home");
                                String title = StringPool.BLANK;
                                String description = StringPool.BLANK;
    
     booleanhidden = false;
                                String friendlyURL1 = StringPool.BLANK;
    

                           ServiceContext serviceContext1 = ServiceContextFactory.getInstance(po_actionRequest);
                          LayoutServiceUtil.addLayout(groupid, false ,parentLayoutId ,name1 ,title ,description ,LayoutConstants.TYPE_PORTLET ,false ,friendlyURL1,lo_serviceContext);

po_actionResponse.setRenderParameter("siteId", ""+groupobj.getGroupId());
     po_actionResponse.setRenderParameter("siteName", lc_siteName);
    
                po_actionResponse.setRenderParameter("action", "test");
               
                             }

                        }

             }
catch (SystemException e) {
e.printStackTrace();
        }
    }




Creating SiteForm:
        After creating form keeping that site id and site name we will create a from like which site need which jsp pages.in the form of Yes or No.
View.jsp:
<%@includefile="init.jsp"%>
<%@pageimport="com.liferay.portal.kernel.portlet.LiferayPortletMode"%>
<%@tagliburi="http://liferay.com/tld/aui"prefix="aui"%>
<%String sitename=request.getParameter("siteName");
String siteId=request.getParameter("siteId");
System.out.println("sitename : "+siteId);
%>
<portlet:actionURLvar="siteFromURL">
<portlet:paramname="action"value="siteFormAction"/>
</portlet:actionURL>

<portlet:renderURLvar="Sucess">
<portlet:paramname="action"value="Sucess"/>
</portlet:renderURL>

<aui:formaction="<%=siteFromURL %>"method="post">
    
           <aui:inputname="siteId"value="<%=siteId %>"label="Site Id"readonly="true"></aui:input>

           <aui:inputname="sitename"value="<%=sitename %>"label="Site Name"readonly="true"></aui:input>
           <aui:selectname="screen1"placeholder="Select Screen 1"label="Screen1">
                <aui:optionvalue="YES">YES</aui:option>
<aui:optionvalue="NO">NO</aui:option>
           </aui:select>
           <aui:selectname="screen2"placeholder="Select Screen 2"label="Screen2">
                <aui:optionvalue="YES">YES</aui:option>
<aui:optionvalue="NO">NO</aui:option>
           </aui:select>
           <aui:selectname="screen3"placeholder="Select Screen 3"label="Screen3">
                <aui:optionvalue="YES">YES</aui:option>
<aui:optionvalue="NO">NO</aui:option>
           </aui:select>
<aui:selectname="screen4"placeholder="Select Screen 4"label="Screen4">
                <aui:optionvalue="YES">YES</aui:option>
<aui:optionvalue="NO">NO</aui:option>
           </aui:select>
<aui:selectname="screen5"placeholder="Select Screen 5"label="Screen5">
                <aui:optionvalue="YES">YES</aui:option>
<aui:optionvalue="NO">NO</aui:option>
           </aui:select>
<aui:selectname="screen6"placeholder="Select Screen 6"label="Screen6">
                <aui:optionvalue="YES">YES</aui:option>
<aui:optionvalue="NO">NO</aui:option>
           </aui:select>
<aui:selectname="screen7"placeholder="Select Screen 7"label="Screen7">
                <aui:optionvalue="YES">YES</aui:option>
<aui:optionvalue="NO">NO</aui:option>
           </aui:select>
<aui:selectname="screen8"placeholder="Select Screen 8"label="Screen8">
                <aui:optionvalue="YES">YES</aui:option>
<aui:optionvalue="NO">NO</aui:option>
           </aui:select>
<aui:selectname="screen9"placeholder="Select Screen 9"label="Screen9">
                <aui:optionvalue="YES">YES</aui:option>
<aui:optionvalue="NO">NO</aui:option>
           </aui:select>
<aui:selectname="screen10"placeholder="Select Screen 10"label="Screen10">
                <aui:optionvalue="YES">YES</aui:option>
<aui:optionvalue="NO">NO</aui:option>
           </aui:select>
<aui:buttontype="submit"value="Create Site From"></aui:button>
</aui:form>



Contoller:
@ActionMapping(params= "action=siteFormAction")
publicvoid siteFormAction(ActionRequest actionRequest,ActionResponse actionResponse){
           /*Service tracker calling method*/
           st =new ServiceTrackerUtil(this); 
           st.open();
          
           counter = new CounterServiceTracker(this);
           counter.open();
          
           CounterLocalService counterLocalService = counter.getService();
           SiteFormLocalService stieFormLocalService =st.getService();
          
          
           String siteId = ParamUtil.getString(actionRequest, "siteId");
           String sitename = ParamUtil.getString(actionRequest, "sitename");
          
          
           String screen1 = ParamUtil.getString(actionRequest, "screen1");
           String screen2 = ParamUtil.getString(actionRequest, "screen2");
           String screen3 = ParamUtil.getString(actionRequest, "screen3");
           String screen4 = ParamUtil.getString(actionRequest, "screen4");
           String screen5 = ParamUtil.getString(actionRequest, "screen5");
           String screen6 = ParamUtil.getString(actionRequest, "screen6");
           String screen7 = ParamUtil.getString(actionRequest, "screen7");
           String screen8 = ParamUtil.getString(actionRequest, "screen8");
           String screen9 = ParamUtil.getString(actionRequest, "screen9");
           String screen10 = ParamUtil.getString(actionRequest, "screen10");
          
          
           try {
           } catch (Exception e) {
                e.printStackTrace();
           }
               
            ThemeDisplay td = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
          
               
               
          
           longsiteFormId;
           siteFormId = counterLocalService.increment();
           //siteFormId = CounterLocalServiceUtil.increment();
           // SiteForm siteFrom= SiteFormLocalServiceUtil.createSiteForm(siteFormId);
            SiteForm siteFrom = stieFormLocalService.createSiteForm(siteFormId);
           siteFrom.setSiteId(Long.parseLong(siteId));
           siteFrom.setSiteName(sitename);
           siteFrom.setScreen1(screen1);
           siteFrom.setScreen2(screen2);
           siteFrom.setScreen3(screen3);
           siteFrom.setScreen4(screen4);
           siteFrom.setScreen5(screen5);
           siteFrom.setScreen6(screen6);
           siteFrom.setScreen7(screen7);
           siteFrom.setScreen8(screen8);
           siteFrom.setScreen9(screen9);
           siteFrom.setScreen10(screen10);
           siteFrom.setCreatedBy(td.getUserId());
           siteFrom.setModifiedBy(td.getUserId());
           siteFrom.setCreateDate(new Date());
           siteFrom.setModifiedDate(new Date());
          
           stieFormLocalService.addSiteForm(siteFrom);
           //SiteFormLocalServiceUtil.addSiteForm(siteFrom);
            actionResponse.setRenderParameter("siteName", sitename);
            actionResponse.setRenderParameter("action", "Sucess");
    }

After creating site and site form we need to save the data in the database in liferay spring mvc it is recommended that not to LocalServiceUtill class rather than that we can use Service Tracker concept.

Service Tracker:
To implement a service tracker
To minimize the service tracker code, use a type-safe wrapper class that extends org.osgi.util.tracker.ServiceTracker. Your ServiceTracker takes two generic type parameters: the type of service being tracked, and the type of object being produced. In the present use case, both types are the same.
Create Separate class for each service tracker.

publicclass ServiceTrackerUtil extends
ServiceTracker<SiteFormLocalService, SiteFormLocalService> {

     public ServiceTrackerUtil(Object context) {
          super(FrameworkUtil.getBundle(context.getClass()).getBundleContext(),
                      SiteFormLocalService.class, null);
     } 
}
In controller class create init method inside that create an object of service tracker and open the tracker as shown below.
/*Service Tracker Objects*/
     publicstatic ServiceTrackerUtil st=null;
     publicstatic CounterServiceTracker counter = null;
     publicstatic GroupServiceTracker group = null;
     publicstatic LayoutSetServiceTracker layoutSet = null;
    
     @PostConstruct
     privatevoid init() {
           System.out.println("Inside the init method");
           st =new ServiceTrackerUtil(this); 
           st.open();
          
           counter = new CounterServiceTracker(this);
           counter.open();
          
           group = new GroupServiceTracker(this);
           group.open();
          
           layoutSet = new LayoutSetServiceTracker(this);
           layoutSet.open();

     }

After that to call service tracker in method we need to create object again and open after that we can get the service
st =new ServiceTrackerUtil(this); 
           st.open();
          
           counter = new CounterServiceTracker(this);
           counter.open();
          
           CounterLocalService counterLocalService = counter.getService();
           SiteFormLocalService stieFormLocalService =st.getService();

SiteForm siteFrom = stieFormLocalService.createSiteForm(siteFormId);
          
After getting all the service class we need to close all the resouses using destroy method.

@PreDestroy
           publicvoid destroy(){
                st.close();
                counter.close();
                group.close();
                layoutSet.close();
           }





No comments:

Post a Comment