Example usage for org.hibernate.criterion Restrictions between

List of usage examples for org.hibernate.criterion Restrictions between

Introduction

In this page you can find the example usage for org.hibernate.criterion Restrictions between.

Prototype

public static Criterion between(String propertyName, Object low, Object high) 

Source Link

Document

Apply a "between" constraint to the named property

Usage

From source file:de.lemo.dms.processing.questions.async.AFrequentPathsBIDE.java

License:Open Source License

/**
 * Generates the necessary list of input-strings, containing the sequences (user paths) for the BIDE+ algorithm
 * /*from   ww  w.ja v  a 2  s  . c  o m*/
 * @param courses
 *            Course-Ids
 * @param users
 *            User-Ids
 * @param starttime
 *            Start time
 * @param endtime
 *            End time
 * @param gender 
 * @return The path to the generated file
 */
@SuppressWarnings("unchecked")
private LinkedList<String> generateLinkedListSessionBound(final List<Long> courses, List<Long> users,
        final List<String> types, final Long minLength, final Long maxLength, final Long starttime,
        final Long endtime, Session session, List<Long> gender) {
    final LinkedList<String> result = new LinkedList<String>();
    try {

        if (users == null || users.size() == 0) {
            users = new ArrayList<Long>(StudentHelper.getCourseStudentsAliasKeys(courses, gender).values());
        } else {
            Map<Long, Long> userMap = StudentHelper.getCourseStudentsAliasKeys(courses, gender);
            List<Long> tmp = new ArrayList<Long>();
            for (int i = 0; i < users.size(); i++) {
                tmp.add(userMap.get(users.get(i)));
            }
            users = tmp;
        }

        final Criteria criteria = session.createCriteria(ILogMining.class, "log");
        if (courses.size() > 0) {
            criteria.add(Restrictions.in("log.course.id", courses));
        }
        if (users.size() > 0) {
            criteria.add(Restrictions.in("log.user.id", users));
        }
        criteria.add(Restrictions.between("log.timestamp", starttime, endtime));
        final ArrayList<ILogMining> list = (ArrayList<ILogMining>) criteria.list();

        logger.debug("Read " + list.size() + " logs.");
        final ArrayList<ArrayList<ILogMining>> uhis = new ArrayList<ArrayList<ILogMining>>();

        final HashMap<Long, ArrayList<ILogMining>> logMap = new HashMap<Long, ArrayList<ILogMining>>();
        int max = 0;
        for (int i = 0; i < list.size(); i++) {
            if ((list.get(i).getUser() != null) && (list.get(i).getLearnObjId() != null)) {
                if (logMap.get(list.get(i).getUser().getId()) == null) {
                    final ArrayList<ILogMining> a = new ArrayList<ILogMining>();
                    a.add(list.get(i));
                    logMap.put(list.get(i).getUser().getId(), a);
                    if (logMap.get(list.get(i).getUser().getId()).size() > max) {
                        max = logMap.get(list.get(i).getUser().getId()).size();
                    }
                } else {

                    logMap.get(list.get(i).getUser().getId()).add(list.get(i));
                    Collections.sort(logMap.get(list.get(i).getUser().getId()));
                    if (logMap.get(list.get(i).getUser().getId()).size() > max) {
                        max = logMap.get(list.get(i).getUser().getId()).size();
                    }
                    if (list.get(i).getDuration() == -1) {
                        uhis.add(new ArrayList<ILogMining>(logMap.get(list.get(i).getUser().getId())));
                        logMap.remove(list.get(i).getUser().getId());
                    }
                }
            }
        }

        uhis.addAll(logMap.values());

        final Integer[] lengths = new Integer[(max / 10) + 1];
        for (int i = 0; i < lengths.length; i++) {
            lengths[i] = 0;
        }

        for (int i = 0; i < uhis.size(); i++) {
            lengths[uhis.get(i).size() / 10]++;
        }

        for (int i = 0; i < lengths.length; i++) {
            if (lengths[i] != 0) {
                logger.debug("Paths of length " + i + "0 - " + (i + 1) + "0: " + lengths[i]);
            }
        }

        logger.debug("Generated " + uhis.size() + " user histories. Max length @ " + max);

        int z = 0;

        // Write data to output file
        for (final Iterator<ArrayList<ILogMining>> iter = uhis.iterator(); iter.hasNext();) {
            final ArrayList<ILogMining> l = iter.next();
            if (l.size() > 5) {
                String line = "";
                for (int i = 0; i < l.size(); i++) {
                    if (idToLogM.get(l.get(i).getPrefix() + " " + l.get(i).getLearnObjId()) == null) {
                        idToLogM.put(l.get(i).getPrefix() + " " + l.get(i).getLearnObjId(), l.get(i));
                    }

                    if (requests.get(l.get(i).getPrefix() + " " + l.get(i).getLearnObjId()) == null) {
                        final ArrayList<Long> us = new ArrayList<Long>();
                        us.add(l.get(i).getUser().getId());
                        requests.put(l.get(i).getPrefix() + " " + l.get(i).getLearnObjId(), us);
                    } else {
                        requests.get(l.get(i).getPrefix() + " " + l.get(i).getLearnObjId())
                                .add(l.get(i).getUser().getId());
                    }

                    line += l.get(i).getPrefix() + "" + l.get(i).getLearnObjId() + " -1 ";
                }
                line += "-2";
                result.add(line);
                z++;
            }

        }
        logger.debug("Wrote " + z + " user histories.");

    } catch (final Exception e) {
        logger.error(e.getMessage());
    }
    return result;
}

From source file:de.lemo.dms.processing.questions.async.AFrequentPathsBIDE.java

License:Open Source License

/**
 * Generates the necessary list of input-strings, containing the sequences (user paths) for the BIDE+ algorithm
 * //from   w  w w . j  a  v  a2 s.co  m
 * @param courses
 *            Course-Ids
 * @param users
 *            User-Ids
 * @param starttime
 *            Start time
 * @param endtime
 *            End time
 * @param gender2 
 * @return The path to the generated file
 */
