public MDRRuleGroup copyRuleGroup(
      MDRRuleGroup ruleGroup, long groupId, ServiceContext serviceContext)
      throws PortalException, SystemException {

    Group group = groupPersistence.findByPrimaryKey(groupId);

    Map<Locale, String> nameMap = ruleGroup.getNameMap();

    for (Map.Entry<Locale, String> entry : nameMap.entrySet()) {
      Locale locale = entry.getKey();
      String name = entry.getValue();

      if (Validator.isNull(name)) {
        continue;
      }

      String postfix =
          LanguageUtil.get(locale, PropsValues.MOBILE_DEVICE_RULES_RULE_GROUP_COPY_POSTFIX);

      nameMap.put(locale, name.concat(StringPool.SPACE).concat(postfix));
    }

    MDRRuleGroup newRuleGroup =
        addRuleGroup(group.getGroupId(), nameMap, ruleGroup.getDescriptionMap(), serviceContext);

    List<MDRRule> rules = mdrRulePersistence.findByRuleGroupId(ruleGroup.getRuleGroupId());

    for (MDRRule rule : rules) {
      serviceContext.setUuid(PortalUUIDUtil.generate());

      mdrRuleLocalService.copyRule(rule, newRuleGroup.getRuleGroupId(), serviceContext);
    }

    return newRuleGroup;
  }
  public void deleteRuleGroup(MDRRuleGroup ruleGroup) throws SystemException {

    // Rule group

    mdrRuleGroupPersistence.remove(ruleGroup);

    // Rules

    mdrRuleLocalService.deleteRules(ruleGroup.getRuleGroupId());

    //	Rule group instances

    mdrRuleGroupInstanceLocalService.deleteRuleGroupInstances(ruleGroup.getRuleGroupId());
  }
  @Override
  @SystemEvent(action = SystemEventConstants.ACTION_SKIP, type = SystemEventConstants.TYPE_DELETE)
  public void deleteRuleGroup(MDRRuleGroup ruleGroup) throws SystemException {

    // Rule group

    mdrRuleGroupPersistence.remove(ruleGroup);

    // Rules

    mdrRuleLocalService.deleteRules(ruleGroup.getRuleGroupId());

    // Rule group instances

    mdrRuleGroupInstanceLocalService.deleteRuleGroupInstances(ruleGroup.getRuleGroupId());
  }
  @Test
  public void testUpdateExisting() throws Exception {
    long pk = RandomTestUtil.nextLong();

    MDRRuleGroup newMDRRuleGroup = _persistence.create(pk);

    newMDRRuleGroup.setUuid(RandomTestUtil.randomString());

    newMDRRuleGroup.setGroupId(RandomTestUtil.nextLong());

    newMDRRuleGroup.setCompanyId(RandomTestUtil.nextLong());

    newMDRRuleGroup.setUserId(RandomTestUtil.nextLong());

    newMDRRuleGroup.setUserName(RandomTestUtil.randomString());

    newMDRRuleGroup.setCreateDate(RandomTestUtil.nextDate());

    newMDRRuleGroup.setModifiedDate(RandomTestUtil.nextDate());

    newMDRRuleGroup.setName(RandomTestUtil.randomString());

    newMDRRuleGroup.setDescription(RandomTestUtil.randomString());

    _persistence.update(newMDRRuleGroup);

    MDRRuleGroup existingMDRRuleGroup =
        _persistence.findByPrimaryKey(newMDRRuleGroup.getPrimaryKey());

    Assert.assertEquals(existingMDRRuleGroup.getUuid(), newMDRRuleGroup.getUuid());
    Assert.assertEquals(existingMDRRuleGroup.getRuleGroupId(), newMDRRuleGroup.getRuleGroupId());
    Assert.assertEquals(existingMDRRuleGroup.getGroupId(), newMDRRuleGroup.getGroupId());
    Assert.assertEquals(existingMDRRuleGroup.getCompanyId(), newMDRRuleGroup.getCompanyId());
    Assert.assertEquals(existingMDRRuleGroup.getUserId(), newMDRRuleGroup.getUserId());
    Assert.assertEquals(existingMDRRuleGroup.getUserName(), newMDRRuleGroup.getUserName());
    Assert.assertEquals(
        Time.getShortTimestamp(existingMDRRuleGroup.getCreateDate()),
        Time.getShortTimestamp(newMDRRuleGroup.getCreateDate()));
    Assert.assertEquals(
        Time.getShortTimestamp(existingMDRRuleGroup.getModifiedDate()),
        Time.getShortTimestamp(newMDRRuleGroup.getModifiedDate()));
    Assert.assertEquals(existingMDRRuleGroup.getName(), newMDRRuleGroup.getName());
    Assert.assertEquals(existingMDRRuleGroup.getDescription(), newMDRRuleGroup.getDescription());
  }
  @Override
  protected StagedModel addStagedModel(
      Group group, Map<String, List<StagedModel>> dependentStagedModelsMap) throws Exception {

    List<StagedModel> dependentStagedModels =
        dependentStagedModelsMap.get(MDRRuleGroup.class.getName());

    MDRRuleGroup ruleGroup = (MDRRuleGroup) dependentStagedModels.get(0);

    return MDRTestUtil.addRuleGroupInstance(
        group.getGroupId(), Layout.class.getName(), layout.getPlid(), ruleGroup.getRuleGroupId());
  }
  @Test
  public void testDynamicQueryByPrimaryKeyExisting() throws Exception {
    MDRRuleGroup newMDRRuleGroup = addMDRRuleGroup();

    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(MDRRuleGroup.class, MDRRuleGroup.class.getClassLoader());

    dynamicQuery.add(RestrictionsFactoryUtil.eq("ruleGroupId", newMDRRuleGroup.getRuleGroupId()));

    List<MDRRuleGroup> result = _persistence.findWithDynamicQuery(dynamicQuery);

    Assert.assertEquals(1, result.size());

    MDRRuleGroup existingMDRRuleGroup = result.get(0);

    Assert.assertEquals(existingMDRRuleGroup, newMDRRuleGroup);
  }
  @Override
  protected void doImportStagedModel(PortletDataContext portletDataContext, MDRRuleGroup ruleGroup)
      throws Exception {

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

    ServiceContext serviceContext = portletDataContext.createServiceContext(ruleGroup);

    serviceContext.setUserId(userId);

    MDRRuleGroup importedRuleGroup = null;

    if (portletDataContext.isDataStrategyMirror()) {
      MDRRuleGroup existingRuleGroup =
          fetchStagedModelByUuidAndGroupId(
              ruleGroup.getUuid(), portletDataContext.getScopeGroupId());

      if (existingRuleGroup == null) {
        serviceContext.setUuid(ruleGroup.getUuid());

        importedRuleGroup =
            MDRRuleGroupLocalServiceUtil.addRuleGroup(
                portletDataContext.getScopeGroupId(),
                ruleGroup.getNameMap(),
                ruleGroup.getDescriptionMap(),
                serviceContext);
      } else {
        importedRuleGroup =
            MDRRuleGroupLocalServiceUtil.updateRuleGroup(
                existingRuleGroup.getRuleGroupId(),
                ruleGroup.getNameMap(),
                ruleGroup.getDescriptionMap(),
                serviceContext);
      }
    } else {
      importedRuleGroup =
          MDRRuleGroupLocalServiceUtil.addRuleGroup(
              portletDataContext.getScopeGroupId(),
              ruleGroup.getNameMap(),
              ruleGroup.getDescriptionMap(),
              serviceContext);
    }

    portletDataContext.importClassedModel(ruleGroup, importedRuleGroup);
  }
  @Test
  public void testDynamicQueryByProjectionExisting() throws Exception {
    MDRRuleGroup newMDRRuleGroup = addMDRRuleGroup();

    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(MDRRuleGroup.class, MDRRuleGroup.class.getClassLoader());

    dynamicQuery.setProjection(ProjectionFactoryUtil.property("ruleGroupId"));

    Object newRuleGroupId = newMDRRuleGroup.getRuleGroupId();

    dynamicQuery.add(RestrictionsFactoryUtil.in("ruleGroupId", new Object[] {newRuleGroupId}));

    List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);

    Assert.assertEquals(1, result.size());

    Object existingRuleGroupId = result.get(0);

    Assert.assertEquals(existingRuleGroupId, newRuleGroupId);
  }