Search in sources :

Example 1 with Permission

use of com.google.api.services.drive.model.Permission in project local-data-aragopedia by aragonopendata.

the class GoogleDriveAPI method createSpreadsheetFromFile.

public boolean createSpreadsheetFromFile(String idParentFolder, String emailUserOwner, String extensionFile, String nameFile, java.io.File fileOrigin, String mimeType) {
    boolean result = true;
    Permission newPermission = createPermission();
    File body = new File();
    body.setTitle(nameFile);
    body.setMimeType("application/vnd.google-apps.spreadsheet");
    body.setEditable(true);
    body.setShared(true);
    body.setPermissions(Arrays.asList(newPermission));
    body.setUserPermission(newPermission);
    body.setWritersCanShare(true);
    body.setFileExtension(extensionFile);
    User user = new User();
    user.setEmailAddress(emailUserOwner);
    user.setIsAuthenticatedUser(true);
    List<User> list = new ArrayList<User>();
    list.add(user);
    body.setOwners(list);
    body.setParents(Arrays.asList(new ParentReference().setId(idParentFolder)));
    FileContent mediaContent = new FileContent(mimeType, fileOrigin);
    String fileId = "";
    try {
        File file = service.files().insert(body, mediaContent).execute();
        fileId = file.getId();
        insertPermission(newPermission, fileId);
    } catch (IOException e) {
        log.error("Error create spreadsheet from file ", e);
        result = false;
    }
    log.info("create Spreadsheet in google Drive from " + nameFile);
    return result;
}
Also used : FileContent(com.google.api.client.http.FileContent) ParentReference(com.google.api.services.drive.model.ParentReference) User(com.google.api.services.drive.model.User) Permission(com.google.api.services.drive.model.Permission) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(com.google.api.services.drive.model.File)

Example 2 with Permission

use of com.google.api.services.drive.model.Permission in project local-data-aragopedia by aragonopendata.

the class GoogleDriveAPI method createFolder.

public boolean createFolder(String nameFolder, String emailUserOwner) {
    log.info("init createFolder");
    boolean resultado = true;
    Permission newPermission = createPermission();
    File body = new File();
    body.setTitle(nameFolder);
    body.setMimeType("application/vnd.google-apps.folder");
    body.setEditable(true);
    body.setShared(true);
    List<Permission> listPermisions = new ArrayList<Permission>();
    listPermisions.add(newPermission);
    body.setPermissions(listPermisions);
    body.setUserPermission(newPermission);
    body.setWritersCanShare(true);
    User user = new User();
    user.setEmailAddress(emailUserOwner);
    user.setIsAuthenticatedUser(true);
    List<User> list = new ArrayList<User>();
    list.add(user);
    body.setOwners(list);
    File file;
    String fileId = null;
    try {
        file = service.files().insert(body).execute();
        fileId = file.getId();
        log.info("File ID: " + file.getId());
        insertPermission(newPermission, fileId);
    } catch (IOException e) {
        log.error("Error create folder", e);
        resultado = false;
    }
    log.info("end createFolder");
    return resultado;
}
Also used : User(com.google.api.services.drive.model.User) Permission(com.google.api.services.drive.model.Permission) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(com.google.api.services.drive.model.File)

Example 3 with Permission

use of com.google.api.services.drive.model.Permission in project local-data-aragopedia by aragonopendata.

the class GoogleDriveAPI method createSpreadsheetFromFile.

/**
	 * Method to create a google spreadsheet from a file
	 * 
	 * @param idParentFolder
	 *            String to identify the parent folder
	 * @param emailUserOwner
	 *            String to identify email user owner
	 * @param extensionFile
	 *            String with extension file
	 * @param nameFile
	 *            String with name google spreadsheet
	 * @param fileOrigin
	 *            File with content file origin
	 * @param mimeType
	 *            String with mime type file origin
	 * 
	 * @return True if create is ok, False otherwise
	 */
