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.lar;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.lar.BaseStagedModelDataHandler;
019    import com.liferay.portal.kernel.lar.ExportImportPathUtil;
020    import com.liferay.portal.kernel.lar.PortletDataContext;
021    import com.liferay.portal.kernel.xml.Element;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroup;
024    import com.liferay.portlet.mobiledevicerules.service.MDRRuleGroupLocalServiceUtil;
025    
026    /**
027     * @author Mate Thurzo
028     */
029    public class MDRRuleGroupStagedModelDataHandler
030            extends BaseStagedModelDataHandler<MDRRuleGroup> {
031    
032            public static final String[] CLASS_NAMES = {MDRRuleGroup.class.getName()};
033    
034            @Override
035            public void deleteStagedModel(
036                            String uuid, long groupId, String className, String extraData)
037                    throws SystemException {
038    
039                    MDRRuleGroup ruleGroup =
040                            MDRRuleGroupLocalServiceUtil.fetchMDRRuleGroupByUuidAndGroupId(
041                                    uuid, groupId);
042    
043                    if (ruleGroup != null) {
044                            MDRRuleGroupLocalServiceUtil.deleteRuleGroup(ruleGroup);
045                    }
046            }
047    
048            @Override
049            public String[] getClassNames() {
050                    return CLASS_NAMES;
051            }
052    
053            @Override
054            public String getDisplayName(MDRRuleGroup ruleGroup) {
055                    return ruleGroup.getNameCurrentValue();
056            }
057    
058            @Override
059            protected void doExportStagedModel(
060                            PortletDataContext portletDataContext, MDRRuleGroup ruleGroup)
061                    throws Exception {
062    
063                    Element ruleGroupElement = portletDataContext.getExportDataElement(
064                            ruleGroup);
065    
066                    portletDataContext.addClassedModel(
067                            ruleGroupElement, ExportImportPathUtil.getModelPath(ruleGroup),
068                            ruleGroup);
069            }
070    
071            @Override
072            protected void doImportStagedModel(
073                            PortletDataContext portletDataContext, MDRRuleGroup ruleGroup)
074                    throws Exception {
075    
076                    long userId = portletDataContext.getUserId(ruleGroup.getUserUuid());
077    
078                    ServiceContext serviceContext = portletDataContext.createServiceContext(
079                            ruleGroup);
080    
081                    serviceContext.setUserId(userId);
082    
083                    MDRRuleGroup importedRuleGroup = null;
084    
085                    if (portletDataContext.isDataStrategyMirror()) {
086                            MDRRuleGroup existingRuleGroup =
087                                    MDRRuleGroupLocalServiceUtil.fetchMDRRuleGroupByUuidAndGroupId(
088                                            ruleGroup.getUuid(), portletDataContext.getScopeGroupId());
089    
090                            if (existingRuleGroup == null) {
091                                    serviceContext.setUuid(ruleGroup.getUuid());
092    
093                                    importedRuleGroup = MDRRuleGroupLocalServiceUtil.addRuleGroup(
094                                            portletDataContext.getScopeGroupId(),
095                                            ruleGroup.getNameMap(), ruleGroup.getDescriptionMap(),
096                                            serviceContext);
097                            }
098                            else {
099                                    importedRuleGroup =
100                                            MDRRuleGroupLocalServiceUtil.updateRuleGroup(
101                                                    existingRuleGroup.getRuleGroupId(),
102                                                    ruleGroup.getNameMap(), ruleGroup.getDescriptionMap(),
103                                                    serviceContext);
104                            }
105                    }
106                    else {
107                            importedRuleGroup = MDRRuleGroupLocalServiceUtil.addRuleGroup(
108                                    portletDataContext.getScopeGroupId(), ruleGroup.getNameMap(),
109                                    ruleGroup.getDescriptionMap(), serviceContext);
110                    }
111    
112                    portletDataContext.importClassedModel(ruleGroup, importedRuleGroup);
113            }
114    
115    }