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