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            }
115    
116            @Override
117            protected void doImportStagedModel(
118                            PortletDataContext portletDataContext, MDRAction action)
119                    throws Exception {
120    
121                    String ruleGroupInstancePath = ExportImportPathUtil.getModelPath(
122                            portletDataContext, MDRRuleGroupInstance.class.getName(),
123                            action.getRuleGroupInstanceId());
124    
125                    MDRRuleGroupInstance ruleGroupInstance =
126                            (MDRRuleGroupInstance)portletDataContext.getZipEntryAsObject(
127                                    ruleGroupInstancePath);
128    
129                    StagedModelDataHandlerUtil.importReferenceStagedModel(
130                            portletDataContext, ruleGroupInstance);
131    
132                    Map<Long, Long> ruleGroupInstanceIds =
133                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
134                                    MDRRuleGroupInstance.class);
135    
136                    long ruleGroupInstanceId = MapUtil.getLong(
137                            ruleGroupInstanceIds, action.getRuleGroupInstanceId(),
138                            action.getRuleGroupInstanceId());
139    
140                    ServiceContext serviceContext = portletDataContext.createServiceContext(
141                            action);
142    
143                    serviceContext.setUserId(
144                            portletDataContext.getUserId(action.getUserUuid()));
145    
146                    Element element = portletDataContext.getImportDataStagedModelElement(
147                            action);
148    
149                    validateLayout(element, action);
150    
151                    MDRAction importedAction = null;
152    
153                    if (portletDataContext.isDataStrategyMirror()) {
154                            MDRAction existingAction =
155                                    MDRActionLocalServiceUtil.fetchMDRActionByUuidAndGroupId(
156                                            action.getUuid(), portletDataContext.getScopeGroupId());
157    
158                            if (existingAction == null) {
159                                    serviceContext.setUuid(action.getUuid());
160    
161                                    importedAction = MDRActionLocalServiceUtil.addAction(
162                                            ruleGroupInstanceId, action.getNameMap(),
163                                            action.getDescriptionMap(), action.getType(),
164                                            action.getTypeSettingsProperties(), serviceContext);
165                            }
166                            else {
167                                    importedAction = MDRActionLocalServiceUtil.updateAction(
168                                            existingAction.getActionId(), action.getNameMap(),
169                                            action.getDescriptionMap(), action.getType(),
170                                            action.getTypeSettingsProperties(), serviceContext);
171                            }
172                    }
173                    else {
174                            importedAction = MDRActionLocalServiceUtil.addAction(
175                                    ruleGroupInstanceId, action.getNameMap(),
176                                    action.getDescriptionMap(), action.getType(),
177                                    action.getTypeSettingsProperties(), serviceContext);
178                    }
179    
180                    portletDataContext.importClassedModel(action, importedAction);
181            }
182    
183            protected void validateLayout(Element actionElement, MDRAction action) {
184                    String type = action.getType();
185    
186                    if (!type.equals(SiteRedirectActionHandler.class.getName())) {
187                            return;
188                    }
189    
190                    String layoutUuid = actionElement.attributeValue("layout-uuid");
191    
192                    if (Validator.isNull(layoutUuid)) {
193                            return;
194                    }
195    
196                    UnicodeProperties typeSettingsProperties =
197                            action.getTypeSettingsProperties();
198    
199                    long groupId = GetterUtil.getLong(
200                            typeSettingsProperties.getProperty("groupId"));
201                    boolean privateLayout = GetterUtil.getBoolean(
202                            actionElement.attributeValue("private-layout"));
203    
204                    try {
205                            Layout layout = LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
206                                    layoutUuid, groupId, privateLayout);
207    
208                            typeSettingsProperties.setProperty(
209                                    "plid", String.valueOf(layout.getPlid()));
210                    }
211                    catch (Exception e) {
212                            if (_log.isWarnEnabled()) {
213                                    StringBundler sb = new StringBundler(5);
214    
215                                    sb.append("Unable to find layout with uuid ");
216                                    sb.append(layoutUuid);
217                                    sb.append(" in group ");
218                                    sb.append(groupId);
219                                    sb.append(". Site redirect may not match the target layout.");
220    
221                                    _log.warn(sb.toString(), e);
222                            }
223                    }
224            }
225    
226            private static Log _log = LogFactoryUtil.getLog(
227                    MDRActionStagedModelDataHandler.class);
228    
229    }