@Test
  public void testUpdateExisting() throws Exception {
    long pk = ServiceTestUtil.nextLong();

    LayoutPrototype newLayoutPrototype = _persistence.create(pk);

    newLayoutPrototype.setMvccVersion(ServiceTestUtil.nextLong());

    newLayoutPrototype.setUuid(ServiceTestUtil.randomString());

    newLayoutPrototype.setCompanyId(ServiceTestUtil.nextLong());

    newLayoutPrototype.setUserId(ServiceTestUtil.nextLong());

    newLayoutPrototype.setUserName(ServiceTestUtil.randomString());

    newLayoutPrototype.setCreateDate(ServiceTestUtil.nextDate());

    newLayoutPrototype.setModifiedDate(ServiceTestUtil.nextDate());

    newLayoutPrototype.setName(ServiceTestUtil.randomString());

    newLayoutPrototype.setDescription(ServiceTestUtil.randomString());

    newLayoutPrototype.setSettings(ServiceTestUtil.randomString());

    newLayoutPrototype.setActive(ServiceTestUtil.randomBoolean());

    _persistence.update(newLayoutPrototype);

    LayoutPrototype existingLayoutPrototype =
        _persistence.findByPrimaryKey(newLayoutPrototype.getPrimaryKey());

    Assert.assertEquals(
        existingLayoutPrototype.getMvccVersion(), newLayoutPrototype.getMvccVersion());
    Assert.assertEquals(existingLayoutPrototype.getUuid(), newLayoutPrototype.getUuid());
    Assert.assertEquals(
        existingLayoutPrototype.getLayoutPrototypeId(), newLayoutPrototype.getLayoutPrototypeId());
    Assert.assertEquals(existingLayoutPrototype.getCompanyId(), newLayoutPrototype.getCompanyId());
    Assert.assertEquals(existingLayoutPrototype.getUserId(), newLayoutPrototype.getUserId());
    Assert.assertEquals(existingLayoutPrototype.getUserName(), newLayoutPrototype.getUserName());
    Assert.assertEquals(
        Time.getShortTimestamp(existingLayoutPrototype.getCreateDate()),
        Time.getShortTimestamp(newLayoutPrototype.getCreateDate()));
    Assert.assertEquals(
        Time.getShortTimestamp(existingLayoutPrototype.getModifiedDate()),
        Time.getShortTimestamp(newLayoutPrototype.getModifiedDate()));
    Assert.assertEquals(existingLayoutPrototype.getName(), newLayoutPrototype.getName());
    Assert.assertEquals(
        existingLayoutPrototype.getDescription(), newLayoutPrototype.getDescription());
    Assert.assertEquals(existingLayoutPrototype.getSettings(), newLayoutPrototype.getSettings());
    Assert.assertEquals(existingLayoutPrototype.getActive(), newLayoutPrototype.getActive());
  }
  @Override
  @SystemEvent(action = SystemEventConstants.ACTION_SKIP, type = SystemEventConstants.TYPE_DELETE)
  public LayoutPrototype deleteLayoutPrototype(LayoutPrototype layoutPrototype)
      throws PortalException, SystemException {

    // Group

    if (layoutPersistence.countByLayoutPrototypeUuid(layoutPrototype.getUuid()) > 0) {

      throw new RequiredLayoutPrototypeException();
    }

    Group group = layoutPrototype.getGroup();

    groupLocalService.deleteGroup(group);

    // Resources

    resourceLocalService.deleteResource(
        layoutPrototype.getCompanyId(),
        LayoutPrototype.class.getName(),
        ResourceConstants.SCOPE_INDIVIDUAL,
        layoutPrototype.getLayoutPrototypeId());

    // Layout Prototype

    layoutPrototypePersistence.remove(layoutPrototype);

    // Permission cache

    PermissionCacheUtil.clearCache();

    return layoutPrototype;
  }
  @Override
  protected void doImportStagedModel(
      PortletDataContext portletDataContext, LayoutPrototype layoutPrototype) throws Exception {

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

    ServiceContext serviceContext =
        portletDataContext.createServiceContext(
            layoutPrototype, LayoutPrototypePortletDataHandler.NAMESPACE);

    serviceContext.setAttribute("addDefaultLayout", false);

    LayoutPrototype importedLayoutPrototype = null;

    if (portletDataContext.isDataStrategyMirror()) {
      LayoutPrototype existingLayoutPrototype =
          LayoutPrototypeLocalServiceUtil.fetchLayoutPrototypeByUuidAndCompanyId(
              layoutPrototype.getUuid(), portletDataContext.getCompanyId());

      if (existingLayoutPrototype == null) {
        serviceContext.setUuid(layoutPrototype.getUuid());

        importedLayoutPrototype =
            LayoutPrototypeLocalServiceUtil.addLayoutPrototype(
                userId,
                portletDataContext.getCompanyId(),
                layoutPrototype.getNameMap(),
                layoutPrototype.getDescription(),
                layoutPrototype.isActive(),
                serviceContext);
      } else {
        importedLayoutPrototype =
            LayoutPrototypeLocalServiceUtil.updateLayoutPrototype(
                existingLayoutPrototype.getLayoutPrototypeId(),
                layoutPrototype.getNameMap(),
                layoutPrototype.getDescription(),
                layoutPrototype.isActive(),
                serviceContext);
      }
    } else {
      importedLayoutPrototype =
          LayoutPrototypeLocalServiceUtil.addLayoutPrototype(
              userId,
              portletDataContext.getCompanyId(),
              layoutPrototype.getNameMap(),
              layoutPrototype.getDescription(),
              layoutPrototype.isActive(),
              serviceContext);
    }

    importLayouts(portletDataContext, layoutPrototype, importedLayoutPrototype.getGroupId());

    portletDataContext.importClassedModel(
        layoutPrototype, importedLayoutPrototype, LayoutPrototypePortletDataHandler.NAMESPACE);
  }
  @Test
  public void testDynamicQueryByPrimaryKeyExisting() throws Exception {
    LayoutPrototype newLayoutPrototype = addLayoutPrototype();

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

    dynamicQuery.add(
        RestrictionsFactoryUtil.eq("layoutPrototypeId", newLayoutPrototype.getLayoutPrototypeId()));

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

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

    LayoutPrototype existingLayoutPrototype = result.get(0);

    Assert.assertEquals(existingLayoutPrototype, newLayoutPrototype);
  }
  @Override
  public List<LayoutPrototype> search(long companyId, Boolean active, OrderByComparator obc)
      throws PortalException, SystemException {

    List<LayoutPrototype> filteredLayoutPrototypes = new ArrayList<LayoutPrototype>();

    List<LayoutPrototype> layoutPrototypes =
        layoutPrototypeLocalService.search(
            companyId, active, QueryUtil.ALL_POS, QueryUtil.ALL_POS, obc);

    for (LayoutPrototype layoutPrototype : layoutPrototypes) {
      if (LayoutPrototypePermissionUtil.contains(
          getPermissionChecker(), layoutPrototype.getLayoutPrototypeId(), ActionKeys.VIEW)) {

        filteredLayoutPrototypes.add(layoutPrototype);
      }
    }

    return filteredLayoutPrototypes;
  }
  @Test
  public void testDynamicQueryByProjectionExisting() throws Exception {
    LayoutPrototype newLayoutPrototype = addLayoutPrototype();

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

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

    Object newLayoutPrototypeId = newLayoutPrototype.getLayoutPrototypeId();

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

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

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

    Object existingLayoutPrototypeId = result.get(0);

    Assert.assertEquals(existingLayoutPrototypeId, newLayoutPrototypeId);
  }
  @Override
  public LayoutPrototype addLayoutPrototype(
      long userId,
      long companyId,
      Map<Locale, String> nameMap,
      String description,
      boolean active,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    // Layout prototype

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

    long layoutPrototypeId = counterLocalService.increment();

    LayoutPrototype layoutPrototype = layoutPrototypePersistence.create(layoutPrototypeId);

    layoutPrototype.setUuid(serviceContext.getUuid());
    layoutPrototype.setCompanyId(companyId);
    layoutPrototype.setUserId(userId);
    layoutPrototype.setUserName(user.getFullName());
    layoutPrototype.setCreateDate(serviceContext.getCreateDate(now));
    layoutPrototype.setModifiedDate(serviceContext.getModifiedDate(now));
    layoutPrototype.setNameMap(nameMap);
    layoutPrototype.setDescription(description);
    layoutPrototype.setActive(active);

    layoutPrototypePersistence.update(layoutPrototype);

    // Resources

    if (userId > 0) {
      resourceLocalService.addResources(
          companyId,
          0,
          userId,
          LayoutPrototype.class.getName(),
          layoutPrototype.getLayoutPrototypeId(),
          false,
          false,
          false);
    }

    // Group

    String friendlyURL = "/template-" + layoutPrototype.getLayoutPrototypeId();

    Group group =
        groupLocalService.addGroup(
            userId,
            GroupConstants.DEFAULT_PARENT_GROUP_ID,
            LayoutPrototype.class.getName(),
            layoutPrototype.getLayoutPrototypeId(),
            GroupConstants.DEFAULT_LIVE_GROUP_ID,
            layoutPrototype.getName(LocaleUtil.getDefault()),
            null,
            0,
            true,
            GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION,
            friendlyURL,
            false,
            true,
            null);

    if (GetterUtil.getBoolean(serviceContext.getAttribute("addDefaultLayout"), true)) {

      layoutLocalService.addLayout(
          userId,
          group.getGroupId(),
          true,
          LayoutConstants.DEFAULT_PARENT_LAYOUT_ID,
          layoutPrototype.getName(LocaleUtil.getDefault()),
          null,
          null,
          LayoutConstants.TYPE_PORTLET,
          false,
          "/layout",
          serviceContext);
    }

    return layoutPrototype;
  }
 @Override
 public Long get(LayoutPrototype layoutPrototype) {
   return layoutPrototype.getLayoutPrototypeId();
 }