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