protected MDRRuleGroup addMDRRuleGroup() throws Exception {
    long pk = RandomTestUtil.nextLong();

    MDRRuleGroup mdrRuleGroup = _persistence.create(pk);

    mdrRuleGroup.setUuid(RandomTestUtil.randomString());

    mdrRuleGroup.setGroupId(RandomTestUtil.nextLong());

    mdrRuleGroup.setCompanyId(RandomTestUtil.nextLong());

    mdrRuleGroup.setUserId(RandomTestUtil.nextLong());

    mdrRuleGroup.setUserName(RandomTestUtil.randomString());

    mdrRuleGroup.setCreateDate(RandomTestUtil.nextDate());

    mdrRuleGroup.setModifiedDate(RandomTestUtil.nextDate());

    mdrRuleGroup.setName(RandomTestUtil.randomString());

    mdrRuleGroup.setDescription(RandomTestUtil.randomString());

    _persistence.update(mdrRuleGroup);

    return mdrRuleGroup;
  }
  @Override
  public MDRRuleGroup addRuleGroup(
      long groupId,
      Map<Locale, String> nameMap,
      Map<Locale, String> descriptionMap,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    User user = userPersistence.findByPrimaryKey(serviceContext.getUserId());
    Date now = new Date();

    long ruleGroupId = counterLocalService.increment();

    MDRRuleGroup ruleGroup = createMDRRuleGroup(ruleGroupId);

    ruleGroup.setUuid(serviceContext.getUuid());
    ruleGroup.setGroupId(groupId);
    ruleGroup.setCompanyId(serviceContext.getCompanyId());
    ruleGroup.setCreateDate(serviceContext.getCreateDate(now));
    ruleGroup.setModifiedDate(serviceContext.getModifiedDate(now));
    ruleGroup.setUserId(user.getUserId());
    ruleGroup.setUserName(user.getFullName());
    ruleGroup.setNameMap(nameMap);
    ruleGroup.setDescriptionMap(descriptionMap);

    return updateMDRRuleGroup(ruleGroup);
  }
  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;
  }
  @Test
  public void testFetchByPrimaryKeyExisting() throws Exception {
    MDRRuleGroup newMDRRuleGroup = addMDRRuleGroup();

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

    Assert.assertEquals(existingMDRRuleGroup, newMDRRuleGroup);
  }
  @Test
  public void testCreate() throws Exception {
    long pk = RandomTestUtil.nextLong();

    MDRRuleGroup mdrRuleGroup = _persistence.create(pk);

    Assert.assertNotNull(mdrRuleGroup);

    Assert.assertEquals(mdrRuleGroup.getPrimaryKey(), pk);
  }
  @Test
  public void testRemove() throws Exception {
    MDRRuleGroup newMDRRuleGroup = addMDRRuleGroup();

    _persistence.remove(newMDRRuleGroup);

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

    Assert.assertNull(existingMDRRuleGroup);
  }
  @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 testFetchByPrimaryKeysWithOnePrimaryKey() throws Exception {
    MDRRuleGroup newMDRRuleGroup = addMDRRuleGroup();

    Set<Serializable> primaryKeys = new HashSet<Serializable>();

    primaryKeys.add(newMDRRuleGroup.getPrimaryKey());

    Map<Serializable, MDRRuleGroup> mdrRuleGroups = _persistence.fetchByPrimaryKeys(primaryKeys);

    Assert.assertEquals(1, mdrRuleGroups.size());
    Assert.assertEquals(newMDRRuleGroup, mdrRuleGroups.get(newMDRRuleGroup.getPrimaryKey()));
  }
  @Override
  protected void validateImport(
      Map<String, List<StagedModel>> dependentStagedModelsMap, Group group) throws Exception {

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

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

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

    MDRRuleGroupLocalServiceUtil.getMDRRuleGroupByUuidAndGroupId(
        ruleGroup.getUuid(), group.getGroupId());
  }
  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());
  }
  /**
   * Adds the m d r rule group to the database. Also notifies the appropriate model listeners.
   *
   * @param mdrRuleGroup the m d r rule group
   * @return the m d r rule group that was added
   * @throws SystemException if a system exception occurred
   */
  @Indexable(type = IndexableType.REINDEX)
  @Override
  public MDRRuleGroup addMDRRuleGroup(MDRRuleGroup mdrRuleGroup) throws SystemException {
    mdrRuleGroup.setNew(true);

    return mdrRuleGroupPersistence.update(mdrRuleGroup);
  }
  @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);
  }
  public MDRRuleGroup updateRuleGroup(
      long ruleGroupId,
      Map<Locale, String> nameMap,
      Map<Locale, String> descriptionMap,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    MDRRuleGroup ruleGroup = mdrRuleGroupPersistence.findByPrimaryKey(ruleGroupId);

    ruleGroup.setModifiedDate(serviceContext.getModifiedDate(null));
    ruleGroup.setNameMap(nameMap);
    ruleGroup.setDescriptionMap(descriptionMap);

    mdrRuleGroupPersistence.update(ruleGroup, false);

    return ruleGroup;
  }
  @Test
  public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereSomePrimaryKeysExist()
      throws Exception {
    MDRRuleGroup newMDRRuleGroup = addMDRRuleGroup();

    long pk = RandomTestUtil.nextLong();

    Set<Serializable> primaryKeys = new HashSet<Serializable>();

    primaryKeys.add(newMDRRuleGroup.getPrimaryKey());
    primaryKeys.add(pk);

    Map<Serializable, MDRRuleGroup> mdrRuleGroups = _persistence.fetchByPrimaryKeys(primaryKeys);

    Assert.assertEquals(1, mdrRuleGroups.size());
    Assert.assertEquals(newMDRRuleGroup, mdrRuleGroups.get(newMDRRuleGroup.getPrimaryKey()));
  }
  @Test
  public void testResetOriginalValues() throws Exception {
    if (!PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
      return;
    }

    MDRRuleGroup newMDRRuleGroup = addMDRRuleGroup();

    _persistence.clearCache();

    MDRRuleGroupModelImpl existingMDRRuleGroupModelImpl =
        (MDRRuleGroupModelImpl) _persistence.findByPrimaryKey(newMDRRuleGroup.getPrimaryKey());

    Assert.assertTrue(
        Validator.equals(
            existingMDRRuleGroupModelImpl.getUuid(),
            existingMDRRuleGroupModelImpl.getOriginalUuid()));
    Assert.assertEquals(
        existingMDRRuleGroupModelImpl.getGroupId(),
        existingMDRRuleGroupModelImpl.getOriginalGroupId());
  }
  @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);
  }
  @Test
  public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereAllPrimaryKeysExist()
      throws Exception {
    MDRRuleGroup newMDRRuleGroup1 = addMDRRuleGroup();
    MDRRuleGroup newMDRRuleGroup2 = addMDRRuleGroup();

    Set<Serializable> primaryKeys = new HashSet<Serializable>();

    primaryKeys.add(newMDRRuleGroup1.getPrimaryKey());
    primaryKeys.add(newMDRRuleGroup2.getPrimaryKey());

    Map<Serializable, MDRRuleGroup> mdrRuleGroups = _persistence.fetchByPrimaryKeys(primaryKeys);

    Assert.assertEquals(2, mdrRuleGroups.size());
    Assert.assertEquals(newMDRRuleGroup1, mdrRuleGroups.get(newMDRRuleGroup1.getPrimaryKey()));
    Assert.assertEquals(newMDRRuleGroup2, mdrRuleGroups.get(newMDRRuleGroup2.getPrimaryKey()));
  }
  @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
 public String getDisplayName(MDRRuleGroup ruleGroup) {
   return ruleGroup.getNameCurrentValue();
 }
  @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);
  }