/*
* Copyright 2014 Objectos, Fábrica de Software LTDA.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package br.com.objectos.way.gdrive;
import static com.google.common.collect.Lists.transform;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.Drive.Changes;
import com.google.api.services.drive.model.Change;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.ParentReference;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterators;
/**
* @author marcio.endo@objectos.com.br (Marcio Endo)
*/
class DriveExecIterateChangesOf extends AbstractDriveExecCommand<Iterator<Change>> {
private final ChangeId changeId;
public DriveExecIterateChangesOf(Drive drive, String id, ChangeId changeId) {
super(drive, id);
this.changeId = changeId;
}
@Override
Iterator<Change> execute(Drive drive, String id) throws IOException {
Changes.List _request = drive
.changes()
.list();
RequestChange request = new RequestChange(_request);
changeId.setStartChangeIdOf(_request);
Iterator<Change> iterator;
iterator = new RequestIterator<Change>(request);
return Iterators.filter(iterator, new FilterInDirectory(id));
}
private class FilterInDirectory implements Predicate<Change> {
private final String directoryId;
public FilterInDirectory(String directoryId) {
this.directoryId = directoryId;
}
@Override
public boolean apply(Change input) {
File file = input.getFile();
boolean res = false;
if (file != null) {
List<ParentReference> parents;
parents = file.getParents();
List<String> _ids;
_ids = transform(parents, ParentReferenceToId.INSTANCE);
Set<String> ids;
ids = ImmutableSet.copyOf(_ids);
res = ids.contains(directoryId);
}
return res;
}
}
private enum ParentReferenceToId implements Function<ParentReference, String> {
INSTANCE;
@Override
public String apply(ParentReference input) {
return input.getId();
}
}
}