package org.zstack.core.jsonlabel;

import org.springframework.beans.factory.annotation.Autowired;
import org.zstack.core.db.DatabaseFacade;
import org.zstack.core.db.HardDeleteEntityExtensionPoint;
import org.zstack.core.db.SimpleQuery.Op;
import org.zstack.core.db.SoftDeleteEntityExtensionPoint;
import org.zstack.core.db.UpdateQuery;

import java.util.Collection;
import java.util.List;

/**
 * Created by xing5 on 2016/9/14.
 */
public class JsonLabelResourceDeletionExtension implements SoftDeleteEntityExtensionPoint,
        HardDeleteEntityExtensionPoint {

    @Autowired
    private DatabaseFacade dbf;

    @Override
    public List<Class> getEntityClassForHardDeleteEntityExtension() {
        // hook all
        return null;
    }

    private void delete(Collection ids) {
        UpdateQuery q = UpdateQuery.New();
        q.entity(JsonLabelVO.class);
        q.condAnd(JsonLabelVO_.resourceUuid, Op.IN, ids);
        q.delete();
    }

    @Override
    public void postHardDelete(Collection entityIds, Class entityClass) {
        if (entityClass.isAssignableFrom(JsonLabelVO.class)) {
            return;
        }

        delete(entityIds);
    }

    @Override
    public List<Class> getEntityClassForSoftDeleteEntityExtension() {
        // hook all
        return null;
    }

    @Override
    public void postSoftDelete(Collection entityIds, Class entityClass) {
        if (entityClass.isAssignableFrom(JsonLabelVO.class)) {
            return;
        }

        delete(entityIds);
    }
}

@@ -338,16 +338,60 @@
 eQuery;%0D
+%0Aimport org.zstack.core.thread.AsyncThread;%0D
 %0A%0D%0Aimpor
@@ -807,24 +807,42 @@
 ;%0D%0A    %7D%0D%0A%0D%0A
+    @AsyncThread%0D%0A
     private 
@@ -868,24 +868,173 @@
 ion ids) %7B%0D%0A
+        // the resourceUuid must be in type of String%0D%0A        if (!(ids.iterator().next() instanceof  String)) %7B%0D%0A            return;%0D%0A        %7D%0D%0A%0D%0A
         Upda
@@ -1372,32 +1372,73 @@
 ;%0D%0A        %7D%0D%0A%0D%0A
+        if (!entityIds.isEmpty()) %7B%0D%0A    
         delete(e
@@ -1445,24 +1445,35 @@
 ntityIds);%0D%0A
+        %7D%0D%0A
     %7D%0D%0A%0D%0A   
@@ -1787,32 +1787,73 @@
 ;%0D%0A        %7D%0D%0A%0D%0A
+        if (!entityIds.isEmpty()) %7B%0D%0A    
         delete(e
@@ -1860,22 +1860,33 @@
 ntityIds);%0D%0A
+        %7D%0D%0A
     %7D%0D%0A%7D%0D%0A

fix unit test