001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                    // Rule group
053    
054                    User user = userPersistence.findByPrimaryKey(
055                            serviceContext.getUserId());
056                    Date now = new Date();
057    
058                    long ruleGroupId = counterLocalService.increment();
059    
060                    MDRRuleGroup ruleGroup = createMDRRuleGroup(ruleGroupId);
061    
062                    ruleGroup.setUuid(serviceContext.getUuid());
063                    ruleGroup.setGroupId(groupId);
064                    ruleGroup.setCompanyId(serviceContext.getCompanyId());
065                    ruleGroup.setCreateDate(serviceContext.getCreateDate(now));
066                    ruleGroup.setModifiedDate(serviceContext.getModifiedDate(now));
067                    ruleGroup.setUserId(user.getUserId());
068                    ruleGroup.setUserName(user.getFullName());
069                    ruleGroup.setNameMap(nameMap);
070                    ruleGroup.setDescriptionMap(descriptionMap);
071    
072                    // Resources
073    
074                    resourceLocalService.addModelResources(ruleGroup, serviceContext);
075    
076                    return updateMDRRuleGroup(ruleGroup);
077            }
078    
079            @Override
080            public MDRRuleGroup copyRuleGroup(
081                            long ruleGroupId, long groupId, ServiceContext serviceContext)
082                    throws PortalException, SystemException {
083    
084                    MDRRuleGroup ruleGroup = mdrRuleGroupPersistence.findByPrimaryKey(
085                            ruleGroupId);
086    
087                    return copyRuleGroup(ruleGroup, groupId, serviceContext);
088            }
089    
090            @Override
091            public MDRRuleGroup copyRuleGroup(
092                            MDRRuleGroup ruleGroup, long groupId, ServiceContext serviceContext)
093                    throws PortalException, SystemException {
094    
095                    Group group = groupPersistence.findByPrimaryKey(groupId);
096    
097                    Map<Locale, String> nameMap = ruleGroup.getNameMap();
098    
099                    for (Map.Entry<Locale, String> entry : nameMap.entrySet()) {
100                            Locale locale = entry.getKey();
101                            String name = entry.getValue();
102    
103                            if (Validator.isNull(name)) {
104                                    continue;
105                            }
106    
107                            String postfix = LanguageUtil.get(
108                                    locale,
109                                    PropsValues.MOBILE_DEVICE_RULES_RULE_GROUP_COPY_POSTFIX);
110    
111                            nameMap.put(locale, name.concat(StringPool.SPACE).concat(postfix));
112                    }
113    
114                    MDRRuleGroup newRuleGroup = addRuleGroup(
115                            group.getGroupId(), nameMap, ruleGroup.getDescriptionMap(),
116                            serviceContext);
117    
118                    List<MDRRule> rules = mdrRulePersistence.findByRuleGroupId(
119                            ruleGroup.getRuleGroupId());
120    
121                    for (MDRRule rule : rules) {
122                            serviceContext.setUuid(PortalUUIDUtil.generate());
123    
124                            mdrRuleLocalService.copyRule(
125                                    rule, newRuleGroup.getRuleGroupId(), serviceContext);
126                    }
127    
128                    return newRuleGroup;
129            }
130    
131            @Override
132            public void deleteRuleGroup(long ruleGroupId) throws SystemException {
133                    MDRRuleGroup ruleGroup = mdrRuleGroupPersistence.fetchByPrimaryKey(
134                            ruleGroupId);
135    
136                    if (ruleGroup != null) {
137                            mdrRuleGroupLocalService.deleteRuleGroup(ruleGroup);
138                    }
139            }
140    
141            @Override
142            @SystemEvent(
143                    action = SystemEventConstants.ACTION_SKIP,
144                    type = SystemEventConstants.TYPE_DELETE)
145            public void deleteRuleGroup(MDRRuleGroup ruleGroup) throws SystemException {
146    
147                    // Rule group
148    
149                    mdrRuleGroupPersistence.remove(ruleGroup);
150    
151                    // Rules
152    
153                    mdrRuleLocalService.deleteRules(ruleGroup.getRuleGroupId());
154    
155                    // Rule group instances
156    
157                    mdrRuleGroupInstanceLocalService.deleteRuleGroupInstances(
158                            ruleGroup.getRuleGroupId());
159            }
160    
161            @Override
162            public void deleteRuleGroups(long groupId) throws SystemException {
163                    List<MDRRuleGroup> ruleGroups = mdrRuleGroupPersistence.findByGroupId(
164                            groupId);
165    
166                    for (MDRRuleGroup ruleGroup : ruleGroups) {
167                            mdrRuleGroupLocalService.deleteRuleGroup(ruleGroup);
168                    }
169            }
170    
171            @Override
172            public MDRRuleGroup fetchRuleGroup(long ruleGroupId)
173                    throws SystemException {
174    
175                    return mdrRuleGroupPersistence.fetchByPrimaryKey(ruleGroupId);
176            }
177    
178            @Override
179            public MDRRuleGroup getRuleGroup(long ruleGroupId)
180                    throws PortalException, SystemException {
181    
182                    return mdrRuleGroupPersistence.findByPrimaryKey(ruleGroupId);
183            }
184    
185            @Override
186            public List<MDRRuleGroup> getRuleGroups(long groupId)
187                    throws SystemException {
188    
189                    return mdrRuleGroupPersistence.findByGroupId(groupId);
190            }
191    
192            @Override
193            public List<MDRRuleGroup> getRuleGroups(long groupId, int start, int end)
194                    throws SystemException {
195    
196                    return mdrRuleGroupPersistence.findByGroupId(groupId, start, end);
197            }
198    
199            @Override
200            public int getRuleGroupsCount(long groupId) throws SystemException {
201                    return mdrRuleGroupPersistence.countByGroupId(groupId);
202            }
203    
204            /**
205             * @deprecated As of 6.2.0, replaced by {@link #search(long, String,
206             *             LinkedHashMap, boolean, int, int)}
207             */
208            @Override
209            public List<MDRRuleGroup> search(
210                            long groupId, String name, boolean andOperator, int start, int end)
211                    throws SystemException {
212    
213                    LinkedHashMap<String, Object> params =
214                            new LinkedHashMap<String, Object>();
215    
216                    params.put("includeGlobalScope", Boolean.TRUE);
217    
218                    return mdrRuleGroupFinder.findByG_N(
219                            groupId, name, params, andOperator, start, end);
220            }
221    
222            @Override
223            public List<MDRRuleGroup> search(
224                            long groupId, String name, LinkedHashMap<String, Object> params,
225                            boolean andOperator, int start, int end)
226                    throws SystemException {
227    
228                    return mdrRuleGroupFinder.findByG_N(
229                            groupId, name, params, andOperator, start, end);
230            }
231    
232            /**
233             * @deprecated As of 6.2.0, replaced by {@link #searchByKeywords(long,
234             *             String, LinkedHashMap, boolean, int, int)}
235             */
236            @Override
237            public List<MDRRuleGroup> searchByKeywords(
238                            long groupId, String keywords, boolean andOperator, int start,
239                            int end)
240                    throws SystemException {
241    
242                    LinkedHashMap<String, Object> params =
243                            new LinkedHashMap<String, Object>();
244    
245                    params.put("includeGlobalScope", Boolean.TRUE);
246    
247                    return mdrRuleGroupFinder.findByKeywords(
248                            groupId, keywords, params, start, end);
249            }
250    
251            @Override
252            public List<MDRRuleGroup> searchByKeywords(
253                            long groupId, String keywords, LinkedHashMap<String, Object> params,
254                            boolean andOperator, int start, int end)
255                    throws SystemException {
256    
257                    return mdrRuleGroupFinder.findByKeywords(
258                            groupId, keywords, params, start, end);
259            }
260    
261            /**
262             * @deprecated As of 6.2.0, replaced by {@link #searchByKeywordsCount(long,
263             *             String, LinkedHashMap, boolean)}
264             */
265            @Override
266            public int searchByKeywordsCount(
267                            long groupId, String keywords, boolean andOperator)
268                    throws SystemException {
269    
270                    LinkedHashMap<String, Object> params =
271                            new LinkedHashMap<String, Object>();
272    
273                    params.put("includeGlobalScope", Boolean.TRUE);
274    
275                    return mdrRuleGroupFinder.countByKeywords(groupId, keywords, params);
276            }
277    
278            @Override
279            public int searchByKeywordsCount(
280                            long groupId, String keywords, LinkedHashMap<String, Object> params,
281                            boolean andOperator)
282                    throws SystemException {
283    
284                    return mdrRuleGroupFinder.countByKeywords(groupId, keywords, params);
285            }
286    
287            /**
288             * @deprecated As of 6.2.0, replaced by {@link #searchCount(long, String,
289             *             LinkedHashMap, boolean)}
290             */
291            @Override
292            public int searchCount(long groupId, String name, boolean andOperator)
293                    throws SystemException {
294    
295                    LinkedHashMap<String, Object> params =
296                            new LinkedHashMap<String, Object>();
297    
298                    params.put("includeGlobalScope", Boolean.TRUE);
299    
300                    return mdrRuleGroupFinder.countByG_N(
301                            groupId, name, params, andOperator);
302            }
303    
304            @Override
305            public int searchCount(
306                            long groupId, String name, LinkedHashMap<String, Object> params,
307                            boolean andOperator)
308                    throws SystemException {
309    
310                    return mdrRuleGroupFinder.countByG_N(
311                            groupId, name, params, andOperator);
312            }
313    
314            @Override
315            public MDRRuleGroup updateRuleGroup(
316                            long ruleGroupId, Map<Locale, String> nameMap,
317                            Map<Locale, String> descriptionMap, ServiceContext serviceContext)
318                    throws PortalException, SystemException {
319    
320                    MDRRuleGroup ruleGroup = mdrRuleGroupPersistence.findByPrimaryKey(
321                            ruleGroupId);
322    
323                    ruleGroup.setModifiedDate(serviceContext.getModifiedDate(null));
324                    ruleGroup.setNameMap(nameMap);
325                    ruleGroup.setDescriptionMap(descriptionMap);
326    
327                    mdrRuleGroupPersistence.update(ruleGroup);
328    
329                    return ruleGroup;
330            }
331    
332    }