1

I have a site that is currently based on a site template. I would like to delete all pages in this site (keeping the content), and link the site to a new site template.

The Jira issue LPS-33179 asks precisely that question from a GUI perspective. I would like to know from a coder's perspective.

I am using Liferay 6.1.0 GA2.

Thanks, Alain

4

1 回答 1

2

This is what I used, and it seems to work :

LayoutSetLocalServiceUtil.updateLayoutSetPrototypeLinkEnabled

to set the link to the prototype, and

SitesUtil.mergeLayoutSetProtypeLayouts

to update the sites immediately. The latter is called using PortalClassInvoker based on code from Jelmer Kuperus

public static void setupSitesFromSiteTemplate(long groupId, long publicSiteTemplateId,
                                              long privateSiteTemplateId) throws PortalException, SystemException {
    Group group = GroupLocalServiceUtil.getGroup(groupId);
    if (publicSiteTemplateId != 0) setSiteTemplate(group, publicSiteTemplateId, false);
    if (privateSiteTemplateId != 0) setSiteTemplate(group, privateSiteTemplateId, true);
}

public static void setSiteTemplate(Group group, long siteTemplateId, boolean isPrivateLayout)
        throws PortalException, SystemException {
    long groupId = group.getGroupId();
    LayoutSetPrototype prototype = LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototype(siteTemplateId);
    boolean layoutSetPrototypeLinkEnabled = true;
    LayoutSetLocalServiceUtil.updateLayoutSetPrototypeLinkEnabled(groupId, isPrivateLayout,
            layoutSetPrototypeLinkEnabled, prototype.getUuid());
    try {
        LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(groupId, isPrivateLayout);
        mergeLayoutSetProtypeLayouts(group, layoutSet);
    } catch (Exception e) {
        if (_log.isWarnEnabled()) {
            String privatePublic = isPrivateLayout ? "private" : "public";
            _log.warn(String.format("Could not merge %s layouts for group[%d] from template[%d]", privatePublic,
                    groupId, siteTemplateId));
            e.printStackTrace();
        }
    }
}

public static void mergeLayoutSetProtypeLayouts(Group group, LayoutSet layoutSet) throws Exception {

    MethodKey key = SitesUtilMethodKey("mergeLayoutSetProtypeLayouts", Group.class, LayoutSet.class);
    invokePortalClassMethod(key, group, layoutSet);
}
/*
 * copied from
 * http://www.liferay.com/community/forums/-/message_boards/view_message /10488983#_19_message_10488983 
 * post by Jelmer Kuperus
 *
 * key: key of method to be called, e.g. com.liferay.portlet.sites.util.SitesUtil
 * arguments:  arguments to be passed to the invoked method
 * returns: result of the invoked method
 */
private static Object invokePortalClassMethod(MethodKey key, Object... arguments) throws PortalException {
    try {
        // noinspection unchecked
        return PortalClassInvoker.invoke(false, key, arguments);
    } catch (PortalException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
private static final String SITES_UTIL_CLASS_NAME = "com.liferay.portlet.sites.util.SitesUtil";
private static MethodKey SitesUtilMethodKey(String methodName, Class<?>... parameterTypes) {
    return new MethodKey(SITES_UTIL_CLASS_NAME, methodName, parameterTypes);
}
于 2013-08-21T17:25:38.337 回答