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.lar.BasePortletDataHandler;
018    import com.liferay.portal.kernel.lar.PortletDataContext;
019    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
020    import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.UnicodeProperties;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.kernel.xml.Document;
028    import com.liferay.portal.kernel.xml.Element;
029    import com.liferay.portal.kernel.xml.SAXReaderUtil;
030    import com.liferay.portal.mobile.device.rulegroup.action.impl.SiteRedirectActionHandler;
031    import com.liferay.portal.model.Layout;
032    import com.liferay.portal.model.LayoutSet;
033    import com.liferay.portal.service.LayoutLocalServiceUtil;
034    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
035    import com.liferay.portal.service.ServiceContext;
036    import com.liferay.portal.util.PortletKeys;
037    import com.liferay.portlet.mobiledevicerules.model.MDRAction;
038    import com.liferay.portlet.mobiledevicerules.model.MDRRule;
039    import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroup;
040    import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroupInstance;
041    import com.liferay.portlet.mobiledevicerules.service.MDRActionLocalServiceUtil;
042    import com.liferay.portlet.mobiledevicerules.service.MDRRuleGroupInstanceLocalServiceUtil;
043    import com.liferay.portlet.mobiledevicerules.service.MDRRuleGroupLocalServiceUtil;
044    import com.liferay.portlet.mobiledevicerules.service.MDRRuleLocalServiceUtil;
045    import com.liferay.portlet.mobiledevicerules.service.persistence.MDRActionUtil;
046    import com.liferay.portlet.mobiledevicerules.service.persistence.MDRRuleGroupInstanceUtil;
047    import com.liferay.portlet.mobiledevicerules.service.persistence.MDRRuleGroupUtil;
048    import com.liferay.portlet.mobiledevicerules.service.persistence.MDRRuleUtil;
049    
050    import java.util.List;
051    import java.util.Map;
052    
053    import javax.portlet.PortletPreferences;
054    
055    /**
056     * @author Michael C. Han
057     * @author Brian Wing Shun Chan
058     */
059    public class MDRPortletDataHandlerImpl extends BasePortletDataHandler {
060    
061            @Override
062            public PortletDataHandlerControl[] getExportControls() {
063                    return new PortletDataHandlerControl[] {
064                            _ruleGroups, _ruleGroupInstances
065                    };
066            }
067    
068            @Override
069            public boolean isAlwaysExportable() {
070                    return _ALWAYS_EXPORTABLE;
071            }
072    
073            @Override
074            public boolean isAlwaysStaged() {
075                    return _ALWAYS_STAGED;
076            }
077    
078            @Override
079            public boolean isPublishToLiveByDefault() {
080                    return _PUBLISH_TO_LIVE_BY_DEFAULT;
081            }
082    
083            @Override
084            protected PortletPreferences doDeleteData(
085                            PortletDataContext portletDataContext, String portletId,
086                            PortletPreferences portletPreferences)
087                    throws Exception {
088    
089                    if (!portletDataContext.addPrimaryKey(
090                                    MDRPortletDataHandlerImpl.class, "deleteData")) {
091    
092                            MDRRuleGroupInstanceLocalServiceUtil.deleteGroupRuleGroupInstances(
093                                    portletDataContext.getScopeGroupId());
094    
095                            MDRRuleGroupLocalServiceUtil.deleteRuleGroups(
096                                    portletDataContext.getGroupId());
097                    }
098    
099                    return null;
100            }
101    
102            @Override
103            protected String doExportData(
104                            PortletDataContext portletDataContext, String portletId,
105                            PortletPreferences portletPreferences)
106                    throws Exception {
107    
108                    portletDataContext.addPermissions(
109                            "com.liferay.portlet.mobiledevicerules",
110                            portletDataContext.getScopeGroupId());
111    
112                    Document document = SAXReaderUtil.createDocument();
113    
114                    Element rootElement = document.addElement("mobiledevicerules-data");
115    
116                    Element ruleGroupsElement = rootElement.addElement("rule-groups");
117    
118                    List<MDRRuleGroup> ruleGroups = MDRRuleGroupUtil.findByGroupId(
119                            portletDataContext.getGroupId());
120    
121                    for (MDRRuleGroup ruleGroup : ruleGroups) {
122                            exportRuleGroup(portletDataContext, ruleGroupsElement, ruleGroup);
123                    }
124    
125                    Element ruleGroupInstancesElement = rootElement.addElement(
126                            "rule-group-instances");
127    
128                    List<MDRRuleGroupInstance> ruleGroupInstances =
129                            MDRRuleGroupInstanceUtil.findByGroupId(
130                                    portletDataContext.getScopeGroupId());
131    
132                    for (MDRRuleGroupInstance ruleGroupInstance : ruleGroupInstances) {
133                            exportRuleGroupInstance(
134                                    portletDataContext, ruleGroupInstancesElement,
135                                    ruleGroupInstance);
136                    }
137    
138                    return document.formattedString();
139            }
140    
141            @Override
142            protected PortletPreferences doImportData(
143                            PortletDataContext portletDataContext, String portletId,
144                            PortletPreferences portletPreferences, String data)
145                    throws Exception {
146    
147                    portletDataContext.importPermissions(
148                            "com.liferay.portlet.mobiledevicerules",
149                            portletDataContext.getSourceGroupId(),
150                            portletDataContext.getScopeGroupId());
151    
152                    Document document = SAXReaderUtil.read(data);
153    
154                    Element rootElement = document.getRootElement();
155    
156                    Element ruleGroupsElement = rootElement.element("rule-groups");
157    
158                    List<Element> ruleGroupElements = ruleGroupsElement.elements(
159                            "rule-group");
160    
161                    for (Element ruleGroupElement : ruleGroupElements) {
162                            String path = ruleGroupElement.attributeValue("path");
163    
164                            if (!portletDataContext.isPathNotProcessed(path)) {
165                                    continue;
166                            }
167    
168                            MDRRuleGroup ruleGroup =
169                                    (MDRRuleGroup)portletDataContext.getZipEntryAsObject(path);
170    
171                            importRuleGroup(portletDataContext, ruleGroupElement, ruleGroup);
172                    }
173    
174                    Element ruleGroupInstancesElement = rootElement.element(
175                            "rule-group-instances");
176    
177                    List<Element> ruleGroupInstanceElements =
178                            ruleGroupInstancesElement.elements("rule-group-instance");
179    
180                    for (Element ruleGroupInstanceElement : ruleGroupInstanceElements) {
181                            String path = ruleGroupInstanceElement.attributeValue("path");
182    
183                            if (!portletDataContext.isPathNotProcessed(path)) {
184                                    continue;
185                            }
186    
187                            MDRRuleGroupInstance ruleGroupInstance =
188                                    (MDRRuleGroupInstance)portletDataContext.getZipEntryAsObject(
189                                            path);
190    
191                            importRuleGroupInstance(
192                                    portletDataContext, ruleGroupInstanceElement,
193                                    ruleGroupInstance);
194                    }
195    
196                    return null;
197            }
198    
199            protected void exportAction(
200                            PortletDataContext portletDataContext, Element actionsElement,
201                            MDRAction action)
202                    throws Exception {
203    
204                    String path = getActionPath(portletDataContext, action);
205    
206                    if (!portletDataContext.isPathNotProcessed(path)) {
207                            return;
208                    }
209    
210                    Element actionElement = actionsElement.addElement("action");
211    
212                    String type = action.getType();
213    
214                    if (type.equals(SiteRedirectActionHandler.class.getName())) {
215                            UnicodeProperties typeSettingsProperties =
216                                    action.getTypeSettingsProperties();
217    
218                            long targetPlid = GetterUtil.getLong(
219                                    typeSettingsProperties.getProperty("plid"));
220    
221                            try {
222                                    Layout targetLayout = LayoutLocalServiceUtil.getLayout(
223                                            targetPlid);
224    
225                                    actionElement.addAttribute(
226                                            "layout-uuid", targetLayout.getUuid());
227                            }
228                            catch (Exception e) {
229                                    if (_log.isWarnEnabled()) {
230                                            _log.warn(
231                                                    "Unable to set the layout uuid of the target " +
232                                                            targetPlid +
233                                                                    ". Site redirect may not match after import.",
234                                                    e);
235                                    }
236                            }
237                    }
238    
239                    portletDataContext.addClassedModel(
240                            actionElement, path, action, _NAMESPACE);
241            }
242    
243            protected void exportRule(
244                            PortletDataContext portletDataContext, Element rulesElement,
245                            MDRRule rule)
246                    throws Exception {
247    
248                    String path = getRulePath(portletDataContext, rule);
249    
250                    if (!portletDataContext.isPathNotProcessed(path)) {
251                            return;
252                    }
253    
254                    Element ruleElement = rulesElement.addElement("rule");
255    
256                    portletDataContext.addClassedModel(ruleElement, path, rule, _NAMESPACE);
257            }
258    
259            protected void exportRuleGroup(
260                            PortletDataContext portletDataContext, Element ruleGroupsElement,
261                            MDRRuleGroup ruleGroup)
262                    throws Exception {
263    
264                    if (!portletDataContext.isWithinDateRange(
265                                    ruleGroup.getModifiedDate())) {
266    
267                            return;
268                    }
269    
270                    String path = getRuleGroupPath(portletDataContext, ruleGroup);
271    
272                    if (!portletDataContext.isPathNotProcessed(path)) {
273                            return;
274                    }
275    
276                    Element ruleGroupElement = ruleGroupsElement.addElement("rule-group");
277    
278                    portletDataContext.addClassedModel(
279                            ruleGroupElement, path, ruleGroup, _NAMESPACE);
280    
281                    Element mdrRulesElement = ruleGroupElement.addElement("rules");
282    
283                    List<MDRRule> rules = ruleGroup.getRules();
284    
285                    for (MDRRule rule : rules) {
286                            exportRule(portletDataContext, mdrRulesElement, rule);
287                    }
288            }
289    
290            protected void exportRuleGroupInstance(
291                            PortletDataContext portletDataContext,
292                            Element ruleGroupInstancesElement,
293                            MDRRuleGroupInstance ruleGroupInstance)
294                    throws Exception {
295    
296                    if (!portletDataContext.isWithinDateRange(
297                                    ruleGroupInstance.getModifiedDate())) {
298    
299                            return;
300                    }
301    
302                    String path = getRuleGroupInstancePath(
303                            portletDataContext, ruleGroupInstance);
304    
305                    if (!portletDataContext.isPathNotProcessed(path)) {
306                            return;
307                    }
308    
309                    Element ruleGroupInstanceElement = ruleGroupInstancesElement.addElement(
310                            "rule-group-instance");
311    
312                    MDRRuleGroup ruleGroup = ruleGroupInstance.getRuleGroup();
313    
314                    String className = ruleGroupInstance.getClassName();
315    
316                    if (className.equals(Layout.class.getName())) {
317                            Layout layout = LayoutLocalServiceUtil.getLayout(
318                                    ruleGroupInstance.getClassPK());
319    
320                            ruleGroupInstanceElement.addAttribute(
321                                    "layout-uuid", layout.getUuid());
322                    }
323    
324                    String ruleGroupUuid = ruleGroup.getUuid();
325    
326                    ruleGroupInstanceElement.addAttribute("rule-group-uuid", ruleGroupUuid);
327    
328                    portletDataContext.addClassedModel(
329                            ruleGroupInstanceElement, path, ruleGroupInstance, _NAMESPACE);
330    
331                    Element actionsElement = ruleGroupInstanceElement.addElement("actions");
332    
333                    List<MDRAction> actions = ruleGroupInstance.getActions();
334    
335                    for (MDRAction action : actions) {
336                            exportAction(portletDataContext, actionsElement, action);
337                    }
338            }
339    
340            protected String getActionPath(
341                    PortletDataContext portletDataContext, MDRAction action) {
342    
343                    StringBundler sb = new StringBundler(4);
344    
345                    sb.append(
346                            portletDataContext.getPortletPath(
347                                    PortletKeys.MOBILE_DEVICE_SITE_ADMIN));
348                    sb.append("/actions/");
349                    sb.append(action.getActionId());
350                    sb.append(".xml");
351    
352                    return sb.toString();
353            }
354    
355            protected String getRuleGroupInstancePath(
356                    PortletDataContext portletDataContext,
357                    MDRRuleGroupInstance ruleGroupInstance) {
358    
359                    StringBundler sb = new StringBundler(4);
360    
361                    sb.append(
362                            portletDataContext.getPortletPath(
363                                    PortletKeys.MOBILE_DEVICE_SITE_ADMIN));
364                    sb.append("/rule-group-instances/");
365                    sb.append(ruleGroupInstance.getRuleGroupInstanceId());
366                    sb.append(".xml");
367    
368                    return sb.toString();
369            }
370    
371            protected String getRuleGroupPath(
372                    PortletDataContext portletDataContext, MDRRuleGroup ruleGroup) {
373    
374                    StringBundler sb = new StringBundler(4);
375    
376                    sb.append(
377                            portletDataContext.getPortletPath(
378                                    PortletKeys.MOBILE_DEVICE_SITE_ADMIN));
379                    sb.append("/rule-groups/");
380                    sb.append(ruleGroup.getRuleGroupId());
381                    sb.append(".xml");
382    
383                    return sb.toString();
384            }
385    
386            protected String getRulePath(
387                    PortletDataContext portletDataContext, MDRRule rule) {
388    
389                    StringBundler sb = new StringBundler(4);
390    
391                    sb.append(
392                            portletDataContext.getPortletPath(
393                                    PortletKeys.MOBILE_DEVICE_SITE_ADMIN));
394                    sb.append("/rules/");
395                    sb.append(rule.getRuleId());
396                    sb.append(".xml");
397    
398                    return sb.toString();
399            }
400    
401            protected void importAction(
402                            PortletDataContext portletDataContext, Element actionElement,
403                            MDRRuleGroupInstance ruleGroupInstance, MDRAction action)
404                    throws Exception {
405    
406                    long userId = portletDataContext.getUserId(action.getUserUuid());
407    
408                    ServiceContext serviceContext = portletDataContext.createServiceContext(
409                            actionElement, action, _NAMESPACE);
410    
411                    serviceContext.setUserId(userId);
412    
413                    validateTargetLayoutPlid(actionElement, action);
414    
415                    MDRAction importedAction = null;
416    
417                    if (portletDataContext.isDataStrategyMirror()) {
418                            MDRAction existingAction = MDRActionUtil.fetchByUUID_G(
419                                    action.getUuid(), portletDataContext.getScopeGroupId());
420    
421                            if (existingAction == null) {
422                                    serviceContext.setUuid(action.getUuid());
423    
424                                    importedAction = MDRActionLocalServiceUtil.addAction(
425                                            ruleGroupInstance.getRuleGroupInstanceId(),
426                                            action.getNameMap(), action.getDescriptionMap(),
427                                            action.getType(), action.getTypeSettingsProperties(),
428                                            serviceContext);
429                            }
430                            else {
431                                    importedAction = MDRActionLocalServiceUtil.updateAction(
432                                            existingAction.getActionId(), action.getNameMap(),
433                                            action.getDescriptionMap(), action.getType(),
434                                            action.getTypeSettingsProperties(), serviceContext);
435                            }
436                    }
437                    else {
438                            importedAction = MDRActionLocalServiceUtil.addAction(
439                                    ruleGroupInstance.getRuleGroupInstanceId(), action.getNameMap(),
440                                    action.getDescriptionMap(), action.getType(),
441                                    action.getTypeSettingsProperties(), serviceContext);
442                    }
443    
444                    portletDataContext.importClassedModel(
445                            action, importedAction, _NAMESPACE);
446            }
447    
448            protected void importRule(
449                            PortletDataContext portletDataContext, Element ruleElement,
450                            MDRRuleGroup ruleGroup, MDRRule rule)
451                    throws Exception {
452    
453                    long userId = portletDataContext.getUserId(rule.getUserUuid());
454    
455                    ServiceContext serviceContext = portletDataContext.createServiceContext(
456                            ruleElement, rule, _NAMESPACE);
457    
458                    serviceContext.setUserId(userId);
459    
460                    MDRRule importedRule = null;
461    
462                    if (portletDataContext.isDataStrategyMirror()) {
463                            MDRRule existingRule = MDRRuleUtil.fetchByUUID_G(
464                                    rule.getUuid(), portletDataContext.getScopeGroupId());
465    
466                            if (existingRule == null) {
467                                    serviceContext.setUuid(rule.getUuid());
468    
469                                    importedRule = MDRRuleLocalServiceUtil.addRule(
470                                            ruleGroup.getRuleGroupId(), rule.getNameMap(),
471                                            rule.getDescriptionMap(), rule.getType(),
472                                            rule.getTypeSettingsProperties(), serviceContext);
473                            }
474                            else {
475                                    importedRule = MDRRuleLocalServiceUtil.updateRule(
476                                            existingRule.getRuleId(), rule.getNameMap(),
477                                            rule.getDescriptionMap(), rule.getType(),
478                                            rule.getTypeSettingsProperties(), serviceContext);
479                            }
480                    }
481                    else {
482                            importedRule = MDRRuleLocalServiceUtil.addRule(
483                                    ruleGroup.getRuleGroupId(), rule.getNameMap(),
484                                    rule.getDescriptionMap(), rule.getType(),
485                                    rule.getTypeSettingsProperties(), serviceContext);
486                    }
487    
488                    portletDataContext.importClassedModel(rule, importedRule, _NAMESPACE);
489            }
490    
491            protected void importRuleGroup(
492                            PortletDataContext portletDataContext, Element ruleGroupElement,
493                            MDRRuleGroup ruleGroup)
494                    throws Exception {
495    
496                    long userId = portletDataContext.getUserId(ruleGroup.getUserUuid());
497    
498                    ServiceContext serviceContext = portletDataContext.createServiceContext(
499                            ruleGroupElement, ruleGroup, _NAMESPACE);
500    
501                    serviceContext.setUserId(userId);
502    
503                    MDRRuleGroup importedRuleGroup = null;
504    
505                    if (portletDataContext.isDataStrategyMirror()) {
506                            MDRRuleGroup existingRuleGroup = MDRRuleGroupUtil.fetchByUUID_G(
507                                    ruleGroup.getUuid(), portletDataContext.getScopeGroupId());
508    
509                            if (existingRuleGroup == null) {
510                                    serviceContext.setUuid(ruleGroup.getUuid());
511    
512                                    importedRuleGroup = MDRRuleGroupLocalServiceUtil.addRuleGroup(
513                                            portletDataContext.getScopeGroupId(),
514                                            ruleGroup.getNameMap(), ruleGroup.getDescriptionMap(),
515                                            serviceContext);
516                            }
517                            else {
518                                    importedRuleGroup =
519                                            MDRRuleGroupLocalServiceUtil.updateRuleGroup(
520                                                    existingRuleGroup.getRuleGroupId(),
521                                                    ruleGroup.getNameMap(), ruleGroup.getDescriptionMap(),
522                                                    serviceContext);
523                            }
524                    }
525                    else {
526                            importedRuleGroup = MDRRuleGroupLocalServiceUtil.addRuleGroup(
527                                    portletDataContext.getScopeGroupId(), ruleGroup.getNameMap(),
528                                    ruleGroup.getDescriptionMap(), serviceContext);
529                    }
530    
531                    portletDataContext.importClassedModel(
532                            ruleGroup, importedRuleGroup, _NAMESPACE);
533    
534                    Element rulesElement = ruleGroupElement.element("rules");
535    
536                    List<Element> ruleElements = rulesElement.elements("rule");
537    
538                    for (Element ruleElement : ruleElements) {
539                            String path = ruleElement.attributeValue("path");
540    
541                            if (!portletDataContext.isPathNotProcessed(path)) {
542                                    continue;
543                            }
544    
545                            MDRRule rule = (MDRRule)portletDataContext.getZipEntryAsObject(
546                                    path);
547    
548                            importRule(
549                                    portletDataContext, ruleElement, importedRuleGroup, rule);
550                    }
551            }
552    
553            protected void importRuleGroupInstance(
554                            PortletDataContext portletDataContext,
555                            Element ruleGroupInstanceElement,
556                            MDRRuleGroupInstance ruleGroupInstance)
557                    throws Exception {
558    
559                    long userId = portletDataContext.getUserId(
560                            ruleGroupInstance.getUserUuid());
561    
562                    Map<Long, Long> ruleGroupIds =
563                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
564                                    MDRRuleGroup.class);
565    
566                    Long ruleGroupId = ruleGroupIds.get(ruleGroupInstance.getRuleGroupId());
567    
568                    if (ruleGroupId == null) {
569                            try {
570                                    String ruleGroupUuid = ruleGroupInstanceElement.attributeValue(
571                                            "rule-group-uuid");
572    
573                                    MDRRuleGroup ruleGroup = MDRRuleGroupUtil.findByUuid_First(
574                                            ruleGroupUuid, null);
575    
576                                    ruleGroupId = ruleGroup.getRuleGroupId();
577                            }
578                            catch (Exception e) {
579                                    if (_log.isWarnEnabled()) {
580                                            _log.warn(
581                                                    "Unable to import rule group instance " +
582                                                            ruleGroupInstance,
583                                                    e);
584                                    }
585    
586                                    return;
587                            }
588                    }
589    
590                    long classPK = 0;
591    
592                    String layoutUuid = ruleGroupInstanceElement.attributeValue(
593                            "layout-uuid");
594    
595                    try {
596                            if (Validator.isNotNull(layoutUuid)) {
597                                    Layout layout =
598                                            LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
599                                                    layoutUuid, portletDataContext.getScopeGroupId(),
600                                                    portletDataContext.isPrivateLayout());
601    
602                                    classPK = layout.getPrimaryKey();
603                            }
604                            else {
605                                    LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
606                                            portletDataContext.getScopeGroupId(),
607                                            portletDataContext.isPrivateLayout());
608    
609                                    classPK = layoutSet.getLayoutSetId();
610                            }
611                    }
612                    catch (Exception e) {
613                            if (_log.isWarnEnabled()) {
614                                    StringBundler sb = new StringBundler(5);
615    
616                                    sb.append("Layout ");
617                                    sb.append(layoutUuid);
618                                    sb.append(" is missing for rule group instance ");
619                                    sb.append(ruleGroupInstance.getRuleGroupInstanceId());
620                                    sb.append(", skipping this rule group instance.");
621    
622                                    _log.warn(sb.toString());
623                            }
624    
625                            return;
626                    }
627    
628                    ServiceContext serviceContext = portletDataContext.createServiceContext(
629                            ruleGroupInstanceElement, ruleGroupInstance, _NAMESPACE);
630    
631                    serviceContext.setUserId(userId);
632    
633                    MDRRuleGroupInstance importedRuleGroupInstance = null;
634    
635                    if (portletDataContext.isDataStrategyMirror()) {
636                            MDRRuleGroupInstance existingMDRRuleGroupInstance =
637                                    MDRRuleGroupInstanceUtil.fetchByUUID_G(
638                                            ruleGroupInstance.getUuid(),
639                                            portletDataContext.getScopeGroupId());
640    
641                            if (existingMDRRuleGroupInstance == null) {
642                                    serviceContext.setUuid(ruleGroupInstance.getUuid());
643    
644                                    importedRuleGroupInstance =
645                                            MDRRuleGroupInstanceLocalServiceUtil.addRuleGroupInstance(
646                                                    portletDataContext.getScopeGroupId(),
647                                                    ruleGroupInstance.getClassName(), classPK, ruleGroupId,
648                                                    ruleGroupInstance.getPriority(), serviceContext);
649                            }
650                            else {
651                                    importedRuleGroupInstance =
652                                            MDRRuleGroupInstanceLocalServiceUtil.
653                                                    updateRuleGroupInstance(
654                                                            existingMDRRuleGroupInstance.
655                                                                    getRuleGroupInstanceId(),
656                                                            ruleGroupInstance.getPriority());
657                            }
658                    }
659                    else {
660                            importedRuleGroupInstance =
661                                    MDRRuleGroupInstanceLocalServiceUtil.addRuleGroupInstance(
662                                            portletDataContext.getScopeGroupId(),
663                                            ruleGroupInstance.getClassName(), classPK, ruleGroupId,
664                                            ruleGroupInstance.getPriority(), serviceContext);
665                    }
666    
667                    portletDataContext.importClassedModel(
668                            ruleGroupInstance, importedRuleGroupInstance, _NAMESPACE);
669    
670                    Element actionsElement = ruleGroupInstanceElement.element("actions");
671    
672                    List<Element> actionElements = actionsElement.elements("action");
673    
674                    for (Element actionElement : actionElements) {
675                            String path = actionElement.attributeValue("path");
676    
677                            if (!portletDataContext.isPathNotProcessed(path)) {
678                                    continue;
679                            }
680    
681                            MDRAction action =
682                                    (MDRAction)portletDataContext.getZipEntryAsObject(path);
683    
684                            importAction(
685                                    portletDataContext, actionElement, importedRuleGroupInstance,
686                                    action);
687                    }
688            }
689    
690            protected void validateTargetLayoutPlid(
691                    Element actionElement, MDRAction action) {
692    
693                    String type = action.getType();
694    
695                    if (!type.equals(SiteRedirectActionHandler.class.getName())) {
696                            return;
697                    }
698    
699                    String targetLayoutUuid = actionElement.attributeValue("layout-uuid");
700    
701                    if (Validator.isNull(targetLayoutUuid)) {
702                            return;
703                    }
704    
705                    UnicodeProperties typeSettingsProperties =
706                            action.getTypeSettingsProperties();
707    
708                    long targetGroupId = GetterUtil.getLong(
709                            typeSettingsProperties.getProperty("groupId"));
710                    boolean privateLayout = GetterUtil.getBoolean(
711                            actionElement.attributeValue("private-layout"));
712    
713                    try {
714                            Layout targetLayout =
715                                    LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
716                                            targetLayoutUuid, targetGroupId, privateLayout);
717    
718                            typeSettingsProperties.setProperty(
719                                    "plid", String.valueOf(targetLayout.getPlid()));
720                    }
721                    catch (Exception e) {
722                            if (_log.isWarnEnabled()) {
723                                    _log.warn(
724                                            "Unable to find target layout with uuid " +
725                                                    targetLayoutUuid + " in group " + targetGroupId +
726                                                            ". Site redirect may not match target layout.",
727                                            e);
728                            }
729                    }
730            }
731    
732            private static final boolean _ALWAYS_EXPORTABLE = true;
733    
734            private static final boolean _ALWAYS_STAGED = true;
735    
736            private static final String _NAMESPACE = "mobile_device_rules";
737    
738            private static final boolean _PUBLISH_TO_LIVE_BY_DEFAULT = true;
739    
740            private static Log _log = LogFactoryUtil.getLog(
741                    MDRPortletDataHandlerImpl.class);
742    
743            private static PortletDataHandlerBoolean _ruleGroupInstances =
744                    new PortletDataHandlerBoolean(
745                            _NAMESPACE, "rule-group-instances", true, true);
746            private static PortletDataHandlerBoolean _ruleGroups =
747                    new PortletDataHandlerBoolean(_NAMESPACE, "rule-groups", true, true);
748    
749    }