@SuppressWarnings("unchecked")
private LinkedList<String> generateLinkedList(final List<Long> courses, List<Long> users,
        final List<String> types, final Long minLength, final Long maxLength, final Long starttime,
        final Long endtime, Session session, List<Long> gender2) {
    final LinkedList<String> result = new LinkedList<String>();
    final boolean hasBorders = (minLength != null) && (maxLength != null) && (maxLength > 0)
            && (minLength < maxLength);
    final boolean hasTypes = (types != null) && (types.size() > 0);

    Criteria criteria;
    if (users == null || users.size() == 0) {
        users = new ArrayList<Long>(StudentHelper.getCourseStudentsAliasKeys(courses, gender).values());
    } else {
        Map<Long, Long> userMap = StudentHelper.getCourseStudentsAliasKeys(courses, gender);
        List<Long> tmp = new ArrayList<Long>();
        for (int i = 0; i < users.size(); i++) {
            tmp.add(userMap.get(users.get(i)));
        }
        users = tmp;
    }

    criteria = session.createCriteria(ILogMining.class, "log");
    if (courses.size() > 0) {
        criteria.add(Restrictions.in("log.course.id", courses));
    }
    if (users.size() > 0) {
        criteria.add(Restrictions.in("log.user.id", users));
    }
    criteria.add(Restrictions.between("log.timestamp", starttime, endtime));
    final ArrayList<ILogMining> list = (ArrayList<ILogMining>) criteria.list();

    logger.debug("Read " + list.size() + " logs.");

    int max = 0;

    final HashMap<Long, ArrayList<ILogMining>> logMap = new HashMap<Long, ArrayList<ILogMining>>();

    for (int i = 0; i < list.size(); i++) {
        if ((list.get(i).getUser() != null) && (list.get(i).getLearnObjId() != null)) {
            // If there isn't a user history for this user-id create a new one
            if (logMap.get(list.get(i).getUser().getId()) == null) {
                // User histories are saved in an ArrayList of ILogMining-objects
                final ArrayList<ILogMining> a = new ArrayList<ILogMining>();
                // Add current ILogMining-object to user-history
                a.add(list.get(i));
                // Add user history to the user history map
                logMap.put(list.get(i).getUser().getId(), a);
            } else {
                // Add current ILogMining-object to user-history
                logMap.get(list.get(i).getUser().getId()).add(list.get(i));
                // Sort the user's history (by time stamp)
                Collections.sort(logMap.get(list.get(i).getUser().getId()));
            }
        }
    }

    // Just changing the container for the user histories
    final ArrayList<ArrayList<ILogMining>> uhis = new ArrayList<ArrayList<ILogMining>>();
    int id = 1;
    for (final ArrayList<ILogMining> uLog : logMap.values()) {

        final ArrayList<ILogMining> tmp = new ArrayList<ILogMining>();
        boolean containsType = false;
        for (final ILogMining iLog : uLog) {
            if (idToInternalId.get(iLog.getPrefix() + " " + iLog.getLearnObjId()) == null) {
                internalIdToId.put(id, iLog.getPrefix() + " " + iLog.getLearnObjId());
                idToInternalId.put(iLog.getPrefix() + " " + iLog.getLearnObjId(), id);
                id++;
            }
            if (hasTypes) {
                for (final String type : types) {
                    if (iLog.getClass().getSimpleName().toLowerCase().contains(type.toLowerCase())) {
                        containsType = true;
                        tmp.add(iLog);
                        break;
                    }

                }
            }
            if (!hasTypes) {
                tmp.add(iLog);
            }
        }
        if ((!hasBorders || ((tmp.size() >= minLength) && (tmp.size() <= maxLength)))
                && (!hasTypes || containsType)) {
            uhis.add(tmp);
            if (tmp.size() > max) {
                max = tmp.size();
            }
        }
    }

    // This part is only for statistics - group histories of similar length together and display there
    // respective lengths
    final int dif = 10;
    final Integer[] lengths = new Integer[(max / dif) + 1];
    for (int i = 0; i < lengths.length; i++) {
        lengths[i] = 0;
    }

    for (int i = 0; i < uhis.size(); i++) {
        lengths[uhis.get(i).size() / dif]++;
    }

    for (int i = 0; i < lengths.length; i++) {
        if (lengths[i] != 0) {
            logger.debug("Paths of length " + i + "0 - " + (i + 1) + "0: " + lengths[i]);
        }
    }

    logger.debug("Generated " + uhis.size() + " user histories. Max length @ " + max);

    int z = 0;
    // Convert all user histories or "paths" into the format, that is requested by the BIDE-algorithm-class
    for (final ArrayList<ILogMining> l : uhis) {
        String line = "";
        for (int i = 0; i < l.size(); i++) {
            if (idToLogM.get(l.get(i).getPrefix() + " " + l.get(i).getLearnObjId()) == null) {
                idToLogM.put(l.get(i).getPrefix() + " " + l.get(i).getLearnObjId(), l.get(i));
            }

            // Update request numbers
            if (requests.get(l.get(i).getPrefix() + " " + l.get(i).getLearnObjId()) == null) {
                final ArrayList<Long> us = new ArrayList<Long>();
                us.add(l.get(i).getUser().getId());
                requests.put(l.get(i).getPrefix() + " " + l.get(i).getLearnObjId(), us);
            } else {
                requests.get(l.get(i).getPrefix() + " " + l.get(i).getLearnObjId())
                        .add(l.get(i).getUser().getId());
            }
            // The id of the object gets the prefix, indicating it's class. This is important for distinction
            // between objects of different ILogMining-classes but same ids
            line += idToInternalId.get(l.get(i).getPrefix() + " " + l.get(i).getLearnObjId()) + " -1 ";
        }
        line += "-2";
        logger.debug(line);
        result.add(line);
        z++;
    }
    logger.debug("Wrote " + z + " logs.");
    return result;
}

From source file:de.lemo.dms.processing.questions.QActivityResourceType.java

License:Open Source License

