001    /**
002     * Copyright (c) 2000-present 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.language.LanguageUtil;
019    import com.liferay.portal.kernel.systemevent.SystemEvent;
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.SystemEventConstants;
025    import com.liferay.portal.model.User;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portal.util.PropsValues;
028    import com.liferay.portlet.mobiledevicerules.model.MDRRule;
029    import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroup;
030    import com.liferay.portlet.mobiledevicerules.service.base.MDRRuleGroupLocalServiceBaseImpl;
031    
032    import java.util.Date;
033    import java.util.LinkedHashMap;
034    import java.util.List;
035    import java.util.Locale;
036    import java.util.Map;
037    
038    /**
039     * @author Edward C. Han
040     * @author Manuel de la Pe??a
041     */
042    public class MDRRuleGroupLocalServiceImpl
043            extends MDRRuleGroupLocalServiceBaseImpl {
044    
045            @Override
046            public MDRRuleGroup addRuleGroup(
047                            long groupId, Map<Locale, String> nameMap,
048                            Map<Locale, String> descriptionMap, ServiceContext serviceContext)
049                    throws PortalException {
050    
051                    User user = userPersistence.findByPrimaryKey(
052                            serviceContext.getUserId());
053                    Date now = new Date();
054    
055                    long ruleGroupId = counterLocalService.increment();
056    
057                    MDRRuleGroup ruleGroup = createMDRRuleGroup(ruleGroupId);
058    
059                    ruleGroup.setUuid(serviceContext.getUuid());
060                    ruleGroup.setGroupId(groupId);
061                    ruleGroup.setCompanyId(serviceContext.getCompanyId());
062                    ruleGroup.setCreateDate(serviceContext.getCreateDate(now));
063                    ruleGroup.setModifiedDate(serviceContext.getModifiedDate(now));
064                    ruleGroup.setUserId(user.getUserId());
065                    ruleGroup.setUserName(user.getFullName());
066                    ruleGroup.setNameMap(nameMap);
067                    ruleGroup.setDescriptionMap(descriptionMap);
068    
069                    return updateMDRRuleGroup(ruleGroup);
070            }
071    
072            @Override
073            public MDRRuleGroup copyRuleGroup(
074                            long ruleGroupId, long groupId, ServiceContext serviceContext)
075                    throws PortalException {
076    
077                    MDRRuleGroup ruleGroup = mdrRuleGroupPersistence.findByPrimaryKey(
078                            ruleGroupId);
079    
080                    return copyRuleGroup(ruleGroup, groupId, serviceContext);
081            }
082    
083            @Override
084            public MDRRuleGroup copyRuleGroup(
085                            MDRRuleGroup ruleGroup, long groupId, ServiceContext serviceContext)
086                    throws PortalException {
087    
088                    Group group = groupPersistence.findByPrimaryKey(groupId);
089    
090                    Map<Locale, String> nameMap = ruleGroup.getNameMap();
091    
092                    for (Map.Entry<Locale, String> entry : nameMap.entrySet()) {
093                            Locale locale = entry.getKey();
094                            String name = entry.getValue();
095    
096                            if (Validator.isNull(name)) {
097                                    continue;
098                            }
099    
100                            String postfix = LanguageUtil.get(
101                                    locale,
102                                    PropsValues.MOBILE_DEVICE_RULES_RULE_GROUP_COPY_POSTFIX);
103    
104                            nameMap.put(locale, name.concat(StringPool.SPACE).concat(postfix));
105                    }
106    
107                    MDRRuleGroup newRuleGroup = addRuleGroup(
108                            group.getGroupId(), nameMap, ruleGroup.getDescriptionMap(),
109                            serviceContext);
110    
111                    List<MDRRule> rules = mdrRulePersistence.findByRuleGroupId(
112                            ruleGroup.getRuleGroupId());
113    
114                    for (MDRRule rule : rules) {
115                            serviceContext.setUuid(PortalUUIDUtil.generate());
116    
117                            mdrRuleLocalService.copyRule(
118                                    rule, newRuleGroup.getRuleGroupId(), serviceContext);
119                    }
120    
121                    return newRuleGroup;
122            }
123    
124            @Override
125            public void deleteRuleGroup(long ruleGroupId) {
126                    MDRRuleGroup ruleGroup = mdrRuleGroupPersistence.fetchByPrimaryKey(
127                            ruleGroupId);
128    
129                    if (ruleGroup != null) {
130                            mdrRuleGroupLocalService.deleteRuleGroup(ruleGroup);
131                    }
132            }
133    
134            @Override
135            @SystemEvent(
136                    action = SystemEventConstants.ACTION_SKIP,
137                    type = SystemEventConstants.TYPE_DELETE)
138            public void deleteRuleGroup(MDRRuleGroup ruleGroup) {
139    
140                    // Rule group
141    
142                    mdrRuleGroupPersistence.remove(ruleGroup);
143    
144                    // Rules
145    
146                    mdrRuleLocalService.deleteRules(ruleGroup.getRuleGroupId());
147    
148                    // Rule group instances
149    
150                    mdrRuleGroupInstanceLocalService.deleteRuleGroupInstances(
151                            ruleGroup.getRuleGroupId());
152            }
153    
154            @Override
155            public void deleteRuleGroups(long groupId) {
156                    List<MDRRuleGroup> ruleGroups = mdrRuleGroupPersistence.findByGroupId(
157                            groupId);
158    
159                    for (MDRRuleGroup ruleGroup : ruleGroups) {
160                            mdrRuleGroupLocalService.deleteRuleGroup(ruleGroup);
161                    }
162            }
163    
164            @Override
165            public MDRRuleGroup fetchRuleGroup(long ruleGroupId) {
166                    return mdrRuleGroupPersistence.fetchByPrimaryKey(ruleGroupId);
167            }
168    
169            @Override
170            public MDRRuleGroup getRuleGroup(long ruleGroupId) throws PortalException {
171                    return mdrRuleGroupPersistence.findByPrimaryKey(ruleGroupId);
172            }
173    
174            @Override
175            public List<MDRRuleGroup> getRuleGroups(long groupId) {
176                    return mdrRuleGroupPersistence.findByGroupId(groupId);
177            }
178    
179            @Override
180            public List<MDRRuleGroup> getRuleGroups(long groupId, int start, int end) {
181                    return mdrRuleGroupPersistence.findByGroupId(groupId, start, end);
182            }
183    
184            @Override
185            public int getRuleGroupsCount(long groupId) {
186                    return mdrRuleGroupPersistence.countByGroupId(groupId);
187            }
188    
189            /**
190             * @deprecated As of 6.2.0, replaced by {@link #search(long, String,
191             *             LinkedHashMap, boolean, int, int)}
192             */
193            @Deprecated
194            @Override
195            public List<MDRRuleGroup> search(
196                    long groupId, String name, boolean andOperator, int start, int end) {
197    
198                    LinkedHashMap<String, Object> params =
199                            new LinkedHashMap<String, Object>();
200    
201                    params.put("includeGlobalScope", Boolean.TRUE);
202    
203                    return mdrRuleGroupFinder.findByG_N(
204                            groupId, name, params, andOperator, start, end);
205            }
206    
207            @Override
208            public List<MDRRuleGroup> search(
209                    long groupId, String name, LinkedHashMap<String, Object> params,
210                    boolean andOperator, int start, int end) {
211    
212                    return mdrRuleGroupFinder.findByG_N(
213                            groupId, name, params, andOperator, start, end);
214            }
215    
216            /**
217             * @deprecated As of 6.2.0, replaced by {@link #searchByKeywords(long,
218             *             String, LinkedHashMap, boolean, int, int)}
219             */
220            @Deprecated
221            @Override
222            public List<MDRRuleGroup> searchByKeywords(
223                    long groupId, String keywords, boolean andOperator, int start,
224                    int end) {
225    
226                    LinkedHashMap<String, Object> params =
227                            new LinkedHashMap<String, Object>();
228    
229                    params.put("includeGlobalScope", Boolean.TRUE);
230    
231                    return mdrRuleGroupFinder.findByKeywords(
232                            groupId, keywords, params, start, end);
233            }
234    
235            @Override
236            public List<MDRRuleGroup> searchByKeywords(
237                    long groupId, String keywords, LinkedHashMap<String, Object> params,
238                    boolean andOperator, int start, int end) {
239    
240                    return mdrRuleGroupFinder.findByKeywords(
241                            groupId, keywords, params, start, end);
242            }
243    
244            /**
245             * @deprecated As of 6.2.0, replaced by {@link #searchByKeywordsCount(long,
246             *             String, LinkedHashMap, boolean)}
247             */
248            @Deprecated
249            @Override
250            public int searchByKeywordsCount(
251                    long groupId, String keywords, boolean andOperator) {
252    
253                    LinkedHashMap<String, Object> params =
254                            new LinkedHashMap<String, Object>();
255    
256                    params.put("includeGlobalScope", Boolean.TRUE);
257    
258                    return mdrRuleGroupFinder.countByKeywords(groupId, keywords, params);
259            }
260    
261            @Override
262            public int searchByKeywordsCount(
263                    long groupId, String keywords, LinkedHashMap<String, Object> params,
264                    boolean andOperator) {
265    
266                    return mdrRuleGroupFinder.countByKeywords(groupId, keywords, params);
267            }
268    
269            /**
270             * @deprecated As of 6.2.0, replaced by {@link #searchCount(long, String,
271             *             LinkedHashMap, boolean)}
272             */
273            @Deprecated
274            @Override
275            public int searchCount(long groupId, String name, boolean andOperator) {
276                    LinkedHashMap<String, Object> params =
277                            new LinkedHashMap<String, Object>();
278    
279                    params.put("includeGlobalScope", Boolean.TRUE);
280    
281                    return mdrRuleGroupFinder.countByG_N(
282                            groupId, name, params, andOperator);
283            }
284    
285            @Override
286            public int searchCount(
287                    long groupId, String name, LinkedHashMap<String, Object> params,
288                    boolean andOperator) {
289    
290                    return mdrRuleGroupFinder.countByG_N(
291                            groupId, name, params, andOperator);
292            }
293    
294            @Override
295            public MDRRuleGroup updateRuleGroup(
296                            long ruleGroupId, Map<Locale, String> nameMap,
297                            Map<Locale, String> descriptionMap, ServiceContext serviceContext)
298                    throws PortalException {
299    
300                    MDRRuleGroup ruleGroup = mdrRuleGroupPersistence.findByPrimaryKey(
301                            ruleGroupId);
302    
303                    ruleGroup.setModifiedDate(serviceContext.getModifiedDate(null));
304                    ruleGroup.setNameMap(nameMap);
305                    ruleGroup.setDescriptionMap(descriptionMap);
306    
307                    mdrRuleGroupPersistence.update(ruleGroup);
308    
309                    return ruleGroup;
310            }
311    
312    }