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