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