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