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