Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/teammates/common/util/Templates.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
public final class Templates {

public static final String INSTRUCTOR_SAMPLE_DATA = FileHelper.readResourceFile("InstructorSampleData.json");
public static final String INSTRUCTOR_SAMPLE_DATA = FileHelper.readResourceFile("InstructorSampleDataSql.json");

private Templates() {
// utility class
Expand Down
49 changes: 25 additions & 24 deletions src/main/java/teammates/ui/webapi/CreateAccountAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
import org.apache.http.HttpStatus;

import teammates.common.datatransfer.AccountRequestStatus;
import teammates.common.datatransfer.DataBundle;
import teammates.common.datatransfer.attributes.InstructorAttributes;
import teammates.common.datatransfer.attributes.StudentAttributes;
import teammates.common.datatransfer.SqlDataBundle;
import teammates.common.exception.EntityAlreadyExistsException;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
import teammates.common.util.Const;
import teammates.common.util.FieldValidator;
import teammates.common.util.JsonUtils;
import teammates.common.util.Logger;
import teammates.common.util.StringHelper;
import teammates.common.util.Templates;
import teammates.common.util.TimeHelper;
import teammates.sqllogic.core.DataBundleLogic;
import teammates.storage.sqlentity.AccountRequest;
import teammates.storage.sqlentity.Instructor;
import teammates.storage.sqlentity.Student;
import teammates.ui.request.InvalidHttpRequestBodyException;

/**
Expand Down Expand Up @@ -71,18 +71,18 @@ public JsonResult execute() throws InvalidHttpRequestBodyException, InvalidOpera

try {
courseId = importDemoData(instructorEmail, instructorName, instructorInstitution, timezone);
} catch (InvalidParametersException ipe) {
// There should not be any invalid parameter here
log.severe("Unexpected error", ipe);
return new JsonResult(ipe.getMessage(), HttpStatus.SC_INTERNAL_SERVER_ERROR);
} catch (InvalidParametersException | EntityAlreadyExistsException | EntityDoesNotExistException e) {
// There should not be any invalid parameter or entity conflict here
log.severe("Unexpected error", e);
return new JsonResult(e.getMessage(), HttpStatus.SC_INTERNAL_SERVER_ERROR);
}

List<InstructorAttributes> instructorList = logic.getInstructorsForCourse(courseId);
List<Instructor> instructorList = sqlLogic.getInstructorsByCourse(courseId);

assert !instructorList.isEmpty();

try {
logic.joinCourseForInstructor(instructorList.get(0).getKey(), userInfo.id);
sqlLogic.joinCourseForInstructor(instructorList.get(0).getRegKey(), userInfo.id);
} catch (EntityDoesNotExistException | EntityAlreadyExistsException | InvalidParametersException e) {
// EntityDoesNotExistException should not be thrown as all entities should exist in demo course.
// EntityAlreadyExistsException should not be thrown as updated entities should not have
Expand Down Expand Up @@ -127,7 +127,7 @@ private static String getDateString(Instant instant) {
* @return the ID of demo course
*/
private String importDemoData(String instructorEmail, String instructorName, String instructorInstitute, String timezone)
throws InvalidParametersException {
throws InvalidParametersException, EntityAlreadyExistsException, EntityDoesNotExistException {

String courseId = generateDemoCourseId(instructorEmail);
Instant now = Instant.now();
Expand All @@ -143,8 +143,11 @@ private String importDemoData(String instructorEmail, String instructorName, Str
// Used for timestamp of comments
String dateString5 = getDateString(now);

String instructorEmailAsStudent = instructorEmail.replace("@", "+student@");
String dataBundleString = Templates.populateTemplate(Templates.INSTRUCTOR_SAMPLE_DATA,
// replace email
// replace instructor-as-student email
"teammates.demo.instructor.student@demo.course", instructorEmailAsStudent,
// replace instructor email
"teammates.demo.instructor@demo.course", instructorEmail,
// replace name
"Demo_Instructor", instructorName,
Expand All @@ -165,20 +168,18 @@ private String importDemoData(String instructorEmail, String instructorName, Str
dataBundleString = replaceAdjustedTimeAndTimezone(dataBundleString, timezone);
}

DataBundle data = JsonUtils.fromJson(dataBundleString, DataBundle.class);

logic.persistDataBundle(data);

List<StudentAttributes> students = logic.getStudentsForCourse(courseId);
List<InstructorAttributes> instructors = logic.getInstructorsForCourse(courseId);
SqlDataBundle dataBundle = DataBundleLogic.deserializeDataBundle(dataBundleString);

for (StudentAttributes student : students) {
taskQueuer.scheduleStudentForSearchIndexing(student.getCourse(), student.getEmail());
}
sqlLogic.persistDataBundle(dataBundle);

for (InstructorAttributes instructor : instructors) {
List<Student> students = sqlLogic.getStudentsForCourse(courseId);
List<Instructor> instructors = sqlLogic.getInstructorsByCourse(courseId);
students.stream().forEach(student -> {
taskQueuer.scheduleStudentForSearchIndexing(student.getCourseId(), student.getEmail());
});
instructors.stream().forEach(instructor -> {
taskQueuer.scheduleInstructorForSearchIndexing(instructor.getCourseId(), instructor.getEmail());
}
});

return courseId;
}
Expand Down Expand Up @@ -211,7 +212,7 @@ private String importDemoData(String instructorEmail, String instructorName, Str
*/
private String generateDemoCourseId(String instructorEmail) {
String proposedCourseId = generateNextDemoCourseId(instructorEmail, FieldValidator.COURSE_ID_MAX_LENGTH);
while (logic.getCourse(proposedCourseId) != null) {
while (sqlLogic.getCourse(proposedCourseId) != null) {
proposedCourseId = generateNextDemoCourseId(proposedCourseId, FieldValidator.COURSE_ID_MAX_LENGTH);
}
return proposedCourseId;
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/teammates/ui/webapi/DeleteAccountAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ public class DeleteAccountAction extends AdminOnlyAction {
public JsonResult execute() {
String googleId = getNonNullRequestParamValue(Const.ParamsNames.INSTRUCTOR_ID);

// deleteAccountCascade is needed for datastore for dual DB
// as it deletes the student and instructor entities which are not yet migrated
logic.deleteAccountCascade(googleId);
sqlLogic.deleteAccountCascade(googleId);

return new JsonResult("Account is successfully deleted.");
Expand Down
19 changes: 5 additions & 14 deletions src/main/java/teammates/ui/webapi/GetAccountsAction.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package teammates.ui.webapi;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import teammates.common.datatransfer.attributes.AccountAttributes;
import teammates.common.util.Const;
import teammates.common.util.SanitizationHelper;
import teammates.storage.sqlentity.Account;
Expand All @@ -20,19 +19,11 @@ public JsonResult execute() {
String email = getNonNullRequestParamValue(Const.ParamsNames.USER_EMAIL);
email = SanitizationHelper.sanitizeEmail(email);

List<AccountAttributes> premigratedAccounts = logic.getAccountsForEmail(email);
List<Account> migratedAccounts = sqlLogic.getAccountsForEmail(email);
List<AccountData> accounts = new ArrayList<>();
List<Account> accounts = sqlLogic.getAccountsForEmail(email);
List<AccountData> accountDataList = accounts.stream()
.map(AccountData::new).collect(Collectors.toList());

for (AccountAttributes accountAttribute : premigratedAccounts) {
accounts.add(new AccountData(accountAttribute));
}

for (Account account : migratedAccounts) {
accounts.add(new AccountData(account));
}

return new JsonResult(new AccountsData(accounts));
return new JsonResult(new AccountsData(accountDataList));
}

}
22 changes: 2 additions & 20 deletions src/main/java/teammates/ui/webapi/GetRegkeyValidityAction.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package teammates.ui.webapi;

import teammates.common.datatransfer.attributes.InstructorAttributes;
import teammates.common.datatransfer.attributes.StudentAttributes;
import teammates.common.util.Const;
import teammates.common.util.StringHelper;
import teammates.storage.sqlentity.Instructor;
Expand Down Expand Up @@ -35,30 +33,14 @@ public JsonResult execute() {
String googleId = null;

if (intent == Intent.STUDENT_SUBMISSION || intent == Intent.STUDENT_RESULT) {
// Try to get googleId for not migrated user
StudentAttributes studentAttributes = logic.getStudentForRegistrationKey(regKey);
if (studentAttributes != null && !isCourseMigrated(studentAttributes.getCourse())) {
isValid = true;
googleId = studentAttributes.getGoogleId();
}

// Try to get googleId for migrated user
Student student = sqlLogic.getStudentByRegistrationKey(regKey);
if (student != null) { // assume that if student has been migrated, course has been migrated
if (student != null) {
isValid = true;
googleId = student.getGoogleId();
}
} else if (intent == Intent.INSTRUCTOR_SUBMISSION || intent == Intent.INSTRUCTOR_RESULT) {
// Try to get googleId for not migrated user
InstructorAttributes instructorAttributes = logic.getInstructorForRegistrationKey(regKey);
if (instructorAttributes != null && !isCourseMigrated(instructorAttributes.getCourseId())) {
isValid = true;
googleId = instructorAttributes.getGoogleId();
}

// Try to get googleId for migrated user
Instructor instructor = sqlLogic.getInstructorByRegistrationKey(regKey);
if (instructor != null) { // assume that if instructor has been migrated, course has been migrated
if (instructor != null) {
isValid = true;
googleId = instructor.getGoogleId();
}
Expand Down
86 changes: 20 additions & 66 deletions src/main/java/teammates/ui/webapi/ResetAccountAction.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package teammates.ui.webapi;

import teammates.common.datatransfer.attributes.InstructorAttributes;
import teammates.common.datatransfer.attributes.StudentAttributes;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.util.Const;
import teammates.storage.sqlentity.Instructor;
Expand All @@ -22,77 +20,33 @@ public JsonResult execute() {
}

String courseId = getNonNullRequestParamValue(Const.ParamsNames.COURSE_ID);
String wrongGoogleId = null;

if (isCourseMigrated(courseId)) {
if (studentEmail != null) {
Student existingStudent = sqlLogic.getStudentForEmail(courseId, studentEmail);
if (studentEmail != null) {
Student existingStudent = sqlLogic.getStudentForEmail(courseId, studentEmail);

if (existingStudent == null) {
throw new EntityNotFoundException("Student does not exist.");
}

wrongGoogleId = existingStudent.getGoogleId();

try {
sqlLogic.resetStudentGoogleId(studentEmail, courseId, wrongGoogleId);
taskQueuer.scheduleCourseRegistrationInviteToStudent(courseId, studentEmail, true);
} catch (EntityDoesNotExistException e) {
throw new EntityNotFoundException(e);
}
} else if (instructorEmail != null) {
Instructor existingInstructor = sqlLogic.getInstructorForEmail(courseId, instructorEmail);

if (existingInstructor == null) {
throw new EntityNotFoundException("Instructor does not exist.");
}

wrongGoogleId = existingInstructor.getGoogleId();

try {
sqlLogic.resetInstructorGoogleId(instructorEmail, courseId, wrongGoogleId);
taskQueuer.scheduleCourseRegistrationInviteToInstructor(null, instructorEmail, courseId, true);
} catch (EntityDoesNotExistException e) {
throw new EntityNotFoundException(e);
}
if (existingStudent == null) {
throw new EntityNotFoundException("Student does not exist.");
}
} else {
if (studentEmail != null) {
StudentAttributes existingStudent = logic.getStudentForEmail(courseId, studentEmail);
if (existingStudent == null) {
throw new EntityNotFoundException("Student does not exist.");
}

wrongGoogleId = existingStudent.getGoogleId();

try {
logic.resetStudentGoogleId(studentEmail, courseId);
taskQueuer.scheduleCourseRegistrationInviteToStudent(courseId, studentEmail, true);
} catch (EntityDoesNotExistException e) {
throw new EntityNotFoundException(e);
}
} else if (instructorEmail != null) {
InstructorAttributes existingInstructor = logic.getInstructorForEmail(courseId, instructorEmail);
if (existingInstructor == null) {
throw new EntityNotFoundException("Instructor does not exist.");
}

wrongGoogleId = existingInstructor.getGoogleId();
try {
sqlLogic.resetStudentGoogleId(studentEmail, courseId, existingStudent.getGoogleId());
taskQueuer.scheduleCourseRegistrationInviteToStudent(courseId, studentEmail, true);
} catch (EntityDoesNotExistException e) {
throw new EntityNotFoundException(e);
}
} else if (instructorEmail != null) {
Instructor existingInstructor = sqlLogic.getInstructorForEmail(courseId, instructorEmail);

try {
logic.resetInstructorGoogleId(instructorEmail, courseId);
taskQueuer.scheduleCourseRegistrationInviteToInstructor(null, instructorEmail, courseId, true);
} catch (EntityDoesNotExistException e) {
throw new EntityNotFoundException(e);
}
if (existingInstructor == null) {
throw new EntityNotFoundException("Instructor does not exist.");
}
}

if (wrongGoogleId != null
&& !isAccountMigrated(wrongGoogleId)
&& logic.getStudentsForGoogleId(wrongGoogleId).isEmpty()
&& logic.getInstructorsForGoogleId(wrongGoogleId).isEmpty()) {
logic.deleteAccountCascade(wrongGoogleId);
try {
sqlLogic.resetInstructorGoogleId(instructorEmail, courseId, existingInstructor.getGoogleId());
taskQueuer.scheduleCourseRegistrationInviteToInstructor(null, instructorEmail, courseId, true);
} catch (EntityDoesNotExistException e) {
throw new EntityNotFoundException(e);
}
}

return new JsonResult("Account is successfully reset.");
Expand Down
Loading
Loading