<commit_before>package de.stups.slottool.data

import de.stups.slottool.data.entities.AbstractUnit
import de.stups.slottool.data.entities.AbstractUnitUnitSemester
import de.stups.slottool.data.entities.Course
import de.stups.slottool.data.entities.Group
import de.stups.slottool.data.entities.Info
import de.stups.slottool.data.entities.Level
import de.stups.slottool.data.entities.Module
import de.stups.slottool.data.entities.ModuleAbstractUnitSemester
import de.stups.slottool.data.entities.Unit
import groovy.sql.Sql
import org.hibernate.SessionFactory
import org.hibernate.cfg.Configuration
import org.hibernate.criterion.Restrictions

import java.nio.file.Files
import java.nio.file.Paths

import static java.lang.Thread.currentThread
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING

class Store extends AbstractStore {

    LinkedHashMap units
    String dbpath
    SessionFactory sessionFactory
    org.hibernate.Session session

    def Store(String dbpath) {
        this.dbpath = dbpath
        openDataBase(dbpath)
        checkSchemaVersion()
    }

    def List<Info> getInfo() {
        session.createQuery("from Info").setCacheable(true).list()
    }

    def List<AbstractUnit> getAbstractUnits() {
        session.createQuery("from AbstractUnit").setCacheable(false).list()
    }

    def getAbstractUnitUnitSemesterByUnitID(def unit_id) {
        session.createCriteria(AbstractUnitUnitSemester.class).add(Restrictions.eq("unit.id", unit_id)).list()
    }

    def List<AbstractUnitUnitSemester> getAbstractUnitUnitSemester() {
        session.createQuery("from AbstractUnitUnitSemester").setCacheable(false).list()
    }

    def List<Course> getCourses() {
        session.createQuery("from Course").setCacheable(false).list()
    }

    Course getCourseByKey(String key) {
        session.createCriteria(Course.class).add(Restrictions.eq("key", key)).setCacheable(false).uniqueResult() as Course
    }

    def List<Course> getMinors() {
        session.createCriteria(Course.class).add(Restrictions.eq("kzfa", Course.KZFA.MINOR)).setCacheable(false).list()
    }

    def List<Course> getMajors() {
        session.createCriteria(Course.class).add(Restrictions.eq("kzfa", Course.KZFA.MAJOR)).setCacheable(false).list()
    }

    def List<Group> getGroups() {
        session.createQuery("from Group").setCacheable(false).list()
    }

    def List<Group> getGroupsByIDs(Collection<Integer> entries) {
        session.createCriteria(Group.class).add(Restrictions.in("id", entries)).setCacheable(false).list();
    }

    def List<Level> getLevels() {
        session.createQuery("from Level").setCacheable(false).list()
    }

    def List<Module> getModules() {
        session.createQuery("from Module").setCacheable(false).list()
    }

    def List<ModuleAbstractUnitSemester> getModuleAbstractUnitSemester() {
        session.createQuery("from ModuleAbstractUnitSemester").setCacheable(false).list()
    }

    def List<de.stups.slottool.data.entities.Session> getSessions() {
        session.createQuery("from Session").setCacheable(false).list()
    }
    def de.stups.slottool.data.entities.Session getSessionByID(int id) {
        session.createCriteria(de.stups.slottool.data.entities.Session.class).add(Restrictions.eq("id", id)).setCacheable(false).uniqueResult() as de.stups.slottool.data.entities.Session
    }

    def List<Unit> getUnits() {
        session.createQuery("from Unit").setCacheable(false).list()
    }

