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