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 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 .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exclude_patterns:
- "**/test/"
- "**/androidTest/"
- "**test"
- "**test"
204 changes: 204 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,204 @@
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 com.github.sdp.mediato.model.Location;
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 {

public static final double MAX_LATITUDE = 90;
public static final double MAX_LONGITUDE = 180;
public static final double MIN_LATITUDE = -90;
public static final double MIN_LONGITUDE = -180;

/**
* 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.checkRegisterDate(user.getRegisterDate());
Preconditions.checkLocation(user.getLocation());
}

/**
* Checks if Media attributes are valid
* @param mediaType
* @param title
* @param summary
* @param imageUrl
*/
public static void checkMedia(MediaType mediaType, String title, String summary, String imageUrl){
Preconditions.checkTitle(title);
Preconditions.checkSummary(summary);
Preconditions.checkImageURL(imageUrl);
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 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 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(Location location) {
if (location.isValid() && !locationWithinBounds(location)){
throw new IllegalArgumentException("Location mus be between -90 and 90 for latitude and -180 and 180 for longitude");
}
}

/**
* Checks if the latitude and longitude are within the authorised bounds
*/
public static boolean locationWithinBounds(Location location){
return (location.getLatitude() > MIN_LATITUDE && location.getLatitude() < MAX_LATITUDE)
&& (location.getLongitude() > MIN_LONGITUDE && location.getLongitude() < MAX_LONGITUDE);
}

/**
* 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 image url is valid
* @param imageUrl to be checked
* @TODO determine remaining criteria: format, existence etc...
*/
public static void checkImageURL(String imageUrl){ checkNullOrEmptyString(imageUrl, "image url");}


/**
* 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");
}
}
}
41 changes: 41 additions & 0 deletions app/src/main/java/com/github/sdp/mediato/model/Location.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.github.sdp.mediato.model;

public class Location {

double latitude;
double longitude;

boolean valid;

public Location(){valid = false;}

public Location(double latitude, double longitude){
this.latitude = latitude;
this.longitude = longitude;
this.valid = true;
}

public double getLatitude() {
return latitude;
}

public void setLatitude(double latitude) {
this.latitude = latitude;
}

public double getLongitude() {
return longitude;
}

public void setLongitude(double longitude) {
this.longitude = longitude;
}

public boolean isValid() {
return valid;
}

public void setValid(boolean valid) {
this.valid = valid;
}
}
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