001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                    return updateMDRRule(rule, false);
066            }
067    
068            public MDRRule addRule(
069                            long ruleGroupId, Map<Locale, String> nameMap,
070                            Map<Locale, String> descriptionMap, String type,
071                            UnicodeProperties typeSettingsProperties,
072                            ServiceContext serviceContext)
073                    throws PortalException, SystemException {
074    
075                    return addRule(
076                            ruleGroupId, nameMap, descriptionMap, type,
077                            typeSettingsProperties.toString(), serviceContext);
078            }
079    
080            public MDRRule copyRule(
081                            long ruleId, long ruleGroupId, ServiceContext serviceContext)
082                    throws PortalException, SystemException {
083    
084                    MDRRule rule = mdrRulePersistence.findByPrimaryKey(ruleId);
085    
086                    return copyRule(rule, ruleGroupId, serviceContext);
087            }
088    
089            public MDRRule copyRule(
090                            MDRRule rule, long ruleGroupId, ServiceContext serviceContext)
091                    throws PortalException, SystemException {
092    
093                    MDRRuleGroup ruleGroup = mdrRuleGroupPersistence.findByPrimaryKey(
094                            ruleGroupId);
095    
096                    MDRRule newRule = addRule(
097                            ruleGroup.getRuleGroupId(), rule.getNameMap(),
098                            rule.getDescriptionMap(), rule.getType(), rule.getTypeSettings(),
099                            serviceContext);
100    
101                    return newRule;
102            }
103    
104            public void deleteRule(long ruleId) throws SystemException {
105                    MDRRule rule = mdrRulePersistence.fetchByPrimaryKey(ruleId);
106    
107                    if (rule != null) {
108                            deleteRule(rule);
109                    }
110            }
111    
112            public void deleteRule(MDRRule rule) throws SystemException {
113                    mdrRulePersistence.remove(rule);
114            }
115    
116            public void deleteRules(long ruleGroupId) throws SystemException {
117                    List<MDRRule> rules = mdrRulePersistence.findByRuleGroupId(ruleGroupId);
118    
119                    for (MDRRule rule : rules) {
120                            deleteRule(rule);
121                    }
122            }
123    
124            public MDRRule fetchRule(long ruleId) throws SystemException {
125                    return mdrRulePersistence.fetchByPrimaryKey(ruleId);
126            }
127    
128            public List<MDRRule> getRules(long ruleGroupId) throws SystemException {
129                    return mdrRulePersistence.findByRuleGroupId(ruleGroupId);
130            }
131    
132            public List<MDRRule> getRules(long ruleGroupId, int start, int end)
133                    throws SystemException {
134    
135                    return mdrRulePersistence.findByRuleGroupId(ruleGroupId, start, end);
136            }
137    
138            public int getRulesCount(long ruleGroupId) throws SystemException {
139                    return mdrRulePersistence.countByRuleGroupId(ruleGroupId);
140            }
141    
142            public MDRRule updateRule(
143                            long ruleId, Map<Locale, String> nameMap,
144                            Map<Locale, String> descriptionMap, String type,
145                            String typeSettings, ServiceContext serviceContext)
146                    throws PortalException, SystemException {
147    
148                    MDRRule rule = mdrRulePersistence.findByPrimaryKey(ruleId);
149    
150                    rule.setModifiedDate(serviceContext.getModifiedDate(null));
151                    rule.setNameMap(nameMap);
152                    rule.setDescriptionMap(descriptionMap);
153                    rule.setType(type);
154                    rule.setTypeSettings(typeSettings);
155    
156                    mdrRulePersistence.update(rule, false);
157    
158                    return rule;
159            }
160    
161            public MDRRule updateRule(
162                            long ruleId, Map<Locale, String> nameMap,
163                            Map<Locale, String> descriptionMap, String type,
164                            UnicodeProperties typeSettingsProperties,
165                            ServiceContext serviceContext)
166                    throws PortalException, SystemException {
167    
168                    return updateRule(
169                            ruleId, nameMap, descriptionMap, type,
170                            typeSettingsProperties.toString(), serviceContext);
171            }
172    
173    }