Skip to content

Commit

Permalink
Initial draft for migrated
Browse files Browse the repository at this point in the history
Remove unnecessary comments

Remove unnecessary comments

Save progress

Add draft

Add draft
  • Loading branch information
FergusMok committed Feb 5, 2024
1 parent 333f582 commit c4228c7
Show file tree
Hide file tree
Showing 14 changed files with 567 additions and 31 deletions.
14 changes: 14 additions & 0 deletions src/it/java/teammates/it/storage/sqlapi/FeedbackResponsesDbIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ public void testDeleteFeedbackResponsesForQuestionCascade() {
assertNull(frcDb.getFeedbackResponseComment(frc1.getId()));
}

@Test
public void testDeleteFeedbackResponsesAndCommentsCascade() {
______TS("success: typical case");
FeedbackResponse fr1 = typicalDataBundle.feedbackResponses.get("response1ForQ1");
FeedbackResponseComment frc1 = typicalDataBundle.feedbackResponseComments.get("comment1ToResponse1ForQ1");

// List<FeedbackResponseComment> actualFrc = frcDb.getFeedbackResponseCommentForResponse(fr1.getId());
frDb.deleteFeedbackResponsesAndCommentsCascade(fr1);

assertNull(frDb.getFeedbackResponse(fr1.getId()));

assertNull(frcDb.getFeedbackResponseComment(frc1.getId()));
}

