001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.security.permission;
016    
017    import com.liferay.portal.model.BaseModelListener;
018    import com.liferay.portal.model.ResourceBlock;
019    import com.liferay.portal.model.impl.ResourceBlockModelImpl;
020    
021    /**
022     * @author Preston Crary
023     */
024    public class ResourceBlockModelListener
025            extends BaseModelListener<ResourceBlock> {
026    
027            @Override
028            public void onAfterCreate(ResourceBlock resourceBlock) {
029                    _clearCache(resourceBlock);
030            }
031    
032            @Override
033            public void onAfterRemove(ResourceBlock resourceBlock) {
034                    _clearCache(resourceBlock);
035            }
036    
037            @Override
038            public void onAfterUpdate(ResourceBlock resourceBlock) {
039                    _clearCache(resourceBlock);
040            }
041    
042            @Override
043            public void onBeforeUpdate(ResourceBlock resourceBlock) {
044                    ResourceBlockModelImpl resourceBlockModelImpl =
045                            (ResourceBlockModelImpl)resourceBlock;
046    
047                    long columnBitmask = resourceBlockModelImpl.getColumnBitmask();
048    
049                    if ((columnBitmask & _CLEAR_ON_BEFORE_BITMASK) != 0) {
050                            PermissionCacheUtil.clearResourceBlockCache(
051                                    resourceBlockModelImpl.getOriginalCompanyId(),
052                                    resourceBlockModelImpl.getOriginalGroupId(),
053                                    resourceBlockModelImpl.getOriginalName());
054                    }
055            }
056    
057            private void _clearCache(ResourceBlock resourceBlock) {
058                    if (resourceBlock != null) {
059                            PermissionCacheUtil.clearResourceBlockCache(
060                                    resourceBlock.getCompanyId(), resourceBlock.getGroupId(),
061                                    resourceBlock.getName());
062                    }
063            }
064    
065            private static final long _CLEAR_ON_BEFORE_BITMASK =
066                    ResourceBlockModelImpl.COMPANYID_COLUMN_BITMASK |
067                            ResourceBlockModelImpl.GROUPID_COLUMN_BITMASK |
068                                    ResourceBlockModelImpl.NAME_COLUMN_BITMASK;
069    
070    }