/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ee.tlu.htk.dippler.course;
import ee.tlu.htk.dippler.backoffice.StatusCodes;
import ee.tlu.htk.dippler.entities.Activity;
import ee.tlu.htk.dippler.entities.Course;
import ee.tlu.htk.dippler.entities.User;
import ee.tlu.htk.dippler.managers.UserManagerLocal;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
/**
*
* @author metz
*/
enum ActivityActions {
LOAD
}
@Stateless
public class ActivityManager implements ActivityManagerLocal {
@PersistenceContext private EntityManager em;
@EJB private UserManagerLocal userManager;;
public static final Integer LOAD = 1;
@Override
public String manageActivity(String action, String data, User user) {
switch (ActivityActions.valueOf(action)) {
case LOAD:
if (userManager.hasPermission(user, LOAD)) {
//return this.loadActivity(data);
} else {
return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "No rights for load activity");
}
default:
return StatusCodes.respond(StatusCodes.OPERATION_NOT_ALLOWED, "");
}
}
@Override
public void addActivity(String event, Course course, User user, Long object_id, String object_title, String object_type) {
Activity activity = new Activity();
activity.setEvent(event);
activity.setCourse(course);
activity.setCourseTitle(course.getTitle());
activity.setCreator(user);
activity.setCreatorFullname(user.getFullname());
activity.setObjectId(object_id);
activity.setObjectTitle(object_title);
activity.setObjectType(object_type);
em.persist(activity);
}
@Override
public String loadActivities(Course course) {
StringBuilder xml = new StringBuilder("");
try {
List activities = (List) course.getActivities();
for(Activity activity : activities ){
xml.append(marshal(activity));
}
} catch (Throwable t) {
t.printStackTrace(System.out);
}
xml.append("");
return xml.toString();
}
@Override
public String marshal(Activity activity) {
StringBuilder xml = new StringBuilder("");
xml.append("").append(activity.getId()).append("");
xml.append("").append(activity.getCourse().getId().toString()).append("");
xml.append("");
xml.append("");
xml.append("").append(activity.getObjectId().toString()).append("");
xml.append("");
xml.append("");
xml.append("").append(activity.getCreator().getId().toString()).append("");
xml.append("");
xml.append("").append(activity.getCreated().toString()).append("");
xml.append("");
return xml.toString();
}
@Override
public Long getUserCourseActivityCount(Long user_id, Long course_id) {
Query activity_count_query = em.createNativeQuery("SELECT COUNT(*) FROM Activity a WHERE a.creator=?1 AND a.course=?2");
activity_count_query.setParameter(1, user_id.toString());
activity_count_query.setParameter(2, course_id.toString());
return (Long) activity_count_query.getSingleResult();
}
@Override
public String getUserCourseActivityLastDate(Long user_id, Long course_id) {
Query user_activity_query = em.createNativeQuery("SELECT * FROM Activity a WHERE a.creator=?1 AND a.course=?2 ORDER BY created DESC", Activity.class);
user_activity_query.setParameter(1, user_id.toString());
user_activity_query.setParameter(2, course_id.toString());
user_activity_query.setMaxResults(1);
String last_activity_date = "";
try {
Activity activity = (Activity) user_activity_query.getSingleResult();
last_activity_date = activity.getCreated().toString();
} catch (Exception e) {
if (!(e instanceof javax.persistence.NoResultException)) {
e.printStackTrace(System.err);
}
}
return last_activity_date;
}
}