@Test
public void testHasResponsesFromGiverInSession() {
______TS("success: typical case");
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/teammates/common/util/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.time.Duration;
import java.time.Instant;

import teammates.storage.sqlentity.Section;

/**
* Stores constants that are widely used across classes.
* this class contains several nested classes, each containing a specific
Expand All @@ -25,6 +27,7 @@ public final class Const {
public static final int SECTION_SIZE_LIMIT = 100;

public static final String DEFAULT_SECTION = "None";
public static final Section DEFAULT_SQL_SECTION = null;

public static final String UNKNOWN_INSTITUTION = "Unknown Institution";

Expand Down
52 changes: 52 additions & 0 deletions src/main/java/teammates/sqllogic/api/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import javax.annotation.Nullable;

import teammates.common.datatransfer.DataBundle;
import teammates.common.datatransfer.FeedbackQuestionRecipient;
import teammates.common.datatransfer.NotificationStyle;
import teammates.common.datatransfer.NotificationTargetUser;
Expand Down Expand Up @@ -1147,6 +1148,57 @@ public FeedbackResponse getFeedbackResponse(UUID frId) {
return feedbackResponsesLogic.getFeedbackResponse(frId);
}

/**
* Creates a feedback response.
*
* <br/>Preconditions: <br/>
* * All parameters are non-null.
*
* @return created feedback response
* @throws InvalidParametersException if the response is not valid
* @throws EntityAlreadyExistsException if the response already exist
*/
public FeedbackResponse createFeedbackResponse(FeedbackResponse feedbackResponse)
throws InvalidParametersException, EntityAlreadyExistsException {
assert feedbackResponse != null;
return feedbackResponsesLogic.createFeedbackResponse(feedbackResponse);
}

/**
* Updates a non-null feedback response.
*
* <p>Cascade updates its associated feedback response comment
* (e.g. associated response ID, giverSection and recipientSection).
*
* <p>If the giver/recipient field is changed, the response is updated by recreating the response
* as question-giver-recipient is the primary key.
*
* <br/>Preconditions: <br/>
* * All parameters are non-null.
*
* @return updated feedback response
* @throws InvalidParametersException if attributes to update are not valid
* @throws EntityDoesNotExistException if the comment cannot be found
* @throws EntityAlreadyExistsException if the response cannot be updated
* by recreation because of an existent response
*/
public FeedbackResponse updateFeedbackResponseCascade(FeedbackResponse feedbackResponse)
throws InvalidParametersException, EntityDoesNotExistException, EntityAlreadyExistsException {
assert feedbackResponse != null;
return feedbackResponsesLogic.updateFeedbackResponseCascade(feedbackResponse);
}

/**
* Deletes a feedback response cascade its associated comments.
*
* <br/>Preconditions: <br/>
* * All parameters are non-null.
*/
public void deleteFeedbackResponsesAndCommentsCascade(FeedbackResponse feedbackResponse) {
assert feedbackResponse != null;
feedbackResponsesLogic.deleteFeedbackResponsesAndCommentsCascade(feedbackResponse.getId());
}

/**
* Get existing feedback responses from instructor for the given question.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ public List<FeedbackResponseComment> getFeedbackResponseCommentsForResponse(UUID
/**
* Gets the comment associated with the response.
*/
public List<FeedbackResponseComment> getFeedbackResponseCommentForResponse(UUID feedbackResponseId) {
return frcDb.getFeedbackResponseCommentForResponse(feedbackResponseId);
}

/** TODO: If there's a bug here, then update the comment
* Gets the comment associated with the response.
*/
public FeedbackResponseComment getFeedbackResponseCommentForResponseFromParticipant(
UUID feedbackResponseId) {
return frcDb.getFeedbackResponseCommentForResponseFromParticipant(feedbackResponseId);
Expand All @@ -87,6 +94,19 @@ public void deleteFeedbackResponseComment(Long frcId) {
frcDb.deleteFeedbackResponseComment(frcId);
}

/**
* Updates a feedback response comment by {@link FeedbackResponseComment}.
*
* @return updated comment
* @throws InvalidParametersException if attributes to update are not valid
* @throws EntityDoesNotExistException if the comment cannot be found
*/
public FeedbackResponseComment updateFeedbackResponseComment(FeedbackResponseComment feedbackResponseComment)
throws InvalidParametersException, EntityDoesNotExistException {

return frcDb.updateFeedbackResponseComment(feedbackResponseComment);
}

/**
* Updates a feedback response comment.
* @throws EntityDoesNotExistException if the comment does not exist
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/teammates/sqllogic/core/FeedbackResponsesLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import teammates.storage.sqlentity.Course;
import teammates.storage.sqlentity.FeedbackQuestion;
import teammates.storage.sqlentity.FeedbackResponse;
import teammates.storage.sqlentity.FeedbackResponseComment;
import teammates.storage.sqlentity.Instructor;
import teammates.storage.sqlentity.Section;
import teammates.storage.sqlentity.Student;
Expand Down Expand Up @@ -176,6 +177,61 @@ private List<FeedbackResponse> getFeedbackResponsesFromTeamForQuestion(
return responses;
}

/**
* Updates a non-null feedback response by {@link FeedbackResponse}.
*
* <p>Cascade updates its associated feedback response comment
* (e.g. associated response ID, giverSection and recipientSection).
*
* <p>If the giver/recipient field is changed, the response is updated by recreating the response
* as question-giver-recipient is the primary key.
*
* @return updated feedback response
* @throws InvalidParametersException if attributes to update are not valid
* @throws EntityDoesNotExistException if the comment cannot be found
* @throws EntityAlreadyExistsException if the response cannot be updated
* by recreation because of an existent response
*/
public FeedbackResponse updateFeedbackResponseCascade(FeedbackResponse feedbackResponse)
throws InvalidParametersException, EntityDoesNotExistException, EntityAlreadyExistsException {

FeedbackResponse oldResponse = frDb.getFeedbackResponse(feedbackResponse.getId());
FeedbackResponse newResponse = frDb.updateFeedbackResponse(feedbackResponse);

boolean isResponseIdChanged = !oldResponse.getId().equals(newResponse.getId());
boolean isGiverSectionChanged = !oldResponse.getGiverSection().equals(newResponse.getGiverSection());
boolean isRecipientSectionChanged = !oldResponse.getRecipientSection().equals(newResponse.getRecipientSection());

if (isResponseIdChanged || isGiverSectionChanged || isRecipientSectionChanged) {
List<FeedbackResponseComment> oldResponseComments =
frcLogic.getFeedbackResponseCommentForResponse(oldResponse.getId());
for (FeedbackResponseComment oldResponseComment : oldResponseComments) {
if (isResponseIdChanged) {
oldResponseComment.setFeedbackResponse(newResponse);
}

if (isGiverSectionChanged) {
oldResponseComment.setGiverSection(newResponse.getGiverSection());
}

if (isRecipientSectionChanged) {
oldResponseComment.setRecipientSection(newResponse.getRecipientSection());
}

frcLogic.updateFeedbackResponseComment(oldResponseComment);
}

}
return newResponse;
}

/**
* Deletes a feedback response cascade its associated feedback response comments.
*/
public void deleteFeedbackResponsesAndCommentsCascade(UUID feedbackResponseId) {
frDb.deleteFeedbackResponsesAndCommentsCascade(feedbackResponseId);
}

/**
* Deletes all feedback responses of a question cascade its associated comments.
*/
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/teammates/storage/sqlapi/EntitiesDb.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

package teammates.storage.sqlapi;

import com.google.common.base.Objects;

import teammates.common.util.HibernateUtil;
import teammates.common.util.Logger;
import teammates.storage.sqlentity.BaseEntity;
Expand All @@ -10,6 +12,12 @@
*/
class EntitiesDb {

/**
* Info message when entity is not saved because it does not change.
*/
static final String OPTIMIZED_SAVING_POLICY_APPLIED =
"Saving request is not issued because entity %s does not change by the update (%s)";

static final Logger log = Logger.getLogger();

/**
Expand Down Expand Up @@ -43,4 +51,11 @@ protected void delete(BaseEntity entity) {
HibernateUtil.remove(entity);
log.info("Entity deleted: " + entity.toString());
}

/**
* Checks whether two values are the same.
*/
<T> boolean hasSameValue(T oldValue, T newValue) {
return Objects.equal(oldValue, newValue);
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
package teammates.storage.sqlapi;

import static teammates.common.util.Const.ERROR_CREATE_ENTITY_ALREADY_EXISTS;
import static teammates.common.util.Const.ERROR_UPDATE_NON_EXISTENT;

import java.time.Instant;
import java.util.List;
import java.util.UUID;

import teammates.common.datatransfer.FeedbackParticipantType;
import teammates.common.exception.EntityAlreadyExistsException;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
import teammates.common.util.HibernateUtil;
import teammates.storage.sqlentity.Course;
import teammates.storage.sqlentity.FeedbackQuestion;
import teammates.storage.sqlentity.FeedbackResponse;
import teammates.storage.sqlentity.FeedbackResponseComment;
import teammates.storage.sqlentity.FeedbackSession;

import teammates.storage.sqlentity.Section;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaDelete;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Join;
import jakarta.persistence.criteria.Root;
import jakarta.persistence.criteria.Subquery;

/**
* Handles CRUD operations for feedbackResponseComments.
Expand Down Expand Up @@ -78,6 +84,24 @@ public void deleteFeedbackResponseComment(Long frcId) {
}
}

/**
* Deletes all feedbackResponseComments based on feedback response ID.
*/
public void deleteFeedbackResponseCommentForFeedbackResponseCascade(UUID feedbackResponseId) {
assert feedbackResponseId != null;

CriteriaBuilder cb = HibernateUtil.getCriteriaBuilder();
CriteriaDelete<FeedbackResponseComment> cd = cb.createCriteriaDelete(FeedbackResponseComment.class);
Root<FeedbackResponseComment> sRoot = cd.from(FeedbackResponseComment.class);
Subquery<UUID> subquery = cd.subquery(UUID.class);
Root<FeedbackResponseComment> subqueryRoot = subquery.from(FeedbackResponseComment.class);
Join<FeedbackResponseComment, FeedbackResponse> sqJoin = subqueryRoot.join("feedbackResponse");
subquery.select(subqueryRoot.get("id"));
subquery.where(cb.equal(sqJoin.get("id"), feedbackResponseId));
cd.where(cb.in(sRoot.get("id")).value(subquery));
HibernateUtil.createMutationQuery(cd).executeUpdate();
}

/**
* Gets all feedback response comments for a response.
*/
Expand Down Expand Up @@ -110,6 +134,56 @@ public FeedbackResponseComment getFeedbackResponseCommentForResponseFromParticip
return HibernateUtil.createQuery(cq).getResultStream().findFirst().orElse(null);
}

/**
* Updates a feedback response comment by {@link FeedbackResponseComment}.
*
* @return updated feedback response comment
* @throws InvalidParametersException if attributes to update are not valid
* @throws EntityDoesNotExistException if the comment cannot be found
*/
public FeedbackResponseComment updateFeedbackResponseComment(FeedbackResponseComment newFeedbackResponseComment)
throws InvalidParametersException, EntityDoesNotExistException {
assert newFeedbackResponseComment != null;

FeedbackResponseComment oldFeedbackResponseComment = getFeedbackResponseComment(newFeedbackResponseComment.getId());
if (oldFeedbackResponseComment == null) {
throw new EntityDoesNotExistException(ERROR_UPDATE_NON_EXISTENT + newFeedbackResponseComment);
}

newFeedbackResponseComment.sanitizeForSaving();
if (!newFeedbackResponseComment.isValid()) {
throw new InvalidParametersException(newFeedbackResponseComment.getInvalidityInfo());
}

// update only if change
boolean hasSameAttributes =
this.<Long>hasSameValue(newFeedbackResponseComment.getId(), oldFeedbackResponseComment.getId())
&& this.<String>hasSameValue(
newFeedbackResponseComment.getCommentText(), oldFeedbackResponseComment.getCommentText())
&& this.<List<FeedbackParticipantType>>hasSameValue(
newFeedbackResponseComment.getShowCommentTo(), oldFeedbackResponseComment.getShowCommentTo())
&& this.<List<FeedbackParticipantType>>hasSameValue(
newFeedbackResponseComment.getShowGiverNameTo(), oldFeedbackResponseComment.getShowGiverNameTo())
&& this.<String>hasSameValue(
newFeedbackResponseComment.getLastEditorEmail(), oldFeedbackResponseComment.getLastEditorEmail())
&& this.<Instant>hasSameValue(
newFeedbackResponseComment.getUpdatedAt(), oldFeedbackResponseComment.getUpdatedAt())
&& this.<Section>hasSameValue(
newFeedbackResponseComment.getGiverSection(), oldFeedbackResponseComment.getGiverSection())
&& this.<Section>hasSameValue(
newFeedbackResponseComment.getRecipientSection(), oldFeedbackResponseComment.getRecipientSection());
if (hasSameAttributes) {
log.info(String.format(
OPTIMIZED_SAVING_POLICY_APPLIED,
FeedbackResponseComment.class.getSimpleName(),
newFeedbackResponseComment));
return newFeedbackResponseComment;
}

merge(newFeedbackResponseComment);
return newFeedbackResponseComment;
}

/**
* Updates the giver email for all of the giver's comments in a course.
*/
Expand Down
Loading

0 comments on commit c4228c7

Please sign in to comment.