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