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.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.MapUtil;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.UnicodeProperties;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.kernel.xml.Element;
026    import com.liferay.portal.mobile.device.rulegroup.action.impl.SiteRedirectActionHandler;
027    import com.liferay.portal.model.Layout;
028    import com.liferay.portal.service.LayoutLocalServiceUtil;
029    import com.liferay.portal.service.ServiceContext;
030    import com.liferay.portlet.exportimport.lar.BaseStagedModelDataHandler;
031    import com.liferay.portlet.exportimport.lar.ExportImportPathUtil;
032    import com.liferay.portlet.exportimport.lar.PortletDataContext;
033    import com.liferay.portlet.exportimport.lar.StagedModelDataHandlerUtil;
034    import com.liferay.portlet.exportimport.lar.StagedModelModifiedDateComparator;
035    import com.liferay.portlet.mobiledevicerules.model.MDRAction;
036    import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroupInstance;
037    import com.liferay.portlet.mobiledevicerules.service.MDRActionLocalServiceUtil;
038    import com.liferay.portlet.mobiledevicerules.service.MDRRuleGroupInstanceLocalServiceUtil;
039    
040    import java.util.List;
041    import java.util.Map;
042    
043    /**
044     * @author Mate Thurzo
045     */
046    public class MDRActionStagedModelDataHandler
047            extends BaseStagedModelDataHandler<MDRAction> {
048    
049            public static final String[] CLASS_NAMES = {MDRAction.class.getName()};
050    
051            @Override
052            public void deleteStagedModel(MDRAction action) {
053                    MDRActionLocalServiceUtil.deleteAction(action);
054            }
055    
056            @Override
057            public void deleteStagedModel(
058                    String uuid, long groupId, String className, String extraData) {
059    
060                    MDRAction action = fetchStagedModelByUuidAndGroupId(uuid, groupId);
061    
062                    if (action != null) {
063                            deleteStagedModel(action);
064                    }
065            }
066    
067            @Override
068            public MDRAction fetchStagedModelByUuidAndGroupId(
069                    String uuid, long groupId) {
070    
071                    return MDRActionLocalServiceUtil.fetchMDRActionByUuidAndGroupId(
072                            uuid, groupId);
073            }
074    
075            @Override
076            public List<MDRAction> fetchStagedModelsByUuidAndCompanyId(
077                    String uuid, long companyId) {
078    
079                    return MDRActionLocalServiceUtil.getMDRActionsByUuidAndCompanyId(
080                            uuid, companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
081                            new StagedModelModifiedDateComparator<MDRAction>());
082            }
083    
084            @Override
085            public String[] getClassNames() {
086                    return CLASS_NAMES;
087            }
088    
089            @Override
090            public String getDisplayName(MDRAction action) {
091                    return action.getNameCurrentValue();
092            }
093    
094            @Override
095            protected void doExportStagedModel(
096                            PortletDataContext portletDataContext, MDRAction action)
097                    throws Exception {
098    
099                    MDRRuleGroupInstance ruleGroupInstance =
100                            MDRRuleGroupInstanceLocalServiceUtil.getRuleGroupInstance(
101                                    action.getRuleGroupInstanceId());
102    
103                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
104                            portletDataContext, action, ruleGroupInstance,
105                            PortletDataContext.REFERENCE_TYPE_PARENT);
106    
107                    Element actionElement = portletDataContext.getExportDataElement(action);
108    
109                    String type = action.getType();
110    
111                    if (type.equals(SiteRedirectActionHandler.class.getName())) {
112                            UnicodeProperties typeSettingsProperties =
113                                    action.getTypeSettingsProperties();
114    
115                            long plid = GetterUtil.getLong(
116                                    typeSettingsProperties.getProperty("plid"));
117    
118                            try {
119                                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
120    
121                                    actionElement.addAttribute("layout-uuid", layout.getUuid());
122                            }
123                            catch (Exception e) {
124                                    if (_log.isWarnEnabled()) {
125                                            _log.warn(
126                                                    "Unable to set the layout uuid of layout " + plid +
127                                                            ". Site redirect may not match after import.",
128                                                    e);
129                                    }
130                            }
131                    }
132    
133                    portletDataContext.addClassedModel(
134                            actionElement, ExportImportPathUtil.getModelPath(action), action);
135            }
136    
137            @Override
138            protected void doImportStagedModel(
139                            PortletDataContext portletDataContext, MDRAction action)
140                    throws Exception {
141    
142                    Map<Long, Long> ruleGroupInstanceIds =
143                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
144                                    MDRRuleGroupInstance.class);
145    
146                    long ruleGroupInstanceId = MapUtil.getLong(
147                            ruleGroupInstanceIds, action.getRuleGroupInstanceId(),
148                            action.getRuleGroupInstanceId());
149    
150                    ServiceContext serviceContext = portletDataContext.createServiceContext(
151                            action);
152    
153                    serviceContext.setUserId(
154                            portletDataContext.getUserId(action.getUserUuid()));
155    
156                    Element element = portletDataContext.getImportDataStagedModelElement(
157                            action);
158    
159                    validateLayout(element, action);
160    
161                    MDRAction importedAction = null;
162    
163                    if (portletDataContext.isDataStrategyMirror()) {
164                            MDRAction existingAction = fetchStagedModelByUuidAndGroupId(
165                                    action.getUuid(), portletDataContext.getScopeGroupId());
166    
167                            if (existingAction == null) {
168                                    serviceContext.setUuid(action.getUuid());
169    
170                                    importedAction = MDRActionLocalServiceUtil.addAction(
171                                            ruleGroupInstanceId, action.getNameMap(),
172                                            action.getDescriptionMap(), action.getType(),
173                                            action.getTypeSettingsProperties(), serviceContext);
174                            }
175                            else {
176                                    importedAction = MDRActionLocalServiceUtil.updateAction(
177                                            existingAction.getActionId(), action.getNameMap(),
178                                            action.getDescriptionMap(), action.getType(),
179                                            action.getTypeSettingsProperties(), serviceContext);
180                            }
181                    }
182                    else {
183                            importedAction = MDRActionLocalServiceUtil.addAction(
184                                    ruleGroupInstanceId, action.getNameMap(),
185                                    action.getDescriptionMap(), action.getType(),
186                                    action.getTypeSettingsProperties(), serviceContext);
187                    }
188    
189                    portletDataContext.importClassedModel(action, importedAction);
190            }
191    
192            protected void validateLayout(Element actionElement, MDRAction action) {
193                    String type = action.getType();
194    
195                    if (!type.equals(SiteRedirectActionHandler.class.getName())) {
196                            return;
197                    }
198    
199                    String layoutUuid = actionElement.attributeValue("layout-uuid");
200    
201                    if (Validator.isNull(layoutUuid)) {
202                            return;
203                    }
204    
205                    UnicodeProperties typeSettingsProperties =
206                            action.getTypeSettingsProperties();
207    
208                    long groupId = GetterUtil.getLong(
209                            typeSettingsProperties.getProperty("groupId"));
210                    boolean privateLayout = GetterUtil.getBoolean(
211                            actionElement.attributeValue("private-layout"));
212    
213                    try {
214                            Layout layout = LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
215                                    layoutUuid, groupId, privateLayout);
216    
217                            typeSettingsProperties.setProperty(
218                                    "plid", String.valueOf(layout.getPlid()));
219                    }
220                    catch (Exception e) {
221                            if (_log.isWarnEnabled()) {
222                                    StringBundler sb = new StringBundler(5);
223    
224                                    sb.append("Unable to find layout with uuid ");
225                                    sb.append(layoutUuid);
226                                    sb.append(" in group ");
227                                    sb.append(groupId);
228                                    sb.append(". Site redirect may not match the target layout.");
229    
230                                    _log.warn(sb.toString(), e);
231                            }
232                    }
233            }
234    
235            private static final Log _log = LogFactoryUtil.getLog(
236                    MDRActionStagedModelDataHandler.class);
237    
238    }