Lukas Grygar's

Personal enterprise Java web development blog.

Liferay: How to create expando (custom) attributes and roles programatically

I’d like to share Groovy script for Liferay 6.2 to create expando (custom) attributes and roles programaticaly. Expando attributes can extend objects with custom properties/field which is useful due to fact that these properties are first class Liferay objects.

My Groovy script is able to:

  • add new roles
  • add new expando attributes
  • set default values to expando attributes
  • set permissions to expando attributes

Run this script through: Liferay -> Control Panel -> Configuration -> Server Administration -> Script (and choose Groovy as Language)

create-new-expando-attributes.groovy

  1 import java.util.HashMap;
  2 import java.util.Map;
  3 import java.util.ArrayList;
  4 import java.util.List;
  5 import java.util.Date;
  6 import com.liferay.portal.security.auth.CompanyThreadLocal;
  7 import com.liferay.portlet.expando.model.ExpandoColumnConstants;
  8 import com.liferay.portlet.expando.model.ExpandoTableConstants;
  9 import com.liferay.portlet.expando.model.impl.ExpandoBridgeImpl;
 10 import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
 11 import com.liferay.portlet.expando.model.ExpandoColumn;
 12 import com.liferay.portal.model.ResourceConstants;
 13 import com.liferay.portal.model.RoleConstants;
 14 import com.liferay.portal.security.permission.ActionKeys;
 15 import com.liferay.portal.service.ResourceLocalServiceUtil;
 16 import com.liferay.portal.model.User;
 17 import com.liferay.portal.model.Role;
 18 import com.liferay.portal.model.Resource;
 19 import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
 20 import com.liferay.portal.service.RoleLocalServiceUtil;
 21 import com.liferay.portal.service.UserLocalServiceUtil;
 22 import com.liferay.portal.service.ClassNameLocalServiceUtil;
 23 import com.liferay.counter.service.CounterLocalServiceUtil;
 24 
 25 /*
 26  *  Run this script through: Liferay -> Control Panel -> Configuration -> Server Administration -> Script (and choose Groovy as Language)
 27  *  Note: You can run this script repeatedly.
 28  *  @author: Lukas Grygar
 29  *
 30  */
 31 
 32 void addRoles(List<String> roleNames) {
 33     
 34     long companyId = CompanyThreadLocal.getCompanyId();
 35     
 36     for(String roleName : roleNames) {
 37         if(RoleLocalServiceUtil.fetchRole(companyId, roleName) != null) {
 38             println "Role " + roleName + " already exists!";
 39         } else {
 40             println "Creating new role " + roleName;
 41             
 42             Date now = new Date();
 43             
 44             Long defaultUserId = UserLocalServiceUtil.getDefaultUserId(companyId);
 45             Long roleClassNameId = ClassNameLocalServiceUtil.getClassNameId(Role.class);
 46             Long roleId = CounterLocalServiceUtil.increment();
 47 
 48             Role role = RoleLocalServiceUtil.createRole(roleId);
 49             role.setName(roleName);
 50             role.setDescription("Role " + roleName + " was created by script create-new-expando-attributes.groovy");
 51             role.setType(1); // role type. 1=regular, 2=site, 3=organization
 52             role.setUserId(defaultUserId);
 53             role.setCompanyId(companyId);
 54             role.setClassNameId(roleClassNameId);
 55             role.setClassPK(roleId);
 56             role.setCreateDate(now);
 57             role.setModifiedDate(now);
 58 
 59             RoleLocalServiceUtil.addRole(role);
 60         }
 61     }
 62     
 63     println "";
 64     
 65 }
 66 
 67 List<String> rolesList = new ArrayList<String>();
 68 rolesList.add("FOO");
 69 rolesList.add("BAR");
 70 
 71 addRoles(rolesList);
 72 
 73 void addExpandos(Map<String, Integer> expandos, String typeName) {
 74 
 75     ExpandoBridgeImpl expandoBridge = new ExpandoBridgeImpl(CompanyThreadLocal.getCompanyId(), typeName);
 76     
 77     for (Map.Entry<String, Integer> expando : expandos.entrySet()) {
 78         if (expandoBridge.hasAttribute(expando.getKey())) {
 79             println typeName + " expando attribute " + expando.getKey() + " already exists!";
 80         }
 81         else {
 82             println typeName + " expando attribute does not exists, adding new " + typeName + " attribute: " + expando.getKey();
 83             expandoBridge.addAttribute(expando.getKey(), expando.getValue());
 84         }
 85     }
 86     
 87     println "";
 88 }
 89 
 90 // Create User expando attribute
 91 Map<String, Integer> userExpandos = new HashMap<String, Integer>();
 92 
 93 userExpandos.put("SOME_STRING_EXPANDO", ExpandoColumnConstants.STRING);
 94 userExpandos.put("SOME_OTHER_STRING_EXPANDO", ExpandoColumnConstants.STRING);
 95 
 96 addExpandos(userExpandos, User.class.getName());
 97 
 98 
 99 
