コード例 #1
0
  @Override
  public void deleteImage(String path) {
    List<ImageFileImpl> imageFiles;
    // No file extension, should be a directory
    if (StringUtils.isEmpty(FilenameUtils.getExtension(path))) {
      if (!path.endsWith(SystemUtils.FILE_SEPARATOR)) {
        path = path + SystemUtils.FILE_SEPARATOR;
      }
      path = FilenameUtils.separatorsToUnix(path);
      imageFiles =
          this.entityManager
              .createQuery(
                  "SELECT i FROM IMAGE_FILE i WHERE i.filePath LIKE ?1", ImageFileImpl.class)
              .setParameter(1, path + "%")
              .getResultList();
    }
    // Path is file
    else {
      path = FilenameUtils.separatorsToUnix(path);
      imageFiles =
          this.entityManager
              .createQuery(
                  "SELECT i FROM IMAGE_FILE i WHERE i.absolutePath = ?1", ImageFileImpl.class)
              .setParameter(1, path)
              .getResultList();
    }

    // Delete old images
    if (imageFiles != null && !imageFiles.isEmpty()) {
      for (ImageFile imageFile : imageFiles) {
        Image image = imageFile.getImage();
        if (image != null) {
          this.entityManager.remove(image);
        } else {
          this.entityManager.remove(imageFile);
        }
      }
    }
  }
コード例 #2
0
  @Override
  public void synchronizeImage(ImageFile imageFile) {
    // If the file already exist in DB, keep current Image, replace ImageFile
    List<ImageFileImpl> imageFiles =
        this.entityManager
            .createQuery(
                "SELECT i FROM IMAGE_FILE i WHERE i.absolutePath = ?1", ImageFileImpl.class)
            .setParameter(1, imageFile.getAbsolutePath())
            .getResultList();
    if (imageFiles != null && imageFiles.size() == 1) {
      imageFiles.get(0).getImage().setOriginalFile(imageFile);
    }

    // Delete old ImageFiles
    this.entityManager
        .createQuery("DELETE FROM IMAGE_FILE i WHERE i.absolutePath = ?1")
        .setParameter(1, imageFile.getAbsolutePath())
        .executeUpdate();

    // Merge/Persist Image/ImageFile into DB
    if (imageFile.getImage() != null) {
      this.entityManager.merge(imageFile.getImage());
    } else {
      // TODO: Should use factory here
      Image image = new ImageImpl();
      image.setOriginalFile(imageFile);
      image.setAlbum(
          FilenameUtils.getName(FilenameUtils.getFullPathNoEndSeparator(imageFile.getFilePath())));
      image.setTitle(FilenameUtils.removeExtension(imageFile.getFileName()));
      this.entityManager.merge(image);
    }

    // Delete empty Images
    this.entityManager
        .createQuery("DELETE FROM IMAGE i WHERE i.originalFile IS NULL")
        .executeUpdate();
  }