Skip to content
This repository has been archived by the owner on Feb 1, 2025. It is now read-only.

Model definition #33

Merged
merged 16 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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 .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exclude_patterns:
- "**/test/"
- "**/androidTest/"
- "**test"
- "**test"
199 changes: 199 additions & 0 deletions app/src/main/java/com/github/sdp/mediato/errorCheck/Preconditions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
package com.github.sdp.mediato.errorCheck;

import static com.github.sdp.mediato.model.Review.MAX_GRADE;
import static com.github.sdp.mediato.model.Review.MIN_GRADE;
import static com.github.sdp.mediato.model.User.LIMIT_LOCATION_SIZE;

import com.github.sdp.mediato.model.User;
import com.github.sdp.mediato.model.media.Media;
import com.github.sdp.mediato.model.media.MediaType;

import java.util.List;
import java.util.Objects;

/**
* Class to check preconditions on attributes
* @TODO Corner cases, dates and email formatting to be discussed and checked
*/
public class Preconditions {

/**
* Checks if mandatory user fields are valid
* @param user
*/
public static void checkUser(User user){
Preconditions.checkUID(user.getId());
Preconditions.checkUsername(user.getUsername());
Preconditions.checkEmail(user.getEmail());
Preconditions.checkBirthDate(user.getBirthDate());
Preconditions.checkRegisterDate(user.getRegisterDate());
Preconditions.checkLocation(user.getLocation());
}

/**
* Checks if Media attributes are valid
* @param mediaType
* @param title
* @param summary
*/
public static void checkMedia(MediaType mediaType, String title, String summary){
Preconditions.checkTitle(title);
Preconditions.checkSummary(summary);
Objects.requireNonNull(mediaType);
}

/**
* Checks if Review mandatory attributes are valid
* @param username
* @param media
*/
public static void checkReview(String username, Media media){
Preconditions.checkUsername(username);
Preconditions.checkMedia(media);
}

/**
* Checks if Review attributes are valid when adding the grade
* @param username
* @param media
* @param grade
*/
public static void checkReview(String username, Media media, int grade){
Preconditions.checkReview(username, media);
Preconditions.checkGrade(grade);
}

/**
* Checks if Review attributes are valid when adding the grade and a comment
* @param username
* @param media
* @param grade
* @param comment
*/
public static void checkReview(String username, Media media, int grade, String comment){
Preconditions.checkReview(username, media, grade);
Preconditions.checkComment(comment);
}

/**
* Checks if the user id is valid
* @param uId the user id
* @TODO determine conditions for user id validity
*/
public static void checkUID(String uId){
checkNullOrEmptyString(uId, "user Id");
}

/**
* Checks if username is valid
* @param username
* @TODO determine condition for username validity (formatting, uniqueness...)
*/
public static void checkUsername(String username){
checkNullOrEmptyString(username, "username");
}

/**
* Checks if displayed name is valid
* @param displayedName
* @TODO determine condition for displayed name validity (formatting, uniqueness...)
*/
public static void checkDisplayedName(String displayedName){
checkNullOrEmptyString(displayedName, "displayed name");
}

/**
* Checks if email address is valid
* @param email
* @TODO determine - if necessary - condition for email validity (format, existence using google auth)
*/
public static void checkEmail(String email){
checkNullOrEmptyString(email, "email");
}

/**
* Checks if birth date is valid
* @param birthDate
* @TODO determine birth date validity (format, date)
*/
public static void checkBirthDate(String birthDate){
checkNullOrEmptyString(birthDate, "birth date");
}

/**
* Checks if register date is valid
* @param registerDate
* @TODO determine register date validity (format, date)
*/
public static void checkRegisterDate(String registerDate){
checkNullOrEmptyString(registerDate, "register date");
}

/**
* Checks if the location is valid
* @param location
*/
public static void checkLocation(List<Double> location) {
if (location == null || location.isEmpty()) throw new IllegalArgumentException("Location is unknown: list is null or empty");
if (location.size() != LIMIT_LOCATION_SIZE) throw new IllegalArgumentException("Location should contain exactly two doubles");
}

/**
* Checks if media is not null
* @param media
*/
public static void checkMedia(Media media){
if (media == null) {
throw new IllegalArgumentException("Media must not be null");
}
}

/**
* Checks if grade is within bounds
* @param grade
*/
public static void checkGrade(int grade){
if ((grade > MAX_GRADE) || (grade < MIN_GRADE)) {
throw new IllegalArgumentException("Grade must be between " + MIN_GRADE + " and " + MAX_GRADE);
}
}

/**
* Checks if comment is valid
* @param comment
* @TODO determine remaining criteria: char limit etc...
*/
public static void checkComment(String comment){
checkNullOrEmptyString(comment, "comment");
}

/**
* Checks if title is valid
* @param title
* @TODO determine - if necessary - other criteria for title validity
*/
public static void checkTitle(String title){
checkNullOrEmptyString(title, "title");
}

/**
* Checks if summary is valid
* @param summary
* @TODO determine remaining criteria: char limit etc...
*/
public static void checkSummary(String summary){
checkNullOrEmptyString(summary, "summary");
}

/**
* Checks if a string is null or empty
* @param string to be checked
* @param variable corresponding to string
* @throws IllegalArgumentException indicating that the @param variable cannot not be null or empty
*/
public static void checkNullOrEmptyString(String string, String variable){
if (string == null || string.isEmpty()){
throw new IllegalArgumentException(variable + "must not be null or empty");
}
}
}
72 changes: 72 additions & 0 deletions app/src/main/java/com/github/sdp/mediato/model/Review.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.github.sdp.mediato.model;

import android.provider.MediaStore;

import com.github.sdp.mediato.errorCheck.Preconditions;
import com.github.sdp.mediato.model.media.Media;

public class Review {

public static final int MAX_GRADE = 10;
public static final int MIN_GRADE = 1;
private final String username;
private final Media media;
private int grade;
private String comment;

public Review(String username, Media media) {
Preconditions.checkReview(username, media);
this.username = username;
this.media = media;
}

public Review(String username, Media media, int grade) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

Preconditions.checkReview(username, media, grade);
this.username = username;
this.media = media;
this.grade = grade;
}

public Review(String username, Media media, int grade, String comment) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

Preconditions.checkReview(username, media, grade, comment);
this.username = username;
this.media = media;
this.grade = grade;
this.comment = comment;
}

public String getUsername() {
return username;
}
public Media getMedia() {
return media;
}
public int getGrade() throws Exception {
try {
Preconditions.checkGrade(grade);
return grade;
}
catch (Exception e){
throw new Exception("This review does not have a grade.");
}
}
public void setGrade(int grade) {
Preconditions.checkGrade(grade);
this.grade = grade;
}
public String getComment() throws Exception {
try {
Preconditions.checkComment(comment);
return comment;
}
catch (Exception e){
throw new Exception("This review does not have a comment.");
}
}

public void setComment(String comment) {
Preconditions.checkComment(comment);
this.comment = comment;
}

}
Loading