package service.impl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import model.Grade;
import model.Rateable;
import model.Rating;
import model.Student;
import model.User;
import persistence.PersistenceContext;
import service.RatingService;
import service.exception.NotAllowedException;
import service.exception.RateLimitExceededException;
/**
*
* @author Christian Beikov, Florian Zaruba
*/
public class RateServiceImpl implements RatingService {
private Map<Student, Map<Rateable, List<Rating>>> ratings;
@SuppressWarnings("unchecked")
public RateServiceImpl(PersistenceContext context) {
ratings = context.getPersistentMap("ratings");
}
public void rate(User rater, Student student, Rateable subject, Grade grade) throws NotAllowedException, RateLimitExceededException {
// get all Ratings for the specified student
Map<Rateable, List<Rating>> userRatings = ratings.get(student);
List<Rating> l = null;
if (userRatings == null) {
userRatings = new HashMap<Rateable, List<Rating>>();
ratings.put(student, userRatings);
} else {
l = userRatings.get(subject);
}
// check if the rater is allowed to rate the student for the specified
// subject if not throw a NotAllowedException
if (User.ADMIN != rater && !subject.getRaters().contains(rater)) {
throw new NotAllowedException(rater + " is not allowed to rate ");
}
// check if for the given subject exists at least one rating
// if not create a new empty list
if (l == null) {
l = new ArrayList<Rating>();
userRatings.put(subject, l);
}
if (l.size() + 1 > subject.getMaxRateCount()) {
throw new RateLimitExceededException();
}
//add the rating for the subject
l.add(new DefaulRating(subject, rater, student, grade));
}
public Map<Rateable, List<Rating>> getRatings(User requester, Student student) throws NotAllowedException {
if (!requester.equals(student) && requester != User.ADMIN) {
throw new NotAllowedException(requester + " is not allowed to retrieve the ratings for " + student);
}
Map<Rateable, List<Rating>> userRatings = ratings.get(student);
if (userRatings == null) {
return Collections.emptyMap();
}
return new HashMap<Rateable, List<Rating>>(userRatings);
}
public List<Rating> getRatings(User requester, Rateable subject, Student student) throws NotAllowedException {
if (!requester.equals(student) && !subject.getRaters().contains(requester) && requester != User.ADMIN) {
throw new NotAllowedException(requester + " is not allowed to retrieve the ratings for " + student);
}
Map<Rateable, List<Rating>> userRatings = ratings.get(student);
List<Rating> subjectRatings = null;
if (userRatings != null) {
subjectRatings = userRatings.get(subject);
}
if (subjectRatings == null) {
return Collections.emptyList();
}
return new ArrayList<Rating>(subjectRatings);
}
private class DefaulRating implements Rating {
private Rateable subject;
private User rater;
private User ratedUser;
private Grade grade;
public DefaulRating(Rateable subject, User rater, User ratedUser, Grade grade) {
this.subject = subject;
this.rater = rater;
this.ratedUser = ratedUser;
this.grade = grade;
}
public Rateable getSubject() {
return subject;
}
public User getRater() {
return rater;
}
public User getRatedUser() {
return ratedUser;
}
public Grade getGrade() {
return grade;
}
}
}