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.systemevent.SystemEvent;
019    import com.liferay.portal.kernel.util.UnicodeProperties;
020    import com.liferay.portal.model.SystemEventConstants;
021    import com.liferay.portal.model.User;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portlet.mobiledevicerules.model.MDRRule;
024    import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroup;
025    import com.liferay.portlet.mobiledevicerules.service.base.MDRRuleLocalServiceBaseImpl;
026    
027    import java.util.Date;
028    import java.util.List;
029    import java.util.Locale;
030    import java.util.Map;
031    
032    /**
033     * @author Edward C. Han
034     */
035    public class MDRRuleLocalServiceImpl extends MDRRuleLocalServiceBaseImpl {
036    
037            @Override
038            public MDRRule addRule(
039                            long ruleGroupId, Map<Locale, String> nameMap,
040                            Map<Locale, String> descriptionMap, String type,
041                            String typeSettings, ServiceContext serviceContext)
042                    throws PortalException {
043    
044                    User user = userPersistence.findByPrimaryKey(
045                            serviceContext.getUserId());
046                    MDRRuleGroup ruleGroup = mdrRuleGroupPersistence.findByPrimaryKey(
047                            ruleGroupId);
048    
049                    long ruleId = counterLocalService.increment();
050    
051                    MDRRule rule = mdrRulePersistence.create(ruleId);
052    
053                    rule.setUuid(serviceContext.getUuid());
054                    rule.setGroupId(ruleGroup.getGroupId());
055                    rule.setCompanyId(serviceContext.getCompanyId());
056                    rule.setUserId(user.getUserId());
057                    rule.setUserName(user.getFullName());
058                    rule.setRuleGroupId(ruleGroupId);
059                    rule.setNameMap(nameMap);
060                    rule.setDescriptionMap(descriptionMap);
061                    rule.setType(type);
062                    rule.setTypeSettings(typeSettings);
063    
064                    rule = updateMDRRule(rule);
065    
066                    ruleGroup.setModifiedDate(new Date());
067    
068                    mdrRuleGroupPersistence.update(ruleGroup);
069    
070                    return rule;
071            }
072    
073            @Override
074            public MDRRule addRule(
075                            long ruleGroupId, Map<Locale, String> nameMap,
076                            Map<Locale, String> descriptionMap, String type,
077                            UnicodeProperties typeSettingsProperties,
078                            ServiceContext serviceContext)
079                    throws PortalException {
080    
081                    return addRule(
082                            ruleGroupId, nameMap, descriptionMap, type,
083                            typeSettingsProperties.toString(), serviceContext);
084            }
085    
086            @Override
087            public MDRRule copyRule(
088                            long ruleId, long ruleGroupId, ServiceContext serviceContext)
089                    throws PortalException {
090    
091                    MDRRule rule = mdrRulePersistence.findByPrimaryKey(ruleId);
092    
093                    return copyRule(rule, ruleGroupId, serviceContext);
094            }
095    
096            @Override
097            public MDRRule copyRule(
098                            MDRRule rule, long ruleGroupId, ServiceContext serviceContext)
099                    throws PortalException {
100    
101                    MDRRuleGroup ruleGroup = mdrRuleGroupPersistence.findByPrimaryKey(
102                            ruleGroupId);
103    
104                    MDRRule newRule = addRule(
105                            ruleGroup.getRuleGroupId(), rule.getNameMap(),
106                            rule.getDescriptionMap(), rule.getType(), rule.getTypeSettings(),
107                            serviceContext);
108    
109                    return newRule;
110            }
111    
112            @Override
113            public void deleteRule(long ruleId) {
114                    MDRRule rule = mdrRulePersistence.fetchByPrimaryKey(ruleId);
115    
116                    if (rule != null) {
117                            mdrRuleLocalService.deleteRule(rule);
118                    }
119            }
120    
121            @Override
122            @SystemEvent(type = SystemEventConstants.TYPE_DELETE)
123            public void deleteRule(MDRRule rule) {
124                    mdrRulePersistence.remove(rule);
125    
126                    MDRRuleGroup ruleGroup = mdrRuleGroupPersistence.fetchByPrimaryKey(
127                            rule.getRuleGroupId());
128    
129                    if (ruleGroup != null) {
130                            ruleGroup.setModifiedDate(new Date());
131    
132                            mdrRuleGroupPersistence.update(ruleGroup);
133                    }
134            }
135    
136            @Override
137            public void deleteRules(long ruleGroupId) {
138                    List<MDRRule> rules = mdrRulePersistence.findByRuleGroupId(ruleGroupId);
139    
140                    for (MDRRule rule : rules) {
141                            mdrRuleLocalService.deleteRule(rule);
142                    }
143            }
144    
145            @Override
146            public MDRRule fetchRule(long ruleId) {
147                    return mdrRulePersistence.fetchByPrimaryKey(ruleId);
148            }
149    
150            @Override
151            public MDRRule getRule(long ruleId) throws PortalException {
152                    return mdrRulePersistence.findByPrimaryKey(ruleId);
153            }
154    
155            @Override
156            public List<MDRRule> getRules(long ruleGroupId) {
157                    return mdrRulePersistence.findByRuleGroupId(ruleGroupId);
158            }
159    
160            @Override
161            public List<MDRRule> getRules(long ruleGroupId, int start, int end) {
162                    return mdrRulePersistence.findByRuleGroupId(ruleGroupId, start, end);
163            }
164    
165            @Override
166            public int getRulesCount(long ruleGroupId) {
167                    return mdrRulePersistence.countByRuleGroupId(ruleGroupId);
168            }
169    
170            @Override
171            public MDRRule updateRule(
172                            long ruleId, Map<Locale, String> nameMap,
173                            Map<Locale, String> descriptionMap, String type,
174                            String typeSettings, ServiceContext serviceContext)
175                    throws PortalException {
176    
177                    MDRRule rule = mdrRulePersistence.findByPrimaryKey(ruleId);
178    
179                    rule.setNameMap(nameMap);
180                    rule.setDescriptionMap(descriptionMap);
181                    rule.setType(type);
182                    rule.setTypeSettings(typeSettings);
183    
184                    mdrRulePersistence.update(rule);
185    
186                    MDRRuleGroup ruleGroup = mdrRuleGroupPersistence.findByPrimaryKey(
187                            rule.getRuleGroupId());
188    
189                    ruleGroup.setModifiedDate(serviceContext.getModifiedDate(null));
190    
191                    mdrRuleGroupPersistence.update(ruleGroup);
192    
193                    return rule;
194            }
195    
196            @Override
197            public MDRRule updateRule(
198                            long ruleId, Map<Locale, String> nameMap,
199                            Map<Locale, String> descriptionMap, String type,
200                            UnicodeProperties typeSettingsProperties,
201                            ServiceContext serviceContext)
202                    throws PortalException {
203    
204                    return updateRule(
205                            ruleId, nameMap, descriptionMap, type,
206                            typeSettingsProperties.toString(), serviceContext);
207            }
208    
209    }