100 // Set default values for User expando attributes
101 Map<String, String> userExpandosDefaultValues = new HashMap<String, String>();
102 
103 userExpandosDefaultValues.put("SOME_STRING_EXPANDO", "Some default value");
104 userExpandosDefaultValues.put("SOME_OTHER_STRING_EXPANDO", "Some default value");
105 
106 void setDefaultExpandosValue(Map<String, String> expandos, String typeName) {
107 
108     ExpandoBridgeImpl expandoBridge = new ExpandoBridgeImpl(CompanyThreadLocal.getCompanyId(), typeName);
109     for (Map.Entry<String, Integer> expando : expandos.entrySet()) {
110         if (expandoBridge.hasAttribute(expando.getKey())) {
111             if (expandoBridge.getAttributeDefault(expando.getKey()).equals(expando.getValue())) {
112                 println "(" + typeName + ") expando attribute: " + expando.getKey() + ", already set do default value: " + expando.getValue();
113             } else {
114                 println "Setting default value for " + typeName + " type expando attribute: " + expando.getKey() + ", default value: " + expando.getValue();
115                 expandoBridge.setAttributeDefault(expando.getKey(), expando.getValue());
116             }
117         }
118         else {
119             println typeName + " expando attribute does not exists!";
120         }
121     }
122     
123     println "";
124 }
125 
126 setDefaultExpandosValue(userExpandosDefaultValues, User.class.getName())
127 
128 
129 
130 // Create Role expando attribute
131 Map<String, Integer> roleExpandos = new HashMap<String, Integer>();
132 
133 roleExpandos.put("APPLICATION_ID", ExpandoColumnConstants.STRING);
134 roleExpandos.put("CUSTOMER_ID", ExpandoColumnConstants.STRING);
135 
136 addExpandos(roleExpandos, Role.class.getName());
137 
138 // Set permissions to expando attributes
139 void setPermissions(Map<String, Integer> expandos, String typeName, Map<String, String[]> expandosPermissions) {
140         
141     long companyId = CompanyThreadLocal.getCompanyId();
142     
143     println "setPermissions():: Entry";
144     
145     ExpandoBridgeImpl expandoBridge = new ExpandoBridgeImpl(companyId, typeName);
146     
147     for(Map<String, Integer> expando: expandos.entrySet()) { // for each expando attribute
148         
149         print " (" + typeName + ") " + expando.getKey();
150     
151         ExpandoColumn column = ExpandoColumnLocalServiceUtil.getColumn(companyId , expandoBridge.getClassName(), ExpandoTableConstants.DEFAULT_TABLE_NAME, expando.getKey());
152         
153         for(Map.Entry<String, String[]> expandoPermission : expandosPermissions.entrySet()) { // for each role
154             
155             print ", role: " + expandoPermission.getKey();
156             
157             Role role = RoleLocalServiceUtil.getRole(companyId, expandoPermission.getKey());
158             
159             if (role != null) {
160                 if (column != null) {
161                    print ", permission: " + expandoPermission.getValue();
162                    ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, ExpandoColumn.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(column.getColumnId()), role.getRoleId(), expandoPermission.getValue());
163                } else {
164                    print ", column does not exists";
165                }
166             } else {
167                 print ", role does not exists";
168             }
169         }
170         println ".";
171     }
172     
173     println "";
174 }
175     
176 Map<String, String[]> expandosPermissions = new HashMap<String, String[]>();
177 expandosPermissions.put("FOO", (String[])[ActionKeys.VIEW, ActionKeys.UPDATE]);
178 expandosPermissions.put("BAR", (String[])[ActionKeys.VIEW, ActionKeys.UPDATE]);
179     
180 setPermissions(userExpandos, User.class.getName(), expandosPermissions);
181 setPermissions(roleExpandos, Role.class.getName(), expandosPermissions);

Tags: liferay  liferay-portal 

Share on: Twitter  Facebook  Google+ 


Previous post: Liferay 6.2 on Apache Tomcat 7: How to fix ClassNotFoundException

Next post: Liferay Theme localization without Hook

Related posts: