/** * Like now(), but throws NotFoundException instead of returning null. * @throws NotFoundException if the loaded value was not found */ public final T safe() throws NotFoundException { T t = now(); if (t == null) throw new NotFoundException(key); else return t; } }
@Override public <E> LoadResult<E> key(final Key<E> key) { return new LoadResult<>(key, createLoadEngine().load(key)); }
@Override public void vrun() { FormEntity rootEntity = ofy().load().key(FormEntity.key(formId)).safe(); FormClass formClass = ofy().load().key(FormSchemaEntity.key(formId)).safe().readFormClass(); FormRecordEntity existingEntity = ofy().load().key(FormRecordEntity.key(formClass, update.getRecordId())).now(); FormRecordEntity updated; RecordChangeType changeType;
@Override public IndexInput openInput(String name, IOContext context) throws IOException { ensureOpen(); try { return new SegmentIndexInput(ofy().load().key(newSegmentKey(name)).safe()); } catch (NotFoundException e) { throw new IOException(name, e); } } /*
public SiteFormStorage(Activity activity, TableMapping baseMapping, QueryExecutor queryExecutor, ActivityLoader activityLoader) { this.activity = activity; this.baseMapping = baseMapping; this.queryExecutor = queryExecutor; this.activityLoader = activityLoader; if(activity.isMigratedToHrd()) { formEntity = Hrd.ofy().load().key(FormEntity.key(activity.getSiteFormClassId())).safe(); } }
@Override protected boolean isMigrationNeeded(Key<FeedbackResponseComment> entity) { FeedbackResponseComment comment = ofy().load().key(entity).now(); try { Field commentGiverType = comment.getClass().getDeclaredField("commentGiverType"); commentGiverType.setAccessible(true); return commentGiverType.get(comment) == null; } catch (ReflectiveOperationException e) { return true; } }
private void update(FormSchemaEntity formClassEntity) { FormEntity rootEntity = ofy().load().key(FormEntity.key(formClass)).safe(); // Increment the version counter long newVersion = rootEntity.getVersion() + 1; rootEntity.setVersion(newVersion); rootEntity.setSchemaVersion(newVersion); // Update the schema formClassEntity.setSchema(formClass); formClassEntity.setSchemaVersion(newVersion); ofy().save().entities(rootEntity, formClassEntity); } }
@Override public LoadResult<Key<T>> first() { final Iterator<Key<T>> it = impl.limit(1).keysIterator(); final Result<Key<T>> result = new IteratorFirstResult<>(it); return new LoadResult<>(null, result); }
@Override protected boolean isMigrationNeeded(Key<Account> key) throws Exception { Account account = ofy().load().key(key).now(); if (!isMigrationOfGoogleIdNeeded(account)) { return false; } String newGoogleId = generateNewGoogleId(account); println(String.format("Going to migrate account with googleId %s to new googleId %s", account.getGoogleId(), newGoogleId)); return true; }
@Override public LoadResult<T> first() { // By the way, this is the same thing that PreparedQuery.asSingleEntity() does internally final Iterator<T> it = this.limit(1).iterator(); return new LoadResult<>(null, new IteratorFirstResult<>(it)); }
@Override protected boolean isMigrationNeeded(Key<Instructor> key) throws Exception { Instructor instructor = ofy().load().key(key).now(); if (SanitizationHelper.isSanitizedHtml(instructor.getRole())) { System.err.println(String.format("Instructor %s has unsanitized role %s, this should not happen", instructor.getUniqueId(), instructor.getRole())); } return SanitizationHelper.isSanitizedHtml(instructor.getDisplayedName()); }
@Override protected boolean isMigrationNeeded(Key<Course> key) throws Exception { Course course = ofy().load().key(key).now(); return SanitizationHelper.isSanitizedHtml(course.getName()); }
@Override protected boolean isMigrationNeeded(Key<CourseStudent> key) throws Exception { CourseStudent student = ofy().load().key(key).now(); if (SanitizationHelper.isSanitizedHtml(student.getCourseId())) { System.err.println(String.format("Student %s has unsanitized courseId %s, this should not happen", student.getUniqueId(), student.getCourseId())); } if (SanitizationHelper.isSanitizedHtml(student.getEmail())) { System.err.println(String.format("Student %s has unsanitized email %s, this should not happen", student.getUniqueId(), student.getEmail())); } if (SanitizationHelper.isSanitizedHtml(student.getGoogleId())) { System.err.println(String.format("Student %s has unsanitized googleId %s, this should not happen", student.getUniqueId(), student.getGoogleId())); } if (SanitizationHelper.isSanitizedHtml(student.getSectionName())) { System.err.println(String.format("Student %s has unsanitized sectionName %s, this should not happen", student.getUniqueId(), student.getSectionName())); } if (SanitizationHelper.isSanitizedHtml(student.getTeamName())) { System.err.println(String.format("Student %s has unsanitized teamName %s, this should not happen", student.getUniqueId(), student.getTeamName())); } return SanitizationHelper.isSanitizedHtml(student.getComments()) || SanitizationHelper.isSanitizedHtml(student.getLastName()) || SanitizationHelper.isSanitizedHtml(student.getName()); }
@Override protected void migrateEntity(Key<FeedbackResponseComment> entity) { FeedbackResponseComment comment = ofy().load().key(entity).now(); comment.setCommentGiverType(FeedbackParticipantType.INSTRUCTORS); comment.setIsCommentFromFeedbackParticipant(false); ofy().save().entity(comment).now(); } }
@Override protected void migrateEntity(Key<Course> key) throws Exception { Course course = ofy().load().key(key).now(); course.setName(SanitizationHelper.desanitizeIfHtmlSanitized(course.getName())); ofy().save().entity(course).now(); } }
@Override protected void migrateEntity(Key<Instructor> key) throws Exception { Instructor instructor = ofy().load().key(key).now(); instructor.setDisplayedName(SanitizationHelper.desanitizeIfHtmlSanitized(instructor.getDisplayedName())); ofy().save().entity(instructor).now(); } }
@Override protected void migrateEntity(Key<Account> oldAccountKey) throws Exception { Account oldAccount = ofy().load().key(oldAccountKey).now(); String oldGoogleId = oldAccount.getGoogleId(); String newGoogleId = generateNewGoogleId(oldAccount); StudentProfile oldStudentProfile = ofy().load().key(oldStudentProfileKey).now(); if (ofy().load().type(Account.class).id(newGoogleId).now() == null) { ofy().save().entity(oldAccount).now(); } else {
@Override protected void migrateEntity(Key<CourseStudent> key) throws Exception { CourseStudent student = ofy().load().key(key).now(); student.setComments(SanitizationHelper.desanitizeIfHtmlSanitized(student.getComments())); student.setName(SanitizationHelper.desanitizeIfHtmlSanitized(student.getName())); student.setLastName(SanitizationHelper.desanitizeIfHtmlSanitized(student.getLastName())); ofy().save().entity(student).now(); } }
@Override public String load(String courseId) { List<Instructor> instructors = ofy().load().type(Instructor.class).filter("courseId =", courseId).list(); for (Instructor instructor : instructors) { if (StringHelper.isEmpty(instructor.getGoogleId())) { continue; } Account account = ofy().load().key(Key.create(Account.class, instructor.getGoogleId())).now(); if (account != null && !StringHelper.isEmpty(account.getInstitute())) { return account.getInstitute(); } } return UNKNOWN_INSTITUTE; } });
/** * Helper method to get the {@link CounterData} associated with the supplied {@code counterName}. * * @param counterName * @return */ @VisibleForTesting protected Optional<CounterData> getCounterData(final String counterName) { Preconditions.checkNotNull(counterName); final Key<CounterData> counterKey = CounterData.key(counterName); final CounterData counterData = ObjectifyService.ofy().load().key(counterKey).now(); return Optional.fromNullable(counterData); }