Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Startup Listener or events in Liferay

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.17k
    Comment on it

    Hello Guys

    Liferay provide startup funcationality to deploy theme. portlet and hooks etc.

    You can utilize the StartupAction. It is merely an Event of liferay, which is get triggered on startup of a plugin in liferay portal. This method will only get triggered once on a liferay tomcat server start or deploy.

    For better understanding see below :

    Step 1: Create startup class and extends SimpleAction class.

    package bhagwan.singh.startup;
    
    import com.liferay.portal.kernel.events.ActionException;
    import com.liferay.portal.kernel.events.SimpleAction;
    
    public class StartupAction extends SimpleAction {
    
        @Override
        public void run(String[] companyIds) throws ActionException {
            setupExpandoAction.run(companyIds);
            importMessagesAction.run(companyIds);
        }
    
        private SetupExpandoAction setupExpandoAction = new SetupExpandoAction();
        private ImportMessagesAction importMessagesAction = new ImportMessagesAction();
    }
    

    Step 2: Create SetupExpandoAction.java file and put below code:

    package bhagwan.singh.startup;
    
    import com.liferay.portal.kernel.events.ActionException;
    import com.liferay.portal.kernel.events.SimpleAction;
    import com.liferay.portal.model.Group;
    import com.liferay.portal.model.ResourceConstants;
    import com.liferay.portal.model.Role;
    import com.liferay.portal.model.RoleConstants;
    import com.liferay.portal.security.permission.ActionKeys;
    import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
    import com.liferay.portal.service.RoleLocalServiceUtil;
    import com.liferay.portlet.expando.DuplicateColumnNameException;
    import com.liferay.portlet.expando.DuplicateTableNameException;
    import com.liferay.portlet.expando.model.ExpandoColumn;
    import com.liferay.portlet.expando.model.ExpandoColumnConstants;
    import com.liferay.portlet.expando.model.ExpandoTable;
    import com.liferay.portlet.expando.model.ExpandoTableConstants;
    import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
    import com.liferay.portlet.expando.service.ExpandoTableLocalServiceUtil;
    import com.liferay.portlet.journal.model.JournalArticle;
    
    import bhagwan.singh.service.constants.ExpandoConstants;
    
    public class SetupExpandoAction extends SimpleAction {
    
        @Override
        public void run(String[] companyIds) throws ActionException {
            for (int i = 0; i < companyIds.length; i++) {
                long companyId = Long.parseLong(companyIds[i]);
                try {
                    setupExpandoGroup(companyId);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    
        protected void setupExpandoGroup(long companyId) throws Exception {
            ExpandoTable table = null;
            try {
                table = ExpandoTableLocalServiceUtil.addTable(companyId, Group.class.getName(), ExpandoTableConstants.DEFAULT_TABLE_NAME);
            } catch (DuplicateTableNameException dtne) {
                table = ExpandoTableLocalServiceUtil.getTable(companyId, Group.class.getName(), ExpandoTableConstants.DEFAULT_TABLE_NAME);
            }
            /*
             * Setup StartDate
             */
            try {
                ExpandoColumn column = ExpandoColumnLocalServiceUtil.addColumn(table.getTableId(),
                        ExpandoConstants.EXPANDO_COLUMN_NAME_START_DATE, ExpandoColumnConstants.DATE);
                column.setDefaultData("");
                Role user = RoleLocalServiceUtil.getRole(companyId, RoleConstants.GUEST);
                ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, ExpandoColumn.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL,
                        String.valueOf(column.getColumnId()), user.getRoleId(), new String[] { ActionKeys.VIEW});
            }
            catch (DuplicateColumnNameException dcne) {
            }
            /*
             * Setup EndDate
             */
            try {
                ExpandoColumn column = ExpandoColumnLocalServiceUtil.addColumn(table.getTableId(),
                        ExpandoConstants.EXPANDO_COLUMN_NAME_END_DATE, ExpandoColumnConstants.DATE);
                column.setDefaultData("");
                Role user = RoleLocalServiceUtil.getRole(companyId, RoleConstants.GUEST);
                ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, ExpandoColumn.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL,
                        String.valueOf(column.getColumnId()), user.getRoleId(), new String[] { ActionKeys.VIEW});
            }
            catch (DuplicateColumnNameException dcne) {
            }
    
    
            try {
                ExpandoColumn column = ExpandoColumnLocalServiceUtil.addColumn(table.getTableId(),
                        ExpandoConstants.EXPANDO_COLUMN_NAME_CREATE_DATE, ExpandoColumnConstants.DATE);
                column.setDefaultData("");
                Role user = RoleLocalServiceUtil.getRole(companyId, RoleConstants.GUEST);
                ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, ExpandoColumn.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL,
                        String.valueOf(column.getColumnId()), user.getRoleId(), new String[] { ActionKeys.VIEW});
            }
            catch (DuplicateColumnNameException dcne) {
            }
    
        }
    
    }
    

    Step :3 In your liferay-hook.xml file, you do as below:

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

    Step :4 add below line in your portal.properties:

    application.startup.events=bhagwan.singh.startup.StartupAction
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: