private void importAlbums(Collection<PhotoAlbum> albums, TempPhotosData tempPhotosData) { for (PhotoAlbum album : albums) { tempPhotosData.addTempAlbumMapping(album.getId(), album); } }
@Override public boolean equals(Object object) { if (object == null) { return false; } if (!IdOnlyResource.class.isAssignableFrom(object.getClass())) { return false; } IdOnlyResource resource = (IdOnlyResource) object; return this.id.equals(resource.getId()); } }
private TempMailData getOrCreateMailData(UUID id) throws IOException { TempMailData tempMailData = jobStore.findData(id, createCacheKey(), TempMailData.class); if (tempMailData == null) { tempMailData = new TempMailData(id.toString()); jobStore.create(id, createCacheKey(), tempMailData); } return tempMailData; }
@Test public void verifySerializeDeserialize() throws Exception { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.registerSubtypes( ContinuationData.class, IntPaginationToken.class, IdOnlyContainerResource.class); ContinuationData continuationData = new ContinuationData(new IntPaginationToken(100)); continuationData.addContainerResource(new IdOnlyContainerResource("123")); String serialized = objectMapper.writeValueAsString(continuationData); ContinuationData deserialized = objectMapper.readValue(serialized, ContinuationData.class); Assert.assertNotNull(deserialized); Assert.assertEquals(100, ((IntPaginationToken) deserialized.getPaginationData()).getStart()); Assert.assertEquals( "123", ((IdOnlyContainerResource) deserialized.getContainerResources().get(0)).getId()); } }
private ExportResult<PhotosContainerResource> exportAlbums( TokensAndUrlAuthData authData, Optional<StringPaginationToken> paginationData) { Optional<String> paginationToken = stripTokenPrefix(paginationData, ALBUM_TOKEN_PREFIX); ArrayList<PhotoAlbum> exportAlbums = new ArrayList<>(); // Get albums Connection<Album> connection = getOrCreatePhotosInterface(authData).getAlbums(paginationToken); PaginationData nextPageData = null; String token = connection.getAfterCursor(); if (!Strings.isNullOrEmpty(token)) { nextPageData = new StringPaginationToken(ALBUM_TOKEN_PREFIX + token); } ContinuationData continuationData = new ContinuationData(nextPageData); List<Album> albums = connection.getData(); for (Album album : albums) { exportAlbums.add(new PhotoAlbum(album.getId(), album.getName(), album.getDescription())); continuationData.addContainerResource(new IdOnlyContainerResource(album.getId())); } return new ExportResult<>( ExportResult.ResultType.CONTINUE, new PhotosContainerResource(exportAlbums, null), continuationData); }
String newAlbumId = tempData.lookupNewAlbumId(oldAlbumId); PhotoAlbum album = tempData.lookupTempAlbum(oldAlbumId); tempData.addAlbumId(oldAlbumId, photoset.getId()); tempData.removeTempPhotoAlbum(oldAlbumId); } else {
@VisibleForTesting void importSingleAlbum(UUID jobId, PhotoAlbum inputAlbum, SmugMugInterface smugMugInterface) throws IOException { SmugMugAlbumResponse response = smugMugInterface.createAlbum(inputAlbum.getName()); // Put new album ID in job store so photos can be assigned to correct album // TODO(olsona): thread safety! TempPhotosData tempPhotosData = jobStore.findData(jobId, createCacheKey(), TempPhotosData.class); if (tempPhotosData == null) { tempPhotosData = new TempPhotosData(jobId); jobStore.create(jobId, createCacheKey(), tempPhotosData); } tempPhotosData.addAlbumId(inputAlbum.getId(), response.getUri()); jobStore.update(jobId, createCacheKey(), tempPhotosData); }
@Test public void testExportAlbum() { ExportResult<PhotosContainerResource> result = facebookPhotosExporter.export( uuid, new TokensAndUrlAuthData("accessToken", null, null), Optional.empty()); assertEquals(ExportResult.ResultType.CONTINUE, result.getType()); PhotosContainerResource exportedData = result.getExportedData(); assertEquals(1, exportedData.getAlbums().size()); assertEquals( new PhotoAlbum(ALBUM_ID, ALBUM_NAME, ALBUM_DESCRIPTION), exportedData.getAlbums().toArray()[0]); assertNull(result.getContinuationData().getPaginationData()); assertThat(result.getContinuationData().getContainerResources()) .contains(new IdOnlyContainerResource(ALBUM_ID)); }
private ExportResult exportTaskList(RememberTheMilkService service) { List<TaskListModel> lists = new ArrayList<>(); List<IdOnlyContainerResource> subResources = new ArrayList<>(); List<ListInfo> listInfoList; try { listInfoList = service.getLists().lists; } catch (IOException e) { return new ExportResult(e); } for (ListInfo oldListInfo : listInfoList) { if (oldListInfo.name.equals("All Tasks")) { // All Tasks is a special list that contains everything, don't copy that over continue; } lists.add(new TaskListModel(Integer.toString(oldListInfo.id), oldListInfo.name)); subResources.add(new IdOnlyContainerResource(Integer.toString(oldListInfo.id))); } TaskContainerResource taskContainerResource = new TaskContainerResource(lists, null); ContinuationData continuationData = new ContinuationData(null); subResources.forEach(continuationData::addContainerResource); // TODO: what do we do with pagination data? return new ExportResult(ResultType.CONTINUE, taskContainerResource, continuationData); }
@Test public void verifySerializeDeserialize() throws Exception { ObjectMapper objectMapper = new ObjectMapper(); TempCalendarData calendarData = new TempCalendarData(JOB_ID, Collections.singletonMap("old1", "new1")); String serialized = objectMapper.writeValueAsString(calendarData); TempCalendarData deserialized = objectMapper.readValue(serialized, TempCalendarData.class); Assert.assertEquals(JOB_ID, deserialized.getJobId()); Assert.assertEquals("new1", deserialized.getImportedId("old1")); } }
@VisibleForTesting void importSinglePhoto(UUID jobId, PhotoModel inputPhoto, SmugMugInterface smugMugInterface) throws IOException { // Find album to upload photo to TempPhotosData tempPhotosData = jobStore.findData(jobId, createCacheKey(), TempPhotosData.class); checkState( tempPhotosData != null, "cached temp photos data for %s is null", inputPhoto.getAlbumId()); String newAlbumUri = tempPhotosData.lookupNewAlbumId(inputPhoto.getAlbumId()); checkState( !Strings.isNullOrEmpty(newAlbumUri), "Cached album URI for %s is null", inputPhoto.getAlbumId()); InputStream inputStream; if (inputPhoto.isInTempStore()) { inputStream = jobStore.getStream(jobId, inputPhoto.getFetchableUrl()); } else { inputStream = smugMugInterface.getImageAsStream(inputPhoto.getFetchableUrl()); } SmugMugImageUploadResponse response = smugMugInterface.uploadImage(inputPhoto, newAlbumUri, inputStream); }
@VisibleForTesting void importSingleCalendar(UUID jobId, TokensAndUrlAuthData authData, CalendarModel calendarModel) throws IOException { com.google.api.services.calendar.model.Calendar toInsert = convertToGoogleCalendar( calendarModel); com.google.api.services.calendar.model.Calendar calendarResult = getOrCreateCalendarInterface(authData).calendars().insert(toInsert).execute(); TempCalendarData calendarMappings = jobStore.findData(jobId, createCacheKey(), TempCalendarData.class); if (calendarMappings == null) { calendarMappings = new TempCalendarData(jobId); jobStore.create(jobId, createCacheKey(), calendarMappings); } calendarMappings.addIdMapping(calendarModel.getId(), calendarResult.getId()); jobStore.update(jobId, createCacheKey(), calendarMappings); }
@VisibleForTesting void importSingleEvent(UUID jobId, TokensAndUrlAuthData authData, CalendarEventModel eventModel) throws IOException { Event event = convertToGoogleCalendarEvent(eventModel); // calendarMappings better not be null! TempCalendarData calendarMappings = jobStore.findData(jobId, createCacheKey(), TempCalendarData.class); String newCalendarId = calendarMappings.getImportedId(eventModel.getCalendarId()); getOrCreateCalendarInterface(authData) .events() .insert(newCalendarId, event) .execute(); }
jobStore.findData(jobId, createCacheKey(), TempPhotosData.class); if (tempPhotosData == null) { tempPhotosData = new TempPhotosData(jobId); jobStore.create(jobId, createCacheKey(), tempPhotosData);
boolean newMappingsCreated = false; String migratedLabelId = tempMailData.getImportedId(LABEL); if (migratedLabelId == null) { tempMailData.addFolderIdMapping(LABEL, migratedLabelId); newMappingsCreated = true; return new ImportResult(e); tempMailData.addFolderIdMapping(LABEL, migratedLabelId); newMappingsCreated = true;
private List<PhotoModel> convertPhotosList(Optional<String> albumId, GoogleMediaItem[] mediaItems, UUID jobId) throws IOException { List<PhotoModel> photos = new ArrayList<>(mediaItems.length); TempPhotosData tempPhotosData = null; InputStream stream = jobStore.getStream(jobId, createCacheKey()); if (stream != null) { tempPhotosData = new ObjectMapper().readValue(stream, TempPhotosData.class); stream.close(); } for (GoogleMediaItem mediaItem : mediaItems) { if (mediaItem.getMediaMetadata().getPhoto() != null) { // TODO: address videos boolean shouldUpload = albumId.isPresent(); if (tempPhotosData != null) { shouldUpload = shouldUpload || !tempPhotosData.isContainedPhotoId(mediaItem.getId()); } if (shouldUpload) { photos.add(convertToPhotoModel(albumId, mediaItem)); } } } return photos; }
@VisibleForTesting void importSingleAlbum(UUID jobId, TokensAndUrlAuthData authData, PhotoAlbum inputAlbum) throws IOException { // Set up album GoogleAlbum googleAlbum = new GoogleAlbum(); googleAlbum.setTitle(COPY_PREFIX + inputAlbum.getName()); GoogleAlbum responseAlbum = getOrCreatePhotosInterface(authData).createAlbum(googleAlbum); TempPhotosData tempPhotosData = jobStore.findData(jobId, TEMP_PHOTOS_KEY, TempPhotosData.class); if (tempPhotosData == null) { tempPhotosData = new TempPhotosData(jobId); jobStore.create(jobId, TEMP_PHOTOS_KEY, tempPhotosData); } tempPhotosData.addAlbumId(inputAlbum.getId(), responseAlbum.getId()); jobStore.update(jobId, TEMP_PHOTOS_KEY, tempPhotosData); }
@Test public void exportAlbum() throws IOException { // Set up String albumName = "Album Name"; String albumDescription = "Album description"; PhotoAlbum albumModel = new PhotoAlbum(OLD_ALBUM_ID, albumName, albumDescription); GoogleAlbum responseAlbum = new GoogleAlbum(); responseAlbum.setId(NEW_ALBUM_ID); when(googlePhotosInterface.createAlbum(Matchers.any(GoogleAlbum.class))) .thenReturn(responseAlbum); // Run test googlePhotosImporter.importSingleAlbum(uuid, null, albumModel); // Check results ArgumentCaptor<GoogleAlbum> albumArgumentCaptor = ArgumentCaptor.forClass(GoogleAlbum.class); verify(googlePhotosInterface).createAlbum(albumArgumentCaptor.capture()); assertEquals(albumArgumentCaptor.getValue().getTitle(), "Copy of " + albumName); assertNull(albumArgumentCaptor.getValue().getId()); TempPhotosData tempPhotosData = jobStore.findData(uuid, "tempPhotosData", TempPhotosData.class); assertEquals(tempPhotosData.lookupNewAlbumId(OLD_ALBUM_ID), NEW_ALBUM_ID); }
String importerLabelId = tempMailData.getImportedId(exportedLabelName); if (importerLabelId == null) { tempMailData.addFolderIdMapping(exportedLabelName, importerLabelId); newMappingsCreated = true;
String exportedLabelName = mailContainerModel.getName(); String importerLabelId = tempMailData.getImportedId(exportedLabelName); if (importerLabelId == null) { tempMailData.addFolderIdMapping(exportedLabelName, importerLabelId); newMappingsCreated = true;