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.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    }