List<Key> putKeys = null;
try {
// Prepare control table.
Key controlId = datastore.allocateIds("dump", 1).getStart();
List<Entity> list = new ArrayList<Entity>();
Queue queue = QueueFactory.getDefaultQueue();
List<TaskOptions> taskList = Lists.newArrayList();
int countDown = 10000;
for (int i = 0; i < kinds.length; i++) {
Key childKey = KeyFactory.createKey(controlId, GbControl.NAME, kinds[i]);
Entity control = new Entity(childKey);
control.setProperty(GbControl.KIND_NAME, kinds[i]);
control.setProperty(GbControl.COUNT, 0);
if (currentUser != null) {
control.setProperty(GbControl.REPORT_TO, new Email(currentUser.getEmail()));
}
control.setProperty(GbControl.AUTH_SUB_TOKEN, token);
control.setProperty(GbControl.SPREADSHEET_KEY, spreadsheet.getKey());
control.setProperty(GbControl.UPDATE_DATE, new Date());
list.add(control);
// Start task queue chain for each kind.
taskList
.add(TaskOptions.Builder.url("/tasks/dump.gobo").param(
"controlKey",
KeyFactory.keyToString(childKey)).countdownMillis(countDown).method(
Method.GET));
// to avoid the collision on the spreadsheet.
countDown += 500;
}
putKeys = datastore.put(list);
queue.add(taskList);
} catch (Exception e) {
if (putKeys != null) {
try {
datastore.delete(putKeys);