@SuppressWarnings("unchecked")
@POST/*from w  w  w  . j  a  va 2s.c o m*/
public ResultListResourceRequestInfo compute(@FormParam(MetaParam.COURSE_IDS) final List<Long> courses,
        @FormParam(MetaParam.START_TIME) final Long startTime,
        @FormParam(MetaParam.END_TIME) final Long endTime,
        @FormParam(MetaParam.TYPES) List<String> resourceTypes,
        @FormParam(MetaParam.GENDER) List<Long> gender) {

    validateTimestamps(startTime, endTime);

    final ResultListResourceRequestInfo result = new ResultListResourceRequestInfo();
    boolean allTypes = resourceTypes.isEmpty();
    final Session session = ServerConfiguration.getInstance().getMiningDbHandler().getMiningSession();

    List<Long> users = new ArrayList<Long>(StudentHelper.getCourseStudentsAliasKeys(courses, gender).values());
    if (users.isEmpty()) {
        logger.warn("Could not find associated users.");
        return new ResultListResourceRequestInfo();
    }

    if (resourceTypes != null && !allTypes) {
        List<String> tmp = new ArrayList<String>();
        for (String s : resourceTypes) {
            tmp.add(s.toUpperCase());
        }
        resourceTypes = tmp;
    }

    Criteria criteria = session.createCriteria(ILogMining.class, "log");
    criteria.add(Restrictions.in("log.course.id", courses))
            .add(Restrictions.between("log.timestamp", startTime, endTime));
    criteria.add(Restrictions.in("log.user.id", users));

    final List<ILogMining> logs = criteria.list();

    final Map<Long, ResourceRequestInfo> rriMap = new HashMap<Long, ResourceRequestInfo>();
    final Map<Long, Set<Long>> userMap = new HashMap<Long, Set<Long>>();
    for (ILogMining log : logs) {
        String type = log.getClass().getSimpleName().toUpperCase();
        if (type.contains("LOG")) {
            type = type.substring(0, type.indexOf("LOG"));
        }
        if (log.getLearnObjId() != null && log.getUser() != null
                && (resourceTypes.contains(type) || allTypes)) {
            Long id = Long.valueOf(log.getPrefix() + "" + log.getLearnObjId());
            if (rriMap.get(id) == null) {
                Set<Long> userSet = new HashSet<Long>();
                userSet.add(log.getUser().getId());
                userMap.put(id, userSet);
                rriMap.put(id, new ResourceRequestInfo(id, ELearningObjectType.valueOf(type), 1L, 1L,
                        log.getTitle(), 0L));
            } else {
                userMap.get(id).add(log.getUser().getId());
                rriMap.get(id).incRequests();
                rriMap.get(id).setUsers(((Integer) userMap.get(id).size()).longValue());
            }
        }
    }

    //Add unused Objects
    criteria = session.createCriteria(ICourseLORelation.class, "aso");
    criteria.add(Restrictions.in("aso.course.id", courses));
    List<ICourseLORelation> asoList = criteria.list();

    for (ICourseLORelation aso : asoList) {
        Long id = Long.valueOf(aso.getLearningObject().getPrefix() + "" + aso.getLearningObject().getId());
        if (!rriMap.containsKey(id)) {
            String type = aso.getLearningObject().getClass().getSimpleName().toUpperCase();
            if (type.contains("MINING")) {
                type = type.substring(0, type.indexOf("MINING"));
            }
            if (allTypes || resourceTypes.contains(type)) {
                final ResourceRequestInfo rri = new ResourceRequestInfo(id, ELearningObjectType.valueOf(type),
                        0L, 0L, aso.getLearningObject().getTitle(), 0L);
                result.add(rri);
                id++;
            }
        }
    }

    if (rriMap.values() != null) {
        result.addAll(rriMap.values());
    }
    session.close();
    return result;
}

From source file:de.lemo.dms.processing.questions.QActivityResourceTypeResolution.java

License:Open Source License

@SuppressWarnings("unchecked")
@POST/*from   w  ww .  j  av  a2 s  .c om*/
public ResultListRRITypes compute(@FormParam(MetaParam.COURSE_IDS) final List<Long> courses,
        @FormParam(MetaParam.START_TIME) final Long startTime,
        @FormParam(MetaParam.END_TIME) final Long endTime,
        @FormParam(MetaParam.RESOLUTION) final Long resolution,
        @FormParam(MetaParam.TYPES) final List<String> resourceTypes,
        @FormParam(MetaParam.GENDER) List<Long> gender) {

    validateTimestamps(startTime, endTime, resolution);

    final ResultListRRITypes result = new ResultListRRITypes();
    final IDBHandler dbHandler = ServerConfiguration.getInstance().getMiningDbHandler();
    final Session session = dbHandler.getMiningSession();
    final double intervall = (endTime - startTime) / (resolution);
    final boolean allTypes = resourceTypes.isEmpty();

    for (ELearningObjectType loType : ELearningObjectType.values()) {
        if (allTypes || resourceTypes.contains(loType.name().toLowerCase())) {
            Criteria criteria;
            List<Long> users = new ArrayList<Long>(
                    StudentHelper.getCourseStudentsAliasKeys(courses, gender).values());
            criteria = session.createCriteria(loType.getLogMiningType(), "log")
                    .add(Restrictions.between("log.timestamp", startTime, endTime));
            criteria.add(Restrictions.in("log.course.id", courses));
            if (users.size() > 0) {
                criteria.add(Restrictions.in("log.user.id", users));
            }

            final List<ILogMining> logs = criteria.list();
            HashMap<String, ResourceRequestInfo> rri = loadLogMining(logs, loType, startTime, endTime,
                    resolution, intervall);
            ArrayList<ResourceRequestInfo> rriList = new ArrayList<ResourceRequestInfo>(rri.values());

            switch (loType) {
            case ASSIGNMENT:
                result.setQuestionRRI(rriList);
                break;
            case CHAT:
                result.setChatRRI(rriList);
                break;
            case FORUM:
                result.setForumRRI(rriList);
                break;
            case QUESTION:
                result.setQuestionRRI(rriList);
                break;
            case QUIZ:
                result.setQuizRRI(rriList);
                break;
            case RESOURCE:
                result.setResourceRRI(rriList);
                break;
            case SCORM:
                result.setScormRRI(rriList);
                break;
            case WIKI:
                result.setWikiRRI(rriList);
                break;

            default:
                break;
            }
        }
    }
    session.close();

    return result;
}

