use of org.opencastproject.usertracking.api.UserAction in project opencast by opencast.
the class UserTrackingRestServiceTest method testNullContext.
@Test
@Ignore
public void testNullContext() {
service.activate(null);
// This is broken, the Response object generated in the REST service can't find the appropriate class :(
HttpServletRequest request = getMockHttpSession();
Response r = service.addFootprint("test", "0", "10", "FOOTPRINT", "true", request);
UserAction a = (UserAction) r.getEntity();
Assert.assertEquals(0, a.getInpoint());
Assert.assertEquals(10, a.getOutpoint());
Assert.assertEquals(10, a.getLength());
Assert.assertEquals("FOOTPRINT", a.getType());
Assert.assertTrue(a.getIsPlaying());
Assert.assertEquals("test", a.getMediapackageId());
Assert.assertEquals(MOCK_SESSION_ID, a.getSession().getSessionId());
Assert.assertEquals(REMOTE_IP, a.getSession().getUserIp());
Assert.assertEquals(MOCK_USER, a.getSession().getUserId());
request = getMockHttpSessionWithProxy();
r = service.addFootprint("test", "20", "30", "FOOTPRINT", "true", request);
a = (UserAction) r.getEntity();
Assert.assertEquals(20, a.getInpoint());
Assert.assertEquals(30, a.getOutpoint());
Assert.assertEquals(10, a.getLength());
Assert.assertEquals("FOOTPRINT", a.getType());
Assert.assertTrue(a.getIsPlaying());
Assert.assertEquals("test", a.getMediapackageId());
Assert.assertEquals(MOCK_SESSION_ID, a.getSession().getSessionId());
Assert.assertEquals(REMOTE_IP, a.getSession().getUserIp());
Assert.assertEquals(MOCK_USER, a.getSession().getUserId());
}
use of org.opencastproject.usertracking.api.UserAction in project opencast by opencast.
the class UserTrackingServiceImpl method getUserActionsByType.
@SuppressWarnings("unchecked")
public UserActionList getUserActionsByType(String type, int offset, int limit) {
UserActionList result = new UserActionListImpl();
result.setTotal(getTotal(type));
result.setOffset(offset);
result.setLimit(limit);
EntityManager em = null;
try {
em = emf.createEntityManager();
Query q = em.createNamedQuery("findUserActionsByType");
q.setParameter("type", type);
q.setFirstResult(offset);
if (limit > 0)
q.setMaxResults(limit);
Collection<UserAction> userActions = q.getResultList();
for (UserAction a : userActions) {
result.add(a);
}
return result;
} finally {
if (em != null && em.isOpen()) {
em.close();
}
}
}
use of org.opencastproject.usertracking.api.UserAction in project opencast by opencast.
the class UserTrackingServiceImpl method getFootprints.
public FootprintList getFootprints(String mediapackageId, String userId) {
EntityManager em = null;
if (!logUser)
userId = null;
try {
em = emf.createEntityManager();
Query q = null;
if (StringUtils.trimToNull(userId) == null) {
q = em.createNamedQuery("findUserActionsByTypeAndMediapackageIdOrderByOutpointDESC");
} else {
q = em.createNamedQuery("findUserActionsByTypeAndMediapackageIdByUserOrderByOutpointDESC");
q.setParameter("userid", userId);
}
q.setParameter("type", FOOTPRINT_KEY);
q.setParameter("mediapackageId", mediapackageId);
@SuppressWarnings("unchecked") Collection<UserAction> userActions = q.getResultList();
int[] resultArray = new int[1];
boolean first = true;
for (UserAction a : userActions) {
if (first) {
// Get one more item than the known outpoint to append a footprint of 0 views at the end of the result set
resultArray = new int[a.getOutpoint() + 1];
first = false;
}
for (int i = a.getInpoint(); i < a.getOutpoint(); i++) {
resultArray[i]++;
}
}
FootprintList list = new FootprintsListImpl();
int current = -1;
int last = -1;
for (int i = 0; i < resultArray.length; i++) {
current = resultArray[i];
if (last != current) {
Footprint footprint = new FootprintImpl();
footprint.setPosition(i);
footprint.setViews(current);
list.add(footprint);
}
last = current;
}
return list;
} finally {
if (em != null && em.isOpen()) {
em.close();
}
}
}
use of org.opencastproject.usertracking.api.UserAction in project opencast by opencast.
the class UserTrackingServiceImpl method addUserFootprint.
@SuppressWarnings("unchecked")
public UserAction addUserFootprint(UserAction a, UserSession session) throws UserTrackingException {
a.setType(FOOTPRINT_KEY);
EntityManager em = null;
EntityTransaction tx = null;
if (!logIp)
session.setUserIp("-omitted-");
if (!logUser)
session.setUserId("-omitted-");
if (!logSession)
session.setSessionId("-omitted-");
try {
em = emf.createEntityManager();
tx = em.getTransaction();
tx.begin();
UserSession userSession = populateSession(em, session);
Query q = em.createNamedQuery("findLastUserFootprintOfSession");
q.setMaxResults(1);
q.setParameter("session", userSession);
Collection<UserAction> userActions = q.getResultList();
if (userActions.size() >= 1) {
UserAction last = userActions.iterator().next();
if (last.getMediapackageId().equals(a.getMediapackageId()) && last.getType().equals(a.getType()) && last.getOutpoint() == a.getInpoint()) {
// We are assuming in this case that the sessions match and are unchanged (IP wise, for example)
last.setOutpoint(a.getOutpoint());
a = last;
a.setId(last.getId());
} else {
a.setSession(userSession);
em.persist(a);
}
} else {
a.setSession(userSession);
em.persist(a);
}
tx.commit();
return a;
} catch (Exception e) {
if (tx != null && tx.isActive()) {
tx.rollback();
}
throw new UserTrackingException(e);
} finally {
if (em != null && em.isOpen()) {
em.close();
}
}
}
use of org.opencastproject.usertracking.api.UserAction in project opencast by opencast.
the class UserTrackingServiceImpl method getUserActionsByTypeAndMediapackageIdByDescendingDate.
public UserActionList getUserActionsByTypeAndMediapackageIdByDescendingDate(String type, String mediapackageId, int offset, int limit) {
UserActionList result = new UserActionListImpl();
result.setTotal(getTotal(type, mediapackageId));
result.setOffset(offset);
result.setLimit(limit);
EntityManager em = null;
try {
em = emf.createEntityManager();
Query q = em.createNamedQuery("findUserActionsByMediaPackageAndTypeDescendingByDate");
q.setParameter("type", type);
q.setParameter("mediapackageId", mediapackageId);
q.setFirstResult(offset);
if (limit > 0)
q.setMaxResults(limit);
@SuppressWarnings("unchecked") Collection<UserAction> userActions = q.getResultList();
for (UserAction a : userActions) {
result.add(a);
}
return result;
} finally {
if (em != null && em.isOpen()) {
em.close();
}
}
}
Aggregations