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.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.util.OrderByComparator;
021    import com.liferay.portal.model.User;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portal.util.PortalUtil;
024    import com.liferay.portlet.mobiledevicerules.DuplicateRuleGroupInstanceException;
025    import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroupInstance;
026    import com.liferay.portlet.mobiledevicerules.service.base.MDRRuleGroupInstanceLocalServiceBaseImpl;
027    import com.liferay.portlet.mobiledevicerules.util.RuleGroupInstancePriorityComparator;
028    
029    import java.util.Date;
030    import java.util.List;
031    
032    /**
033     * @author Edward C. Han
034     */
035    public class MDRRuleGroupInstanceLocalServiceImpl
036            extends MDRRuleGroupInstanceLocalServiceBaseImpl {
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                    User user = userPersistence.findByPrimaryKey(
045                            serviceContext.getUserId());
046                    long classNameId = PortalUtil.getClassNameId(className);
047                    Date now = new Date();
048    
049                    validate(classNameId, classPK, ruleGroupId);
050    
051                    long ruleGroupInstanceId = counterLocalService.increment();
052    
053                    MDRRuleGroupInstance ruleGroupInstance =
054                            mdrRuleGroupInstanceLocalService.createMDRRuleGroupInstance(
055                                    ruleGroupInstanceId);
056    
057                    ruleGroupInstance.setUuid(serviceContext.getUuid());
058                    ruleGroupInstance.setGroupId(groupId);
059                    ruleGroupInstance.setCompanyId(serviceContext.getCompanyId());
060                    ruleGroupInstance.setCreateDate(serviceContext.getCreateDate(now));
061                    ruleGroupInstance.setModifiedDate(serviceContext.getModifiedDate(now));
062                    ruleGroupInstance.setUserId(serviceContext.getUserId());
063                    ruleGroupInstance.setUserName(user.getFullName());
064                    ruleGroupInstance.setClassNameId(classNameId);
065                    ruleGroupInstance.setClassPK(classPK);
066                    ruleGroupInstance.setRuleGroupId(ruleGroupId);
067                    ruleGroupInstance.setPriority(priority);
068    
069                    return updateMDRRuleGroupInstance(ruleGroupInstance, false);
070            }
071    
072            @Override
073            public MDRRuleGroupInstance addRuleGroupInstance(
074                            long groupId, String className, long classPK, long ruleGroupId,
075                            ServiceContext serviceContext)
076                    throws PortalException, SystemException {
077    
078                    List<MDRRuleGroupInstance> ruleGroupInstances = getRuleGroupInstances(
079                            className, classPK, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
080                            new RuleGroupInstancePriorityComparator());
081    
082                    int priority = 0;
083    
084                    if (!ruleGroupInstances.isEmpty()) {
085                            MDRRuleGroupInstance highestPriorityRuleGroupInstance =
086                                    ruleGroupInstances.get(ruleGroupInstances.size() - 1);
087    
088                            priority = highestPriorityRuleGroupInstance.getPriority() + 1;
089                    }
090    
091                    return addRuleGroupInstance(
092                            groupId, className, classPK, ruleGroupId, priority, serviceContext);
093            }
094    
095            @Override
096            public void deleteGroupRuleGroupInstances(long groupId)
097                    throws SystemException {
098    
099                    List<MDRRuleGroupInstance> ruleGroupInstances =
100                            mdrRuleGroupInstancePersistence.findByGroupId(groupId);
101    
102                    for (MDRRuleGroupInstance ruleGroupInstance : ruleGroupInstances) {
103                            deleteRuleGroupInstance(ruleGroupInstance);
104                    }
105            }
106    
107            @Override
108            public void deleteRuleGroupInstance(long ruleGroupInstanceId)
109                    throws SystemException {
110    
111                    MDRRuleGroupInstance ruleGroupInstance =
112                            mdrRuleGroupInstancePersistence.fetchByPrimaryKey(
113                                    ruleGroupInstanceId);
114    
115                    deleteRuleGroupInstance(ruleGroupInstance);
116            }
117    
118            @Override
119            public void deleteRuleGroupInstance(MDRRuleGroupInstance ruleGroupInstance)
120                    throws SystemException {
121    
122                    // Rule group instance
123    
124                    mdrRuleGroupInstancePersistence.remove(ruleGroupInstance);
125    
126                    // Rule actions
127    
128                    mdrActionLocalService.deleteActions(
129                            ruleGroupInstance.getRuleGroupInstanceId());
130            }
131    
132            @Override
133            public void deleteRuleGroupInstances(long ruleGroupId)
134                    throws SystemException {
135    
136                    List<MDRRuleGroupInstance> ruleGroupInstances =
137                            mdrRuleGroupInstancePersistence.findByRuleGroupId(ruleGroupId);
138    
139                    for (MDRRuleGroupInstance ruleGroupInstance : ruleGroupInstances) {
140                            deleteRuleGroupInstance(ruleGroupInstance);
141                    }
142            }
143    
144            @Override
145            public MDRRuleGroupInstance fetchRuleGroupInstance(long ruleGroupInstanceId)
146                    throws SystemException {
147    
148                    return mdrRuleGroupInstancePersistence.fetchByPrimaryKey(
149                            ruleGroupInstanceId);
150            }
151    
152            @Override
153            public MDRRuleGroupInstance fetchRuleGroupInstance(
154                            String className, long classPK, long ruleGroupId)
155                    throws SystemException {
156    
157                    long classNameId = PortalUtil.getClassNameId(className);
158    
159                    return mdrRuleGroupInstancePersistence.fetchByC_C_R(
160                            classNameId, classPK, ruleGroupId);
161            }
162    
163            @Override
164            public MDRRuleGroupInstance getRuleGroupInstance(long ruleGroupInstanceId)
165                    throws PortalException, SystemException {
166    
167                    return mdrRuleGroupInstancePersistence.findByPrimaryKey(
168                            ruleGroupInstanceId);
169            }
170    
171            @Override
172            public MDRRuleGroupInstance getRuleGroupInstance(
173                            String className, long classPK, long ruleGroupId)
174                    throws PortalException, SystemException {
175    
176                    long classNameId = PortalUtil.getClassNameId(className);
177    
178                    return mdrRuleGroupInstancePersistence.findByC_C_R(
179                            classNameId, classPK, ruleGroupId);
180            }
181    
182            @Override
183            public List<MDRRuleGroupInstance> getRuleGroupInstances(long ruleGroupId)
184                    throws SystemException {
185    
186                    return mdrRuleGroupInstancePersistence.findByRuleGroupId(ruleGroupId);
187            }
188    
189            @Override
190            public List<MDRRuleGroupInstance> getRuleGroupInstances(
191                            long ruleGroupId, int start, int end)
192                    throws SystemException {
193    
194                    return mdrRuleGroupInstancePersistence.findByRuleGroupId(
195                            ruleGroupId, start, end);
196            }
197    
198            @Override
199            public List<MDRRuleGroupInstance> getRuleGroupInstances(
200                            String className, long classPK)
201                    throws SystemException {
202    
203                    long classNameId = PortalUtil.getClassNameId(className);
204    
205                    return mdrRuleGroupInstancePersistence.findByC_C(classNameId, classPK);
206            }
207    
208            @Override
209            public List<MDRRuleGroupInstance> getRuleGroupInstances(
210                            String className, long classPK, int start, int end,
211                            OrderByComparator orderByComparator)
212                    throws SystemException {
213    
214                    long classNameId = PortalUtil.getClassNameId(className);
215    
216                    return mdrRuleGroupInstancePersistence.findByC_C(
217                            classNameId, classPK, start, end, orderByComparator);
218            }
219    
220            @Override
221            public int getRuleGroupInstancesCount(long ruleGroupId)
222                    throws SystemException {
223    
224                    return mdrRuleGroupInstancePersistence.countByRuleGroupId(ruleGroupId);
225            }
226    
227            @Override
228            public int getRuleGroupInstancesCount(String className, long classPK)
229                    throws SystemException {
230    
231                    long classNameId = PortalUtil.getClassNameId(className);
232    
233                    return mdrRuleGroupInstancePersistence.countByC_C(classNameId, classPK);
234            }
235    
236            @Override
237            public MDRRuleGroupInstance updateRuleGroupInstance(
238                            long ruleGroupInstanceId, int priority)
239                    throws PortalException, SystemException {
240    
241                    MDRRuleGroupInstance ruleGroupInstance =
242                            mdrRuleGroupInstancePersistence.findByPrimaryKey(
243                                    ruleGroupInstanceId);
244    
245                    ruleGroupInstance.setPriority(priority);
246    
247                    mdrRuleGroupInstancePersistence.update(ruleGroupInstance, false);
248    
249                    return ruleGroupInstance;
250            }
251    
252            protected void validate(long classNameId, long classPK, long ruleGroupId)
253                    throws PortalException, SystemException {
254    
255                    MDRRuleGroupInstance ruleGroupInstance =
256                            mdrRuleGroupInstancePersistence.fetchByC_C_R(
257                                    classNameId, classPK, ruleGroupId);
258    
259                    if (ruleGroupInstance != null) {
260                            throw new DuplicateRuleGroupInstanceException();
261                    }
262    
263                    mdrRuleGroupLocalService.getMDRRuleGroup(ruleGroupId);
264            }
265    
266    }