package com.blstream.myhoard.biz.service;

import com.blstream.myhoard.biz.exception.MyHoardException;
import java.util.ArrayList;
import java.util.List;
import com.blstream.myhoard.biz.model.*;
import com.blstream.myhoard.db.dao.*;
import com.blstream.myhoard.db.model.CollectionDS;
import java.util.Map;
import org.springframework.stereotype.Service;

@Service
public class CollectionService implements ResourceService<CollectionDTO> {

    private ResourceDAO<CollectionDS> collectionDAO;

    public void setCollectionDAO(ResourceDAO<CollectionDS> collectionDAO) {
        this.collectionDAO = collectionDAO;
    }

    @Override
    public List<CollectionDTO> getList() {
        try {
            List<CollectionDTO> result = new ArrayList<>();
            for (CollectionDS i : collectionDAO.getList())
                result.add(i.toDTO());
            return result;
        } catch (RuntimeException ex) {
            throw new MyHoardException(300, "Nieznany błąd: " + ex.toString() + " > " + ex.getCause().toString());
        }
    }

    @Override
    public List<CollectionDTO> getList(Map<String, String> params) {
        if (!"asc".equals(params.get("sort_dir")) && !"desc".equals(params.get("sort_dir")))
            throw new MyHoardException(400, "Nieprawidłowy porządek : " + params.get("sort_dir"));
        if (Integer.parseInt(params.get("max_count")) <= 0)
            throw new MyHoardException(400, "Nieprawidłowa warotść max_count: " + params.get("max_count"));
        List<CollectionDTO> list = new ArrayList<>();
        for (CollectionDS i : collectionDAO.getList(params))
            list.add(i.toDTO());
        return list;
    }

    @Override
    public CollectionDTO get(int id) {
        try {
            return collectionDAO.get(id).toDTO();
        } catch (RuntimeException ex) {
            throw new MyHoardException(300, "Element o id(" + id + ") prawdopodobnie nie istnieje.");
        }
    }

    @Override
    public void create(CollectionDTO obj) {
        try {
            CollectionDS collection = new CollectionDS();
            collection.toDTO(obj);
            collectionDAO.create(collection);
            obj.setId(Integer.toString(collection.getId()));
            obj.setOwner(collection.getOwner());
        } catch (RuntimeException ex) {
            throw new MyHoardException(400, "Nieznany błąd: " + ex.toString() + " > " + ex.getCause().toString());
        }
    }

    @Override
    public void update(CollectionDTO obj) {
        try {
            CollectionDS object = new CollectionDS();
            object.fromDTO(obj);
            collectionDAO.update(object);
            obj.fromDS(object);
        } catch (RuntimeException ex) {
            throw new MyHoardException(111, "Nieznany błąd: " + ex.toString() + " > " + ex.getCause().toString());
        }
    }

    @Override
    public void remove(int id) {
        try {
            collectionDAO.remove(id);
        } catch (RuntimeException ex) {
            throw new MyHoardException(400, "Element o id(" + id + ") prawdopodobnie nie istnieje.");
        }
    }

    @Override
    public CollectionDTO getByAccess_token(String access_token) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public CollectionDTO getByRefresh_token(String refresh_token) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

@@ -299,16 +299,47 @@
 il.Map;%0A
+import org.hibernate.Criteria;%0A
 import o
@@ -1137,22 +1137,22 @@
 String, 
-String
+Object
 %3E params
@@ -1167,16 +1167,50 @@
     if (
+params.containsKey(%22sort_dir%22) && 
 !%22asc%22.e
@@ -1389,20 +1389,73 @@
         
-if (
+try %7B%0A            if (params.containsKey(%22max_count%22) && 
 Integer.
@@ -1463,16 +1463,24 @@
 arseInt(
+(String)
 params.g
@@ -1502,16 +1502,20 @@
 ) %3C= 0)%0A
+    
         
@@ -1610,24 +1610,567 @@
 x_count%22));%0A
+        %7D catch (NumberFormatException ex) %7B%0A            throw new MyHoardException(400, params.get(%22max_count%22) + %22 nie jest poprawn%C4%85 liczb%C4%85%22);%0A        %7D%0A        try %7B%0A            if (params.containsKey(%22start_num%22) && Integer.parseInt((String)params.get(%22start_num%22)) %3C= 0)%0A                throw new MyHoardException(400, %22Nieprawid%C5%82owa warot%C5%9B%C4%87 max_count: %22 + params.get(%22start_num%22));%0A        %7D catch (NumberFormatException ex) %7B%0A            throw new MyHoardException(400, params.get(%22start_num%22) + %22 nie jest poprawn%C4%85 liczb%C4%85%22);%0A        %7D%0A
         List
@@ -3990,232 +3990,7 @@
    %7D
-%0A%0A    @Override%0A    public CollectionDTO getByRefresh_token(String refresh_token) %7B%0A        throw new UnsupportedOperationException(%22Not supported yet.%22); //To change body of generated methods, choose Tools %7C Templates.%0A    %7D
 %0A%7D%0A

Update CollectionService.java