OLD | NEW |
(Empty) | |
| 1 // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 |
| 3 package com.google.api.client.sample.bigquery.appengine.dashboard; |
| 4 |
| 5 import com.google.api.services.bigquery.model.Bigqueryfield; |
| 6 import com.google.api.services.bigquery.model.Row; |
| 7 import com.google.api.services.bigquery.model.RowF; |
| 8 import com.google.appengine.api.datastore.DatastoreService; |
| 9 import com.google.appengine.api.datastore.DatastoreServiceFactory; |
| 10 import com.google.appengine.api.datastore.Entity; |
| 11 import com.google.appengine.api.datastore.EntityNotFoundException; |
| 12 import com.google.appengine.api.datastore.FetchOptions; |
| 13 import com.google.appengine.api.datastore.Key; |
| 14 import com.google.appengine.api.datastore.KeyFactory; |
| 15 import com.google.appengine.api.datastore.Query; |
| 16 import com.google.common.base.Preconditions; |
| 17 |
| 18 import java.text.SimpleDateFormat; |
| 19 import java.util.ArrayList; |
| 20 import java.util.Date; |
| 21 import java.util.Iterator; |
| 22 import java.util.List; |
| 23 |
| 24 /** |
| 25 * Utility methods for inserting, accessing, and deleting data in the datastore. |
| 26 * |
| 27 * @author lparkinson@google.com (Laura Parkinson) |
| 28 */ |
| 29 public class DatastoreUtils { |
| 30 |
| 31 public static final String FAILED = "FAILED"; |
| 32 |
| 33 private final Key userEntityKey; |
| 34 private final String resultKind; |
| 35 private final DatastoreService service; |
| 36 private Entity userEntity; |
| 37 |
| 38 public DatastoreUtils(String userId) { |
| 39 userEntityKey = KeyFactory.createKey("User", userId); |
| 40 service = DatastoreServiceFactory.getDatastoreService(); |
| 41 resultKind = userId + "Result"; |
| 42 |
| 43 try { |
| 44 userEntity = service.get(userEntityKey); |
| 45 } catch (EntityNotFoundException e) { |
| 46 userEntity = null; |
| 47 } |
| 48 } |
| 49 |
| 50 public boolean hasUserEntity() { |
| 51 return userEntity != null; |
| 52 } |
| 53 |
| 54 private void createUserIfNull() { |
| 55 if (userEntity == null) { |
| 56 userEntity = new Entity(userEntityKey); |
| 57 } |
| 58 } |
| 59 |
| 60 /** |
| 61 * Updates the user entity with the message and status, creating it if necessa
ry. |
| 62 */ |
| 63 public void putUserInformation(String message, String status) { |
| 64 createUserIfNull(); |
| 65 userEntity.setProperty("jobStatus", status); |
| 66 userEntity.setProperty("message", message); |
| 67 service.put(userEntity); |
| 68 } |
| 69 |
| 70 /** |
| 71 * Updates the user entity with the current time, creating it if necessary. |
| 72 */ |
| 73 public void updateSuccessfulQueryTimestamp() { |
| 74 createUserIfNull(); |
| 75 userEntity.setProperty("timestamp", System.currentTimeMillis()); |
| 76 service.put(userEntity); |
| 77 } |
| 78 |
| 79 public String getUserJobStatus() { |
| 80 return getUserEntityProperty("jobStatus"); |
| 81 } |
| 82 |
| 83 public Boolean hasUserQueryFailed() { |
| 84 return (FAILED).equalsIgnoreCase(getUserJobStatus()); |
| 85 } |
| 86 |
| 87 public String getUserMessage() { |
| 88 return getUserEntityProperty("message"); |
| 89 } |
| 90 |
| 91 public String getUserLastRunMessage() { |
| 92 String timestamp = getUserEntityProperty("timestamp"); |
| 93 if (timestamp == null) { |
| 94 return "never"; |
| 95 } else { |
| 96 SimpleDateFormat format = new SimpleDateFormat("k:mm:ss 'on' MMMM d, yyyy
zzz"); |
| 97 Date date = new Date(Long.valueOf(timestamp)); |
| 98 return format.format(date); |
| 99 } |
| 100 } |
| 101 |
| 102 private String getUserEntityProperty(String propertyName) { |
| 103 if (userEntity != null && userEntity.hasProperty(propertyName)) { |
| 104 return String.valueOf(userEntity.getProperty(propertyName)); |
| 105 } else { |
| 106 return null; |
| 107 } |
| 108 } |
| 109 |
| 110 public List<Entity> getResults() { |
| 111 Query query = new Query(resultKind, userEntityKey); |
| 112 FetchOptions options = FetchOptions.Builder.withChunkSize(2000); |
| 113 return service.prepare(query).asList(options); |
| 114 } |
| 115 |
| 116 /** |
| 117 * Removes any existing results for the user from the datastore. |
| 118 */ |
| 119 public void deleteExistingResults() { |
| 120 ArrayList<Key> keys = new ArrayList<Key>(); |
| 121 List<Entity> results = getResults(); |
| 122 for (Entity entity : results) { |
| 123 keys.add(entity.getKey()); |
| 124 } |
| 125 service.delete(keys); |
| 126 } |
| 127 |
| 128 /** |
| 129 * Copies each row of the given data into an entity, then puts all the entitie
s |
| 130 * to the datastore with the user's entity as their ancestor. |
| 131 */ |
| 132 public void copyQueryResultsToDatastore(List<Bigqueryfield> fields, |
| 133 List<Row> rows) { |
| 134 ArrayList<Entity> entities = new ArrayList<Entity>(); |
| 135 Iterator<Row> rowsIterator = rows.iterator(); |
| 136 while (rowsIterator.hasNext()) { |
| 137 Entity entity = new Entity(resultKind, userEntityKey); |
| 138 |
| 139 // Copy the row into the entity -- fields become properties. |
| 140 Iterator<Bigqueryfield> fieldsIterator = fields.iterator(); |
| 141 Iterator<RowF> dataIterator = rowsIterator.next().getF().iterator(); |
| 142 |
| 143 Preconditions.checkState(fieldsIterator.hasNext() == dataIterator.hasNext(
)); |
| 144 while (fieldsIterator.hasNext() && dataIterator.hasNext()) { |
| 145 Object value = dataIterator.next().getV(); |
| 146 String strValue = (value != null) ? String.valueOf(value) : null; |
| 147 entity.setProperty(fieldsIterator.next().getName(), strValue); |
| 148 Preconditions.checkState(fieldsIterator.hasNext() == dataIterator.hasNex
t()); |
| 149 } |
| 150 entities.add(entity); |
| 151 } |
| 152 service.put(entities); |
| 153 } |
| 154 } |
OLD | NEW |