public static void enforceNoLazyInitialization(ContentEntity content) {
   content.setCategory(
       content.getCategory() != null ? new CategoryEntity(content.getCategory()) : null);
   content.setAssignee(
       content.getAssignee() != null ? new UserEntity(content.getAssignee()) : null);
   content.setAssigner(
       content.getAssigner() != null ? new UserEntity(content.getAssigner()) : null);
   content.setOwner(content.getOwner() != null ? new UserEntity(content.getOwner()) : null);
   content.setLanguage(
       content.getLanguage() != null ? new LanguageEntity(content.getLanguage()) : null);
   content.setDraftVersion(
       content.getDraftVersion() != null
           ? new ContentVersionEntity(content.getDraftVersion())
           : null);
   content.setMainVersion(
       content.getMainVersion() != null
           ? new ContentVersionEntity(content.getMainVersion())
           : null);
 }
  @Test
  public void testCreateContentWithBinary() {
    fixture.createAndStoreNormalUserWithUserGroup("testuser", "Test user", "testuserstore");

    fixture.save(
        factory.createContentHandler(
            "Custom content", ContentHandlerName.CUSTOM.getHandlerClassShortName()));
    fixture.save(
        factory.createContentType(
            "MyContentType", ContentHandlerName.CUSTOM.getHandlerClassShortName(), standardConfig));
    fixture.save(factory.createUnit("MyUnit", "en"));
    fixture.save(
        factory.createCategory("MyCategory", "MyContentType", "MyUnit", "testuser", "testuser"));
    fixture.save(factory.createCategoryAccessForUser("MyCategory", "testuser", "read,create"));

    fixture.flushAndClearHibernateSesssion();

    UserEntity runningUser = fixture.findUserByName("testuser");
    PortalSecurityHolder.setImpersonatedUser(runningUser.getKey());

    ContentDataInput contentData = new ContentDataInput("MyContentType");
    contentData.add(new TextInput("myTitle", "testtitle"));
    contentData.add(new BinaryInput("myBinaryfile", dummyBinary, "dummyBinary"));

    CreateContentParams params = new CreateContentParams();
    params.categoryKey = fixture.findCategoryByName("MyCategory").getKey().toInt();
    params.contentData = contentData;
    params.publishFrom = new Date();
    params.publishTo = null;
    params.status = ContentStatus.STATUS_DRAFT;
    int contentKey = internalClient.createContent(params);

    fixture.flushAndClearHibernateSesssion();

    ContentEntity persistedContent = fixture.findContentByKey(new ContentKey(contentKey));
    assertNotNull(persistedContent);
    assertEquals("MyCategory", persistedContent.getCategory().getName());
    ContentVersionEntity persistedVersion = persistedContent.getMainVersion();
    assertNotNull(persistedVersion);
    assertEquals("testtitle", persistedVersion.getTitle());
    assertEquals(
        com.enonic.cms.core.content.ContentStatus.DRAFT.getKey(),
        persistedVersion.getStatus().getKey());

    // verify binary was saved
    Set<ContentBinaryDataEntity> contentBinaryDatas = persistedVersion.getContentBinaryData();
    assertEquals(1, contentBinaryDatas.size());
    ContentBinaryDataEntity contentBinaryData = contentBinaryDatas.iterator().next();
    BinaryDataEntity binaryData = contentBinaryData.getBinaryData();
    assertEquals("dummyBinary", binaryData.getName());

    CustomContentData customContentData = (CustomContentData) persistedVersion.getContentData();
    assertNotNull(customContentData);
  }
  private void doExecuteSetContentHomeCommand(final SetContentHomeCommand command) {
    Preconditions.checkNotNull(command.getSetter(), "setter cannot be null");
    Preconditions.checkNotNull(command.getContent(), "content to set home for cannot be null");
    Preconditions.checkNotNull(command.getSection(), "section to set home to cannot be null");

    final UserEntity setter = doResolveUser(command.getSetter(), "setter");
    final ContentEntity content = contentDao.findByKey(command.getContent());
    final MenuItemEntity section = doResolveSection(command.getSection());

    final CategoryAccessResolver categoryAccessResolver = new CategoryAccessResolver(groupDao);
    if (!categoryAccessResolver.hasApproveContentAccess(setter, content.getCategory())) {
      throw new CategoryAccessException(
          "Cannot set content home",
          setter.getQualifiedName(),
          CategoryAccessType.APPROVE,
          content.getCategory().getKey());
    }

    PageTemplateEntity pageTemplate = null;
    if (command.getPageTemplate() != null) {
      pageTemplate = pageTemplateDao.findByKey(command.getPageTemplate().toInt());
    }
    doSetContentHome(content, section, pageTemplate);
  }
 private ContentTypeEntity resolveContentType(int contentKey) {
   ContentEntity persistedContent = contentDao.findByKey(new ContentKey(contentKey));
   return persistedContent.getCategory().getContentType();
 }
  private void doExecuteAddContentToSectionCommand(final AddContentToSectionCommand command) {
    Preconditions.checkNotNull(command, "a command is required");
    Preconditions.checkNotNull(
        command.getContributor(), "the command's contributor argument is required");
    Preconditions.checkNotNull(command.getContent(), "the command's content argument is required");
    Preconditions.checkNotNull(command.getSection(), "the command's section argument is required");
    if (!command.isApproveInSection()) {
      Preconditions.checkArgument(
          !command.isAddOnTop(), "no point in adding content to top when not approving");
    }

    final ContentEntity content = contentDao.findByKey(command.getContent());
    Preconditions.checkNotNull(content, "content does not exist: " + command.getContent());
    Preconditions.checkArgument(
        !content.isDeleted(), "content is deleted: " + command.getContent());

    final MenuItemEntity section = doResolveSection(command.getSection());
    if (!section.isOrderedSection()) {
      Preconditions.checkArgument(
          command.getOrderContentsInSectionCommand() == null,
          "section is not ordered, did not expect to get order specified in command");
    }

    final UserEntity contributor = doResolveUser(command.getContributor(), "contributor");
    final MenuItemAccessResolver menuItemAccessResolver = new MenuItemAccessResolver(groupDao);
    menuItemAccessResolver.checkAccessToAddContentToSection(
        contributor, section, "Cannot add content in section.");
    if (command.isApproveInSection()) {
      menuItemAccessResolver.checkAccessToApproveContentInSection(
          contributor, section, "Cannot approve content in section.");
    }

    if (section.isSection() && section.hasSectionContentTypeFilter()) {
      if (!section.supportsSectionContentType(content.getCategory().getContentType())) {
        throw new ContentTypeNotSupportedException(content.getCategory().getContentType(), section);
      }
    } else if (section.getType() == MenuItemType.PAGE
        && section.getPage().getTemplate().getContentTypes().size() > 0) {
      if (!section
          .getPage()
          .getTemplate()
          .supportsContentType(content.getCategory().getContentType())) {
        throw new ContentTypeNotSupportedException(content.getCategory().getContentType(), section);
      }
    }

    final SectionContentEntity sectionContent = new SectionContentEntity();
    sectionContent.setOrder(0);
    sectionContent.setContent(content);
    sectionContent.setMenuItem(section);
    sectionContent.setApproved(command.isApproveInSection());
    sectionContent.setTimestamp(timeService.getNowAsDateTime().toDate());
    if (command.isAddOnTop() && section.isOrderedSection()) {
      sectionContent.setOrder(resolveOrderValueForInsertOnTopOfApprovedContentInSection(section));
    }

    content.addSectionContent(sectionContent);
    sectionContentDao.getHibernateTemplate().flush();

    sectionContentDao
        .getHibernateTemplate()
        .getSessionFactory()
        .evictCollection(MenuItemEntity.class.getName() + ".sectionContents", section.getKey());
    if (!content.hasHome(section.getSite().getKey())) {
      doSetContentHome(content, section, null);
    }

    if (section.isOrderedSection()) {
      if (command.getOrderContentsInSectionCommand() != null) {
        // ensure section will have it's newly added content
        sectionContentDao.getHibernateTemplate().refresh(section);

        List<ContentKey> wantedOrder = command.getOrderContentsInSectionCommand().getWantedOrder();
        ContentsInSectionOrderer orderer =
            new ContentsInSectionOrderer(wantedOrder, section, ORDER_SPACE);
        orderer.order();
      }
    }

    indexTransactionService.registerUpdate(command.getContent(), true);
  }