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