Search in sources :

Example 1 with UserAction

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());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Response(javax.ws.rs.core.Response) UserAction(org.opencastproject.usertracking.api.UserAction) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with UserAction

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();
        }
    }
}
Also used : UserAction(org.opencastproject.usertracking.api.UserAction) UserActionList(org.opencastproject.usertracking.api.UserActionList) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query)

Example 3 with UserAction

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();
        }
    }
}
Also used : UserAction(org.opencastproject.usertracking.api.UserAction) Footprint(org.opencastproject.usertracking.api.Footprint) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) FootprintImpl(org.opencastproject.usertracking.endpoint.FootprintImpl) FootprintList(org.opencastproject.usertracking.api.FootprintList) FootprintsListImpl(org.opencastproject.usertracking.endpoint.FootprintsListImpl) Footprint(org.opencastproject.usertracking.api.Footprint)

Example 4 with UserAction

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();
        }
    }
}
Also used : UserAction(org.opencastproject.usertracking.api.UserAction) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) UserTrackingException(org.opencastproject.usertracking.api.UserTrackingException) UserSession(org.opencastproject.usertracking.api.UserSession) NoResultException(javax.persistence.NoResultException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ParseException(java.text.ParseException) UserTrackingException(org.opencastproject.usertracking.api.UserTrackingException) NotFoundException(org.opencastproject.util.NotFoundException)

Example 5 with UserAction

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();
        }
    }
}
Also used : UserAction(org.opencastproject.usertracking.api.UserAction) UserActionList(org.opencastproject.usertracking.api.UserActionList) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query)

Aggregations

UserAction (org.opencastproject.usertracking.api.UserAction)16 EntityManager (javax.persistence.EntityManager)9 Query (javax.persistence.Query)9 UserActionList (org.opencastproject.usertracking.api.UserActionList)9 UserSession (org.opencastproject.usertracking.api.UserSession)6 Test (org.junit.Test)5 Footprint (org.opencastproject.usertracking.api.Footprint)3 Calendar (java.util.Calendar)2 GregorianCalendar (java.util.GregorianCalendar)2 ParseException (java.text.ParseException)1 LinkedList (java.util.LinkedList)1 EntityTransaction (javax.persistence.EntityTransaction)1 NoResultException (javax.persistence.NoResultException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Response (javax.ws.rs.core.Response)1 Ignore (org.junit.Ignore)1 FootprintList (org.opencastproject.usertracking.api.FootprintList)1 UserTrackingException (org.opencastproject.usertracking.api.UserTrackingException)1 FootprintImpl (org.opencastproject.usertracking.endpoint.FootprintImpl)1 FootprintsListImpl (org.opencastproject.usertracking.endpoint.FootprintsListImpl)1