001    /**
002     * Copyright (c) 2000-2012 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                    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 MDRRule getRule(long ruleId)
129                    throws PortalException, SystemException {
130    
131                    return mdrRulePersistence.findByPrimaryKey(ruleId);
132            }
133    
134            public List<MDRRule> getRules(long ruleGroupId) throws SystemException {
135                    return mdrRulePersistence.findByRuleGroupId(ruleGroupId);
136            }
137    
138            public List<MDRRule> getRules(long ruleGroupId, int start, int end)
139                    throws SystemException {
140    
141                    return mdrRulePersistence.findByRuleGroupId(ruleGroupId, start, end);
142            }
143    
144            public int getRulesCount(long ruleGroupId) throws SystemException {
145                    return mdrRulePersistence.countByRuleGroupId(ruleGroupId);
146            }
147    
148            public MDRRule updateRule(
149                            long ruleId, Map<Locale, String> nameMap,
150                            Map<Locale, String> descriptionMap, String type,
151                            String typeSettings, ServiceContext serviceContext)
152                    throws PortalException, SystemException {
153    
154                    MDRRule rule = mdrRulePersistence.findByPrimaryKey(ruleId);
155    
156                    rule.setModifiedDate(serviceContext.getModifiedDate(null));
157                    rule.setNameMap(nameMap);
158                    rule.setDescriptionMap(descriptionMap);
159                    rule.setType(type);
160                    rule.setTypeSettings(typeSettings);
161    
162                    mdrRulePersistence.update(rule, false);
163    
164                    return rule;
165            }
166    
167            public MDRRule updateRule(
168                            long ruleId, Map<Locale, String> nameMap,
169                            Map<Locale, String> descriptionMap, String type,
170                            UnicodeProperties typeSettingsProperties,
171                            ServiceContext serviceContext)
172                    throws PortalException, SystemException {
173    
174                    return updateRule(
175                            ruleId, nameMap, descriptionMap, type,
176                            typeSettingsProperties.toString(), serviceContext);
177            }
178    
179    }