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.lar.BaseStagedModelDataHandler;
018    import com.liferay.portal.kernel.lar.ExportImportPathUtil;
019    import com.liferay.portal.kernel.lar.PortletDataContext;
020    import com.liferay.portal.kernel.xml.Element;
021    import com.liferay.portal.service.ServiceContext;
022    import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroup;
023    import com.liferay.portlet.mobiledevicerules.service.MDRRuleGroupLocalServiceUtil;
024    import com.liferay.portlet.mobiledevicerules.service.persistence.MDRRuleGroupUtil;
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 String[] getClassNames() {
036                    return CLASS_NAMES;
037            }
038    
039            @Override
040            protected void doExportStagedModel(
041                            PortletDataContext portletDataContext, MDRRuleGroup ruleGroup)
042                    throws Exception {
043    
044                    Element ruleGroupElement =
045                            portletDataContext.getExportDataStagedModelElement(ruleGroup);
046    
047                    portletDataContext.addClassedModel(
048                            ruleGroupElement, ExportImportPathUtil.getModelPath(ruleGroup),
049                            ruleGroup, MDRPortletDataHandler.NAMESPACE);
050            }
051    
052            @Override
053            protected void doImportStagedModel(
054                            PortletDataContext portletDataContext, MDRRuleGroup ruleGroup)
055                    throws Exception {
056    
057                    long userId = portletDataContext.getUserId(ruleGroup.getUserUuid());
058    
059                    ServiceContext serviceContext = portletDataContext.createServiceContext(
060                            ruleGroup, MDRPortletDataHandler.NAMESPACE);
061    
062                    serviceContext.setUserId(userId);
063    
064                    MDRRuleGroup importedRuleGroup = null;
065    
066                    if (portletDataContext.isDataStrategyMirror()) {
067                            MDRRuleGroup existingRuleGroup = MDRRuleGroupUtil.fetchByUUID_G(
068                                    ruleGroup.getUuid(), portletDataContext.getScopeGroupId());
069    
070                            if (existingRuleGroup == null) {
071                                    serviceContext.setUuid(ruleGroup.getUuid());
072    
073                                    importedRuleGroup = MDRRuleGroupLocalServiceUtil.addRuleGroup(
074                                            portletDataContext.getScopeGroupId(),
075                                            ruleGroup.getNameMap(), ruleGroup.getDescriptionMap(),
076                                            serviceContext);
077                            }
078                            else {
079                                    importedRuleGroup =
080                                            MDRRuleGroupLocalServiceUtil.updateRuleGroup(
081                                                    existingRuleGroup.getRuleGroupId(),
082                                                    ruleGroup.getNameMap(), ruleGroup.getDescriptionMap(),
083                                                    serviceContext);
084                            }
085                    }
086                    else {
087                            importedRuleGroup = MDRRuleGroupLocalServiceUtil.addRuleGroup(
088                                    portletDataContext.getScopeGroupId(), ruleGroup.getNameMap(),
089                                    ruleGroup.getDescriptionMap(), serviceContext);
090                    }
091    
092                    portletDataContext.importClassedModel(
093                            ruleGroup, importedRuleGroup, MDRPortletDataHandler.NAMESPACE);
094            }
095    
096    }