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