public List<Blob> getPreview(Blob blob, DocumentModel dm) throws PreviewException {
    if (useTiling(blob, dm)) {
      List<Blob> blobResults = new ArrayList<Blob>();
      String htmlFile = getString().replace("$repoId$", dm.getRepositoryName());
      htmlFile = htmlFile.replace("$docId$", dm.getId());
      htmlFile = htmlFile.replace("$tileWidth$", "" + 200);
      htmlFile = htmlFile.replace("$tileHeight$", "" + 200);
      htmlFile = htmlFile.replace("$maxTiles$", "" + 2);
      Blob mainBlob = new StringBlob(htmlFile);
      mainBlob.setFilename("index.html");
      mainBlob.setMimeType("text/html");
      blob.setFilename("image");

      blobResults.add(mainBlob);
      blobResults.add(blob);

      return blobResults;
    }

    return new ImagePreviewer().getPreview(blob, dm);
  }
  public static void notifyEvent(
      CoreSession coreSession,
      DocumentModel document,
      NuxeoPrincipal principal,
      Task task,
      String eventId,
      Map<String, Serializable> properties,
      String comment,
      String category)
      throws ClientException {
    // Default category
    if (category == null) {
      category = DocumentEventCategories.EVENT_DOCUMENT_CATEGORY;
    }
    if (properties == null) {
      properties = new HashMap<String, Serializable>();
    }

    EventContext eventContext = null;
    if (document != null) {
      properties.put(CoreEventConstants.REPOSITORY_NAME, document.getRepositoryName());
      properties.put(CoreEventConstants.SESSION_ID, coreSession.getSessionId());
      properties.put(CoreEventConstants.DOC_LIFE_CYCLE, document.getCurrentLifeCycleState());
      eventContext = new DocumentEventContext(coreSession, principal, document);
    } else {
      eventContext = new EventContextImpl(coreSession, principal);
    }
    properties.put(DocumentEventContext.COMMENT_PROPERTY_KEY, comment);
    properties.put(DocumentEventContext.CATEGORY_PROPERTY_KEY, category);
    properties.put(TaskService.TASK_INSTANCE_EVENT_PROPERTIES_KEY, task);
    String disableNotif = task.getVariable(TaskEventNames.DISABLE_NOTIFICATION_SERVICE);
    if (disableNotif != null && Boolean.TRUE.equals(Boolean.valueOf(disableNotif))) {
      properties.put(TaskEventNames.DISABLE_NOTIFICATION_SERVICE, Boolean.TRUE);
    }
    eventContext.setProperties(properties);

    Event event = eventContext.newEvent(eventId);
    getEventProducer().fireEvent(event);
  }
 /**
  * Gets the full URL of where a blob can be downloaded.
  *
  * @since 7.2
  */
 private String getBlobUrl(Property prop) {
   DocumentModel doc = ctx.getParameter(ENTITY_TYPE);
   if (doc == null) {
     return "";
   }
   StringBuilder blobUrlBuilder = new StringBuilder(ctx.getBaseUrl());
   blobUrlBuilder
       .append("nxbigfile/")
       .append(doc.getRepositoryName())
       .append("/")
       .append(doc.getId())
       .append("/");
   blobUrlBuilder.append(prop.getSchema().getName());
   blobUrlBuilder.append(":");
   String canonicalXPath = ComplexTypeImpl.canonicalXPath(prop.getPath().substring(1));
   blobUrlBuilder.append(canonicalXPath);
   blobUrlBuilder.append("/");
   String filename = ((Blob) prop.getValue()).getFilename();
   if (filename != null) {
     blobUrlBuilder.append(URIUtils.quoteURIPathComponent(filename, true));
   }
   return blobUrlBuilder.toString();
 }
  @Test
  public void testCreateSingleTaskChain() throws Exception {
    OperationContext ctx = new OperationContext(coreSession);
    ctx.setInput(document);

    List<Task> tasks = taskService.getTaskInstances(document, (NuxeoPrincipal) null, coreSession);
    assertNotNull(tasks);
    assertEquals(0, tasks.size());

    automationService.run(ctx, "createSingleTaskChain");

    tasks = taskService.getTaskInstances(document, (NuxeoPrincipal) null, coreSession);
    assertEquals(1, tasks.size());

    Task task = tasks.get(0);
    assertEquals("single test task", task.getName());

    List<String> pooledActorIds = task.getActors();
    assertEquals(3, pooledActorIds.size());
    assertTrue(pooledActorIds.contains(SecurityConstants.MEMBERS));
    assertTrue(pooledActorIds.contains("myuser"));
    assertTrue(pooledActorIds.contains(SecurityConstants.ADMINISTRATOR));

    List<TaskComment> comments = task.getComments();
    assertEquals(1, comments.size());

    TaskComment comment = comments.get(0);
    assertEquals(SecurityConstants.ADMINISTRATOR, comment.getAuthor());
    assertEquals("test comment", comment.getText());

    Calendar calendar = Calendar.getInstance();
    calendar.set(2006, 6, 6, 15, 10, 15);
    calendar.set(Calendar.MILLISECOND, 0);
    calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
    assertEquals(calendar.getTime(), task.getDueDate());
    // task status
    assertTrue(task.isOpened());
    assertFalse(task.isCancelled());
    assertFalse(task.hasEnded());
    assertEquals(6, task.getVariables().size());
    assertEquals(
        document.getRepositoryName(),
        task.getVariable(TaskService.VariableName.documentRepositoryName.name()));
    assertEquals(document.getId(), task.getVariable(TaskService.VariableName.documentId.name()));
    assertEquals("test directive", task.getVariable(TaskService.VariableName.directive.name()));
    assertEquals(
        "true", task.getVariable(OperationTaskVariableName.createdFromCreateTaskOperation.name()));
    assertEquals("true", task.getVariable(TaskService.VariableName.createdFromTaskService.name()));
    assertEquals(SecurityConstants.ADMINISTRATOR, task.getInitiator());

    // accept task
    taskService.acceptTask(
        coreSession, (NuxeoPrincipal) coreSession.getPrincipal(), task, "ok i'm in");
    coreSession.save();
    // test task again
    tasks = taskService.getTaskInstances(document, (NuxeoPrincipal) null, coreSession);
    // ended tasks are filtered
    assertEquals(0, tasks.size());

    // check document metadata
    assertNull(document.getPropertyValue("dc:description"));
  }
  @Test
  public void testCreateSeveralTasksChain() throws Exception {
    OperationContext ctx = new OperationContext(coreSession);
    ctx.setInput(document);

    List<Task> tasks = taskService.getTaskInstances(document, (NuxeoPrincipal) null, coreSession);
    assertNotNull(tasks);
    assertEquals(0, tasks.size());

    automationService.run(ctx, "createSeveralTasksChain");

    tasks = taskService.getTaskInstances(document, (NuxeoPrincipal) null, coreSession);
    Collections.sort(tasks, new TaskInstanceComparator());
    assertEquals(3, tasks.size());

    Task task1 = tasks.get(0);
    assertEquals("several test tasks", task1.getName());

    List<String> pooledActorIds = task1.getActors();
    assertEquals(1, pooledActorIds.size());
    assertEquals(SecurityConstants.ADMINISTRATOR, pooledActorIds.get(0));

    List<TaskComment> comments = task1.getComments();
    assertEquals(0, comments.size());
    // task status
    assertTrue(task1.isOpened());
    assertFalse(task1.isCancelled());
    assertFalse(task1.hasEnded());
    assertEquals(5, task1.getVariables().size());
    assertEquals(
        document.getRepositoryName(),
        task1.getVariable(TaskService.VariableName.documentRepositoryName.name()));
    assertEquals(document.getId(), task1.getVariable(TaskService.VariableName.documentId.name()));
    assertNull(task1.getVariable(TaskService.VariableName.directive.name()));
    assertEquals(
        "true", task1.getVariable(OperationTaskVariableName.createdFromCreateTaskOperation.name()));
    assertEquals("true", task1.getVariable(TaskService.VariableName.createdFromTaskService.name()));

    assertEquals(SecurityConstants.ADMINISTRATOR, task1.getInitiator());
    // accept task
    taskService.acceptTask(
        coreSession, (NuxeoPrincipal) coreSession.getPrincipal(), task1, "ok i'm in");
    coreSession.save();
    // test task again
    tasks = taskService.getTaskInstances(document, (NuxeoPrincipal) null, coreSession);
    // ended tasks are filtered
    assertEquals(2, tasks.size());

    Collections.sort(tasks, new TaskInstanceComparator());

    // check other tasks
    Task task2 = tasks.get(0);
    assertEquals("several test tasks", task2.getName());

    pooledActorIds = task2.getActors();
    assertEquals(1, pooledActorIds.size());
    assertEquals(SecurityConstants.MEMBERS, pooledActorIds.get(0));

    comments = task2.getComments();
    assertEquals(0, comments.size());
    // task status
    assertTrue(task2.isOpened());
    assertFalse(task2.isCancelled());
    assertFalse(task2.hasEnded());
    assertEquals(5, task2.getVariables().size());
    assertEquals(
        document.getRepositoryName(),
        task2.getVariable(TaskService.VariableName.documentRepositoryName.name()));
    assertEquals(document.getId(), task2.getVariable(TaskService.VariableName.documentId.name()));
    assertNull(task2.getVariable(TaskService.VariableName.directive.name()));
    assertEquals(
        "true", task2.getVariable(OperationTaskVariableName.createdFromCreateTaskOperation.name()));
    assertEquals("true", task2.getVariable(TaskService.VariableName.createdFromTaskService.name()));

    assertEquals(SecurityConstants.ADMINISTRATOR, task2.getInitiator());
    Task task3 = tasks.get(1);
    assertEquals("several test tasks", task3.getName());

    pooledActorIds = task3.getActors();
    assertEquals(1, pooledActorIds.size());
    assertEquals("myuser", pooledActorIds.get(0));

    comments = task3.getComments();
    assertEquals(0, comments.size());
    // task status
    assertTrue(task3.isOpened());
    assertFalse(task3.isCancelled());
    assertFalse(task3.hasEnded());
    assertEquals(5, task3.getVariables().size());
    assertEquals(
        document.getRepositoryName(),
        task3.getVariable(TaskService.VariableName.documentRepositoryName.name()));
    assertEquals(document.getId(), task3.getVariable(TaskService.VariableName.documentId.name()));
    assertNull(task3.getVariable(TaskService.VariableName.directive.name()));
    assertEquals(
        "true", task3.getVariable(OperationTaskVariableName.createdFromCreateTaskOperation.name()));
    assertEquals("true", task3.getVariable(TaskService.VariableName.createdFromTaskService.name()));

    assertEquals(SecurityConstants.ADMINISTRATOR, task3.getInitiator());
    // check document metadata
    assertNull(document.getPropertyValue("dc:description"));
  }