Skip to content

Commit

Permalink
Resolve conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
hhdqirui committed Feb 15, 2023
2 parents 2301922 + a45843d commit 54bef92
Show file tree
Hide file tree
Showing 6 changed files with 486 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/main/java/teammates/common/util/HibernateUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy;
import org.hibernate.cfg.Configuration;

import teammates.storage.sqlentity.Account;
import teammates.storage.sqlentity.BaseEntity;
import teammates.storage.sqlentity.Course;
import teammates.storage.sqlentity.FeedbackSession;
import teammates.storage.sqlentity.Notification;
import teammates.storage.sqlentity.ReadNotification;

/**
* Class containing utils for setting up the Hibernate session factory.
Expand All @@ -18,7 +20,7 @@ public final class HibernateUtil {
private static SessionFactory sessionFactory;

private static final List<Class<? extends BaseEntity>> ANNOTATED_CLASSES = List.of(Course.class,
FeedbackSession.class, Notification.class);
FeedbackSession.class, Account.class, Notification.class, ReadNotification.class);

private HibernateUtil() {
// Utility class
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/teammates/storage/sqlapi/UsageStatisticsDb.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package teammates.storage.sqlapi;

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

import org.hibernate.Session;

import teammates.common.util.HibernateUtil;
import teammates.storage.sqlentity.UsageStatistics;

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;

/**
* Handles CRUD operations for usage statistics.
*
* @see UsageStatistics
*/
public final class UsageStatisticsDb extends EntitiesDb<UsageStatistics> {

private static final UsageStatisticsDb instance = new UsageStatisticsDb();

private UsageStatisticsDb() {
// prevent initialization
}

public static UsageStatisticsDb inst() {
return instance;
}

/**
* Gets a list of statistics objects between start time and end time.
*/
public List<UsageStatistics> getUsageStatisticsForTimeRange(Instant startTime, Instant endTime) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<UsageStatistics> cr = cb.createQuery(UsageStatistics.class);
Root<UsageStatistics> root = cr.from(UsageStatistics.class);

cr.select(root).where(cb.and(
cb.greaterThanOrEqualTo(root.get("startTime"), startTime),
cb.lessThan(root.get("startTime"), endTime)));

return session.createQuery(cr).getResultList();
}
}
164 changes: 164 additions & 0 deletions src/main/java/teammates/storage/sqlentity/Account.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package teammates.storage.sqlentity;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import teammates.common.util.FieldValidator;
import teammates.common.util.SanitizationHelper;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;

