private boolean canOffline(DMSFile file, User user) {
   return file.getIsDirectory() != Boolean.TRUE
       && file.getMetaFile() != null
       && dmsPermissions
               .all()
               .filter("self.file = ? AND self.value = 'OFFLINE' AND self.user = ?", file, user)
               .count()
           > 0;
 }
  @Transactional
  public DMSFile setOffline(DMSFile file, boolean offline) {
    Preconditions.checkNotNull(file, "file can't be null");

    // directory can't be marked as offline
    if (file.getIsDirectory() == Boolean.TRUE) {
      return file;
    }

    final User user = AuthUtils.getUser();
    boolean canOffline = canOffline(file, user);

    if (offline == canOffline) {
      return file;
    }

    DMSPermission permission;

    if (offline) {
      permission = new DMSPermission();
      permission.setValue("OFFLINE");
      permission.setFile(file);
      permission.setUser(user);
      file.addPermission(permission);
    } else {
      permission =
          dmsPermissions
              .all()
              .filter("self.file = ? AND self.value = 'OFFLINE' AND self.user = ?", file, user)
              .fetchOne();
      file.removePermission(permission);
      dmsPermissions.remove(permission);
    }

    return this.save(file);
  }
 private boolean canCreate(DMSFile parent) {
   final User user = AuthUtils.getUser();
   final Group group = user.getGroup();
   if (parent.getCreatedBy() == user
       || security.hasRole("role.super")
       || security.hasRole("role.admin")) {
     return true;
   }
   return dmsPermissions
           .all()
           .filter(
               "self.file = :file AND self.permission.canWrite = true AND "
                   + "(self.user = :user OR self.group = :group)")
           .bind("file", parent)
           .bind("user", user)
           .bind("group", group)
           .autoFlush(false)
           .count()
       > 0;
 }
  @Override
  public Map<String, Object> populate(Map<String, Object> json, Map<String, Object> context) {
    final DMSFile file = findFrom(json);
    if (file == null) {
      return json;
    }

    boolean isFile = file.getIsDirectory() != Boolean.TRUE;
    LocalDateTime dt = file.getUpdatedOn();
    if (dt == null) {
      dt = file.getCreatedOn();
    }

    final User user = AuthUtils.getUser();
    final MetaFile metaFile = file.getMetaFile();

    boolean canShare =
        file.getCreatedBy() == user
            || security.isPermitted(AccessType.CREATE, DMSFile.class, file.getId())
            || dmsPermissions
                    .all()
                    .filter(
                        "self.file = ? AND self.value = 'FULL' AND (self.user = ? OR self.group = ?)",
                        file,
                        user,
                        user.getGroup())
                    .count()
                > 0;

    json.put("typeIcon", isFile ? "fa fa-file" : "fa fa-folder");
    json.put("downloadIcon", "fa fa-download");
    json.put("detailsIcon", "fa fa-info-circle");

    json.put("canShare", canShare);
    json.put("canWrite", canCreate(file));

    if (canOffline(file, user)) {
      json.put("offline", true);
    }

    json.put("lastModified", dt);
    json.put("createdOn", file.getCreatedOn());
    json.put("createdBy", file.getCreatedBy());
    json.put("updatedBy", file.getUpdatedBy());
    json.put("updatedOn", file.getUpdatedOn());

    if ("html".equals(file.getContentType())) {
      json.put("fileType", "text/html");
      json.put("contentType", "html");
      json.put("typeIcon", "fa fa-file-text-o");
      json.remove("downloadIcon");
    }
    if ("spreadsheet".equals(file.getContentType())) {
      json.put("fileType", "text/json");
      json.put("contentType", "spreadsheet");
      json.put("typeIcon", "fa fa-file-excel-o");
      json.remove("downloadIcon");
    }

    if (metaFile != null) {

      String fileType = metaFile.getFileType();
      String fileIcon = "fa-file-o";

      switch (fileType) {
        case "application/msword":
        case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
        case "application/vnd.oasis.opendocument.text":
          fileIcon = "fa-file-word-o";
          break;
        case "application/vnd.ms-excel":
        case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
        case "application/vnd.oasis.opendocument.spreadsheet":
          fileIcon = "fa-file-excel-o";
          break;
        case "application/pdf":
          fileIcon = "fa-file-pdf-o";
          break;
        case "application/zip":
        case "application/gzip":
          fileIcon = "fa-file-archive-o";
          break;
        default:
          String type = metaFile.getFileType();
          if (type != null) {
            if (type.startsWith("text")) fileIcon = "fa-file-text-o";
            if (type.startsWith("image")) fileIcon = "fa-file-image-o";
            if (type.startsWith("video")) fileIcon = "fa-file-video-o";
          }
          break;
      }

      json.put("fileType", fileType);
      json.put("typeIcon", "fa " + fileIcon);
    }

    if (file.getTags() != null) {
      final List<Object> tags = new ArrayList<>();
      for (DMSFileTag tag : file.getTags()) {
        tags.add(Resource.toMap(tag, "id", "code", "name", "style"));
      }
      json.put("tags", tags);
    }

    return json;
  }