public void assignContent(AssignContentParams params) {
    final UserEntity assigner = securityService.getImpersonatedPortalUser();
    final UserParser userParser =
        new UserParser(
            securityService, userStoreService, userDao, new UserStoreParser(userStoreDao));
    final UserEntity assignee = userParser.parseUser(params.assignee);
    final ContentKey contentToAssignOn = new ContentKey(params.contentKey);

    AssignContentCommand command = new AssignContentCommand();
    command.setContentKey(contentToAssignOn);
    command.setAssignerKey(assigner.getKey());
    command.setAssigneeKey(assignee.getKey());
    command.setAssignmentDueDate(params.assignmentDueDate);
    command.setAssignmentDescription(params.assignmentDescription);
    contentService.assignContent(command);
  }
  public void unassignContent(UnassignContentParams params) {
    final UserEntity unassigner = securityService.getImpersonatedPortalUser();

    UnassignContentCommand command = new UnassignContentCommand();
    command.setContentKey(new ContentKey(params.contentKey));
    command.setUnassigner(unassigner.getKey());
    contentService.unassignContent(command);
  }
  public void snapshotContent(SnapshotContentParams params) {
    final UserEntity modifier = securityService.getImpersonatedPortalUser();

    SnapshotContentCommand snapshotCommand = new SnapshotContentCommand();
    snapshotCommand.setSnapshotComment(params.snapshotComment);
    snapshotCommand.setSnapshotterKey(modifier.getKey());
    snapshotCommand.setContentKey(new ContentKey(params.contentKey));
    snapshotCommand.setClearCommentInDraft(params.clearCommentInDraft);

    contentService.snapshotContent(snapshotCommand);
  }
  @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);
  }
  @Test
  public void request_user_image() throws Exception {
    byte[] bytes = loadImage("Arn.JPG");
    UserEntity testUser = fixture.findUserByName("testuser");
    testUser.setPhoto(bytes);

    fixture.flushAndClearHibernateSesssion();

    String imageRequestPath = "_image/user/" + testUser.getKey() + ".jpg";
    setPathInfoAndRequestURI(httpServletRequest, imageRequestPath);
    httpServletRequest.setParameter("_background", "0xffffff");
    httpServletRequest.setParameter("_quality", "100");
    imageController.handleRequestInternal(httpServletRequest, httpServletResponse);

    assertEquals(HttpServletResponse.SC_OK, httpServletResponse.getStatus());
    assertEquals("image/jpg", httpServletResponse.getContentType());
  }
  public int createCategory(CreateCategoryParams params) {
    if (params.contentTypeKey != null && params.contentTypeName != null) {
      throw new ClientException("Specify content type by either key or name, not both");
    }

    if (params.parentCategoryKey == null) {
      throw new ClientException("The parent category must be set");
    }
    CategoryEntity parentCategory =
        categoryDao.findByKey(new CategoryKey(params.parentCategoryKey));
    if (parentCategory == null) {
      throw new ClientException("The parent category does not exist");
    }

    UserEntity runningUser = securityService.getImpersonatedPortalUser();

    CategoryAccessResolver categoryAccessResolver = new CategoryAccessResolver(groupDao);
    if (!categoryAccessResolver.hasAccess(
        runningUser, parentCategory, CategoryAccessType.ADMINISTRATE)) {
      throw new ClientException(
          "The currently logged in user does not have create access on the category");
    }

    ContentTypeKey contentTypeKey = null;
    if (params.contentTypeKey != null) {
      contentTypeKey = new ContentTypeKey(params.contentTypeKey);
    } else if (params.contentTypeName != null) {
      ContentTypeEntity contentType = contentTypeDao.findByName(params.contentTypeName);
      if (contentType == null) {
        throw new ClientException(
            "Specified content type by name '" + params.contentTypeName + "' does not exist");
      }
      contentTypeKey = contentType.getContentTypeKey();
    }

    final StoreNewCategoryCommand command = new StoreNewCategoryCommand();
    command.setCreator(runningUser.getKey());
    command.setName(params.name);
    command.setParentCategory(new CategoryKey(params.parentCategoryKey));
    command.setContentType(contentTypeKey);
    command.setAutoApprove(params.autoApprove);
    final CategoryKey newCategoryKey = categoryService.storeNewCategory(command);
    return newCategoryKey.toInt();
  }
  @Before
  public void before() throws IOException, JDOMException {
    factory = fixture.getFactory();
    fixture.initSystemData();

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteAddr("127.0.0.1");
    ServletRequestAccessor.setRequest(request);

    createContentTypeXml();

    saveNeededEntities();

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

    createUpdateContent();
    createUpdateContentWithBinary();
  }
  private void doArchiveContent(
      UserEntity importer, ContentKey contentKey, ImportResult importResult) {
    final ContentEntity content = contentDao.findByKey(contentKey);

    if (content == null) {
      return;
    }

    boolean contentArchived = contentStorer.archiveMainVersion(importer, content);
    if (contentArchived) {
      importResult.addArchived(content);

      UnassignContentCommand unassignContentCommand = new UnassignContentCommand();
      unassignContentCommand.setContentKey(content.getKey());
      unassignContentCommand.setUnassigner(importer.getKey());
      contentStorer.unassignContent(unassignContentCommand);
    } else {
      importResult.addAlreadyArchived(content);
    }
  }
  @Test
  public void testUpdateContentDoNotChangeAssignment() {
    // exercise: updateContent

    AssignContentCommand assignContentCommand = new AssignContentCommand();
    assignContentCommand.setAssignerKey(fixture.findUserByName("testuser").getKey());
    assignContentCommand.setAssigneeKey(fixture.findUserByName("testuser").getKey());
    assignContentCommand.setAssignmentDescription("test assignment");
    assignContentCommand.setAssignmentDueDate(new DateTime(2010, 6, 6, 10, 0, 0, 0).toDate());
    assignContentCommand.setContentKey(contentWithBinaryKey);
    contentService.assignContent(assignContentCommand);

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

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

    UpdateContentParams params = new UpdateContentParams();
    params.contentKey = contentWithBinaryKey.toInt();
    params.contentData = newContentData;
    params.publishFrom = new Date();
    params.publishTo = null;
    params.createNewVersion = false;
    params.status = ContentStatus.STATUS_DRAFT;
    int contentVersionKey = internalClient.updateContent(params);

    fixture.flushAndClearHibernateSession();

    ContentVersionEntity actualVersion =
        contentVersionDao.findByKey(new ContentVersionKey(contentVersionKey));
    ContentEntity persistedContent = contentDao.findByKey(actualVersion.getContent().getKey());

    assertEquals(runningUser, persistedContent.getAssignee());
    assertEquals(runningUser, persistedContent.getAssigner());
    assertEquals("test assignment", persistedContent.getAssignmentDescription());
    assertEquals(
        new DateTime(2010, 6, 6, 10, 0, 0, 0).toDate(), persistedContent.getAssignmentDueDate());
  }
  @Test
  public void testCreateContentWithBlockGroup() {
    fixture.createAndStoreUserAndUserGroup(
        "testuser", "testuser fullname", UserType.NORMAL, "testuserstore");

    fixture.save(
        factory.createContentHandler(
            "Custom content", ContentHandlerName.CUSTOM.getHandlerClassShortName()));

    // setup content type
    ContentTypeConfigBuilder ctyconf = new ContentTypeConfigBuilder("Skole", "tittel");
    ctyconf.startBlock("Skole");
    ctyconf.addInput("tittel", "text", "contentdata/tittel", "Tittel", true);
    ctyconf.endBlock();
    ctyconf.startBlock("Elever", "contentdata/elever");
    ctyconf.addInput("elev-navn", "text", "navn", "Navn");
    ctyconf.addInput("elev-karakter", "text", "karakter", "Karakter");
    ctyconf.endBlock();
    ctyconf.startBlock("Laerere", "contentdata/laerere");
    ctyconf.addInput("laerer-navn", "text", "navn", "Navn");
    ctyconf.addInput("laerer-karakter", "text", "karakter", "Karakter");
    ctyconf.endBlock();
    Document configAsXmlBytes = XMLDocumentFactory.create(ctyconf.toString()).getAsJDOMDocument();
    fixture.save(
        factory.createContentType(
            "Skole", ContentHandlerName.CUSTOM.getHandlerClassShortName(), configAsXmlBytes));

    fixture.save(factory.createUnit("MyUnit", "en"));
    fixture.save(factory.createCategory("Skole", "Skole", "MyUnit", "testuser", "testuser"));
    fixture.save(factory.createCategoryAccessForUser("Skole", "testuser", "read,create,approve"));

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

    CreateContentParams content = new CreateContentParams();
    content.categoryKey = fixture.findCategoryByName("Skole").getKey().toInt();
    content.publishFrom = new Date();
    content.status = ContentStatus.STATUS_APPROVED;

    ContentDataInput contentData = new ContentDataInput("Skole");
    contentData.add(new TextInput("tittel", "St. Olav Videregaende skole"));

    GroupInput groupInputElev1 = contentData.addGroup("Elever");
    groupInputElev1.add(new TextInput("elev-navn", "Vegar Jansen"));
    groupInputElev1.add(new TextInput("elev-karakter", "S"));

    GroupInput groupInputElev2 = contentData.addGroup("Elever");
    groupInputElev2.add(new TextInput("elev-navn", "Thomas Sigdestad"));
    groupInputElev2.add(new TextInput("elev-karakter", "M"));

    GroupInput groupInputLaerer1 = contentData.addGroup("Laerere");
    groupInputLaerer1.add(new TextInput("laerer-navn", "Mutt Hansen"));
    groupInputLaerer1.add(new TextInput("laerer-karakter", "LG"));

    GroupInput groupInputLaerer2 = contentData.addGroup("Laerere");
    groupInputLaerer2.add(new TextInput("laerer-navn", "Striks Jansen"));
    groupInputLaerer2.add(new TextInput("laerer-karakter", "M"));

    content.contentData = contentData;
    ContentKey contentKey = new ContentKey(internalClient.createContent(content));

    ContentEntity createdContent = fixture.findContentByKey(contentKey);
    ContentVersionEntity createdVersion = createdContent.getMainVersion();
    CustomContentData createdContentData = (CustomContentData) createdVersion.getContentData();
    BlockGroupDataEntries elever = createdContentData.getBlockGroupDataEntries("Elever");

    GroupDataEntry elev1 = elever.getGroupDataEntry(1);
    assertEquals("Vegar Jansen", ((TextDataEntry) elev1.getEntry("elev-navn")).getValue());
    assertEquals("S", ((TextDataEntry) elev1.getEntry("elev-karakter")).getValue());

    GroupDataEntry elev2 = elever.getGroupDataEntry(2);
    assertEquals("Thomas Sigdestad", ((TextDataEntry) elev2.getEntry("elev-navn")).getValue());
    assertEquals("M", ((TextDataEntry) elev2.getEntry("elev-karakter")).getValue());

    BlockGroupDataEntries laerere = createdContentData.getBlockGroupDataEntries("Laerere");

    GroupDataEntry laerer1 = laerere.getGroupDataEntry(1);
    assertEquals("Mutt Hansen", ((TextDataEntry) laerer1.getEntry("laerer-navn")).getValue());
    assertEquals("LG", ((TextDataEntry) laerer1.getEntry("laerer-karakter")).getValue());

    GroupDataEntry laerer2 = laerere.getGroupDataEntry(2);
    assertEquals("Striks Jansen", ((TextDataEntry) laerer2.getEntry("laerer-navn")).getValue());
    assertEquals("M", ((TextDataEntry) laerer2.getEntry("laerer-karakter")).getValue());
  }