Commits

Endy Muhardin committed a963b4b

implement query not in

  • Participants
  • Parent commits 83ce41f

Comments (0)

Files changed (9)

belajar-restful-domain/src/main/java/com/artivisi/belajar/restful/service/BelajarRestfulService.java

         Menu findMenuById(String id);
         List<Menu> findTopLevelMenu();
         List<Menu> findMenuByParent(Menu m);
+        List<Menu> findMenuNotInRole(Role r);
         
         // permission
         void save(Permission m);
         Permission findPermissionById(String id);
         Page<Permission> findAllPermissions(Pageable pageable);
         Long countAllPermissions();
+        List<Permission> findPermissionsNotInRole(Role r);
         
         // role
         void save(Role role);

belajar-restful-domain/src/main/resources/com/artivisi/belajar/restful/db/data/role.csv

 id,name,description
-superuser,Super User,Full Access
+superuser,Super User,Full Access
+staff,Staff,Entri Data

belajar-restful-domain/src/main/resources/com/artivisi/belajar/restful/db/data/role_menu.csv

 superuser,transaksi
 superuser,transaksi-penjualan
 superuser,laporan
-superuser,rekap-penjualan
+superuser,rekap-penjualan
+staff,master
+staff,master-produk
+staff,master-customer

belajar-restful-domain/src/main/resources/com/artivisi/belajar/restful/db/data/role_permission.csv

 superuser,menu-view
 superuser,menu-edit
 superuser,permission-view
-superuser,permission-edit
+superuser,permission-edit
+staff,user-view,
+staff,role-view,
+staff,menu-view,
+staff,permission-view

belajar-restful-service/src/main/java/com/artivisi/belajar/restful/dao/MenuDao.java

 			"where m.parent.id = :id " +
 			"order by m.level, m.order")
     public List<Menu> findMenuByParent(@Param("id") String id);
+
+    public List<Menu> findByIdNotIn(List<String> ids);
     
 }

belajar-restful-service/src/main/java/com/artivisi/belajar/restful/dao/PermissionDao.java

 package com.artivisi.belajar.restful.dao;
 
 import com.artivisi.belajar.restful.domain.Permission;
+import java.util.List;
 import org.springframework.data.repository.PagingAndSortingRepository;
 
 public interface PermissionDao extends PagingAndSortingRepository<Permission, String> {
+    public List<Permission> findByIdNotIn(List<String> ids);
 }

belajar-restful-service/src/main/java/com/artivisi/belajar/restful/service/impl/BelajarRestfulServiceImpl.java

         }
         return menuDao.findMenuByParent(m.getId());
     }
+    
+    @Override
+    public List<Menu> findMenuNotInRole(Role role){
+        if(role == null){
+            return new ArrayList<Menu>();
+        }
+        
+        Role r = findRoleById(role.getId());
+        if(r == null || r.getMenuSet().isEmpty()){
+            return new ArrayList<Menu>();
+        }
+        
+        List<String> ids = new ArrayList<String>();
+        for (Menu m : r.getMenuSet()) {
+            ids.add(m.getId());
+        }
+        
+        return menuDao.findByIdNotIn(ids);
+    }
 
     @Override
     public void save(Permission m) {
     public Long countAllPermissions() {
         return permissionDao.count();
     }
+    
+    @Override
+    public List<Permission> findPermissionsNotInRole(Role role) {
+        if(role == null){
+            return new ArrayList<Permission>();
+        }
+        
+        Role r = findRoleById(role.getId());
+        if(r == null || r.getPermissionSet().isEmpty()){
+            return new ArrayList<Permission>();
+        }
+        
+        List<String> ids = new ArrayList<String>();
+        for (Permission p : r.getPermissionSet()) {
+            ids.add(p.getId());
+        }
+        
+        return permissionDao.findByIdNotIn(ids);
+    }
 
     @Override
     public void save(Role role) {

belajar-restful-service/src/test/java/com/artivisi/belajar/restful/service/impl/MenuServiceTestIT.java

 package com.artivisi.belajar.restful.service.impl;
 
 import com.artivisi.belajar.restful.domain.Menu;
+import com.artivisi.belajar.restful.domain.Role;
 import com.artivisi.belajar.restful.service.BelajarRestfulService;
 import java.util.List;
+import org.junit.Assert;
 import static org.junit.Assert.*;
 import org.junit.Test;
 import org.junit.runner.RunWith;
         assertEquals(Integer.valueOf(7), permission.getOrder());
         assertNotNull(permission.getParent());
     }
+    
+    @Test
+    public void testFindNotInRole() {
+        Role r = new Role();
+        r.setId("staff");
+        
+        List<Menu> hasil = belajarRestfulService.findMenuNotInRole(r);
+        assertEquals(new Integer(13), new Integer(hasil.size()));
+        
+        for (Menu menu : hasil) {
+            if(menu.getId().equals("master")){
+                Assert.fail("Seharusnya tidak ada menu master");
+            }
+        }
+    }
 }

belajar-restful-service/src/test/java/com/artivisi/belajar/restful/service/impl/PermissionServiceTestIT.java

 package com.artivisi.belajar.restful.service.impl;
 
 import com.artivisi.belajar.restful.domain.Permission;
+import com.artivisi.belajar.restful.domain.Role;
 import com.artivisi.belajar.restful.service.BelajarRestfulService;
+import java.util.List;
+import org.junit.Assert;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
         Page<Permission> result = service.findAllPermissions(new PageRequest(0, service.countAllPermissions().intValue()));
         assertTrue(result.getTotalElements() > 0);
     }
+    
+    @Test
+    public void testFindNotInRole() {
+        Role r = new Role();
+        r.setId("staff");
+        
+        List<Permission> hasil = service.findPermissionsNotInRole(r);
+        assertEquals(new Integer(5), new Integer(hasil.size()));
+        
+        for (Permission permission : hasil) {
+            if(permission.getId().equals("role-view")){
+                Assert.fail("Seharusnya tidak ada permission untuk view");
+            }
+        }
+    }
 }