package com.blstream.myhoard.db.dao;

import java.util.List;
import org.hibernate.Session;
import com.blstream.myhoard.db.model.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.SessionFactory;

public class CollectionDAO implements ResourceDAO<CollectionDS> {

    private SessionFactory sessionFactory;

    @Override
    public List<CollectionDS> getList() {
        Session session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        // TODO zmienić na ItemDS
//        List<CollectionDS> result = session.createQuery("select new CollectionDS(c.id, c.owner, c.name, c.description, count(item.id), c.createdDate, c.modifiedDate) from ItemDS as item right join item.collection as c group by c.id").list();
        List<CollectionDS> result = session.createQuery("from CollectionDS").list();
        session.getTransaction().commit();
        return result;
    }

    @Override
    public CollectionDS get(int id) throws IndexOutOfBoundsException {
        Session session = sessionFactory.getCurrentSession();
        session.beginTransaction();
//        CollectionDS result = (CollectionDS)session.createQuery("select new CollectionDS(c.id, c.owner, c.name, c.description, count(item.id), c.createdDate, c.modifiedDate) from ItemDS as item right join item.collection as c where c.id = " + id + " group by c.id").uniqueResult();
        CollectionDS result = (CollectionDS)session.createQuery("from CollectionDS where id = " + id).uniqueResult();
        session.getTransaction().commit();
        return result;
    }

    @Override
    public void create(CollectionDS obj) {
        Session session = sessionFactory.getCurrentSession();
        List<String> tags = new ArrayList<>();
        for (TagDS i : obj.getTags())
            tags.add(i.getTag());
        session.beginTransaction();
        Set<TagDS> result = new HashSet<>((List<TagDS>)session.createQuery("from TagDS where tag in (:tags)").setParameterList("tags", tags).list());
        obj.getTags().removeAll(result);
        result.addAll(obj.getTags());
        obj.setTags(result);
        session.save(obj);
        session.getTransaction().commit();
    }

    @Override
    public void update(CollectionDS obj) {
        CollectionDS object = get(obj.getId());
        object.updateObject(obj);
        List<String> tags = new ArrayList<>();
        for (TagDS i : obj.getTags())
            tags.add(i.getTag());
        
        Session session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        Set<TagDS> result = new HashSet<>((List<TagDS>)session.createQuery("from TagDS where tag in (:tags)").setParameterList("tags", tags).list());
        object.getTags().removeAll(result);
        result.addAll(object.getTags());
        object.setTags(result);
        object.setModifiedDate(Calendar.getInstance().getTime());
        session.update(object);
        session.getTransaction().commit();

        // TODO mapper
        obj.updateObject(object);
        obj.setOwner(object.getOwner());
        obj.setCreatedDate(object.getCreatedDate());
        obj.setModifiedDate(object.getModifiedDate());
    }

    @Override
    public void remove(int id) {
        Session session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        session.createQuery("delete CollectionDS where id = " + id).executeUpdate();
        session.getTransaction().commit();
    }

    @Override
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

}

@@ -376,30 +376,16 @@
 ctory;%0A%0A
-    @Override%0A
     publ
@@ -414,24 +414,24 @@
 getList() %7B%0A
+
         Sess
@@ -520,42 +520,8 @@
 ();%0A
-        // TODO zmieni%C4%87 na ItemDS%0A
 //  
@@ -837,32 +837,293 @@
 ionDS%22).list();%0A
+        List%3CLong%3E count = session.createQuery(%22select count(item.id) from ItemDS as item right join item.collection as c group by c.id%22).list();%0A        for (int i = 0; i %3C result.size(); i++)%0A            result.get(i).setItemsNumber(count.get(i).intValue());%0A
         session.
@@ -1638,32 +1638,32 @@
 uniqueResult();%0A
-
         Collecti
@@ -1756,32 +1756,233 @@
 uniqueResult();%0A
+        result.setItemsNumber(((Long)session.createQuery(%22select count(item.id) from ItemDS as item right join item.collection as c where c.id = %22 + id + %22 group by c.id%22).uniqueResult()).intValue());%0A
         session.

Update CollectionDAO.java