001    /**
002     * Copyright (c) 2000-present 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.portlet.mobiledevicerules.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.util.OrderByComparator;
019    import com.liferay.portal.model.Layout;
020    import com.liferay.portal.model.LayoutSet;
021    import com.liferay.portal.security.permission.ActionKeys;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroupInstance;
024    import com.liferay.portlet.mobiledevicerules.service.base.MDRRuleGroupInstanceServiceBaseImpl;
025    import com.liferay.portlet.mobiledevicerules.service.permission.MDRPermissionUtil;
026    import com.liferay.portlet.mobiledevicerules.service.permission.MDRRuleGroupInstancePermissionUtil;
027    
028    import java.util.List;
029    
030    /**
031     * @author Edward C. Han
032     */
033    public class MDRRuleGroupInstanceServiceImpl
034            extends MDRRuleGroupInstanceServiceBaseImpl {
035    
036            @Override
037            public MDRRuleGroupInstance addRuleGroupInstance(
038                            long groupId, String className, long classPK, long ruleGroupId,
039                            int priority, ServiceContext serviceContext)
040                    throws PortalException {
041    
042                    MDRPermissionUtil.check(
043                            getPermissionChecker(), groupId,
044                            ActionKeys.ADD_RULE_GROUP_INSTANCE);
045    
046                    return mdrRuleGroupInstanceLocalService.addRuleGroupInstance(
047                            groupId, className, classPK, ruleGroupId, priority, serviceContext);
048            }
049    
050            @Override
051            public MDRRuleGroupInstance addRuleGroupInstance(
052                            long groupId, String className, long classPK, long ruleGroupId,
053                            ServiceContext serviceContext)
054                    throws PortalException {
055    
056                    MDRPermissionUtil.check(
057                            getPermissionChecker(), groupId,
058                            ActionKeys.ADD_RULE_GROUP_INSTANCE);
059    
060                    return mdrRuleGroupInstanceLocalService.addRuleGroupInstance(
061                            groupId, className, classPK, ruleGroupId, serviceContext);
062            }
063    
064            @Override
065            public void deleteRuleGroupInstance(long ruleGroupInstanceId)
066                    throws PortalException {
067    
068                    MDRRuleGroupInstance ruleGroupInstance =
069                            mdrRuleGroupInstancePersistence.findByPrimaryKey(
070                                    ruleGroupInstanceId);
071    
072                    MDRRuleGroupInstancePermissionUtil.check(
073                            getPermissionChecker(), ruleGroupInstance, ActionKeys.DELETE);
074    
075                    mdrRuleGroupInstanceLocalService.deleteRuleGroupInstance(
076                            ruleGroupInstance);
077            }
078    
079            @Override
080            public List<MDRRuleGroupInstance> getRuleGroupInstances(
081                    String className, long classPK, int start, int end,
082                    OrderByComparator<MDRRuleGroupInstance> orderByComparator) {
083    
084                    long groupId = getGroupId(className, classPK);
085                    long classNameId = classNameLocalService.getClassNameId(className);
086    
087                    return mdrRuleGroupInstancePersistence.filterFindByG_C_C(
088                            groupId, classNameId, classPK, start, end, orderByComparator);
089            }
090    
091            @Override
092            public int getRuleGroupInstancesCount(String className, long classPK) {
093                    long groupId = getGroupId(className, classPK);
094                    long classNameId = classNameLocalService.getClassNameId(className);
095    
096                    return mdrRuleGroupInstancePersistence.filterCountByG_C_C(
097                            groupId, classNameId, classPK);
098            }
099    
100            @Override
101            public MDRRuleGroupInstance updateRuleGroupInstance(
102                            long ruleGroupInstanceId, int priority)
103                    throws PortalException {
104    
105                    MDRRuleGroupInstance ruleGroupInstance =
106                            mdrRuleGroupInstancePersistence.findByPrimaryKey(
107                                    ruleGroupInstanceId);
108    
109                    MDRRuleGroupInstancePermissionUtil.check(
110                            getPermissionChecker(), ruleGroupInstance.getRuleGroupInstanceId(),
111                            ActionKeys.UPDATE);
112    
113                    return mdrRuleGroupInstanceLocalService.updateRuleGroupInstance(
114                            ruleGroupInstanceId, priority);
115            }
116    
117            protected long getGroupId(String className, long classPK) {
118                    long groupId = 0;
119    
120                    if (className.equals(Layout.class.getName())) {
121                            Layout layout = layoutPersistence.fetchByPrimaryKey(classPK);
122    
123                            if (layout != null) {
124                                    groupId = layout.getGroupId();
125                            }
126                    }
127                    else if (className.equals(LayoutSet.class.getName())) {
128                            LayoutSet layoutSet = layoutSetPersistence.fetchByPrimaryKey(
129                                    classPK);
130    
131                            if (layoutSet != null) {
132                                    groupId = layoutSet.getGroupId();
133                            }
134                    }
135    
136                    return groupId;
137            }
138    
139    }