Example usage for com.liferay.portal.kernel.service ResourceLocalServiceUtil addResources

List of usage examples for com.liferay.portal.kernel.service ResourceLocalServiceUtil addResources

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.service ResourceLocalServiceUtil addResources.

Prototype

public static void addResources(long companyId, long groupId, long userId, String name, String primKey,
        boolean portletActions, boolean addGroupPermissions, boolean addGuestPermissions)
        throws com.liferay.portal.kernel.exception.PortalException 

Source Link

Document

Adds resources for the entity with the name and primary key string, always creating a resource at the individual scope and only creating resources at the group, group template, and company scope if such resources don't already exist.

Usage

From source file:com.liferay.layout.internal.upgrade.v1_0_0.UpgradeLayoutPermissions.java

License:Open Source License

@Override
protected void doUpgrade() throws Exception {
    StringBundler sb = new StringBundler(9);

    sb.append("select Layout.companyId, Layout.plid, Layout.privateLayout");
    sb.append(", Layout.groupId, Layout.userId from Layout left join ");
    sb.append("ResourcePermission on (ResourcePermission.companyId = ");
    sb.append("Layout.companyId and ResourcePermission.name = '");
    sb.append(Layout.class.getName());
    sb.append("' and ResourcePermission.scope = ");
    sb.append(ResourceConstants.SCOPE_INDIVIDUAL);
    sb.append(" and ResourcePermission.primKeyId = Layout.plid) where ");
    sb.append("ResourcePermission.resourcePermissionId is null");

    String sql = SQLTransformer.transform(sb.toString());

    try (LoggingTimer loggingTimer = new LoggingTimer();
            PreparedStatement ps = connection.prepareStatement(sql);
            ResultSet rs = ps.executeQuery()) {

        while (rs.next()) {
            long companyId = rs.getLong("companyId");
            long groupId = rs.getLong("groupId");
            long plid = rs.getLong("plid");
            boolean privateLayout = rs.getBoolean("privateLayout");
            long userId = rs.getLong("userId");

            boolean addGroupPermission = true;
            boolean addGuestPermission = true;

            if (privateLayout) {
                addGuestPermission = false;

                Group group = GroupLocalServiceUtil.getGroup(groupId);

                if (group.isUser() || group.isUserGroup()) {
                    addGroupPermission = false;
                }//from  w w w . ja  v a  2s.c  o  m
            }

            ResourceLocalServiceUtil.addResources(companyId, groupId, userId, Layout.class.getName(), plid,
                    false, addGroupPermission, addGuestPermission);
        }
    }
}