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.xml.Element;
019    import com.liferay.portal.service.ServiceContext;
020    import com.liferay.portlet.exportimport.lar.BaseStagedModelDataHandler;
021    import com.liferay.portlet.exportimport.lar.ExportImportPathUtil;
022    import com.liferay.portlet.exportimport.lar.PortletDataContext;
023    import com.liferay.portlet.exportimport.lar.StagedModelModifiedDateComparator;
024    import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroup;
025    import com.liferay.portlet.mobiledevicerules.service.MDRRuleGroupLocalServiceUtil;
026    
027    import java.util.List;
028    
029    /**
030     * @author Mate Thurzo
031     */
032    public class MDRRuleGroupStagedModelDataHandler
033            extends BaseStagedModelDataHandler<MDRRuleGroup> {
034    
035            public static final String[] CLASS_NAMES = {MDRRuleGroup.class.getName()};
036    
037            @Override
038            public void deleteStagedModel(MDRRuleGroup ruleGroup) {
039                    MDRRuleGroupLocalServiceUtil.deleteRuleGroup(ruleGroup);
040            }
041    
042            @Override
043            public void deleteStagedModel(
044                    String uuid, long groupId, String className, String extraData) {
045    
046                    MDRRuleGroup ruleGroup = fetchStagedModelByUuidAndGroupId(
047                            uuid, groupId);
048    
049                    if (ruleGroup != null) {
050                            deleteStagedModel(ruleGroup);
051                    }
052            }
053    
054            @Override
055            public MDRRuleGroup fetchStagedModelByUuidAndGroupId(
056                    String uuid, long groupId) {
057    
058                    return MDRRuleGroupLocalServiceUtil.fetchMDRRuleGroupByUuidAndGroupId(
059                            uuid, groupId);
060            }
061    
062            @Override
063            public List<MDRRuleGroup> fetchStagedModelsByUuidAndCompanyId(
064                    String uuid, long companyId) {
065    
066                    return MDRRuleGroupLocalServiceUtil.getMDRRuleGroupsByUuidAndCompanyId(
067                            uuid, companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
068                            new StagedModelModifiedDateComparator<MDRRuleGroup>());
069            }
070    
071            @Override
072            public String[] getClassNames() {
073                    return CLASS_NAMES;
074            }
075    
076            @Override
077            public String getDisplayName(MDRRuleGroup ruleGroup) {
078                    return ruleGroup.getNameCurrentValue();
079            }
080    
081            @Override
082            protected void doExportStagedModel(
083                            PortletDataContext portletDataContext, MDRRuleGroup ruleGroup)
084                    throws Exception {
085    
086                    Element ruleGroupElement = portletDataContext.getExportDataElement(
087                            ruleGroup);
088    
089                    portletDataContext.addClassedModel(
090                            ruleGroupElement, ExportImportPathUtil.getModelPath(ruleGroup),
091                            ruleGroup);
092            }
093    
094            @Override
095            protected void doImportStagedModel(
096                            PortletDataContext portletDataContext, MDRRuleGroup ruleGroup)
097                    throws Exception {
098    
099                    long userId = portletDataContext.getUserId(ruleGroup.getUserUuid());
100    
101                    ServiceContext serviceContext = portletDataContext.createServiceContext(
102                            ruleGroup);
103    
104                    serviceContext.setUserId(userId);
105    
106                    MDRRuleGroup importedRuleGroup = null;
107    
108                    if (portletDataContext.isDataStrategyMirror()) {
109                            MDRRuleGroup existingRuleGroup = fetchStagedModelByUuidAndGroupId(
110                                    ruleGroup.getUuid(), portletDataContext.getScopeGroupId());
111    
112                            if (existingRuleGroup == null) {
113                                    serviceContext.setUuid(ruleGroup.getUuid());
114    
115                                    importedRuleGroup = MDRRuleGroupLocalServiceUtil.addRuleGroup(
116                                            portletDataContext.getScopeGroupId(),
117                                            ruleGroup.getNameMap(), ruleGroup.getDescriptionMap(),
118                                            serviceContext);
119                            }
120                            else {
121                                    importedRuleGroup =
122                                            MDRRuleGroupLocalServiceUtil.updateRuleGroup(
123                                                    existingRuleGroup.getRuleGroupId(),
124                                                    ruleGroup.getNameMap(), ruleGroup.getDescriptionMap(),
125                                                    serviceContext);
126                            }
127                    }
128                    else {
129                            importedRuleGroup = MDRRuleGroupLocalServiceUtil.addRuleGroup(
130                                    portletDataContext.getScopeGroupId(), ruleGroup.getNameMap(),
131                                    ruleGroup.getDescriptionMap(), serviceContext);
132                    }
133    
134                    portletDataContext.importClassedModel(ruleGroup, importedRuleGroup);
135            }
136    
137    }