Example usage for com.liferay.portal.kernel.model PermissionedModel PermissionedModel

List of usage examples for com.liferay.portal.kernel.model PermissionedModel PermissionedModel

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.model PermissionedModel PermissionedModel.

Prototype

PermissionedModel

Source Link

Usage

From source file:com.liferay.calendar.internal.upgrade.v1_0_6.UpgradeResourcePermission.java

License:Open Source License

protected void upgradeGuestResourceBlockPermissions() throws Exception {
    List<String> unsupportedActionIds = getCalendarResourceUnsupportedActionIds();

    if (unsupportedActionIds.isEmpty()) {
        return;//from   w  ww .j a  v a 2s  .  co  m
    }

    StringBundler sb = new StringBundler(2);

    sb.append("select companyId, groupId, calendarResourceId, ");
    sb.append("resourceBlockId from CalendarResource");

    try (PreparedStatement ps = connection.prepareStatement(sb.toString()); ResultSet rs = ps.executeQuery()) {

        while (rs.next()) {
            long companyId = rs.getLong(1);
            final long calendarResourceId = rs.getLong(3);
            final long resourceBlockId = rs.getLong(4);

            Role guestRole = _roleLocalService.getRole(companyId, RoleConstants.GUEST);

            PermissionedModel permissionedModel = new PermissionedModel() {

                @Override
                public long getResourceBlockId() {
                    return resourceBlockId;
                }

                @Override
                public void persist() {
                    if (_newResourceBlockId == -1) {
                        return;
                    }

                    StringBundler updateSB = new StringBundler(3);

                    updateSB.append("update CalendarResource set ");
                    updateSB.append("resourceBlockId = ? where ");
                    updateSB.append("calendarResourceId = ?");

                    try (PreparedStatement ps = connection.prepareStatement(updateSB.toString())) {

                        ps.setLong(1, _newResourceBlockId);
                        ps.setLong(2, calendarResourceId);

                        ps.execute();
                    } catch (SQLException sqle) {
                        throw new SystemException(sqle);
                    }
                }

                @Override
                public void setResourceBlockId(long resourceBlockId) {
                    _newResourceBlockId = resourceBlockId;
                }

                private long _newResourceBlockId = -1;

            };

            for (String unsupportedActionId : unsupportedActionIds) {
                _resourcePermissionLocalService.removeResourcePermission(companyId, _CALENDAR_RESOURCE_NAME,
                        ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(permissionedModel),
                        guestRole.getRoleId(), unsupportedActionId);
            }
        }
    }
}