/**
  * Deletes the specified attachment, identified by its unique identifier.?
  *
  * @param attachmentId the unique identifier of the attachment to delete.
  * @param pageContext the context of the page.
  */
 private void deleteAttachment(String attachmentId, PagesContext pageContext) {
   SilverTrace.info(
       "form",
       "VideoFieldDisplayer.deleteAttachment",
       "root.MSG_GEN_ENTER_METHOD",
       "attachmentId = " + attachmentId + ", componentId = " + pageContext.getComponentId());
   AttachmentPK pk = new AttachmentPK(attachmentId, pageContext.getComponentId());
   AttachmentController.deleteAttachment(pk);
 }
  /**
   * Uploads the file containing the video and that is identified by the specified field name.
   *
   * @param items the items of the form. One of them is containg the video file.
   * @param itemKey the key of the item containing the video.
   * @param pageContext the context of the page displaying the form.
   * @throws Exception if an error occurs while uploading the video file.
   * @return the identifier of the uploaded attached video file.
   */
  private String uploadVideoFile(
      final List<FileItem> items, final String itemKey, final PagesContext pageContext)
      throws Exception {
    String attachmentId = "";

    FileItem item = FileUploadUtil.getFile(items, itemKey);
    if (!item.isFormField()) {
      String componentId = pageContext.getComponentId();
      String userId = pageContext.getUserId();
      String objectId = pageContext.getObjectId();
      String logicalName = item.getName();
      long size = item.getSize();
      if (StringUtil.isDefined(logicalName)) {
        if (!FileUtil.isWindows()) {
          logicalName = logicalName.replace('\\', File.separatorChar);
          SilverTrace.info(
              "form",
              "VideoFieldDisplayer.uploadVideoFile",
              "root.MSG_GEN_PARAM_VALUE",
              "fullFileName on Unix = " + logicalName);
        }

        logicalName =
            logicalName.substring(
                logicalName.lastIndexOf(File.separator) + 1, logicalName.length());
        String type = FileRepositoryManager.getFileExtension(logicalName);
        String mimeType = item.getContentType();
        String physicalName = Long.toString(System.currentTimeMillis()) + "." + type;
        File dir = getVideoPath(componentId, physicalName);
        item.write(dir);
        AttachmentDetail attachmentDetail =
            createAttachmentDetail(
                objectId,
                componentId,
                physicalName,
                logicalName,
                mimeType,
                size,
                VideoFieldDisplayer.CONTEXT_FORM_VIDEO,
                userId);
        attachmentDetail = AttachmentController.createAttachment(attachmentDetail, true);
        attachmentId = attachmentDetail.getPK().getId();
      }
    }

    return attachmentId;
  }
 /**
  * Computes the URL of the video to display from the identifier of the attached video file and by
  * taking into account the displaying context.
  *
  * @param attachmentId the identifier of the attached video file.
  * @param pageContext the displaying page context.
  * @return the URL of the video file as String.
  */
 private String computeVideoURL(final String attachmentId, final PagesContext pageContext) {
   String videoURL = "";
   if (StringUtil.isDefined(attachmentId)) {
     if (attachmentId.startsWith("/")) {
       videoURL = attachmentId;
     } else {
       AttachmentDetail attachment =
           AttachmentController.searchAttachmentByPK(
               new AttachmentPK(attachmentId, pageContext.getComponentId()));
       if (attachment != null) {
         String webContext = FileServerUtils.getApplicationContext();
         videoURL = webContext + attachment.getAttachmentURL();
       }
     }
   }
   return videoURL;
 }
  public void deleteReservation(String id, String componentId) {
    Connection con = initCon();
    try {
      deleteIndex(id, "Reservation", componentId);

      // delete attached file
      AttachmentController.deleteAttachmentByCustomerPK(new ForeignPK(id, componentId));

      ResourcesManagerDAO.deleteReservation(con, id);
    } catch (Exception e) {
      throw new ResourcesManagerRuntimeException(
          "ResourcesManagerBmEJB.deleteReservation()",
          SilverpeasRuntimeException.ERROR,
          "resourcesManager.EX_DELETE_RESERVATION",
          e);
    } finally {
      fermerCon(con);
    }
  }
 /**
  * Gets the path of the physical file into which the video will be saved. The path of the file
  * depends on the Silverpeas components for which the video will be uploaded.
  *
  * @param componentId the identifier of the component.
  * @param physicalName the physical name of the video file; the name of the file into which the
  *     video will be saved in Silverpeas side.
  * @return the File object that represents the physical video file.
  */
 private File getVideoPath(String componentId, String physicalName) {
   String path = AttachmentController.createPath(componentId, CONTEXT_FORM_VIDEO);
   return new File(path + physicalName);
 }
  public void removeProcessInstanceData(ProcessInstance instance) throws WorkflowException {
    SilverTrace.info(
        "worflowEngine",
        "ProcessInstanceManagerImpl.removeProcessInstanceData()",
        "root.MSG_GEN_ENTER_METHOD");

    ForeignPK foreignPK = new ForeignPK(instance.getInstanceId(), instance.getModelId());

    // delete attachments
    SilverTrace.info(
        "worflowEngine",
        "ProcessInstanceManagerImpl.removeProcessInstanceData()",
        "root.MSG_GEN_PARAM_VALUE",
        "Delete attachments foreignPK = " + foreignPK);
    AttachmentController.deleteAttachmentByCustomerPK(foreignPK);

    // delete versioning
    SilverTrace.info(
        "worflowEngine",
        "ProcessInstanceManagerImpl.removeProcessInstanceData()",
        "root.MSG_GEN_PARAM_VALUE",
        "Delete versiong foreignPK = " + foreignPK);
    try {
      getVersioningBm().deleteDocumentsByForeignPK(foreignPK);
    } catch (Exception e) {
      throw new WorkflowException(
          "ProcessInstanceManagerImpl.removeProcessInstanceData",
          "EX_ERR_CANT_REMOVE_VERSIONNING_FILES",
          e);
    }

    // delete folder
    SilverTrace.info(
        "worflowEngine",
        "ProcessInstanceManagerImpl.removeProcessInstanceData()",
        "root.MSG_GEN_PARAM_VALUE",
        "Delete folder");
    try {
      RecordSet folderRecordSet = instance.getProcessModel().getFolderRecordSet();
      folderRecordSet.delete(instance.getFolder());
    } catch (FormException e) {
      throw new WorkflowException(
          "ProcessInstanceManagerImpl.removeProcessInstanceData", "EX_ERR_CANT_REMOVE_FOLDER", e);
    }

    // delete history steps
    SilverTrace.info(
        "worflowEngine",
        "ProcessInstanceManagerImpl.removeProcessInstanceData()",
        "root.MSG_GEN_PARAM_VALUE",
        "Delete history steps");
    HistoryStep[] steps = instance.getHistorySteps();
    for (int i = 0; steps != null && i < steps.length; i++) {
      if (!steps[i].getAction().equals("#question#") && !steps[i].getAction().equals("#response#"))
        steps[i].deleteActionRecord();
    }

    // delete associated todos
    SilverTrace.info(
        "worflowEngine",
        "ProcessInstanceManagerImpl.removeProcessInstanceData()",
        "root.MSG_GEN_PARAM_VALUE",
        "Delete associated todos");
    TodoBackboneAccess tbba = new TodoBackboneAccess();
    tbba.removeEntriesFromExternal("useless", foreignPK.getInstanceId(), foreignPK.getId() + "##%");

    SilverTrace.info(
        "worflowEngine",
        "ProcessInstanceManagerImpl.removeProcessInstanceData()",
        "root.MSG_GEN_EXIT_METHOD");
  }