package net.hawkengine.services;

import net.hawkengine.core.ServerConfiguration;
import net.hawkengine.db.IDbRepository;
import net.hawkengine.model.DbEntry;
import net.hawkengine.model.ServiceResult;
import net.hawkengine.model.enums.DatabaseType;
import net.hawkengine.services.interfaces.ICrudService;

import java.util.List;

public abstract class CrudService<T extends DbEntry> extends Service<T> implements ICrudService<T> {
    static final DatabaseType DATABASE_TYPE = ServerConfiguration.getConfiguration().getDatabaseType();

    private IDbRepository<T> repository;

    public IDbRepository<T> getRepository() {
        return this.repository;
    }

    public void setRepository(IDbRepository<T> repository) {
        this.repository = repository;
    }

    @Override
    public ServiceResult getById(String id) {
        T dbObject = this.getRepository().getById(id);

        ServiceResult result = new ServiceResult();
        if (dbObject != null) {
            result = super.createServiceResult(dbObject, false, "retrieved successfully");
        } else {
            result = super.createServiceResult(dbObject, true, "not found");
        }

        return result;
    }

    @Override
    public ServiceResult getAll() {
        List<T> dbObjects = this.getRepository().getAll();

        ServiceResult result = super.createServiceResultArray(dbObjects, false, "retrieved successfully");

        return result;
    }

    @Override
    public ServiceResult add(T object) {
        T dbObject = this.getRepository().add(object);

        ServiceResult result = new ServiceResult();
        if (dbObject != null) {
            result = super.createServiceResult(dbObject, false, "created successfully");
        } else {
            result = super.createServiceResult(dbObject, true, "already exists");
        }

        return result;
    }

    @Override
    public ServiceResult update(T object) {
        if (object == null){
            return super.createServiceResult(object, true, "not found");
        }
        T dbObject = this.getRepository().update(object);

        ServiceResult result = new ServiceResult();
        if (dbObject != null) {
            result = super.createServiceResult(dbObject, false, "updated successfully");
        } else {
            result = super.createServiceResult(dbObject, true, "not found");
        }

        return result;
    }

    @Override
    public ServiceResult delete(String id) {
        T dbObject = this.getRepository().delete(id);

        ServiceResult result = new ServiceResult();
        if (dbObject == null) {
            result = super.createServiceResult((T) result.getObject(), true, "not found" );
        } else {
            result = super.createServiceResult((T) result.getObject(), false, "deleted successfully");
        }

        result.setObject(dbObject);

        return result;
    }
}

@@ -243,16 +243,68 @@
 seType;%0A
+import net.hawkengine.model.enums.NotificationType;%0A
 import n
@@ -959,38 +959,16 @@
 t result
- = new ServiceResult()
 ;%0A      
@@ -1050,21 +1050,40 @@
 Object, 
-false
+NotificationType.SUCCESS
 , %22retri
@@ -1169,36 +1169,54 @@
 esult(dbObject, 
-true
+NotificationType.ERROR
 , %22not found%22);%0A
@@ -1439,21 +1439,40 @@
 bjects, 
-false
+NotificationType.SUCCESS
 , %22retri
@@ -1662,38 +1662,16 @@
 t result
- = new ServiceResult()
 ;%0A      
@@ -1753,21 +1753,40 @@
 Object, 
-false
+NotificationType.SUCCESS
 , %22creat
@@ -1878,20 +1878,38 @@
 Object, 
-true
+NotificationType.ERROR
 , %22alrea
@@ -2047,16 +2047,17 @@
 == null)
+ 
 %7B%0A      
@@ -2099,28 +2099,46 @@
 ult(object, 
-true
+NotificationType.ERROR
 , %22not found
@@ -2238,38 +2238,16 @@
 t result
- = new ServiceResult()
 ;%0A      
@@ -2329,21 +2329,40 @@
 Object, 
-false
+NotificationType.SUCCESS
 , %22updat
@@ -2454,20 +2454,38 @@
 Object, 
-true
+NotificationType.ERROR
 , %22not f
@@ -2806,12 +2806,30 @@
 (), 
-true
+NotificationType.ERROR
 , %22n
@@ -2837,17 +2837,16 @@
 t found%22
- 
 );%0A     
@@ -2932,13 +2932,32 @@
 (), 
-false
+NotificationType.SUCCESS
 , %22d

refactor error property with enum in CrudService #213