@Override
  protected void doExportStagedModel(
      PortletDataContext portletDataContext, MDRRuleGroupInstance ruleGroupInstance)
      throws Exception {

    MDRRuleGroup ruleGroup =
        _mdrRuleGroupLocalService.getRuleGroup(ruleGroupInstance.getRuleGroupId());

    StagedModelDataHandlerUtil.exportReferenceStagedModel(
        portletDataContext, ruleGroupInstance, ruleGroup, PortletDataContext.REFERENCE_TYPE_PARENT);

    Element ruleGroupInstanceElement = portletDataContext.getExportDataElement(ruleGroupInstance);

    String className = ruleGroupInstance.getClassName();

    if (className.equals(Layout.class.getName())) {
      Layout layout = _layoutLocalService.getLayout(ruleGroupInstance.getClassPK());

      ruleGroupInstanceElement.addAttribute("layout-uuid", layout.getUuid());
    }

    portletDataContext.addClassedModel(
        ruleGroupInstanceElement,
        ExportImportPathUtil.getModelPath(ruleGroupInstance),
        ruleGroupInstance);
  }
  public static void check(
      PermissionChecker permissionChecker, MDRRuleGroupInstance ruleGroupInstance, String actionId)
      throws PortalException {

    if (!contains(permissionChecker, ruleGroupInstance, actionId)) {
      throw new PrincipalException.MustHavePermission(
          permissionChecker,
          MDRRuleGroupInstance.class.getName(),
          ruleGroupInstance.getRuleGroupInstanceId(),
          actionId);
    }
  }
  protected void inheritMobileRuleGroups(Layout layout, ServiceContext serviceContext)
      throws PortalException {

    List<MDRRuleGroupInstance> parentMDRRuleGroupInstances =
        mdrRuleGroupInstanceLocalService.getRuleGroupInstances(
            Layout.class.getName(), layout.getParentPlid());

    for (MDRRuleGroupInstance parentMDRRuleGroupInstance : parentMDRRuleGroupInstances) {

      MDRRuleGroupInstance mdrRuleGroupInstance =
          mdrRuleGroupInstanceService.addRuleGroupInstance(
              layout.getGroupId(),
              Layout.class.getName(),
              layout.getPlid(),
              parentMDRRuleGroupInstance.getRuleGroupId(),
              parentMDRRuleGroupInstance.getPriority(),
              serviceContext);

      List<MDRAction> parentMDRActions =
          mdrActionLocalService.getActions(parentMDRRuleGroupInstance.getRuleGroupInstanceId());

      for (MDRAction mdrAction : parentMDRActions) {
        mdrActionService.addAction(
            mdrRuleGroupInstance.getRuleGroupInstanceId(),
            mdrAction.getNameMap(),
            mdrAction.getDescriptionMap(),
            mdrAction.getType(),
            mdrAction.getTypeSettings(),
            serviceContext);
      }
    }
  }
  public static boolean contains(
      PermissionChecker permissionChecker,
      MDRRuleGroupInstance ruleGroupInstance,
      String actionId) {

    Boolean hasPermission =
        StagingPermissionUtil.hasPermission(
            permissionChecker,
            ruleGroupInstance.getGroupId(),
            MDRRuleGroupInstance.class.getName(),
            ruleGroupInstance.getRuleGroupInstanceId(),
            MDRPortletKeys.MOBILE_DEVICE_RULES,
            actionId);

    if (hasPermission != null) {
      return hasPermission.booleanValue();
    }

    return permissionChecker.hasPermission(
        ruleGroupInstance.getGroupId(),
        MDRRuleGroupInstance.class.getName(),
        ruleGroupInstance.getRuleGroupInstanceId(),
        actionId);
  }
  @Override
  protected void doImportStagedModel(
      PortletDataContext portletDataContext, MDRRuleGroupInstance ruleGroupInstance)
      throws Exception {

    long userId = portletDataContext.getUserId(ruleGroupInstance.getUserUuid());

    Map<Long, Long> ruleGroupIds =
        (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(MDRRuleGroup.class);

    Long ruleGroupId =
        MapUtil.getLong(
            ruleGroupIds, ruleGroupInstance.getRuleGroupId(), ruleGroupInstance.getRuleGroupId());

    long classPK = 0;

    Element ruleGroupInstanceElement =
        portletDataContext.getImportDataStagedModelElement(ruleGroupInstance);

    String layoutUuid = ruleGroupInstanceElement.attributeValue("layout-uuid");

    try {
      if (Validator.isNotNull(layoutUuid)) {
        Layout layout =
            _layoutLocalService.getLayoutByUuidAndGroupId(
                layoutUuid,
                portletDataContext.getScopeGroupId(),
                portletDataContext.isPrivateLayout());

        classPK = layout.getPrimaryKey();
      } else {
        LayoutSet layoutSet =
            _layoutSetLocalService.getLayoutSet(
                portletDataContext.getScopeGroupId(), portletDataContext.isPrivateLayout());

        classPK = layoutSet.getLayoutSetId();
      }
    } catch (Exception e) {
      if (_log.isWarnEnabled()) {
        StringBundler sb = new StringBundler(5);

        sb.append("Layout ");
        sb.append(layoutUuid);
        sb.append(" is missing for rule group instance ");
        sb.append(ruleGroupInstance.getRuleGroupInstanceId());
        sb.append(", skipping this rule group instance.");

        _log.warn(sb.toString());
      }

      return;
    }

    ServiceContext serviceContext = portletDataContext.createServiceContext(ruleGroupInstance);

    serviceContext.setUserId(userId);

    MDRRuleGroupInstance importedRuleGroupInstance = null;

    if (portletDataContext.isDataStrategyMirror()) {
      MDRRuleGroupInstance existingMDRRuleGroupInstance =
          fetchStagedModelByUuidAndGroupId(
              ruleGroupInstance.getUuid(), portletDataContext.getScopeGroupId());

      if (existingMDRRuleGroupInstance == null) {
        serviceContext.setUuid(ruleGroupInstance.getUuid());

        importedRuleGroupInstance =
            _mdrRuleGroupInstanceLocalService.addRuleGroupInstance(
                portletDataContext.getScopeGroupId(),
                ruleGroupInstance.getClassName(),
                classPK,
                ruleGroupId,
                ruleGroupInstance.getPriority(),
                serviceContext);
      } else {
        importedRuleGroupInstance =
            _mdrRuleGroupInstanceLocalService.updateRuleGroupInstance(
                existingMDRRuleGroupInstance.getRuleGroupInstanceId(),
                ruleGroupInstance.getPriority());
      }
    } else {
      importedRuleGroupInstance =
          _mdrRuleGroupInstanceLocalService.addRuleGroupInstance(
              portletDataContext.getScopeGroupId(),
              ruleGroupInstance.getClassName(),
              classPK,
              ruleGroupId,
              ruleGroupInstance.getPriority(),
              serviceContext);
    }

    portletDataContext.importClassedModel(ruleGroupInstance, importedRuleGroupInstance);
  }