001    /**
002     * Copyright (c) 2000-present 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.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.util.MapUtil;
019    import com.liferay.portal.kernel.xml.Element;
020    import com.liferay.portal.service.ServiceContext;
021    import com.liferay.portlet.exportimport.lar.BaseStagedModelDataHandler;
022    import com.liferay.portlet.exportimport.lar.ExportImportPathUtil;
023    import com.liferay.portlet.exportimport.lar.PortletDataContext;
024    import com.liferay.portlet.exportimport.lar.StagedModelDataHandlerUtil;
025    import com.liferay.portlet.exportimport.lar.StagedModelModifiedDateComparator;
026    import com.liferay.portlet.mobiledevicerules.model.MDRRule;
027    import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroup;
028    import com.liferay.portlet.mobiledevicerules.service.MDRRuleGroupLocalServiceUtil;
029    import com.liferay.portlet.mobiledevicerules.service.MDRRuleLocalServiceUtil;
030    
031    import java.util.List;
032    import java.util.Map;
033    
034    /**
035     * @author Mate Thurzo
036     */
037    public class MDRRuleStagedModelDataHandler
038            extends BaseStagedModelDataHandler<MDRRule> {
039    
040            public static final String[] CLASS_NAMES = {MDRRule.class.getName()};
041    
042            @Override
043            public void deleteStagedModel(MDRRule rule) {
044                    MDRRuleLocalServiceUtil.deleteRule(rule);
045            }
046    
047            @Override
048            public void deleteStagedModel(
049                    String uuid, long groupId, String className, String extraData) {
050    
051                    MDRRule rule = fetchStagedModelByUuidAndGroupId(uuid, groupId);
052    
053                    if (rule != null) {
054                            deleteStagedModel(rule);
055                    }
056            }
057    
058            @Override
059            public MDRRule fetchStagedModelByUuidAndGroupId(String uuid, long groupId) {
060                    return MDRRuleLocalServiceUtil.fetchMDRRuleByUuidAndGroupId(
061                            uuid, groupId);
062            }
063    
064            @Override
065            public List<MDRRule> fetchStagedModelsByUuidAndCompanyId(
066                    String uuid, long companyId) {
067    
068                    return MDRRuleLocalServiceUtil.getMDRRulesByUuidAndCompanyId(
069                            uuid, companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
070                            new StagedModelModifiedDateComparator<MDRRule>());
071            }
072    
073            @Override
074            public String[] getClassNames() {
075                    return CLASS_NAMES;
076            }
077    
078            @Override
079            public String getDisplayName(MDRRule rule) {
080                    return rule.getNameCurrentValue();
081            }
082    
083            @Override
084            protected void doExportStagedModel(
085                            PortletDataContext portletDataContext, MDRRule rule)
086                    throws Exception {
087    
088                    MDRRuleGroup ruleGroup = MDRRuleGroupLocalServiceUtil.getRuleGroup(
089                            rule.getRuleGroupId());
090    
091                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
092                            portletDataContext, rule, ruleGroup,
093                            PortletDataContext.REFERENCE_TYPE_PARENT);
094    
095                    Element ruleElement = portletDataContext.getExportDataElement(rule);
096    
097                    portletDataContext.addClassedModel(
098                            ruleElement, ExportImportPathUtil.getModelPath(rule), rule);
099            }
100    
101            @Override
102            protected void doImportStagedModel(
103                            PortletDataContext portletDataContext, MDRRule rule)
104                    throws Exception {
105    
106                    Map<Long, Long> ruleGroupIds =
107                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
108                                    MDRRuleGroup.class);
109    
110                    long ruleGroupId = MapUtil.getLong(
111                            ruleGroupIds, rule.getRuleGroupId(), rule.getRuleGroupId());
112    
113                    ServiceContext serviceContext = portletDataContext.createServiceContext(
114                            rule);
115    
116                    serviceContext.setUserId(
117                            portletDataContext.getUserId(rule.getUserUuid()));
118    
119                    MDRRule importedRule = null;
120    
121                    if (portletDataContext.isDataStrategyMirror()) {
122                            MDRRule existingRule = fetchStagedModelByUuidAndGroupId(
123                                    rule.getUuid(), portletDataContext.getScopeGroupId());
124    
125                            if (existingRule == null) {
126                                    serviceContext.setUuid(rule.getUuid());
127    
128                                    importedRule = MDRRuleLocalServiceUtil.addRule(
129                                            ruleGroupId, rule.getNameMap(), rule.getDescriptionMap(),
130                                            rule.getType(), rule.getTypeSettingsProperties(),
131                                            serviceContext);
132                            }
133                            else {
134                                    importedRule = MDRRuleLocalServiceUtil.updateRule(
135                                            existingRule.getRuleId(), rule.getNameMap(),
136                                            rule.getDescriptionMap(), rule.getType(),
137                                            rule.getTypeSettingsProperties(), serviceContext);
138                            }
139                    }
140                    else {
141                            importedRule = MDRRuleLocalServiceUtil.addRule(
142                                    ruleGroupId, rule.getNameMap(), rule.getDescriptionMap(),
143                                    rule.getType(), rule.getTypeSettingsProperties(),
144                                    serviceContext);
145                    }
146    
147                    portletDataContext.importClassedModel(rule, importedRule);
148            }
149    
150    }