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