Search in sources :

Example 1 with ImportResult

use of org.dataportabilityproject.spi.transfer.provider.ImportResult in project data-transfer-project by google.

the class GoogleTasksImporter method importItem.

@Override
public ImportResult importItem(UUID jobId, TokensAndUrlAuthData authData, TaskContainerResource data) {
    Tasks tasksService = getOrCreateTasksService(authData);
    TempTasksData tempTasksData = jobStore.findData(TempTasksData.class, jobId);
    if (tempTasksData == null) {
        tempTasksData = new TempTasksData(jobId.toString());
        jobStore.create(jobId, tempTasksData);
    }
    for (TaskListModel oldTasksList : data.getLists()) {
        // TempTasksData shouldn't be null since we added it.
        tempTasksData = jobStore.findData(TempTasksData.class, jobId);
        TaskList newTaskList = new TaskList().setTitle("Imported copy - " + oldTasksList.getName());
        TaskList insertedTaskList;
        try {
            insertedTaskList = tasksService.tasklists().insert(newTaskList).execute();
        } catch (IOException e) {
            return new ImportResult(ResultType.ERROR, "Error inserting taskList: " + e.getMessage());
        }
        logger.info("Storing {} as {}", oldTasksList.getId(), insertedTaskList.getId());
        tempTasksData.addTaskListId(oldTasksList.getId(), insertedTaskList.getId());
        jobStore.update(jobId, tempTasksData);
    }
    tempTasksData = jobStore.findData(TempTasksData.class, jobId);
    for (TaskModel oldTask : data.getTasks()) {
        Task newTask = new Task().setTitle(oldTask.getText()).setNotes(oldTask.getNotes());
        String newTaskId = tempTasksData.lookupNewTaskListId(newTask.getId());
        try {
            tasksService.tasks().insert(newTaskId, newTask).execute();
        } catch (IOException e) {
            return new ImportResult(ResultType.ERROR, "Error inserting task: " + e.getMessage());
        }
    }
    return new ImportResult(ResultType.OK);
}
Also used : TempTasksData(org.dataportabilityproject.spi.transfer.types.TempTasksData) Task(com.google.api.services.tasks.model.Task) Tasks(com.google.api.services.tasks.Tasks) ImportResult(org.dataportabilityproject.spi.transfer.provider.ImportResult) TaskList(com.google.api.services.tasks.model.TaskList) IOException(java.io.IOException) TaskListModel(org.dataportabilityproject.types.transfer.models.tasks.TaskListModel) TaskModel(org.dataportabilityproject.types.transfer.models.tasks.TaskModel)

Example 2 with ImportResult

use of org.dataportabilityproject.spi.transfer.provider.ImportResult in project data-transfer-project by google.

the class RequestHelper method batchRequest.

/**
 * Creates a Graph API batch request with required the authorization header.
 *
 * @param authData the auth token
 * @param requests the batch request data
 * @param client the client to construct the request with
 * @param objectMapper the mapper to serialize data
 */
@SuppressWarnings("unchecked")
public static BatchResponse batchRequest(TokenAuthData authData, List<Map<String, Object>> requests, String baseUrl, OkHttpClient client, ObjectMapper objectMapper) {
    try {
        Map<String, Object> batch = new LinkedHashMap<>();
        batch.put("requests", requests);
        Request.Builder requestBuilder = new Request.Builder().url(baseUrl + BATCH_URL);
        requestBuilder.header("Authorization", "Bearer " + authData.getToken());
        requestBuilder.post(RequestBody.create(MediaType.parse("application/json"), objectMapper.writeValueAsString(batch)));
        try (Response response = client.newCall(requestBuilder.build()).execute()) {
            int code = response.code();
            if (code >= 200 && code <= 299) {
                ResponseBody body = response.body();
                if (body == null) {
                    // FIXME evaluate HTTP response and return whether to retry
                    return new BatchResponse(new ImportResult(ImportResult.ResultType.ERROR));
                }
                Map<String, Object> responseData = objectMapper.readValue(body.bytes(), Map.class);
                return new BatchResponse(new ImportResult(ImportResult.ResultType.OK), (List<Map<String, Object>>) responseData.get("responses"));
            } else {
                // FIXME evaluate HTTP response and return whether to retry
                return new BatchResponse(new ImportResult(ImportResult.ResultType.ERROR));
            }
        }
    } catch (IOException e) {
        // TODO log
        e.printStackTrace();
        return new BatchResponse(new ImportResult(ImportResult.ResultType.ERROR, "Error batching request: " + e.getMessage()));
    }
}
Also used : ImportResult(org.dataportabilityproject.spi.transfer.provider.ImportResult) Request(okhttp3.Request) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with ImportResult

use of org.dataportabilityproject.spi.transfer.provider.ImportResult in project data-transfer-project by google.

the class SmugMugPhotosImporter method importItem.

@Override
public ImportResult importItem(UUID jobId, AuthData authData, PhotosContainerResource data) {
    try {
        String folder = null;
        if (!data.getAlbums().isEmpty()) {
            SmugMugResponse<SmugMugUserResponse> userResponse = smugMugInterface.makeUserRequest(smugMugInterface.USER_URL);
            folder = userResponse.getResponse().getUser().getUris().get("Folder").getUri();
        }
        for (PhotoAlbum album : data.getAlbums()) {
            importSingleAlbum(jobId, folder, album);
        }
        for (PhotoModel photo : data.getPhotos()) {
            importSinglePhoto(jobId, photo);
        }
    } catch (IOException e) {
        // TODO(olsona): we should retry on individual errors
        return new ImportResult(ResultType.ERROR, e.getMessage());
    }
    return ImportResult.OK;
}
Also used : SmugMugUserResponse(org.dataportabilityproject.transfer.smugmug.photos.model.SmugMugUserResponse) ImportResult(org.dataportabilityproject.spi.transfer.provider.ImportResult) PhotoModel(org.dataportabilityproject.types.transfer.models.photos.PhotoModel) PhotoAlbum(org.dataportabilityproject.types.transfer.models.photos.PhotoAlbum) IOException(java.io.IOException)

