001    /**
002     * Copyright (c) 2000-2012 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.util.UnicodeProperties;
020    import com.liferay.portal.model.User;
021    import com.liferay.portal.service.ServiceContext;
022    import com.liferay.portlet.mobiledevicerules.model.MDRAction;
023    import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroupInstance;
024    import com.liferay.portlet.mobiledevicerules.service.base.MDRActionLocalServiceBaseImpl;
025    
026    import java.util.Date;
027    import java.util.List;
028    import java.util.Locale;
029    import java.util.Map;
030    
031    /**
032     * @author Edward C. Han
033     */
034    public class MDRActionLocalServiceImpl extends MDRActionLocalServiceBaseImpl {
035    
036            public MDRAction addAction(
037                            long ruleGroupInstanceId, Map<Locale, String> nameMap,
038                            Map<Locale, String> descriptionMap, String type,
039                            String typeSettings, ServiceContext serviceContext)
040                    throws PortalException, SystemException {
041    
042                    User user = userPersistence.findByPrimaryKey(
043                            serviceContext.getUserId());
044                    MDRRuleGroupInstance ruleGroupInstance =
045                            mdrRuleGroupInstancePersistence.findByPrimaryKey(
046                                    ruleGroupInstanceId);
047                    Date now = new Date();
048    
049                    long actionId = counterLocalService.increment();
050    
051                    MDRAction action = mdrActionLocalService.createMDRAction(actionId);
052    
053                    action.setUuid(serviceContext.getUuid());
054                    action.setGroupId(ruleGroupInstance.getGroupId());
055                    action.setCompanyId(serviceContext.getCompanyId());
056                    action.setCreateDate(serviceContext.getCreateDate(now));
057                    action.setModifiedDate(serviceContext.getModifiedDate(now));
058                    action.setUserId(serviceContext.getUserId());
059                    action.setUserName(user.getFullName());
060                    action.setClassNameId(ruleGroupInstance.getClassNameId());
061                    action.setClassPK(ruleGroupInstance.getClassPK());
062                    action.setRuleGroupInstanceId(ruleGroupInstanceId);
063                    action.setNameMap(nameMap);
064                    action.setDescriptionMap(descriptionMap);
065                    action.setType(type);
066                    action.setTypeSettings(typeSettings);
067    
068                    return updateMDRAction(action, false);
069            }
070    
071            public MDRAction addAction(
072                            long ruleGroupInstanceId, Map<Locale, String> nameMap,
073                            Map<Locale, String> descriptionMap, String type,
074                            UnicodeProperties typeSettingsProperties,
075                            ServiceContext serviceContext)
076                    throws PortalException, SystemException {
077    
078                    return addAction(
079                            ruleGroupInstanceId, nameMap, descriptionMap, type,
080                            typeSettingsProperties.toString(), serviceContext);
081            }
082    
083            public void deleteAction(long actionId) throws SystemException {
084                    MDRAction action = mdrActionPersistence.fetchByPrimaryKey(actionId);
085    
086                    if (action != null) {
087                            deleteAction(action);
088                    }
089            }
090    
091            public void deleteAction(MDRAction action) throws SystemException {
092                    mdrActionPersistence.remove(action);
093            }
094    
095            public void deleteActions(long ruleGroupInstanceId)
096                    throws SystemException {
097    
098                    List<MDRAction> actions =
099                            mdrActionPersistence.findByRuleGroupInstanceId(ruleGroupInstanceId);
100    
101                    for (MDRAction action : actions) {
102                            deleteAction(action);
103                    }
104            }
105    
106            public MDRAction fetchAction(long actionId) throws SystemException {
107                    return mdrActionPersistence.fetchByPrimaryKey(actionId);
108            }
109    
110            public MDRAction getAction(long actionId)
111                    throws PortalException, SystemException {
112    
113                    return mdrActionPersistence.findByPrimaryKey(actionId);
114            }
115    
116            public List<MDRAction> getActions(long ruleGroupInstanceId)
117                    throws SystemException {
118    
119                    return mdrActionPersistence.findByRuleGroupInstanceId(
120                            ruleGroupInstanceId);
121            }
122    
123            public List<MDRAction> getActions(
124                            long ruleGroupInstanceId, int start, int end)
125                    throws SystemException {
126    
127                    return mdrActionPersistence.findByRuleGroupInstanceId(
128                            ruleGroupInstanceId, start, end);
129            }
130    
131            public int getActionsCount(long ruleGroupInstanceId)
132                    throws SystemException {
133    
134                    return mdrActionPersistence.countByRuleGroupInstanceId(
135                            ruleGroupInstanceId);
136            }
137    
138            public MDRAction updateAction(
139                            long actionId, Map<Locale, String> nameMap,
140                            Map<Locale, String> descriptionMap, String type,
141                            String typeSettings, ServiceContext serviceContext)
142                    throws PortalException, SystemException {
143    
144                    MDRAction action = mdrActionPersistence.findByPrimaryKey(actionId);
145    
146                    action.setModifiedDate(serviceContext.getModifiedDate(null));
147                    action.setNameMap(nameMap);
148                    action.setDescriptionMap(descriptionMap);
149                    action.setType(type);
150                    action.setTypeSettings(typeSettings);
151    
152                    mdrActionPersistence.update(action, false);
153    
154                    return action;
155            }
156    
157            public MDRAction updateAction(
158                            long actionId, Map<Locale, String> nameMap,
159                            Map<Locale, String> descriptionMap, String type,
160                            UnicodeProperties typeSettingsProperties,
161                            ServiceContext serviceContext)
162                    throws PortalException, SystemException {
163    
164                    return updateAction(
165                            actionId, nameMap, descriptionMap, type,
166                            typeSettingsProperties.toString(), serviceContext);
167            }
168    
169    }