Commits

Martin Lau committed dbbd81e

added application start hook to create sample page

Comments (0)

Files changed (4)

src/main/java/ph/hum/sample/action/CreateSamplePageAction.java

+package ph.hum.sample.action;
+
+import com.liferay.portal.NoSuchLayoutException;
+import com.liferay.portal.kernel.events.ActionException;
+import com.liferay.portal.kernel.events.SimpleAction;
+import com.liferay.portal.kernel.exception.PortalException;
+import com.liferay.portal.kernel.exception.SystemException;
+import com.liferay.portal.model.Layout;
+import com.liferay.portal.model.LayoutTypePortlet;
+import com.liferay.portal.model.User;
+import com.liferay.portal.service.LayoutLocalService;
+import com.liferay.portal.service.LayoutLocalServiceUtil;
+import com.liferay.portal.service.ServiceContext;
+import com.liferay.portal.service.UserLocalService;
+import com.liferay.portal.service.UserLocalServiceUtil;
+import com.liferay.portal.util.Portal;
+import com.liferay.portal.util.PortalUtil;
+import org.springframework.stereotype.Component;
+
+import static com.liferay.portal.model.LayoutConstants.TYPE_PORTLET;
+
+@Component
+public class CreateSamplePageAction extends SimpleAction {
+
+    public static final String EMAIL_ADDRESS = "test@liferay.com";
+
+    public static final String FRIENDLY_URL = "/sample-portlet";
+
+    public static final String NAME = "Sample portlet";
+
+    private LayoutLocalService layoutLocalService = LayoutLocalServiceUtil.getService();
+
+    private Portal portal = PortalUtil.getPortal();
+
+    private UserLocalService userLocalService = UserLocalServiceUtil.getService();
+
+    @Override
+    public void run(String[] ids) throws ActionException {
+        final User user = getTestUser();
+
+        deleteExistingLayout(user);
+        createSampleLayout(user);
+    }
+
+    protected void createSampleLayout(final User user) throws ActionException {
+        final Layout layout = addLayout(user.getUserId(),
+                                        getUserGroupId(user),
+                                        false,
+                                        0L,
+                                        NAME,
+                                        NAME,
+                                        NAME,
+                                        TYPE_PORTLET,
+                                        false,
+                                        FRIENDLY_URL,
+                                        new ServiceContext());
+
+        layout.setColorSchemeId("01");
+        layout.setThemeId("classic");
+
+        final LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();
+        layoutTypePortlet.setLayoutTemplateId(user.getUserId(), "3_columns");
+
+        addPortletsToColumn(user.getUserId(),
+                            layoutTypePortlet,
+                            "column-1",
+                            new String[]{"basic_WAR_sampleportlet",
+                                         "requestAttribute_WAR_sampleportlet"});
+
+        addPortletsToColumn(user.getUserId(),
+                            layoutTypePortlet,
+                            "column-2",
+                            new String[]{"knownClass_WAR_sampleportlet",
+                                         "transaction_WAR_sampleportlet"});
+
+        addPortletsToColumn(user.getUserId(),
+                            layoutTypePortlet,
+                            "column-3",
+                            new String[]{"localService_WAR_sampleportlet",
+                                         "validating_WAR_sampleportlet"});
+
+        updateLayout(layout);
+    }
+
+    protected Layout addLayout(final long userId,
+                               final long groupId,
+                               final boolean privateLayout,
+                               final long parentLayoutId,
+                               final String name,
+                               final String title,
+                               final String description,
+                               final String type,
+                               final boolean hidden,
+                               final String friendlyUrl,
+                               final ServiceContext serviceContext) throws ActionException {
+        try {
+            return layoutLocalService.addLayout(userId,
+                                                groupId,
+                                                privateLayout,
+                                                parentLayoutId,
+                                                name,
+                                                title,
+                                                description,
+                                                type,
+                                                hidden,
+                                                friendlyUrl,
+                                                serviceContext);
+        }
+        catch (PortalException e) {
+            throw new ActionException("Error adding layout", e);
+        }
+        catch (SystemException e) {
+            throw new ActionException("Error adding layout", e);
+        }
+    }
+
+    protected void addPortletsToColumn(final long userId,
+                                       final LayoutTypePortlet layoutTypePortlet,
+                                       final String columnId,
+                                       final String[] portletIds) throws ActionException {
+        try {
+            layoutTypePortlet.addPortletIds(userId, portletIds, columnId, false);
+        }
+        catch (PortalException e) {
+            throw new ActionException("Error adding portlets to column", e);
+        }
+        catch (SystemException e) {
+            throw new ActionException("Error adding portlets to column", e);
+        }
+    }
+
+    protected void updateLayout(final Layout layout) throws ActionException {
+        try {
+            layoutLocalService.updateLayout(layout);
+        }
+        catch (SystemException e) {
+            throw new ActionException("Error updating layout", e);
+        }
+    }
+
+    protected void deleteExistingLayout(final User user) throws ActionException {
+        final Layout layout = findSamplePortletLayout(user);
+        if (layout != null) {
+            deleteLayout(layout);
+        }
+    }
+
+    protected void deleteLayout(final Layout layout) throws ActionException {
+        try {
+            layoutLocalService.deleteLayout(layout);
+        }
+        catch (SystemException e) {
+            throw new ActionException("Error deleting existing layout", e);
+        }
+    }
+
+    protected Layout findSamplePortletLayout(final User user) throws ActionException {
+        final long groupId = getUserGroupId(user);
+        try {
+            return layoutLocalService.getFriendlyURLLayout(groupId, false, FRIENDLY_URL);
+        }
+        catch (NoSuchLayoutException e) {
+            // Ignore
+        }
+        catch (SystemException e) {
+            throw new ActionException("Error fetching group id for user: " + user, e);
+        }
+        catch (PortalException e) {
+            throw new ActionException("Error fetching group id for user: " + user, e);
+        }
+        return null;
+    }
+
+    protected long getUserGroupId(final User user) throws ActionException {
+        try {
+            return user.getGroupId();
+        }
+        catch (PortalException e) {
+            throw new ActionException("Error getting user group ID", e);
+        }
+        catch (SystemException e) {
+            throw new ActionException("Error getting user group ID", e);
+        }
+    }
+
+    protected User getTestUser() throws ActionException {
+        final long defaultCompanyId = portal.getDefaultCompanyId();
+        try {
+            return userLocalService.getUserByEmailAddress(defaultCompanyId, EMAIL_ADDRESS);
+        }
+        catch (PortalException e) {
+            throw new ActionException("Error fetching user: " + EMAIL_ADDRESS + " from company: " + defaultCompanyId, e);
+        }
+        catch (SystemException e) {
+            throw new ActionException("Error fetching user: " + EMAIL_ADDRESS + " from company: " + defaultCompanyId, e);
+        }
+    }
+
+}

src/main/resources/portal.properties

+application.startup.events=ph.hum.sample.action.CreateSamplePageAction

src/main/webapp/WEB-INF/liferay-hook.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 6.1.0//EN" "http://www.liferay.com/dtd/liferay-hook_6_1_0.dtd">
+
+<hook>
+    <portal-properties>portal.properties</portal-properties>
+</hook>

src/main/webapp/WEB-INF/transaction-portlet.xml

 
     <context:component-scan base-package="ph.hum.sample.transaction"/>
 
-    <liferay:services/>
-
-    <liferay:transaction-manager/>
+    <liferay:services>
+        <liferay:include-filter type="assignable" expression="com.liferay.portal.kernel.dao.orm.DynamicQueryFactory" />
+        <liferay:include-filter type="assignable" expression="com.liferay.portal.kernel.dao.orm.ProjectionFactory"/>
+        <liferay:include-filter type="assignable" expression="com.liferay.portal.kernel.dao.orm.PropertyFactory"/>
+        <liferay:include-filter type="assignable" expression="com.liferay.portal.service.UserLocalService"/>
+        <liferay:include-filter type="assignable" expression="com.liferay.portal.util.Portal"/>
+    </liferay:services>
 
     <tx:annotation-driven />