From source file:de.lemo.dms.processing.questions.QCourseActivity.java

License:Open Source License

/**
 * Returns a list with the length of 'resolution'. Each entry holds the number of requests in the interval.
 * //from   w  ww .j av  a 2 s  .  c  om
 * @param courses
 *            (Mandatory) Course-identifiers of the courses that should be processed.
 * @param roles
 *            (Optional) Role-identifiers
 * @param startTime
 *            (Mandatory)
 * @param endTime
 *            (Mandatory)
 * @param resolution
 *            (Mandatory)
 * @param resourceTypes
 *            (Optional)
 * @return
 */
@POST
public ResultListHashMapObject compute(@FormParam(MetaParam.COURSE_IDS) final List<Long> courses,
        @FormParam(MetaParam.USER_IDS) List<Long> users, @FormParam(MetaParam.START_TIME) final Long startTime,
        @FormParam(MetaParam.END_TIME) final Long endTime,
        @FormParam(MetaParam.RESOLUTION) final Long resolution,
        @FormParam(MetaParam.TYPES) final List<String> resourceTypes,
        @FormParam(MetaParam.GENDER) List<Long> gender) {

    validateTimestamps(startTime, endTime, resolution);
    final Map<Long, ResultListLongObject> result = new HashMap<Long, ResultListLongObject>();
    Map<Long, Long> userMap = StudentHelper.getCourseStudentsAliasKeys(courses, gender);
    // Set up db-connection
    final IDBHandler dbHandler = ServerConfiguration.getInstance().getMiningDbHandler();
    final Session session = dbHandler.getMiningSession();

    if (users.isEmpty()) {
        users = new ArrayList<Long>(userMap.values());
        if (users.isEmpty()) {
            // TODO no users in course, maybe send some http error
            this.logger.debug("No users found for course. Returning empty resultset.");
            return new ResultListHashMapObject();
        }
    } else {
        List<Long> tmp = new ArrayList<Long>();
        for (int i = 0; i < users.size(); i++) {
            tmp.add(userMap.get(users.get(i)));
        }
        users = tmp;
    }

    // Calculate size of time intervalls
    final double intervall = (endTime - startTime) / (resolution);
    final Map<Long, Long> idToAlias = StudentHelper.getCourseStudentsRealKeys(courses, gender);
    final Map<Long, HashMap<Integer, Set<Long>>> userPerResStep = new HashMap<Long, HashMap<Integer, Set<Long>>>();

    // Create and initialize array for results
    for (int j = 0; j < courses.size(); j++) {

        final Long[] resArr = new Long[resolution.intValue()];
        for (int i = 0; i < resArr.length; i++) {
            resArr[i] = 0L;
        }
        final List<Long> l = new ArrayList<Long>();
        Collections.addAll(l, resArr);
        result.put(courses.get(j), new ResultListLongObject(l));
    }

    for (final Long course : courses) {
        userPerResStep.put(course, new HashMap<Integer, Set<Long>>());
    }

    for (String resourceType : resourceTypes) {
        this.logger.debug("Course Activity Request - CA Selection: " + resourceType);
    }
    if (resourceTypes.isEmpty()) {
        this.logger.info("Course Activity Request - CA Selection: NO Items selected ");
    }

    final Criteria criteria = session.createCriteria(ILogMining.class, "log")
            .add(Restrictions.in("log.course.id", courses))
            .add(Restrictions.between("log.timestamp", startTime, endTime))
            .add(Restrictions.in("log.user.id", users));

    @SuppressWarnings("unchecked")
    List<ILogMining> logs = criteria.list();

    for (int i = 0; i < logs.size(); i++) {
        boolean isInRT = false;
        if ((resourceTypes != null) && (resourceTypes.size() > 0)) {
            for (int j = 0; j < resourceTypes.size(); j++) {
                if (logs.get(i).getClass().toString().toUpperCase().contains(resourceTypes.get(j))) {
                    isInRT = true;
                    break;
                }
            }
        }
        if ((resourceTypes == null) || (resourceTypes.size() == 0) || isInRT) {
            Integer pos = new Double((logs.get(i).getTimestamp() - startTime) / intervall).intValue();
            if (pos > (resolution - 1)) {
                pos = resolution.intValue() - 1;
            }
            result.get(logs.get(i).getCourse().getId()).getElements().set(pos,
                    result.get(logs.get(i).getCourse().getId()).getElements().get(pos) + 1);
            if (userPerResStep.get(logs.get(i).getCourse().getId()).get(pos) == null) {
                final Set<Long> s = new HashSet<Long>();
                s.add(idToAlias.get(logs.get(i).getUser().getId()));
                userPerResStep.get(logs.get(i).getCourse().getId()).put(pos, s);
            } else {
                userPerResStep.get(logs.get(i).getCourse().getId()).get(pos)
                        .add(idToAlias.get(logs.get(i).getUser().getId()));
            }
        }
    }

    for (final Long c : courses) {
        for (int i = 0; i < resolution; i++) {
            if (userPerResStep.get(c).get(i) == null) {
                result.get(c).getElements().add(0L);
            } else {
                result.get(c).getElements().add(Long.valueOf(userPerResStep.get(c).get(i).size()));
            }
        }
    }

    final ResultListHashMapObject resultObject = new ResultListHashMapObject(result);
    if ((resultObject != null) && (resultObject.getElements() != null)) {
        final Set<Long> keySet = resultObject.getElements().keySet();
        final Iterator<Long> it = keySet.iterator();
        while (it.hasNext()) {
            final Long learnObjectTypeName = it.next();
            this.logger.info("Result Course IDs: " + learnObjectTypeName);
        }

    } else {
        this.logger.info("Returning empty resultset.");
    }
    session.close();
    return resultObject;
}

From source file:de.lemo.dms.processing.questions.QCourseUserPaths.java

License:Open Source License

