如何以编程方式在liferay中添加网站?

时间:2016-03-18 12:11:53

标签: java jsp liferay

我想以编程方式在liferay中添加网站,因此我创建了一个portlet,它负责以编程方式添加网站,但它不起作用所以请任何人都可以帮助我。我点击创建网站时收到的错误是Portlet is temporarily unavailable。 这是我的代码:

<%@page import="javax.portlet.PortletPreferences"%>
<%@page import="com.liferay.portal.kernel.util.ParamUtil"%>
<%@page import="com.liferay.portal.kernel.util.HtmlUtil"%>
<%@page import="com.liferay.portal.kernel.util.StringPool"%>
<%@page import="com.liferay.portal.kernel.util.UnicodeProperties"%>
<%@page import="com.liferay.portal.service.LayoutSetPrototypeServiceUtil"%>
<%@page import="com.liferay.portal.model.LayoutSetPrototype"%>
<%@page import="com.liferay.portal.service.GroupLocalServiceUtil"%>
<%@page import="java.util.List"%>
<%@page import="com.liferay.portal.kernel.bean.BeanParamUtil"%>
<%@page import="com.liferay.portal.theme.ThemeDisplay"%>
<%@page import="com.liferay.portal.model.Group"%>
<%@page import="com.liferay.portal.kernel.util.WebKeys"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://liferay.com/tld/theme"  prefix="theme"%>
<%@taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui" %>
<%@taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>

<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);
     request.setAttribute("group", group);
    %>
   <%=commonvalue %>
  <portlet:actionURL var="creatURL" name="Create">
  <portlet:param name="viewJSP" value="/html/sites/view.jsp">   </portlet:param>

</portlet:actionURL>

<aui:form action="<%=creatURL %>" method="post">

<liferay-ui:error key="sitename" message="The site you has enter already eits" />
<aui:input name="commonvalue" type="hidden" value="<%=commonvalue %>"/>
<aui:input name="liveGroupId" type="hidden" value="<%=liveGroupId%>" />
<aui:input name="companyid" type="hidden" value="<%=companyid%>" />
<aui:input name="languageid" type="hidden" value="<%=languageid%>" />
<aui:input name="groupId" type="hidden" value="<%= groupId %>" />
<aui:input name="group" type="hidden" value="<%= group %>" />
<aui:input name="sitename" label="Enter Site" type="text" value="">
<aui:validator name="required"></aui:validator></aui:input>
<aui:input name="discription" label="Enter site discription" type="textarea" value="">
</aui:input>



 <aui:button type="submit" value="Create site" ></aui:button>






 </aui:form>

&安培;这是我的Sites.java,它是一个动作文件:

package com.test;

import java.io.IOException;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletModeException;
import javax.portlet.ReadOnlyException;
import javax.portlet.ValidatorException;
import javax.portlet.WindowStateException; 
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.util.HtmlUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.UnicodeProperties;
import com.liferay.portal.model.Group;
import com.liferay.portal.model.LayoutConstants;
import com.liferay.portal.model.LayoutSet;
import com.liferay.portal.model.LayoutSetPrototype;

import com.liferay.portal.service.GroupServiceUtil;

import com.liferay.portal.service.GroupLocalServiceUtil;
import com.liferay.portal.service.LayoutServiceUtil;
import com.liferay.portal.service.LayoutSetLocalServiceUtil;
import com.liferay.portal.service.LayoutSetPrototypeServiceUtil;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.ServiceContextFactory; 
import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;

