@Override
  public <T extends ExtRepositoryObject> T getExtRepositoryObject(
      ExtRepositoryObjectType<T> extRepositoryObjectType,
      String extRepositoryFolderKey,
      String title)
      throws PortalException {

    try {
      StringBundler sb = new StringBundler();

      sb.append("'");
      sb.append(extRepositoryFolderKey);
      sb.append("' in parents and title contains '");
      sb.append(title);
      sb.append(" and mimeType ");

      if (extRepositoryObjectType.equals(ExtRepositoryObjectType.FOLDER)) {

        sb.append("= ");
      } else {
        sb.append("!= ");
      }

      sb.append(_FOLDER_MIME_TYPE);

      Drive drive = getDrive();

      Drive.Files driveFiles = drive.files();

      Drive.Files.List driveFilesList = driveFiles.list();

      driveFilesList.setQ(sb.toString());

      FileList fileList = driveFilesList.execute();

      List<File> files = fileList.getItems();

      if (files.isEmpty()) {
        if (extRepositoryObjectType == ExtRepositoryObjectType.FOLDER) {
          throw new NoSuchFolderException(title);
        }

        throw new NoSuchFileEntryException(title);
      }

      if (extRepositoryObjectType.equals(ExtRepositoryObjectType.FOLDER)) {

        return (T) new GoogleDriveFolder(files.get(0), getRootFolderKey());
      }

      return (T) new GoogleDriveFileEntry(files.get(0));
    } catch (IOException ioe) {
      _log.error(ioe, ioe);

      throw new SystemException(ioe);
    }
  }
  @Override
  public <T extends ExtRepositoryObject> T getExtRepositoryObject(
      ExtRepositoryObjectType<T> extRepositoryObjectType, String extRepositoryObjectKey)
      throws PortalException {

    try {
      Drive drive = getDrive();

      File file = getFile(drive, extRepositoryObjectKey);

      T extRepositoryObject = null;

      if (extRepositoryObjectType.equals(ExtRepositoryObjectType.FOLDER)) {

        extRepositoryObject = (T) new GoogleDriveFolder(file, getRootFolderKey());
      } else {
        extRepositoryObject = (T) new GoogleDriveFileEntry(file);
      }

      return extRepositoryObject;
    } catch (IOException ioe) {
      if (extRepositoryObjectType == ExtRepositoryObjectType.FOLDER) {
        throw new NoSuchFolderException(ioe);
      }

      throw new NoSuchFileEntryException(ioe);
    }
  }
  @Override
  public <T extends ExtRepositoryObject> T moveExtRepositoryObject(
      ExtRepositoryObjectType<T> extRepositoryObjectType,
      String extRepositoryObjectKey,
      String newExtRepositoryFolderKey,
      String newTitle)
      throws PortalException {

    try {
      Drive drive = getDrive();

      File file = getFile(drive, extRepositoryObjectKey);

      Drive.Parents driveParents = drive.parents();

      List<ParentReference> parentReferences = file.getParents();

      for (ParentReference parentReference : parentReferences) {
        Drive.Parents.Delete driveParentsDelete =
            driveParents.delete(file.getId(), parentReference.getId());

        driveParentsDelete.execute();
      }

      ParentReference parentReference = new ParentReference();

      parentReference.setId(newExtRepositoryFolderKey);

      Drive.Parents.Insert driveParentsInsert = driveParents.insert(file.getId(), parentReference);

      driveParentsInsert.execute();

      if (extRepositoryObjectType.equals(ExtRepositoryObjectType.FILE)) {
        return (T) new GoogleDriveFileEntry(file);
      }

      return (T) new GoogleDriveFolder(file, getRootFolderKey());
    } catch (IOException ioe) {
      _log.error(ioe, ioe);

      throw new SystemException(ioe);
    }
  }
  @Override
  public <T extends ExtRepositoryObject> T copyExtRepositoryObject(
      ExtRepositoryObjectType<T> extRepositoryObjectType,
      String extRepositoryFileEntryKey,
      String newExtRepositoryFolderKey,
      String newTitle)
      throws PortalException {

    try {
      Drive drive = getDrive();

      Drive.Files driveFiles = drive.files();

      File newFile = new File();

      ParentReference parentReference = new ParentReference();

      parentReference.setId(newExtRepositoryFolderKey);

      newFile.setParents(Arrays.asList(parentReference));

      Drive.Files.Copy driveFilesCopy = driveFiles.copy(extRepositoryFileEntryKey, newFile);

      driveFilesCopy.execute();

      T extRepositoryObject = null;

      if (extRepositoryObjectType.equals(ExtRepositoryObjectType.FOLDER)) {

        extRepositoryObject = (T) new GoogleDriveFolder(newFile, getRootFolderKey());
      } else {
        extRepositoryObject = (T) new GoogleDriveFileEntry(newFile);
      }

      return extRepositoryObject;
    } catch (IOException ioe) {
      _log.error(ioe, ioe);

      throw new SystemException(ioe);
    }
  }
  @Override
  public <T extends ExtRepositoryObject> List<T> getExtRepositoryObjects(
      ExtRepositoryObjectType<T> extRepositoryObjectType, String extRepositoryFolderKey)
      throws PortalException {

    try {
      Drive drive = getDrive();

      Drive.Files driveFiles = drive.files();

      Drive.Files.List driveFilesList = driveFiles.list();

      StringBundler sb = new StringBundler();

      if (extRepositoryFolderKey != null) {
        sb.append("'");
        sb.append(extRepositoryFolderKey);
        sb.append("' in parents and ");
      }

      if (!extRepositoryObjectType.equals(ExtRepositoryObjectType.OBJECT)) {

        sb.append("mimeType");

        if (extRepositoryObjectType.equals(ExtRepositoryObjectType.FILE)) {

          sb.append(" != '");
        } else {
          sb.append(" = '");
        }

        sb.append(_FOLDER_MIME_TYPE);
        sb.append("' and ");
      }

      sb.append("trashed = false");

      driveFilesList.setQ(sb.toString());

      FileList fileList = driveFilesList.execute();

      List<File> files = fileList.getItems();

      List<T> extRepositoryObjects = new ArrayList<>();

      GoogleDriveCache googleDriveCache = GoogleDriveCache.getInstance();

      for (File file : files) {
        if (_FOLDER_MIME_TYPE.equals(file.getMimeType())) {
          extRepositoryObjects.add((T) new GoogleDriveFolder(file, getRootFolderKey()));
        } else {
          extRepositoryObjects.add((T) new GoogleDriveFileEntry(file));
        }

        googleDriveCache.put(file);
      }

      return extRepositoryObjects;
    } catch (IOException ioe) {
      _log.error(ioe, ioe);

      throw new SystemException(ioe);
    }
  }