Example 4 with ImportResult

use of org.dataportabilityproject.spi.transfer.provider.ImportResult in project data-transfer-project by google.

the class GoogleContactsImporter method importItem.

@Override
public ImportResult importItem(UUID jobId, TokensAndUrlAuthData authData, ContactsModelWrapper data) {
    JCardReader reader = new JCardReader(data.getVCards());
    try {
        // TODO(olsona): address any other problems that might arise in conversion
        List<VCard> vCardList = reader.readAll();
        for (VCard vCard : vCardList) {
            Person person = convert(vCard);
            getOrCreatePeopleService(authData).people().createContact(person).execute();
        }
        return ImportResult.OK;
    } catch (IOException e) {
        return new ImportResult(ImportResult.ResultType.ERROR, e.getMessage());
    }
}
Also used : ImportResult(org.dataportabilityproject.spi.transfer.provider.ImportResult) IOException(java.io.IOException) JCardReader(ezvcard.io.json.JCardReader) VCard(ezvcard.VCard) Person(com.google.api.services.people.v1.model.Person)

Example 5 with ImportResult

use of org.dataportabilityproject.spi.transfer.provider.ImportResult in project data-transfer-project by google.

the class FlickrPhotosImporter method importItem.

@Override
public ImportResult importItem(UUID jobId, AuthData authData, PhotosContainerResource data) {
    Auth auth;
    try {
        auth = FlickrUtils.getAuth(authData, flickr);
    } catch (FlickrException e) {
        return new ImportResult(ImportResult.ResultType.ERROR, "Error authorizing Flickr Auth: " + e.getErrorMessage());
    }
    RequestContext.getRequestContext().setAuth(auth);
    // Store any album data in the cache because Flickr only allows you to create an album with a
    // photo in it, so we have to wait for the first photo to create the album
    TempPhotosData tempPhotosData = jobStore.findData(TempPhotosData.class, jobId);
    if (tempPhotosData == null) {
        tempPhotosData = new TempPhotosData(jobId);
        jobStore.create(jobId, tempPhotosData);
    }
    for (PhotoAlbum album : data.getAlbums()) {
        tempPhotosData.addAlbum(CACHE_ALBUM_METADATA_PREFIX + album.getId(), album);
    }
    jobStore.update(jobId, tempPhotosData);
    for (PhotoModel photo : data.getPhotos()) {
        try {
            String photoId = uploadPhoto(photo);
            String oldAlbumId = photo.getAlbumId();
            TempPhotosData tempData = jobStore.findData(TempPhotosData.class, jobId);
            String newAlbumId = tempData.lookupNewAlbumId(oldAlbumId);
            if (Strings.isNullOrEmpty(newAlbumId)) {
                // This means that we havent created the new album yet, create the photoset
                PhotoAlbum album = tempData.lookupAlbum(CACHE_ALBUM_METADATA_PREFIX + oldAlbumId);
                Photoset photoset = photosetsInterface.create(COPY_PREFIX + album.getName(), album.getDescription(), photoId);
                tempData.addAlbumId(oldAlbumId, photoset.getId());
            } else {
                // We've already created a new album, add the photo to the new album
                photosetsInterface.addPhoto(newAlbumId, photoId);
            }
            jobStore.update(jobId, tempData);
        } catch (FlickrException | IOException e) {
            // TODO: figure out retries
            return new ImportResult(ImportResult.ResultType.ERROR, "Error importing item: " + e.getMessage());
        }
    }
    return new ImportResult(ImportResult.ResultType.OK);
}
Also used : TempPhotosData(org.dataportabilityproject.spi.transfer.types.TempPhotosData) ImportResult(org.dataportabilityproject.spi.transfer.provider.ImportResult) FlickrException(com.flickr4java.flickr.FlickrException) Photoset(com.flickr4java.flickr.photosets.Photoset) Auth(com.flickr4java.flickr.auth.Auth) PhotoModel(org.dataportabilityproject.types.transfer.models.photos.PhotoModel) PhotoAlbum(org.dataportabilityproject.types.transfer.models.photos.PhotoAlbum) IOException(java.io.IOException)

Aggregations

ImportResult (org.dataportabilityproject.spi.transfer.provider.ImportResult)9 IOException (java.io.IOException)6 Map (java.util.Map)3 Photoset (com.flickr4java.flickr.photosets.Photoset)2 VCard (ezvcard.VCard)2 JCardReader (ezvcard.io.json.JCardReader)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 UUID (java.util.UUID)2 Importer (org.dataportabilityproject.spi.transfer.provider.Importer)2 TempPhotosData (org.dataportabilityproject.spi.transfer.types.TempPhotosData)2 TokenAuthData (org.dataportabilityproject.types.transfer.auth.TokenAuthData)2 ContactsModelWrapper (org.dataportabilityproject.types.transfer.models.contacts.ContactsModelWrapper)2 PhotoAlbum (org.dataportabilityproject.types.transfer.models.photos.PhotoAlbum)2 Test (org.junit.Test)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 FlickrException (com.flickr4java.flickr.FlickrException)1 Auth (com.flickr4java.flickr.auth.Auth)1 UploadMetaData (com.flickr4java.flickr.uploader.UploadMetaData)1 Person (com.google.api.services.people.v1.model.Person)1