import javax.portlet.PortletPreferences;
//import com.liferay.portlet.sites.util.SitesUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 * Portlet implementation class Sites
 */
 public class Sites extends MVCPortlet {


public void fiveplan(ActionRequest fiveplanrequest,ActionResponse fiveplanresponse) throws ReadOnlyException, ValidatorException, IOException, PortletModeException, WindowStateException
{
    String fiveplane=fiveplanrequest.getParameter("basicplan");    

    PortletPreferences fiveplanpref=fiveplanrequest.getPreferences();

    if(fiveplane !=null)
    {
        fiveplanpref.setValue("commonvalue", fiveplane);
        fiveplanpref.store();    

        fiveplanresponse.setRenderParameter("jspPage","/html/sites/view.jsp");

    }


}



public void Create(ActionRequest actionRequest ,ActionResponse res) throws Exception
{

    try {
        String sitename=actionRequest.getParameter("sitename");
         String findsitename="";
          List<Group> siteobj=GroupLocalServiceUtil.getGroups(0, GroupLocalServiceUtil.getGroupsCount());
            for(int k=0;k<siteobj.size();k++)
            {

                if(sitename.equals(siteobj.get(k).getName()))
                {
                    //SessionErrors.add(actionRequest, "sitename");
                    findsitename=siteobj.get(k).getName();
                }

            }
         String commonvalue=actionRequest.getParameter("commonvalue");

            long liveGroupId = Long.parseLong(actionRequest.getParameter("liveGroupId"));
            long companyid=Long.parseLong(actionRequest.getParameter("companyid"));
            String languageid=actionRequest.getParameter("languageid");
            String discription=actionRequest.getParameter("discription");
            System.out.println("Here is domain name"+sitename);
            System.out.println("Here is group name"+liveGroupId);
            int type = ParamUtil.getInteger(actionRequest, "type");
            String friendlyURL = ParamUtil.getString(actionRequest, "friendlyURL");
            System.out.println("Friendlay URL is :"+friendlyURL);
            ServiceContext serviceContext = ServiceContextFactory.getInstance(Group.class.getName(), actionRequest);
            Group liveGroup = null;
            String oldFriendlyURL = null;
            String oldStagingFriendlyURL = null;
            long privateLayoutSetPrototypeId = ParamUtil.getLong(actionRequest, "privateLayoutSetPrototypeId");
            long publicLayoutSetPrototypeId = ParamUtil.getLong(actionRequest, "publicLayoutSetPrototypeId");
            System.out.println("Here are private id: :"+privateLayoutSetPrototypeId);
            System.out.println("Here are public id: :"+publicLayoutSetPrototypeId);
            System.out.println("Here are servicecontext id: :"+serviceContext);
            ServiceContext serviceContexta = new ServiceContext();
            Group liveGroups = null;

           List<LayoutSetPrototype> layoutSetPrototypes = LayoutSetPrototypeServiceUtil.search(companyid, Boolean.TRUE, null); 
                        String uuid=null;
                        for (LayoutSetPrototype curLayoutSetPrototype : layoutSetPrototypes) 
                            {
                            UnicodeProperties settingsProperties = curLayoutSetPrototype.getSettingsProperties();
                            String servletContextName = settingsProperties.getProperty("customJspServletContextName", StringPool.BLANK);
                            String name=HtmlUtil.escape(curLayoutSetPrototype.getName(languageid));
                            System.out.println("hi this is :"+ curLayoutSetPrototype.getLayoutSetPrototypeId());

                              if(commonvalue.equals(name))
                                  {
                                uuid=  curLayoutSetPrototype.getUuid();
                                  }
                                  name="";
                            }

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

                                 if(findsitename.equals(""))        

                                 {                                    
                                GroupServiceUtil.addGroup(sitename,discription, 1, friendlyURL, true, true ,serviceContext);

                                Group groupobj=GroupLocalServiceUtil.fetchGroup(companyid, sitename);
                                long groupid=groupobj.getGroupId();
                               System.out.println("Site is creates"); 
                               LayoutSet layoutsets=LayoutSetLocalServiceUtil.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.setLogo(layoutsets.getLogo());
                               layoutsets.setLogoId(layoutsets.getLogoId());
                               layoutsets.setThemeId(layoutsets.getThemeId());
                               layoutsets.setColorSchemeId(layoutsets.getColorSchemeId());
                               layoutsets.setWapThemeId(layoutsets.getWapThemeId());
                               layoutsets.setWapColorSchemeId(layoutsets.getWapColorSchemeId());
                               layoutsets.setCss(layoutsets.getCss());
                               layoutsets.setPageCount(1);
                               layoutsets.setSettings(layoutsets.getSettings());
                               layoutsets.setLayoutSetPrototypeUuid(uuid);
                               layoutsets.setLayoutSetPrototypeLinkEnabled(true);

                               LayoutSetLocalServiceUtil.updateLayoutSet(layoutsets);




                               boolean privateLayout = ParamUtil.getBoolean(actionRequest, "privateLayout");
                               long parentLayoutId = ParamUtil.getLong(actionRequest, "parentLayoutId");
                               String name1 = ParamUtil.getString(actionRequest, "name", "home");
                               String title = StringPool.BLANK;
                              String description = StringPool.BLANK;

                               boolean hidden = false;
                               String friendlyURL1 = StringPool.BLANK;


                               ServiceContext serviceContext1 = ServiceContextFactory.getInstance(actionRequest);
                               System.out.println("Service cintext is "+serviceContext1);

                               System.out.println("Here are new site id"+groupid);

                              LayoutServiceUtil.addLayout(groupid, false ,parentLayoutId ,name1 ,title ,description ,LayoutConstants.TYPE_PORTLET ,false ,friendlyURL1,serviceContext);
                              res.setRenderParameter("jspPage", "/html/sites/view.jsp");

                                     }
                                else
                                {

                                SessionErrors.add(actionRequest, "sitename");


                                }


                              }

         }
    catch (SystemException e) {
        // TODO Auto-generated catch block
                        e.printStackTrace();
    }    

}

}

我的日志文件是:

13:08:51,568 ERROR [RuntimePageImpl-5][render_portlet_jsp:132] null
java.lang.NullPointerException
at com.test.Sites.Create(Sites.java:118)
at  com.liferay.portal.kernel.portlet.LiferayPortlet.callActionMethod(LiferayPortlet.java:163)
at  com.liferay.util.bridges.mvc.MVCPortlet.callActionMethod(MVCPortlet.java:249)
at com.liferay.portal.kernel.portlet.LiferayPortlet.processAction(LiferayPortlet.java:90)
at com.liferay.util.bridges.mvc.MVCPortlet.processAction(MVCPortlet.java:212)
at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:71)
at com.liferay.portal.kernel.portlet.PortletFilterUtil.doFilter(PortletFilterUtil.java:48)
at com.liferay.portal.kernel.servlet.PortletServlet.service(PortletServlet.java:112)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilter.doFilter(InvokerFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:604)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:543)
at com.liferay.portlet.InvokerPortletImpl.invoke(InvokerPortletImpl.java:583)
at com.liferay.portlet.InvokerPortletImpl.invokeAction(InvokerPortletImpl.java:628)

1 个答案:

答案 0 :(得分:0)

您的问题似乎应该与行NullPointerException的{​​{1}}相关。

唯一可以为空的对象是if(!uuid.equals(""))

查看您的代码,由于以下测试,uuid可能无法将任何内容分配给uuid

在我看来,因为您正在测试if(commonvalue.equals(name))的空值,所以用uuid

替换第104行(或103?)String uuid=null;就足够了