@SuppressWarnings("unchecked")
@POST//from   w w w. j a va  2  s  . c o m
public JSONObject compute(@FormParam(MetaParam.COURSE_IDS) final List<Long> courses,
        @FormParam(MetaParam.START_TIME) final Long startTime, @FormParam(MetaParam.END_TIME) Long endTime,
        @FormParam(MetaParam.GENDER) List<Long> gender) throws JSONException {

    validateTimestamps(startTime, endTime);

    final Stopwatch stopWatch = new Stopwatch();
    stopWatch.start();

    final IDBHandler dbHandler = ServerConfiguration.getInstance().getMiningDbHandler();
    final Session session = dbHandler.getMiningSession();

    Criteria criteria;
    List<Long> users = new ArrayList<Long>(StudentHelper.getCourseStudentsAliasKeys(courses, gender).values());

    criteria = session.createCriteria(ILogMining.class, "log").add(Restrictions.in("log.course.id", courses))
            .add(Restrictions.between("log.timestamp", startTime, endTime))
            .add(Restrictions.eq("log.action", "view"));
    if (!users.isEmpty()) {
        criteria.add(Restrictions.in("log.user.id", users));
    } else {
        this.logger.debug("No users found for courses. Returning empty JSONObject.");
        return new JSONObject();
    }

    final List<ILogMining> logs = criteria.list();

    this.logger.debug("Found " + users.size() + " actions. " + +stopWatch.elapsedTime(TimeUnit.SECONDS));

    long courseCount = 0;
    final BiMap<CourseMining, Long> courseNodePositions = HashBiMap.create();
    final Map<Long/* user id */, List<Long/* course id */>> userPaths = Maps.newHashMap();

    this.logger.debug("Paths fetched: " + logs.size() + ". " + stopWatch.elapsedTime(TimeUnit.SECONDS));

    Map<Long, Long> idToAlias = StudentHelper.getCourseStudentsRealKeys(courses, gender);

    for (final ILogMining log : logs) {

        final CourseMining course = log.getCourse();
        Long nodeID = courseNodePositions.get(course);
        if (nodeID == null) {
            nodeID = courseCount++;
            courseNodePositions.put(course, nodeID);
        }

        final long userId = idToAlias.get(log.getUser().getId());

        List<Long> nodeIDs = userPaths.get(userId);
        if (nodeIDs == null) {
            nodeIDs = Lists.newArrayList();
            userPaths.put(userId, nodeIDs);
        }
        nodeIDs.add(nodeID);
    }

    this.logger.debug("userPaths: " + userPaths.size());

    final Map<Long /* node id */, List<UserPathLink>> coursePaths = Maps.newHashMap();

    for (final Entry<Long, List<Long>> userEntry : userPaths.entrySet()) {

        UserPathLink lastLink = null;

        for (final Long nodeID : userEntry.getValue()) {
            List<UserPathLink> links = coursePaths.get(nodeID);
            if (links == null) {
                links = Lists.newArrayList();
                coursePaths.put(nodeID, links);
            }
            final UserPathLink link = new UserPathLink(String.valueOf(nodeID), "0");
            links.add(link);

            if (lastLink != null) {
                lastLink.setTarget(String.valueOf(nodeID));
            }
            lastLink = link;
        }
    }
    stopWatch.stop();
    this.logger.debug("coursePaths: " + coursePaths.size());
    this.logger.debug("Total Fetched log entries: " + (logs.size() + logs.size()) + " log entries."
            + stopWatch.elapsedTime(TimeUnit.SECONDS));

    final Set<UserPathLink> links = Sets.newHashSet();

    final JSONObject result = new JSONObject();
    final JSONArray nodes = new JSONArray();
    final JSONArray edges = new JSONArray();

    for (final Entry<Long, List<UserPathLink>> courseEntry : coursePaths.entrySet()) {
        final JSONObject node = new JSONObject();
        node.put("name", courseNodePositions.inverse().get(courseEntry.getKey()).getTitle());
        node.put("value", courseEntry.getValue().size());
        node.put("group", courses.contains(courseNodePositions.inverse().get(courseEntry.getKey())) ? 1 : 2);
        nodes.put(node);

        for (final UserPathLink edge : courseEntry.getValue()) {
            if (edge.getTarget() == edge.getSource()) {
                continue;
            }
            links.add(edge);
        }
    }

    for (final UserPathLink link : links) {
        final JSONObject edgeJSON = new JSONObject();
        edgeJSON.put("target", link.getTarget());
        edgeJSON.put("source", link.getSource());
        edges.put(edgeJSON);
    }

    this.logger.debug("Nodes: " + nodes.length() + ", Links: " + edges.length() + "   / time: "
            + stopWatch.elapsedTime(TimeUnit.SECONDS));

    result.put("nodes", nodes);
    result.put("links", edges);
    session.close();
    return result;
}

From source file:de.lemo.dms.processing.questions.QCumulativeUserAccess.java

License:Open Source License

/**
 * @param startTime/*  w  ww  . j a  va  2  s.c o m*/
 *            min time for the data
 * @param endTime
 *            max time for the data
 * @param types
 *            list with learning objects to compute
 * @param course
 *            courses for the request
 * @return a list with the cumulative user access to the learning objects
 * @throws SQLException
 * @throws JSONException
 */
