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.lar.StagedModelDataHandlerUtil;
021    import com.liferay.portal.kernel.util.MapUtil;
022    import com.liferay.portal.kernel.xml.Element;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portlet.mobiledevicerules.model.MDRRule;
025    import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroup;
026    import com.liferay.portlet.mobiledevicerules.service.MDRRuleGroupLocalServiceUtil;
027    import com.liferay.portlet.mobiledevicerules.service.MDRRuleLocalServiceUtil;
028    import com.liferay.portlet.mobiledevicerules.service.persistence.MDRRuleUtil;
029    
030    import java.util.Map;
031    
032    /**
033     * @author Mate Thurzo
034     */
035    public class MDRRuleStagedModelDataHandler
036            extends BaseStagedModelDataHandler<MDRRule> {
037    
038            public static final String[] CLASS_NAMES = {MDRRule.class.getName()};
039    
040            @Override
041            public String[] getClassNames() {
042                    return CLASS_NAMES;
043            }
044    
045            @Override
046            protected void doExportStagedModel(
047                            PortletDataContext portletDataContext, MDRRule rule)
048                    throws Exception {
049    
050                    MDRRuleGroup ruleGroup = MDRRuleGroupLocalServiceUtil.getRuleGroup(
051                            rule.getRuleGroupId());
052    
053                    StagedModelDataHandlerUtil.exportStagedModel(
054                            portletDataContext, ruleGroup);
055    
056                    Element ruleElement =
057                            portletDataContext.getExportDataStagedModelElement(rule);
058    
059                    portletDataContext.addClassedModel(
060                            ruleElement, ExportImportPathUtil.getModelPath(rule), rule,
061                            MDRPortletDataHandler.NAMESPACE);
062            }
063    
064            @Override
065            protected void doImportStagedModel(
066                            PortletDataContext portletDataContext, MDRRule rule)
067                    throws Exception {
068    
069                    String ruleGroupPath = ExportImportPathUtil.getModelPath(
070                            portletDataContext, MDRRuleGroup.class.getName(),
071                            rule.getRuleGroupId());
072    
073                    MDRRuleGroup ruleGroup =
074                            (MDRRuleGroup)portletDataContext.getZipEntryAsObject(ruleGroupPath);
075    
076                    StagedModelDataHandlerUtil.importStagedModel(
077                            portletDataContext, ruleGroup);
078    
079                    Map<Long, Long> ruleGroupIds =
080                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
081                                    MDRRuleGroup.class);
082    
083                    long ruleGroupId = MapUtil.getLong(
084                            ruleGroupIds, rule.getRuleGroupId(), rule.getRuleGroupId());
085    
086                    ServiceContext serviceContext = portletDataContext.createServiceContext(
087                            rule, MDRPortletDataHandler.NAMESPACE);
088    
089                    serviceContext.setUserId(
090                            portletDataContext.getUserId(rule.getUserUuid()));
091    
092                    MDRRule importedRule = null;
093    
094                    if (portletDataContext.isDataStrategyMirror()) {
095                            MDRRule existingRule = MDRRuleUtil.fetchByUUID_G(
096                                    rule.getUuid(), portletDataContext.getScopeGroupId());
097    
098                            if (existingRule == null) {
099                                    serviceContext.setUuid(rule.getUuid());
100    
101                                    importedRule = MDRRuleLocalServiceUtil.addRule(
102                                            ruleGroupId, rule.getNameMap(), rule.getDescriptionMap(),
103                                            rule.getType(), rule.getTypeSettingsProperties(),
104                                            serviceContext);
105                            }
106                            else {
107                                    importedRule = MDRRuleLocalServiceUtil.updateRule(
108                                            existingRule.getRuleId(), rule.getNameMap(),
109                                            rule.getDescriptionMap(), rule.getType(),
110                                            rule.getTypeSettingsProperties(), serviceContext);
111                            }
112                    }
113                    else {
114                            importedRule = MDRRuleLocalServiceUtil.addRule(
115                                    ruleGroupId, rule.getNameMap(), rule.getDescriptionMap(),
116                                    rule.getType(), rule.getTypeSettingsProperties(),
117                                    serviceContext);
118                    }
119    
120                    portletDataContext.importClassedModel(
121                            rule, importedRule, MDRPortletDataHandler.NAMESPACE);
122            }
123    
124    }