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