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.exportStagedModel(
072                            portletDataContext, ruleGroup);
073    
074                    Element ruleElement = portletDataContext.getExportDataElement(rule);
075    
076                    portletDataContext.addClassedModel(
077                            ruleElement, ExportImportPathUtil.getModelPath(rule), rule,
078                            MDRPortletDataHandler.NAMESPACE);
079            }
080    
081            @Override
082            protected void doImportStagedModel(
083                            PortletDataContext portletDataContext, MDRRule rule)
084                    throws Exception {
085    
086                    String ruleGroupPath = ExportImportPathUtil.getModelPath(
087                            portletDataContext, MDRRuleGroup.class.getName(),
088                            rule.getRuleGroupId());
089    
090                    MDRRuleGroup ruleGroup =
091                            (MDRRuleGroup)portletDataContext.getZipEntryAsObject(ruleGroupPath);
092    
093                    StagedModelDataHandlerUtil.importStagedModel(
094                            portletDataContext, ruleGroup);
095    
096                    Map<Long, Long> ruleGroupIds =
097                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
098                                    MDRRuleGroup.class);
099    
100                    long ruleGroupId = MapUtil.getLong(
101                            ruleGroupIds, rule.getRuleGroupId(), rule.getRuleGroupId());
102    
103                    ServiceContext serviceContext = portletDataContext.createServiceContext(
104                            rule, MDRPortletDataHandler.NAMESPACE);
105    
106                    serviceContext.setUserId(
107                            portletDataContext.getUserId(rule.getUserUuid()));
108    
109                    MDRRule importedRule = null;
110    
111                    if (portletDataContext.isDataStrategyMirror()) {
112                            MDRRule existingRule =
113                                    MDRRuleLocalServiceUtil.fetchMDRRuleByUuidAndGroupId(
114                                            rule.getUuid(), portletDataContext.getScopeGroupId());
115    
116                            if (existingRule == null) {
117                                    serviceContext.setUuid(rule.getUuid());
118    
119                                    importedRule = MDRRuleLocalServiceUtil.addRule(
120                                            ruleGroupId, rule.getNameMap(), rule.getDescriptionMap(),
121                                            rule.getType(), rule.getTypeSettingsProperties(),
122                                            serviceContext);
123                            }
124                            else {
125                                    importedRule = MDRRuleLocalServiceUtil.updateRule(
126                                            existingRule.getRuleId(), rule.getNameMap(),
127                                            rule.getDescriptionMap(), rule.getType(),
128                                            rule.getTypeSettingsProperties(), serviceContext);
129                            }
130                    }
131                    else {
132                            importedRule = MDRRuleLocalServiceUtil.addRule(
133                                    ruleGroupId, rule.getNameMap(), rule.getDescriptionMap(),
134                                    rule.getType(), rule.getTypeSettingsProperties(),
135                                    serviceContext);
136                    }
137    
138                    portletDataContext.importClassedModel(
139                            rule, importedRule, MDRPortletDataHandler.NAMESPACE);
140            }
141    
142    }