/**
* Represents a unique account in the system.
*/
@Entity
@Table(name = "Accounts")
public class Account extends BaseEntity {
@Id
@GeneratedValue
private Long id;

@Column(nullable = false)
private String googleId;

@Column(nullable = false)
private String name;

@Column(nullable = false)
private String email;

@OneToMany(mappedBy = "account")
private List<ReadNotification> readNotifications;

@CreationTimestamp
@Column(updatable = false)
private Instant createdAt;

@UpdateTimestamp
@Column
private Instant updatedAt;

protected Account() {
// required by Hibernate
}

public Account(String googleId, String name, String email) {
this.googleId = googleId;
this.name = name;
this.email = email;
this.readNotifications = new ArrayList<>();
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getGoogleId() {
return googleId;
}

public void setGoogleId(String googleId) {
this.googleId = googleId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public List<ReadNotification> getReadNotifications() {
return readNotifications;
}

public void setReadNotifications(List<ReadNotification> readNotifications) {
this.readNotifications = readNotifications;
}

public Instant getCreatedAt() {
return createdAt;
}

public void setCreatedAt(Instant createdAt) {
this.createdAt = createdAt;
}

public Instant getUpdatedAt() {
return updatedAt;
}

public void setUpdatedAt(Instant updatedAt) {
this.updatedAt = updatedAt;
}

@Override
public List<String> getInvalidityInfo() {
List<String> errors = new ArrayList<>();

addNonEmptyError(FieldValidator.getInvalidityInfoForGoogleId(googleId), errors);
addNonEmptyError(FieldValidator.getInvalidityInfoForPersonName(name), errors);
addNonEmptyError(FieldValidator.getInvalidityInfoForEmail(email), errors);

return errors;
}

@Override
public void sanitizeForSaving() {
this.googleId = SanitizationHelper.sanitizeGoogleId(googleId);
this.name = SanitizationHelper.sanitizeName(name);
this.email = SanitizationHelper.sanitizeEmail(email);
}

@Override
public boolean equals(Object other) {
if (other == null) {
return false;
} else if (this == other) {
return true;
} else if (this.getClass() == other.getClass()) {
Account otherAccount = (Account) other;
return Objects.equals(this.email, otherAccount.email)
&& Objects.equals(this.name, otherAccount.name)
&& Objects.equals(this.googleId, otherAccount.googleId)
&& Objects.equals(this.id, otherAccount.id);
} else {
return false;
}
}

@Override
public int hashCode() {
return this.getId().hashCode();
}

@Override
public String toString() {
return "Account [id=" + id + ", googleId=" + googleId + ", name=" + name + ", email=" + email
+ ", readNotifications=" + readNotifications + ", createdAt=" + createdAt + ", updatedAt=" + updatedAt
+ "]";
}
}
27 changes: 24 additions & 3 deletions src/main/java/teammates/storage/sqlentity/Notification.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;

/**
Expand Down Expand Up @@ -56,6 +57,9 @@ public class Notification extends BaseEntity {
@Column(nullable = false)
private boolean shown;

@OneToMany(mappedBy = "notification")
private List<ReadNotification> readNotifications;

@CreationTimestamp
@Column(nullable = false, updatable = false)
private Instant createdAt;
Expand Down Expand Up @@ -164,6 +168,14 @@ public boolean isShown() {
return shown;
}

public List<ReadNotification> getReadNotifications() {
return readNotifications;
}

public void setReadNotifications(List<ReadNotification> readNotifications) {
this.readNotifications = readNotifications;
}

/**
* Sets the notification as shown to the user.
* Only allowed to change value from false to true.
Expand All @@ -190,8 +202,9 @@ public void setUpdatedAt(Instant updatedAt) {

@Override
public String toString() {
return "Notification [id=" + notificationId + ", startTime=" + startTime + ", endTime=" + endTime
+ ", style=" + style + ", targetUser=" + targetUser + ", shown=" + shown + ", createdAt=" + createdAt
return "Notification [notificationId=" + notificationId + ", startTime=" + startTime + ", endTime=" + endTime
+ ", style=" + style + ", targetUser=" + targetUser + ", title=" + title + ", message=" + message
+ ", shown=" + shown + ", readNotifications=" + readNotifications + ", createdAt=" + createdAt
+ ", updatedAt=" + updatedAt + "]";
}

Expand All @@ -209,7 +222,15 @@ public boolean equals(Object other) {
return true;
} else if (this.getClass() == other.getClass()) {
Notification otherNotification = (Notification) other;
return Objects.equals(this.notificationId, otherNotification.getNotificationId());
return Objects.equals(this.notificationId, otherNotification.getNotificationId())
&& Objects.equals(this.startTime, otherNotification.startTime)
&& Objects.equals(this.endTime, otherNotification.endTime)
&& Objects.equals(this.style, otherNotification.style)
&& Objects.equals(this.targetUser, otherNotification.targetUser)
&& Objects.equals(this.title, otherNotification.title)
&& Objects.equals(this.message, otherNotification.message)
&& Objects.equals(this.shown, otherNotification.shown)
&& Objects.equals(this.readNotifications, otherNotification.readNotifications);
} else {
return false;
}
Expand Down
Loading

0 comments on commit 54bef92

Please sign in to comment.