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.lar.StagedModelDataHandlerUtil;
022    import com.liferay.portal.kernel.util.MapUtil;
023    import com.liferay.portal.kernel.xml.Element;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portlet.mobiledevicerules.model.MDRRule;
026    import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroup;
027    import com.liferay.portlet.mobiledevicerules.service.MDRRuleGroupLocalServiceUtil;
028    import com.liferay.portlet.mobiledevicerules.service.MDRRuleLocalServiceUtil;
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 void deleteStagedModel(
042                            String uuid, long groupId, String className, String extraData)
043                    throws SystemException {
044    
045                    MDRRule rule = MDRRuleLocalServiceUtil.fetchMDRRuleByUuidAndGroupId(
046                            uuid, groupId);
047    
048                    if (rule != null) {
049                            MDRRuleLocalServiceUtil.deleteRule(rule);
050                    }
051            }
052    
053            @Override
054            public String[] getClassNames() {
055                    return CLASS_NAMES;
056            }
057    
058            @Override
059            public String getDisplayName(MDRRule rule) {
060                    return rule.getNameCurrentValue();
061            }
062    
063            @Override
064            protected void doExportStagedModel(
065                            PortletDataContext portletDataContext, MDRRule rule)
066                    throws Exception {
067    
068                    MDRRuleGroup ruleGroup = MDRRuleGroupLocalServiceUtil.getRuleGroup(
069                            rule.getRuleGroupId());
070    
071                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
072                            portletDataContext, rule, ruleGroup,
073                            PortletDataContext.REFERENCE_TYPE_PARENT);
074    
075                    Element ruleElement = portletDataContext.getExportDataElement(rule);
076    
077                    portletDataContext.addClassedModel(
078                            ruleElement, ExportImportPathUtil.getModelPath(rule), rule,
079                            MDRPortletDataHandler.NAMESPACE);
080            }
081    
082            @Override
083            protected void doImportStagedModel(
084                            PortletDataContext portletDataContext, MDRRule rule)
085                    throws Exception {
086    
087                    String ruleGroupPath = ExportImportPathUtil.getModelPath(
088                            portletDataContext, MDRRuleGroup.class.getName(),
089                            rule.getRuleGroupId());
090    
091                    MDRRuleGroup ruleGroup =
092                            (MDRRuleGroup)portletDataContext.getZipEntryAsObject(ruleGroupPath);
093    
094                    StagedModelDataHandlerUtil.importStagedModel(
095                            portletDataContext, ruleGroup);
096    
097                    Map<Long, Long> ruleGroupIds =
098                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
099                                    MDRRuleGroup.class);
100    
101                    long ruleGroupId = MapUtil.getLong(
102                            ruleGroupIds, rule.getRuleGroupId(), rule.getRuleGroupId());
103    
104                    ServiceContext serviceContext = portletDataContext.createServiceContext(
105                            rule, MDRPortletDataHandler.NAMESPACE);
106    
107                    serviceContext.setUserId(
108                            portletDataContext.getUserId(rule.getUserUuid()));
109    
110                    MDRRule importedRule = null;
111    
112                    if (portletDataContext.isDataStrategyMirror()) {
113                            MDRRule existingRule =
114                                    MDRRuleLocalServiceUtil.fetchMDRRuleByUuidAndGroupId(
115                                            rule.getUuid(), portletDataContext.getScopeGroupId());
116    
117                            if (existingRule == null) {
118                                    serviceContext.setUuid(rule.getUuid());
119    
120                                    importedRule = MDRRuleLocalServiceUtil.addRule(
121                                            ruleGroupId, rule.getNameMap(), rule.getDescriptionMap(),
122                                            rule.getType(), rule.getTypeSettingsProperties(),
123                                            serviceContext);
124                            }
125                            else {
126                                    importedRule = MDRRuleLocalServiceUtil.updateRule(
127                                            existingRule.getRuleId(), rule.getNameMap(),
128                                            rule.getDescriptionMap(), rule.getType(),
129                                            rule.getTypeSettingsProperties(), serviceContext);
130                            }
131                    }
132                    else {
133                            importedRule = MDRRuleLocalServiceUtil.addRule(
134                                    ruleGroupId, rule.getNameMap(), rule.getDescriptionMap(),
135                                    rule.getType(), rule.getTypeSettingsProperties(),
136                                    serviceContext);
137                    }
138    
139                    portletDataContext.importClassedModel(
140                            rule, importedRule, MDRPortletDataHandler.NAMESPACE);
141            }
142    
143    }