/** * 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 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; } }
@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 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; } });
@Override public Optional<FormRecord> get(ResourceId recordId) { FormRecordEntity entity = ofy().load().key(FormRecordEntity.key(formClass, recordId)).now(); if(entity != null) { FormRecord record = entity.toFormRecord(formClass); return Optional.of(record); } else { return Optional.absent(); } }
@Override public JobStatus getJobStatus(UUID jobStatus) { return ofy().load().type(JobStatus.class).id(jobStatus.toString()).now(); }
@Override public long fileLength(String name) throws IOException { final Segment bigTableIndexFile = ofy().load().key(newSegmentKey(name)).now(); return bigTableIndexFile.length; } /*
@Override public IndexOutput createTempOutput(String prefix, String suffix, IOContext context) throws IOException { final String tempName = prefix + "_" + UUID.randomUUID().toString() + "_" + suffix; ensureOpen(); final Objectify begin = ofy(); Segment segment = begin.load().key(newSegmentKey(tempName)).now(); if(segment == null) { segment = newSegment(tempName); } return new SegmentIndexOutput(segment); }
@Override public IndexOutput createOutput(String name, IOContext context) throws IOException { ensureOpen(); final Objectify begin = ofy(); Segment segment = begin.load().key(newSegmentKey(name)).now(); if(segment == null) { segment = newSegment(name); } return new SegmentIndexOutput(segment); }
@Override public T run() { T t = ofy().load().key(key).now(); if(t == null) { t = builder.newInstance(key); ofy().save().entity(t).now(); } return t; } });
/** * Delete the segment using the specified {@link Objectify} useful for transaction. * @param objectify The {@link Objectify} to use * @param name The name of the segment to delete */ protected void deleteSegment(final Objectify objectify, final String name) { final Key<Segment> segmentKey = newSegmentKey(name); final Segment segment = objectify.load().key(segmentKey).now(); objectify.delete().keys(segment.getHunkKeys(segmentKey)); objectify.delete().key(segmentKey); }
@Override public void deleteFile(String name) { final Objectify objectify = ofy(); final Segment segment = objectify.load().key(newSegmentKey(name)).now(); final long hunkCount = segment.hunkCount; for (int i = 1; i <= hunkCount; i++) { objectify.delete().key(newSegmentHunkKey(name, i)); } objectify.delete().key(newSegmentKey(name)); } @Override