@Override
  public LayoutPrototype updateLayoutPrototype(
      long layoutPrototypeId,
      Map<Locale, String> nameMap,
      String description,
      boolean active,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    // Layout prototype

    LayoutPrototype layoutPrototype =
        layoutPrototypePersistence.findByPrimaryKey(layoutPrototypeId);

    layoutPrototype.setModifiedDate(serviceContext.getModifiedDate(new Date()));
    layoutPrototype.setNameMap(nameMap);
    layoutPrototype.setDescription(description);
    layoutPrototype.setActive(active);

    layoutPrototypePersistence.update(layoutPrototype);

    // Group

    Group group =
        groupLocalService.getLayoutPrototypeGroup(
            layoutPrototype.getCompanyId(), layoutPrototypeId);

    group.setName(layoutPrototype.getName(LocaleUtil.getDefault()));

    groupPersistence.update(group);

    // Layout

    Layout layout = layoutPrototype.getLayout();

    layout.setModifiedDate(layoutPrototype.getModifiedDate());
    layout.setNameMap(nameMap);

    layoutPersistence.update(layout);

    return layoutPrototype;
  }
  protected void runLayoutSetPrototype(
      boolean layoutSetLinkEnabled,
      boolean layoutLinkEnabled,
      boolean addPage,
      boolean deletePage,
      boolean useLayoutPrototype)
      throws Exception {

    LayoutSetPrototype layoutSetPrototype =
        ServiceTestUtil.addLayoutSetPrototype(ServiceTestUtil.randomString());

    Group layoutSetPrototypeGroup = layoutSetPrototype.getGroup();

    int layoutSetPrototypeLayoutsCount =
        LayoutLocalServiceUtil.getLayoutsCount(layoutSetPrototypeGroup, true);

    ServiceTestUtil.addLayout(
        layoutSetPrototypeGroup.getGroupId(), ServiceTestUtil.randomString(), true);
    ServiceTestUtil.addLayout(
        layoutSetPrototypeGroup.getGroupId(), ServiceTestUtil.randomString(), true);

    Group group = ServiceTestUtil.addGroup();

    SitesUtil.updateLayoutSetPrototypesLinks(
        group, layoutSetPrototype.getLayoutSetPrototypeId(), 0, layoutSetLinkEnabled, false);

    propagateChanges(group);

    int groupLayoutsCount = LayoutLocalServiceUtil.getLayoutsCount(group, false);

    Assert.assertEquals(groupLayoutsCount, layoutSetPrototypeLayoutsCount + 2);

    if (addPage) {
      Layout layout = null;

      if (useLayoutPrototype) {
        LayoutPrototype layoutPrototype =
            ServiceTestUtil.addLayoutPrototype(ServiceTestUtil.randomString());

        Layout layoutPrototypeLayout = layoutPrototype.getLayout();

        updateLayoutTemplateId(layoutPrototypeLayout, "2_2_columns");

        layout =
            addLayout(
                group.getGroupId(),
                ServiceTestUtil.randomString(),
                layoutPrototype,
                layoutLinkEnabled);

        if (layoutLinkEnabled) {
          layout = propagateChanges(layout);
        }

        updateLayoutTemplateId(layoutPrototypeLayout, "1_column");

        if (layoutLinkEnabled) {
          Assert.assertEquals(
              "2_2_columns",
              layout.getTypeSettingsProperty(LayoutTypePortletConstants.LAYOUT_TEMPLATE_ID));

          layout = propagateChanges(layout);
        }
      } else {

        // Database will store Date values without milliseconds. Wait
        // for more than one second to ensure that later queries can
        // correctly compare the Date values.

        Thread.sleep(2000);

        layout =
            ServiceTestUtil.addLayout(
                layoutSetPrototypeGroup.getGroupId(), ServiceTestUtil.randomString(), true);
      }

      if (!useLayoutPrototype) {
        groupLayoutsCount = LayoutLocalServiceUtil.getLayoutsCount(group, false);

        Assert.assertEquals(groupLayoutsCount, layoutSetPrototypeLayoutsCount + 2);
      }

      propagateChanges(group);

      groupLayoutsCount = LayoutLocalServiceUtil.getLayoutsCount(group, false);

      if (layoutSetLinkEnabled) {
        Assert.assertEquals(groupLayoutsCount, layoutSetPrototypeLayoutsCount + 3);

        if (useLayoutPrototype) {
          if (layoutLinkEnabled) {
            Assert.assertEquals(
                "1_column",
                layout.getTypeSettingsProperty(LayoutTypePortletConstants.LAYOUT_TEMPLATE_ID));
          } else {
            Assert.assertEquals(
                "2_2_columns",
                layout.getTypeSettingsProperty(LayoutTypePortletConstants.LAYOUT_TEMPLATE_ID));
          }
        }
      }

      if (deletePage) {
        LayoutLocalServiceUtil.deleteLayout(layout, true, ServiceTestUtil.getServiceContext());

        groupLayoutsCount = LayoutLocalServiceUtil.getLayoutsCount(group, false);

        if (layoutSetLinkEnabled) {
          if (!useLayoutPrototype) {
            Assert.assertEquals(groupLayoutsCount, layoutSetPrototypeLayoutsCount + 3);

            propagateChanges(group);
          }

          groupLayoutsCount = LayoutLocalServiceUtil.getLayoutsCount(group, false);
        }

        Assert.assertEquals(groupLayoutsCount, layoutSetPrototypeLayoutsCount + 2);
      }
    }
  }