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.language.LanguageUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
023    import com.liferay.portal.model.Group;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.util.PropsValues;
027    import com.liferay.portlet.mobiledevicerules.model.MDRRule;
028    import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroup;
029    import com.liferay.portlet.mobiledevicerules.service.base.MDRRuleGroupLocalServiceBaseImpl;
030    
031    import java.util.Date;
032    import java.util.List;
033    import java.util.Locale;
034    import java.util.Map;
035    
036    /**
037     * @author Edward C. Han
038     */
039    public class MDRRuleGroupLocalServiceImpl
040            extends MDRRuleGroupLocalServiceBaseImpl {
041    
042            public MDRRuleGroup addRuleGroup(
043                            long groupId, Map<Locale, String> nameMap,
044                            Map<Locale, String> descriptionMap, ServiceContext serviceContext)
045                    throws PortalException, SystemException {
046    
047                    User user = userPersistence.findByPrimaryKey(
048                            serviceContext.getUserId());
049                    Date now = new Date();
050    
051                    long ruleGroupId = counterLocalService.increment();
052    
053                    MDRRuleGroup ruleGroup = createMDRRuleGroup(ruleGroupId);
054    
055                    ruleGroup.setUuid(serviceContext.getUuid());
056                    ruleGroup.setGroupId(groupId);
057                    ruleGroup.setCompanyId(serviceContext.getCompanyId());
058                    ruleGroup.setCreateDate(serviceContext.getCreateDate(now));
059                    ruleGroup.setModifiedDate(serviceContext.getModifiedDate(now));
060                    ruleGroup.setUserId(user.getUserId());
061                    ruleGroup.setUserName(user.getFullName());
062                    ruleGroup.setNameMap(nameMap);
063                    ruleGroup.setDescriptionMap(descriptionMap);
064    
065                    return updateMDRRuleGroup(ruleGroup, false);
066            }
067    
068            public MDRRuleGroup copyRuleGroup(
069                            long ruleGroupId, long groupId, ServiceContext serviceContext)
070                    throws PortalException, SystemException {
071    
072                    MDRRuleGroup ruleGroup = mdrRuleGroupPersistence.findByPrimaryKey(
073                            ruleGroupId);
074    
075                    return copyRuleGroup(ruleGroup, groupId, serviceContext);
076            }
077    
078            public MDRRuleGroup copyRuleGroup(
079                            MDRRuleGroup ruleGroup, long groupId, ServiceContext serviceContext)
080                    throws PortalException, SystemException {
081    
082                    Group group = groupPersistence.findByPrimaryKey(groupId);
083    
084                    Map<Locale, String> nameMap = ruleGroup.getNameMap();
085    
086                    for (Map.Entry<Locale, String> entry : nameMap.entrySet()) {
087                            Locale locale = entry.getKey();
088                            String name = entry.getValue();
089    
090                            if (Validator.isNull(name)) {
091                                    continue;
092                            }
093    
094                            String postfix = LanguageUtil.get(
095                                    locale,
096                                    PropsValues.MOBILE_DEVICE_RULES_RULE_GROUP_COPY_POSTFIX);
097    
098                            nameMap.put(locale, name.concat(StringPool.SPACE).concat(postfix));
099                    }
100    
101                    MDRRuleGroup newRuleGroup = addRuleGroup(
102                            group.getGroupId(), nameMap, ruleGroup.getDescriptionMap(),
103                            serviceContext);
104    
105                    List<MDRRule> rules = mdrRulePersistence.findByRuleGroupId(
106                            ruleGroup.getRuleGroupId());
107    
108                    for (MDRRule rule : rules) {
109                            serviceContext.setUuid(PortalUUIDUtil.generate());
110    
111                            mdrRuleLocalService.copyRule(
112                                    rule, newRuleGroup.getRuleGroupId(), serviceContext);
113                    }
114    
115                    return newRuleGroup;
116            }
117    
118            public void deleteRuleGroup(long ruleGroupId) throws SystemException {
119                    MDRRuleGroup ruleGroup = mdrRuleGroupPersistence.fetchByPrimaryKey(
120                            ruleGroupId);
121    
122                    if (ruleGroup != null) {
123                            deleteRuleGroup(ruleGroup);
124                    }
125            }
126    
127            public void deleteRuleGroup(MDRRuleGroup ruleGroup) throws SystemException {
128    
129                    // Rule group
130    
131                    mdrRuleGroupPersistence.remove(ruleGroup);
132    
133                    // Rules
134    
135                    mdrRuleLocalService.deleteRules(ruleGroup.getRuleGroupId());
136    
137                    //        Rule group instances
138    
139                    mdrRuleGroupInstanceLocalService.deleteRuleGroupInstances(
140                            ruleGroup.getRuleGroupId());
141            }
142    
143            public void deleteRuleGroups(long groupId) throws SystemException {
144                    List<MDRRuleGroup> ruleGroups = mdrRuleGroupPersistence.findByGroupId(
145                            groupId);
146    
147                    for (MDRRuleGroup ruleGroup : ruleGroups) {
148                            deleteRuleGroup(ruleGroup);
149                    }
150            }
151    
152            public MDRRuleGroup fetchRuleGroup(long ruleGroupId)
153                    throws SystemException {
154    
155                    return mdrRuleGroupPersistence.fetchByPrimaryKey(ruleGroupId);
156            }
157    
158            public MDRRuleGroup getRuleGroup(long ruleGroupId)
159                    throws PortalException, SystemException {
160    
161                    return mdrRuleGroupPersistence.findByPrimaryKey(ruleGroupId);
162            }
163    
164            public List<MDRRuleGroup> getRuleGroups(long groupId)
165                    throws SystemException {
166    
167                    return mdrRuleGroupPersistence.findByGroupId(groupId);
168            }
169    
170            public List<MDRRuleGroup> getRuleGroups(long groupId, int start, int end)
171                    throws SystemException {
172    
173                    return mdrRuleGroupPersistence.findByGroupId(groupId, start, end);
174            }
175    
176            public int getRuleGroupsCount(long groupId) throws SystemException {
177                    return mdrRuleGroupPersistence.countByGroupId(groupId);
178            }
179    
180            public List<MDRRuleGroup> search(
181                            long groupId, String name, boolean andOperator, int start, int end)
182                    throws SystemException {
183    
184                    return mdrRuleGroupFinder.findByG_N(
185                            groupId, name, andOperator, start, end);
186            }
187    
188            public List<MDRRuleGroup> searchByKeywords(
189                            long groupId, String keywords, boolean andOperator, int start,
190                            int end)
191                    throws SystemException {
192    
193                    return mdrRuleGroupFinder.findByKeywords(groupId, keywords, start, end);
194            }
195    
196            public int searchByKeywordsCount(
197                            long groupId, String keywords, boolean andOperator)
198                    throws SystemException {
199    
200                    return mdrRuleGroupFinder.countByKeywords(groupId, keywords);
201            }
202    
203            public int searchCount(long groupId, String name, boolean andOperator)
204                    throws SystemException {
205    
206                    return mdrRuleGroupFinder.countByG_N(groupId, name, andOperator);
207            }
208    
209            public MDRRuleGroup updateRuleGroup(
210                            long ruleGroupId, Map<Locale, String> nameMap,
211                            Map<Locale, String> descriptionMap, ServiceContext serviceContext)
212                    throws PortalException, SystemException {
213    
214                    MDRRuleGroup ruleGroup = mdrRuleGroupPersistence.findByPrimaryKey(
215                            ruleGroupId);
216    
217                    ruleGroup.setModifiedDate(serviceContext.getModifiedDate(null));
218                    ruleGroup.setNameMap(nameMap);
219                    ruleGroup.setDescriptionMap(descriptionMap);
220    
221                    mdrRuleGroupPersistence.update(ruleGroup, false);
222    
223                    return ruleGroup;
224            }
225    
226    }