@SuppressWarnings("unchecked")
@POST
public ResultListBoxPlot compute(@FormParam(MetaParam.COURSE_IDS) final List<Long> course,
        @FormParam(MetaParam.TYPES) List<String> types, @FormParam(MetaParam.START_TIME) final Long startTime,
        @FormParam(MetaParam.END_TIME) final Long endTime) {

    validateTimestamps(startTime, endTime);

    final Set<ELearningObjectType> learnObjectTypes = ELearningObjectType.fromNames(types);

    if (types != null) {
        List<String> tmp = new ArrayList<String>();
        for (String s : types) {
            tmp.add(s.toUpperCase());
        }
        types = tmp;
    }

    // generiere querys
    final Map<ELearningObjectType, String> querys = this.generateQuerys(startTime, endTime, learnObjectTypes,
            course);

    super.logger.debug("Query result: " + querys.toString());

    final Session session = ServerConfiguration.getInstance().getMiningDbHandler().getMiningSession();

    // SQL querys
    super.logger.debug("Starting processing ....");
    final BoxPlotGeneratorForDates bpg = new BoxPlotGeneratorForDates();
    try {

        Criteria criteria = session.createCriteria(ILogMining.class, "log");
        criteria.add(Restrictions.between("log.timestamp", startTime, endTime));
        criteria.add(Restrictions.in("log.course.id", course));
        List<ILogMining> logs = criteria.list();

        for (ILogMining log : logs) {
            String type = log.getClass().getSimpleName().toUpperCase();
            if (type.indexOf("LOG") > -1) {
                type = type.substring(0, type.indexOf("LOG"));
                if (types.isEmpty() || types.contains(type))
                    bpg.addAccess(log.getTimestamp());
            }
        }
        logs.clear();
        /*
        for (final ELearningObjectType lo : querys.keySet()) {
           super.logger.debug("Starting processing -- Entering try catch");
           @SuppressWarnings("deprecation")
           final Statement statement = session.connection().createStatement();
           final ResultSet set = statement.executeQuery(querys.get(lo));
                   
                
           // durchlaufen des result sets
           while (set.next()) {
              bpg.addAccess(set.getLong("timestamp"));
           }
        }
        */
        final BoxPlot[] bp = bpg.calculateResult();
        final List<BoxPlot> l = new ArrayList<BoxPlot>();
        for (int i = 0; i < bp.length; i++) {
            l.add(bp[i]);
        }
        final ResultListBoxPlot rlbp = new ResultListBoxPlot(l);
        super.logger.debug("Resultlist created ...." + rlbp.toString() + " Number of entries: "
                + rlbp.getElements().size());
        session.close();
        return rlbp;
    } catch (final Exception e) {
        this.logger.error(e);
        session.close();
        return new ResultListBoxPlot();
    }

}

From source file:de.lemo.dms.processing.questions.QFrequentPathsApriori.java

License:Open Source License

/**
 * Generates the necessary list containing the sequences (user paths) for the algorithm
 * /* w w w .j a  va  2  s  .  c  o m*/
 * @param courses
 *            Course-Ids
 * @param users
 *            User-Ids
 * @param starttime
 *            Start time
 * @param endtime
 *            End time
 * @return The path to the generated file
 */
@SuppressWarnings("unchecked")
public List<List<Integer>> generateList(final List<Long> courses, List<Long> users, final List<String> types,
        final Long minLength, final Long maxLength, final Long starttime, final Long endtime, Session session,
        List<Long> gender, List<Long> learningObjects) {

    final List<List<Integer>> result = new ArrayList<List<Integer>>();
    final boolean hasBorders = (minLength != null) && (maxLength != null) && (maxLength > 0)
            && (minLength < maxLength);
    final boolean hasTypes = (types != null) && (types.size() > 0);
    Map<Long, ILearningObject> objects = new HashMap<Long, ILearningObject>();

    Criteria criteria;
    if (users == null || users.size() == 0) {
        users = new ArrayList<Long>(StudentHelper.getCourseStudentsAliasKeys(courses, gender).values());
    } else {
        Map<Long, Long> userMap = StudentHelper.getCourseStudentsAliasKeys(courses, gender);
        List<Long> tmp = new ArrayList<Long>();
        for (int i = 0; i < users.size(); i++) {
            tmp.add(userMap.get(users.get(i)));
        }
        users = tmp;
    }

    criteria = session.createCriteria(ILog.class, "log");
    if (courses.size() > 0) {
        criteria.add(Restrictions.in("log.course.id", courses));
    }
    if (users.size() > 0) {
        criteria.add(Restrictions.in("log.user.id", users));
    }
    if (!learningObjects.isEmpty()) {
        criteria.add(Restrictions.in("log.learning.id", learningObjects));
    }
    criteria.add(Restrictions.between("log.timestamp", starttime, endtime));
    criteria.addOrder(Property.forName("log.timestamp").asc());

    final ArrayList<ILog> list = (ArrayList<ILog>) criteria.list();
    Map<Long, List<Integer>> userPaths = new HashMap<Long, List<Integer>>();

    logger.debug("Found " + list.size() + " logs.");
    long id = -1;
    List<Integer> path = new ArrayList<Integer>();
    for (ILog log : list) {
        Integer pos = ((Long) log.getLearning().getId()).intValue();

        if (!hasTypes || types.contains(log.getLearning().getLOType())) {
            if (!objInd.contains(pos)) {
                objInd.add(pos);
            }
            if (idToLogM.get(pos) == null) {
                idToLogM.put(pos, log);
            }
            if (this.requests.get(objInd.indexOf(pos)) == null) {
                final ArrayList<Long> us = new ArrayList<Long>();
                us.add(log.getUser().getId());
                this.requests.put(objInd.indexOf(pos), us);
            } else {
                this.requests.get(objInd.indexOf(pos)).add(log.getUser().getId());
            }

            if (userPaths.get(log.getUser().getId()) == null) {
                List<Integer> l = new ArrayList<Integer>();
                l.add(objInd.indexOf(pos));
                userPaths.put(log.getUser().getId(), l);
            } else {
                userPaths.get(log.getUser().getId()).add(objInd.indexOf(pos));
            }
        }
        /*if(!hasTypes || types.contains(log.getLearning().getLOType()))
        {
           if(objects.get(log.getLearning()) == null)
              objects.put(log.getLearning().getId(), log.getLearning());
           if(id != log.getUser().getId())
           {
              id = log.getUser().getId();
              if(!hasBorders || (path.size() < maxLength && path.size() > minLength))
              {
          List<Integer> userPath = new ArrayList<Integer>();
          for(int i = 0; i < path.size(); i++)
          {
             userPath.add(path.get(i).intValue());   
          }
          if(userPath.size() > 0)
             result.add(userPath);
              }
              path = new ArrayList<Integer>();
           }
           if(!objInd.contains(pos))
           {
              objInd.add(pos);
           }
           if(idToLogM.get(pos) == null )
           {
              idToLogM.put(pos, log);
           }
           if (this.requests.get(objInd.indexOf(pos)) == null)
           {
              final ArrayList<Long> us = new ArrayList<Long>();
              us.add(log.getUser().getId());
              this.requests.put(objInd.indexOf(pos), us);
           } else {
              this.requests.get(objInd.indexOf(pos)).add(
             log.getUser().getId());
           }
           path.add(objInd.indexOf(pos));
        }
              */
    }

    for (List<Integer> uhis : userPaths.values()) {
        result.add(uhis);
    }

    userCount = result.size();
    logger.debug("Found " + result.size() + " user paths.");
    return result;
}

