protected LayoutSet toUnwrappedModel(LayoutSet layoutSet) {
    if (layoutSet instanceof LayoutSetImpl) {
      return layoutSet;
    }

    LayoutSetImpl layoutSetImpl = new LayoutSetImpl();

    layoutSetImpl.setNew(layoutSet.isNew());
    layoutSetImpl.setPrimaryKey(layoutSet.getPrimaryKey());

    layoutSetImpl.setLayoutSetId(layoutSet.getLayoutSetId());
    layoutSetImpl.setGroupId(layoutSet.getGroupId());
    layoutSetImpl.setCompanyId(layoutSet.getCompanyId());
    layoutSetImpl.setPrivateLayout(layoutSet.isPrivateLayout());
    layoutSetImpl.setLogo(layoutSet.isLogo());
    layoutSetImpl.setLogoId(layoutSet.getLogoId());
    layoutSetImpl.setThemeId(layoutSet.getThemeId());
    layoutSetImpl.setColorSchemeId(layoutSet.getColorSchemeId());
    layoutSetImpl.setWapThemeId(layoutSet.getWapThemeId());
    layoutSetImpl.setWapColorSchemeId(layoutSet.getWapColorSchemeId());
    layoutSetImpl.setCss(layoutSet.getCss());
    layoutSetImpl.setPageCount(layoutSet.getPageCount());
    layoutSetImpl.setSettings(layoutSet.getSettings());
    layoutSetImpl.setLayoutSetPrototypeId(layoutSet.getLayoutSetPrototypeId());

    return layoutSetImpl;
  }
  @Test
  public void testDynamicQueryByPrimaryKeyExisting() throws Exception {
    LayoutSet newLayoutSet = addLayoutSet();

    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(LayoutSet.class, _dynamicQueryClassLoader);

    dynamicQuery.add(RestrictionsFactoryUtil.eq("layoutSetId", newLayoutSet.getLayoutSetId()));

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

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

    LayoutSet existingLayoutSet = result.get(0);

    Assert.assertEquals(existingLayoutSet, newLayoutSet);
  }
  @Test
  public void testDynamicQueryByProjectionExisting() throws Exception {
    LayoutSet newLayoutSet = addLayoutSet();

    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(LayoutSet.class, _dynamicQueryClassLoader);

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

    Object newLayoutSetId = newLayoutSet.getLayoutSetId();

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

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

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

    Object existingLayoutSetId = result.get(0);

    Assert.assertEquals(existingLayoutSetId, newLayoutSetId);
  }
  @Test
  public void testUpdateExisting() throws Exception {
    long pk = RandomTestUtil.nextLong();

    LayoutSet newLayoutSet = _persistence.create(pk);

    newLayoutSet.setMvccVersion(RandomTestUtil.nextLong());

    newLayoutSet.setGroupId(RandomTestUtil.nextLong());

    newLayoutSet.setCompanyId(RandomTestUtil.nextLong());

    newLayoutSet.setCreateDate(RandomTestUtil.nextDate());

    newLayoutSet.setModifiedDate(RandomTestUtil.nextDate());

    newLayoutSet.setPrivateLayout(RandomTestUtil.randomBoolean());

    newLayoutSet.setLogoId(RandomTestUtil.nextLong());

    newLayoutSet.setThemeId(RandomTestUtil.randomString());

    newLayoutSet.setColorSchemeId(RandomTestUtil.randomString());

    newLayoutSet.setWapThemeId(RandomTestUtil.randomString());

    newLayoutSet.setWapColorSchemeId(RandomTestUtil.randomString());

    newLayoutSet.setCss(RandomTestUtil.randomString());

    newLayoutSet.setPageCount(RandomTestUtil.nextInt());

    newLayoutSet.setSettings(RandomTestUtil.randomString());

    newLayoutSet.setLayoutSetPrototypeUuid(RandomTestUtil.randomString());

    newLayoutSet.setLayoutSetPrototypeLinkEnabled(RandomTestUtil.randomBoolean());

    _layoutSets.add(_persistence.update(newLayoutSet));

    LayoutSet existingLayoutSet = _persistence.findByPrimaryKey(newLayoutSet.getPrimaryKey());

    Assert.assertEquals(existingLayoutSet.getMvccVersion(), newLayoutSet.getMvccVersion());
    Assert.assertEquals(existingLayoutSet.getLayoutSetId(), newLayoutSet.getLayoutSetId());
    Assert.assertEquals(existingLayoutSet.getGroupId(), newLayoutSet.getGroupId());
    Assert.assertEquals(existingLayoutSet.getCompanyId(), newLayoutSet.getCompanyId());
    Assert.assertEquals(
        Time.getShortTimestamp(existingLayoutSet.getCreateDate()),
        Time.getShortTimestamp(newLayoutSet.getCreateDate()));
    Assert.assertEquals(
        Time.getShortTimestamp(existingLayoutSet.getModifiedDate()),
        Time.getShortTimestamp(newLayoutSet.getModifiedDate()));
    Assert.assertEquals(existingLayoutSet.getPrivateLayout(), newLayoutSet.getPrivateLayout());
    Assert.assertEquals(existingLayoutSet.getLogoId(), newLayoutSet.getLogoId());
    Assert.assertEquals(existingLayoutSet.getThemeId(), newLayoutSet.getThemeId());
    Assert.assertEquals(existingLayoutSet.getColorSchemeId(), newLayoutSet.getColorSchemeId());
    Assert.assertEquals(existingLayoutSet.getWapThemeId(), newLayoutSet.getWapThemeId());
    Assert.assertEquals(
        existingLayoutSet.getWapColorSchemeId(), newLayoutSet.getWapColorSchemeId());
    Assert.assertEquals(existingLayoutSet.getCss(), newLayoutSet.getCss());
    Assert.assertEquals(existingLayoutSet.getPageCount(), newLayoutSet.getPageCount());
    Assert.assertEquals(existingLayoutSet.getSettings(), newLayoutSet.getSettings());
    Assert.assertEquals(
        existingLayoutSet.getLayoutSetPrototypeUuid(), newLayoutSet.getLayoutSetPrototypeUuid());
    Assert.assertEquals(
        existingLayoutSet.getLayoutSetPrototypeLinkEnabled(),
        newLayoutSet.getLayoutSetPrototypeLinkEnabled());
  }
  @Override
  protected void doImportStagedModel(
      PortletDataContext portletDataContext,
      Element ruleGroupInstanceElement,
      String path,
      MDRRuleGroupInstance ruleGroupInstance)
      throws Exception {

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

    String ruleGroupPath =
        StagedModelPathUtil.getPath(
            portletDataContext, MDRRuleGroup.class.getName(), ruleGroupInstance.getRuleGroupId());

    MDRRuleGroup ruleGroup = (MDRRuleGroup) portletDataContext.getZipEntryAsObject(ruleGroupPath);

    StagedModelDataHandlerUtil.importStagedModel(
        portletDataContext, ruleGroupInstanceElement, ruleGroupPath, ruleGroup);

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

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

    long classPK = 0;

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

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

        classPK = layout.getPrimaryKey();
      } else {
        LayoutSet layoutSet =
            LayoutSetLocalServiceUtil.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(
            ruleGroupInstanceElement, ruleGroupInstance, MDRPortletDataHandler.NAMESPACE);

    serviceContext.setUserId(userId);

    MDRRuleGroupInstance importedRuleGroupInstance = null;

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

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

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

    portletDataContext.importClassedModel(
        ruleGroupInstance, importedRuleGroupInstance, MDRPortletDataHandler.NAMESPACE);
  }
  @Override
  public JournalArticleDisplay getDisplay(
      long groupId,
      String articleId,
      double version,
      String ddmTemplateKey,
      String viewMode,
      String languageId,
      int page,
      PortletRequestModel portletRequestModel,
      ThemeDisplay themeDisplay) {

    StopWatch stopWatch = new StopWatch();

    stopWatch.start();

    articleId = StringUtil.toUpperCase(GetterUtil.getString(articleId));
    ddmTemplateKey = StringUtil.toUpperCase(GetterUtil.getString(ddmTemplateKey));

    long layoutSetId = 0;
    boolean secure = false;

    if (themeDisplay != null) {
      try {
        if (!JournalArticlePermission.contains(
            themeDisplay.getPermissionChecker(), groupId, articleId, ActionKeys.VIEW)) {

          return null;
        }
      } catch (Exception e) {
      }

      LayoutSet layoutSet = themeDisplay.getLayoutSet();

      layoutSetId = layoutSet.getLayoutSetId();

      secure = themeDisplay.isSecure();
    }

    JournalContentKey journalContentKey =
        new JournalContentKey(
            groupId,
            articleId,
            version,
            ddmTemplateKey,
            layoutSetId,
            viewMode,
            languageId,
            page,
            secure);

    JournalArticleDisplay articleDisplay = _getPortalCache().get(journalContentKey);

    boolean lifecycleRender = false;

    if (portletRequestModel != null) {
      lifecycleRender = RenderRequest.RENDER_PHASE.equals(portletRequestModel.getLifecycle());
    }

    if ((articleDisplay == null) || !lifecycleRender) {
      articleDisplay =
          getArticleDisplay(
              groupId,
              articleId,
              ddmTemplateKey,
              viewMode,
              languageId,
              page,
              portletRequestModel,
              themeDisplay);

      if ((articleDisplay != null) && articleDisplay.isCacheable() && lifecycleRender) {

        _getPortalCache().put(journalContentKey, articleDisplay);
      }
    }

    if (_log.isDebugEnabled()) {
      _log.debug(
          "getDisplay for {"
              + groupId
              + ", "
              + articleId
              + ", "
              + ddmTemplateKey
              + ", "
              + viewMode
              + ", "
              + languageId
              + ", "
              + page
              + "} takes "
              + stopWatch.getTime()
              + " ms");
    }

    return articleDisplay;
  }