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            }
080    
081            @Override
082            protected void doImportStagedModel(
083                            PortletDataContext portletDataContext, MDRRule rule)
084                    throws Exception {
085    
086                    StagedModelDataHandlerUtil.importReferenceStagedModel(
087                            portletDataContext, rule, MDRRuleGroup.class,
088                            rule.getRuleGroupId());
089    
090                    Map<Long, Long> ruleGroupIds =
091                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
092                                    MDRRuleGroup.class);
093    
094                    long ruleGroupId = MapUtil.getLong(
095                            ruleGroupIds, rule.getRuleGroupId(), rule.getRuleGroupId());
096    
097                    ServiceContext serviceContext = portletDataContext.createServiceContext(
098                            rule);
099    
100                    serviceContext.setUserId(
101                            portletDataContext.getUserId(rule.getUserUuid()));
102    
103                    MDRRule importedRule = null;
104    
105                    if (portletDataContext.isDataStrategyMirror()) {
106                            MDRRule existingRule =
107                                    MDRRuleLocalServiceUtil.fetchMDRRuleByUuidAndGroupId(
108                                            rule.getUuid(), portletDataContext.getScopeGroupId());
109    
110                            if (existingRule == null) {
111                                    serviceContext.setUuid(rule.getUuid());
112    
113                                    importedRule = MDRRuleLocalServiceUtil.addRule(
114                                            ruleGroupId, rule.getNameMap(), rule.getDescriptionMap(),
115                                            rule.getType(), rule.getTypeSettingsProperties(),
116                                            serviceContext);
117                            }
118                            else {
119                                    importedRule = MDRRuleLocalServiceUtil.updateRule(
120                                            existingRule.getRuleId(), rule.getNameMap(),
121                                            rule.getDescriptionMap(), rule.getType(),
122                                            rule.getTypeSettingsProperties(), serviceContext);
123                            }
124                    }
125                    else {
126                            importedRule = MDRRuleLocalServiceUtil.addRule(
127                                    ruleGroupId, rule.getNameMap(), rule.getDescriptionMap(),
128                                    rule.getType(), rule.getTypeSettingsProperties(),
129                                    serviceContext);
130                    }
131    
132                    portletDataContext.importClassedModel(rule, importedRule);
133            }
134    
135    }