From source file:de.lemo.dms.processing.questions.QFrequentPathsViger.java

License:Open Source License

/**
 * Generates the necessary list of input-strings, containing the sequences (user paths) for the BIDE+ algorithm
 * /*from   ww  w .  j  a va2s . c  om*/
 * @param courses
 *            Course-Ids
 * @param users
 *            User-Ids
 * @param starttime
 *            Start time
 * @param endtime
 *            End time
 * @return The path to the generated file
 */
@SuppressWarnings("unchecked")
private LinkedList<String> generateLinkedList(final List<Long> courses, List<Long> users,
        final List<String> types, final Long minLength, final Long maxLength, final Long starttime,
        final Long endtime, Session session, List<Long> gender) {
    final LinkedList<String> result = new LinkedList<String>();
    final boolean hasBorders = (minLength != null) && (maxLength != null) && (maxLength > 0)
            && (minLength < maxLength);
    final boolean hasTypes = (types != null) && (types.size() > 0);

    Criteria criteria;
    if (users == null || users.size() == 0) {
        users = new ArrayList<Long>(StudentHelper.getCourseStudentsAliasKeys(courses, gender).values());
    } else {
        Map<Long, Long> userMap = StudentHelper.getCourseStudentsAliasKeys(courses, gender);
        List<Long> tmp = new ArrayList<Long>();
        for (int i = 0; i < users.size(); i++) {
            tmp.add(userMap.get(users.get(i)));
        }
        users = tmp;
    }

    criteria = session.createCriteria(ILogMining.class, "log");
    if (courses.size() > 0) {
        criteria.add(Restrictions.in("log.course.id", courses));
    }
    if (users.size() > 0) {
        criteria.add(Restrictions.in("log.user.id", users));
    }
    criteria.add(Restrictions.between("log.timestamp", starttime, endtime));
    final ArrayList<ILogMining> list = (ArrayList<ILogMining>) criteria.list();

    logger.debug("Read " + list.size() + " logs.");

    int max = 0;

    final HashMap<Long, ArrayList<ILogMining>> logMap = new HashMap<Long, ArrayList<ILogMining>>();

    for (int i = 0; i < list.size(); i++) {
        if ((list.get(i).getUser() != null) && (list.get(i).getLearnObjId() != null)) {
            // If there isn't a user history for this user-id create a new one
            if (logMap.get(list.get(i).getUser().getId()) == null) {
                // User histories are saved in an ArrayList of ILogMining-objects
                final ArrayList<ILogMining> a = new ArrayList<ILogMining>();
                // Add current ILogMining-object to user-history
                a.add(list.get(i));
                // Add user history to the user history map
                logMap.put(list.get(i).getUser().getId(), a);
            } else {
                // Add current ILogMining-object to user-history
                logMap.get(list.get(i).getUser().getId()).add(list.get(i));
                // Sort the user's history (by time stamp)
                Collections.sort(logMap.get(list.get(i).getUser().getId()));
            }
        }
    }

    // Just changing the container for the user histories
    final ArrayList<ArrayList<ILogMining>> uhis = new ArrayList<ArrayList<ILogMining>>();
    int id = 1;
    for (final ArrayList<ILogMining> uLog : logMap.values()) {

        final ArrayList<ILogMining> tmp = new ArrayList<ILogMining>();
        boolean containsType = false;
        for (final ILogMining iLog : uLog) {
            if (this.idToInternalId.get(iLog.getPrefix() + " " + iLog.getLearnObjId()) == null) {
                this.internalIdToId.put(id, iLog.getPrefix() + " " + iLog.getLearnObjId());
                this.idToInternalId.put(iLog.getPrefix() + " " + iLog.getLearnObjId(), id);
                id++;
            }
            if (hasTypes) {
                for (final String type : types) {
                    if (iLog.getClass().getSimpleName().toLowerCase().contains(type.toLowerCase())) {
                        containsType = true;
                        tmp.add(iLog);
                        break;
                    }

                }
            }
            if (!hasTypes) {
                tmp.add(iLog);
            }
        }
        if ((!hasBorders || ((tmp.size() >= minLength) && (tmp.size() <= maxLength)))
                && (!hasTypes || containsType)) {
            uhis.add(tmp);
            if (tmp.size() > max) {
                max = tmp.size();
            }
        }
    }

    // This part is only for statistics - group histories of similar length together and display there
    // respective lengths
    final Integer[] lengths = new Integer[(max / 10) + 1];
    for (int i = 0; i < lengths.length; i++) {
        lengths[i] = 0;
    }

    for (int i = 0; i < uhis.size(); i++) {
        lengths[uhis.get(i).size() / 10]++;
    }

    for (int i = 0; i < lengths.length; i++) {
        if (lengths[i] != 0) {
            logger.debug("Paths of length " + i + "0 - " + (i + 1) + "0: " + lengths[i]);
        }
    }

    logger.debug("Generated " + uhis.size() + " user histories. Max length @ " + max);

    int z = 0;

    // Convert all user histories or "paths" into the format, that is requested by the BIDE-algorithm-class
    for (final ArrayList<ILogMining> l : uhis) {
        String line = "";
        for (int i = 0; i < l.size(); i++) {
            if (this.idToLogM.get(l.get(i).getPrefix() + " " + l.get(i).getLearnObjId()) == null) {
                this.idToLogM.put(l.get(i).getPrefix() + " " + l.get(i).getLearnObjId(), l.get(i));
            }

            // Update request numbers
            if (this.requests.get(l.get(i).getPrefix() + " " + l.get(i).getLearnObjId()) == null) {
                final ArrayList<Long> us = new ArrayList<Long>();
                us.add(l.get(i).getUser().getId());
                this.requests.put(l.get(i).getPrefix() + " " + l.get(i).getLearnObjId(), us);
            } else {
                this.requests.get(l.get(i).getPrefix() + " " + l.get(i).getLearnObjId())
                        .add(l.get(i).getUser().getId());
            }
            // The id of the object gets the prefix, indicating it's class. This is important for distinction
            // between objects of different ILogMining-classes but same ids
            line += "<" + i + "> "
                    + this.idToInternalId.get(l.get(i).getPrefix() + " " + l.get(i).getLearnObjId()) + " -1 ";
        }
        line += "-2";
        result.add(line);
        z++;
    }
    logger.debug("Wrote " + z + " logs.");
    return result;
}