    def getLogEntries() {
        session.createQuery("from Log").setCacheable(true).list()
    }
    def checkSchemaVersion() {
        def properties = new Properties()
        properties.load currentThread().contextClassLoader.getResourceAsStream("schema.properties")
        def version_str =session.createQuery("from Info where key = 'schema_version'").uniqueResult().value
        def schema_version = version_str.split("\\.")
        def required_version = properties.getProperty("schema_version").split("\\.")

        // Major versions must match<commit_msg>Added move session method that moves a session from one slot (day and time) to a different one and creates a log entry for the operation<commit_after>package de.stups.slottool.data

import de.stups.slottool.data.entities.AbstractUnit
import de.stups.slottool.data.entities.AbstractUnitUnitSemester
import de.stups.slottool.data.entities.Course
import de.stups.slottool.data.entities.Group
import de.stups.slottool.data.entities.Info
import de.stups.slottool.data.entities.Level
import de.stups.slottool.data.entities.Log
import de.stups.slottool.data.entities.Module
import de.stups.slottool.data.entities.ModuleAbstractUnitSemester
import de.stups.slottool.data.entities.Unit
import groovy.sql.Sql
import org.hibernate.HibernateException
import org.hibernate.SessionFactory
import org.hibernate.Transaction
import org.hibernate.cfg.Configuration
import org.hibernate.criterion.Restrictions

import java.nio.file.Files
import java.nio.file.Paths

import static java.lang.Thread.currentThread
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING

class Store extends AbstractStore {

    LinkedHashMap units
    String dbpath
    SessionFactory sessionFactory
    org.hibernate.Session session

    def Store(String dbpath) {
        this.dbpath = dbpath
        openDataBase(dbpath)
        checkSchemaVersion()
    }

    def List<Info> getInfo() {
        session.createQuery("from Info").setCacheable(true).list()
    }

    def List<AbstractUnit> getAbstractUnits() {
        session.createQuery("from AbstractUnit").setCacheable(false).list()
    }

    def getAbstractUnitUnitSemesterByUnitID(def unit_id) {
        session.createCriteria(AbstractUnitUnitSemester.class).add(Restrictions.eq("unit.id", unit_id)).list()
    }

    def List<AbstractUnitUnitSemester> getAbstractUnitUnitSemester() {
        session.createQuery("from AbstractUnitUnitSemester").setCacheable(false).list()
    }

    def List<Course> getCourses() {
        session.createQuery("from Course").setCacheable(false).list()
    }

    Course getCourseByKey(String key) {
        session.createCriteria(Course.class).add(Restrictions.eq("key", key)).setCacheable(false).uniqueResult() as Course
    }

    def List<Course> getMinors() {
        session.createCriteria(Course.class).add(Restrictions.eq("kzfa", Course.KZFA.MINOR)).setCacheable(false).list()
    }

    def List<Course> getMajors() {
        session.createCriteria(Course.class).add(Restrictions.eq("kzfa", Course.KZFA.MAJOR)).setCacheable(false).list()
    }

    def List<Group> getGroups() {
        session.createQuery("from Group").setCacheable(false).list()
    }

    def List<Group> getGroupsByIDs(Collection<Integer> entries) {
        session.createCriteria(Group.class).add(Restrictions.in("id", entries)).setCacheable(false).list();
    }

    def List<Level> getLevels() {
        session.createQuery("from Level").setCacheable(false).list()
    }

    def List<Module> getModules() {
        session.createQuery("from Module").setCacheable(false).list()
    }

    def List<ModuleAbstractUnitSemester> getModuleAbstractUnitSemester() {
        session.createQuery("from ModuleAbstractUnitSemester").setCacheable(false).list()
    }

    def List<de.stups.slottool.data.entities.Session> getSessions() {
        session.createQuery("from Session").setCacheable(false).list()
    }
    def de.stups.slottool.data.entities.Session getSessionByID(int id) {
        session.createCriteria(de.stups.slottool.data.entities.Session.class).add(Restrictions.eq("id", id)).setCacheable(false).uniqueResult() as de.stups.slottool.data.entities.Session
    }

    def List<Unit> getUnits() {
        session.createQuery("from Unit").setCacheable(false).list()
    }

    def getLogEntries() {
        session.createQuery("from Log").setCacheable(true).list()
    }

    def moveSession(de.stups.slottool.data.entities.Session session, String target_day, String target_time) {
        String src_day = session.getDay()
        String src_time = session.getTime()
        session.day = target_day
        session.time = target_time.toInteger()

        Log log = new Log();
        // TODO day and time should each be a field in the log table
        log.setSrc(src_day+src_time);
        log.setTarget(target_day+target_time)
        log.setSession(session);

        Transaction tx;
        def s = this.session

        try{
            tx = s.beginTransaction();
            s.persist(session);
            s.persist(log);
            tx.commit();
        }catch (HibernateException e) {
            if (tx!=null) tx.rollback();
            e.printStackTrace();
        }finally {
           s.flush()
        }
    }

    def checkSchemaVersion() {
        def properties = new Properties()
        properties.load currentThread().contextClassLoader.getResourceAsStream("schema.properties")
        def version_str =session.createQuery("from Info where key = 'schema_version'").uniqueResult().value
        def schema_version = version_str.split("\\.")
        def required_version = properties.getProperty("schema_version").split("\\.")

        // Major versions must match
{'fraction_non_alphanumeric': 0.1187179766706639, 'fraction_numerical': 0.0, 'mean_word_length': 3.7288659793814434, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1211256', 'n_tokens_mistral': 2725, 'n_tokens_neox': 2677, 'n_words': 431}