public boolean createSpreadsheetFromFile(String idParentFolder, String emailUserOwner, String extensionFile, String nameFile, java.io.File fileOrigin, String mimeType) {
    boolean result = true;
    Permission newPermission = createPermission();
    File body = new File();
    body.setTitle(nameFile);
    body.setMimeType("application/vnd.google-apps.spreadsheet");
    body.setEditable(true);
    body.setShared(true);
    body.setPermissions(Arrays.asList(newPermission));
    body.setUserPermission(newPermission);
    body.setWritersCanShare(true);
    body.setFileExtension(extensionFile);
    User user = new User();
    user.setEmailAddress(emailUserOwner);
    user.setIsAuthenticatedUser(true);
    List<User> list = new ArrayList<User>();
    list.add(user);
    body.setOwners(list);
    body.setParents(Arrays.asList(new ParentReference().setId(idParentFolder)));
    FileContent mediaContent = new FileContent(mimeType, fileOrigin);
    String fileId = "";
    try {
        File file = drive.files().insert(body, mediaContent).execute();
        fileId = file.getId();
        insertPermission(newPermission, fileId);
    } catch (Exception e) {
        log.error("Error create spreadsheet from file ", e);
        result = false;
    }
    log.info("create Spreadsheet in google Drive from " + nameFile);
    return result;
}
Also used : FileContent(com.google.api.client.http.FileContent) ParentReference(com.google.api.services.drive.model.ParentReference) User(com.google.api.services.drive.model.User) Permission(com.google.api.services.drive.model.Permission) ArrayList(java.util.ArrayList) File(com.google.api.services.drive.model.File) GeneralSecurityException(java.security.GeneralSecurityException) ParseException(java.text.ParseException) IOException(java.io.IOException)

Example 4 with Permission

use of com.google.api.services.drive.model.Permission in project local-data-aragopedia by aragonopendata.

the class GoogleDriveAPI method createPermission.

/**
	 * Method to create permission by create a file or folder
	 * 
	 * @return Permission created
	 */
private Permission createPermission() {
    Permission newPermission = new Permission();
    /* Domain drive for example localidata.com */
    newPermission.setDomain(Prop.domainPermission);
    newPermission.setValue(Prop.domainPermission);
    /* Acceptable types are: anyone, domain, group, user */
    newPermission.setType("domain");
    /* Acceptable roles are: owner, reader, writer */
    newPermission.setRole("writer");
    return newPermission;
}
Also used : Permission(com.google.api.services.drive.model.Permission)

Example 5 with Permission

use of com.google.api.services.drive.model.Permission in project local-data-aragopedia by aragonopendata.

the class GoogleDriveAPI method createFolder.

/**
	 * Metodo para crear una carpeta
	 * 
	 * @param nameFolder
	 *            String name folder
	 * @param emailUserOwner
	 *            String email user owner
	 * @return True if create is ok, false otherwise
	 */
public boolean createFolder(String nameFolder, String emailUserOwner) {
    log.debug("init createFolder");
    boolean resultado = true;
    Permission newPermission = createPermission();
    File body = new File();
    body.setTitle(nameFolder);
    body.setMimeType("application/vnd.google-apps.folder");
    body.setEditable(true);
    body.setShared(true);
    List<Permission> listPermisions = new ArrayList<Permission>();
    listPermisions.add(newPermission);
    body.setPermissions(listPermisions);
    body.setUserPermission(newPermission);
    body.setWritersCanShare(true);
    User user = new User();
    user.setEmailAddress(emailUserOwner);
    user.setIsAuthenticatedUser(true);
    List<User> list = new ArrayList<User>();
    list.add(user);
    body.setOwners(list);
    File file;
    String fileId = null;
    try {
        file = drive.files().insert(body).execute();
        fileId = file.getId();
        log.info("File ID: " + file.getId());
        insertPermission(newPermission, fileId);
    } catch (Exception e) {
        log.error("Error create folder", e);
        resultado = false;
    }
    log.debug("end createFolder");
    return resultado;
}
Also used : User(com.google.api.services.drive.model.User) Permission(com.google.api.services.drive.model.Permission) ArrayList(java.util.ArrayList) File(com.google.api.services.drive.model.File) GeneralSecurityException(java.security.GeneralSecurityException) ParseException(java.text.ParseException) IOException(java.io.IOException)

Aggregations

Permission (com.google.api.services.drive.model.Permission)6 File (com.google.api.services.drive.model.File)4 User (com.google.api.services.drive.model.User)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 FileContent (com.google.api.client.http.FileContent)2 ParentReference (com.google.api.services.drive.model.ParentReference)2 GeneralSecurityException (java.security.GeneralSecurityException)2 ParseException (java.text.ParseException)2