From source file:de.lemo.dms.processing.questions.QLearningObjectUsage.java

License:Open Source License

/**
 * Returns a list of resources and their respective statistics of usage.
 * /*from   w ww. j av a  2  s . c o  m*/
 * @see ELearningObjectType
 * @param courses
 *            List of course-identifiers
 * @param users
 *            List of user-identifiers
 * @param types
 *            List of learn object types (see ELearnObjType)
 * @param startTime
 *            LongInteger time stamp
 * @param endTime
 *            LongInteger time stamp
 * @return
 */
@SuppressWarnings("unchecked")
@POST
public ResultListResourceRequestInfo compute(@FormParam(MetaParam.COURSE_IDS) final List<Long> courses,
        @FormParam(MetaParam.USER_IDS) List<Long> users, @FormParam(MetaParam.TYPES) final List<String> types,
        @FormParam(MetaParam.START_TIME) final Long startTime,
        @FormParam(MetaParam.END_TIME) final Long endTime,
        @FormParam(MetaParam.GENDER) final List<Long> gender) {

    validateTimestamps(startTime, endTime);
    if (users.isEmpty()) {
        users = new ArrayList<Long>(StudentHelper.getCourseStudentsAliasKeys(courses, gender).values());
    } else {
        Map<Long, Long> userMap = StudentHelper.getCourseStudentsAliasKeys(courses, gender);
        List<Long> tmp = new ArrayList<Long>();
        for (int i = 0; i < users.size(); i++) {
            tmp.add(userMap.get(users.get(i)));
        }
        users = tmp;
    }

    final ResultListResourceRequestInfo result = new ResultListResourceRequestInfo();
    final IDBHandler dbHandler = ServerConfiguration.getInstance().getMiningDbHandler();
    final Session session = dbHandler.getMiningSession();

    // Create criteria for log-file-search
    Criteria criteria = session.createCriteria(ILogMining.class, "log")
            .add(Restrictions.between("log.timestamp", startTime, endTime));
    if (!courses.isEmpty()) {
        criteria.add(Restrictions.in("log.course.id", courses));
    }
    if (!users.isEmpty()) {
        criteria.add(Restrictions.in("log.user.id", users));
    }

    final List<ILogMining> logs = criteria.list();

    this.logger.debug("Total matched entries: " + logs.size());

    final HashMap<String, ArrayList<Long>> requests = new HashMap<String, ArrayList<Long>>();
    HashSet<String> requestedObjects = new HashSet<String>();

    for (final ILogMining ilo : logs) {
        // TODO use Class.getSimpleName() instead?
        final String obType = ilo.getClass().toString().substring(
                ilo.getClass().toString().lastIndexOf(".") + 1, ilo.getClass().toString().lastIndexOf("Log"));

        if ((types == null) || (types.isEmpty()) || types.contains(obType.toUpperCase())) {
            requestedObjects.add(ilo.getPrefix() + " " + ilo.getLearnObjId());

            final String id;
            if (ilo.getClass().getSimpleName().toUpperCase().contains("FORUM")) {
                String title = ((ForumLogMining) ilo).getSubject();
                if (title != null)
                    id = ilo.getPrefix() + "_" + ilo.getLearnObjId() + "?" + obType + "$" + title;
                else
                    id = ilo.getPrefix() + "_" + ilo.getLearnObjId() + "?" + obType + "$" + ilo.getTitle();
            } else {
                id = ilo.getPrefix() + "_" + ilo.getLearnObjId() + "?" + obType + "$" + ilo.getTitle();
            }
            if (requests.get(id) == null) {
                final ArrayList<Long> al = new ArrayList<Long>();
                al.add(ilo.getUser().getId());
                requests.put(id, al);
            } else {
                requests.get(id).add(ilo.getUser().getId());
            }

        }
    }

    //Adding RRIs for unused Objects
    criteria = session.createCriteria(ICourseLORelation.class, "aso");
    criteria.add(Restrictions.in("aso.course.id", courses));
    List<ICourseLORelation> asoList = criteria.list();

    Long id = 1L;

    for (ICourseLORelation aso : asoList) {
        String obId = aso.getLearningObject().getPrefix() + " " + aso.getLearningObject().getId();
        if (!requestedObjects.contains(obId)) {
            String type = aso.getLearningObject().getClass().getSimpleName().toUpperCase();
            if (type.contains("MINING")) {
                type = type.substring(0, type.indexOf("MINING"));
            }
            if (types.isEmpty() || types.contains(type)) {
                final ResourceRequestInfo rri = new ResourceRequestInfo(id, ELearningObjectType.valueOf(type),
                        0L, 0L, aso.getLearningObject().getTitle(), 0L);
                result.add(rri);
                id++;
            }
        }
    }

    for (final Entry<String, ArrayList<Long>> item : requests.entrySet()) {
        final String title = item.getKey().substring(item.getKey().indexOf("$") + 1);
        final String type = item.getKey().substring(item.getKey().indexOf("?") + 1, item.getKey().indexOf("$"));
        final ResourceRequestInfo rri = new ResourceRequestInfo(id,
                ELearningObjectType.valueOf(type.toUpperCase()), Long.valueOf(item.getValue().size()),
                Long.valueOf(new HashSet<Long>(item.getValue()).size()), title, 0L);
        id++;
        result.add(rri);
    }
    this.logger.debug("Total returned entries: " + result.getResourceRequestInfos().size());
    session.close();
    return result;
}