JpaUtil使用示例

来自ling
跳转至: 导航搜索

Lingq

  • 		public void beforeDelete(SaveContext context) {
    			Permission permission = context.getEntity();
    			Linq linq = JpaUtil.linq(Permission.class);
    			linq
    				.collect("resourceId")
    				.equal("resourceType", Component.RESOURCE_TYPE)
    				.exists(Component.class)
    					.equalProperty("id", "resourceId")
    					.equal("urlId", permission.getResourceId())
    				.list();
    			Set<String> ids = linq.getLinqContext().getSet("resourceId");
    			if (ids != null) {
    				JpaUtil
    					.lind(Permission.class)
    					.in("resourceId", ids)
    					.delete();
    				JpaUtil
    					.lind(Component.class)
    					.in("id", ids)
    					.delete();
    			}
    			
    		}
  • 									JpaUtil.lind(RoleGrantedAuthority.class)
    										.equal("actorId", a.getActorId())
    										.equal("roleId", a.getRoleId())
    										.delete();
  • 	public List<Permission> getPermissions(String username) {
    		List<Permission> permissions = JpaUtil.linq(Permission.class)
    				.toEntity()
    				.collect(Role.class, "roleId")
    				.collect(Component.class, "resourceId")
    				.equal("resourceType", Component.RESOURCE_TYPE)
    				.exists(RoleGrantedAuthority.class)
    					.equalProperty("roleId", "roleId")
    					.equal("actorId", username)
    				.end()
    				.list();
    		if (!permissions.isEmpty()) {
    			Set<Component> components = JpaUtil.collect(permissions, "component");
    			if (!components.isEmpty()) {
    				List<Url> urls = JpaUtil.linq(Url.class).in("id", JpaUtil.collect(components, "urlId")).list();
    				Map<String, Component> componentMap = JpaUtil.index(components);
    				Map<String, Url> urlMap = JpaUtil.index(urls);
    				for (Permission p : permissions) {
    					Url url = urlMap.get(componentMap.get(p.getResourceId()).getUrlId());
    					EntityUtils.setValue(p, "url", url);
    				}
    
    			}
    		}
    		return permissions;
    	}
  • 	@Override
    	public DictionaryItem getDefaultValueItemBy(String code) {
    		
    		return JpaUtil
    				.linq(DictionaryItem.class)
    				.isTrue("enabled")
    				.exists(Dictionary.class)
    					.equal("code", code)
    					.equalProperty("defaultValue", "key")
    					.equalProperty("id", "dictionaryId")
    				.end()
    				.findOne();
    	}
  • 	public List<DictionaryItem> getDictionaryItemsBy(String code) {
    		List<DictionaryItem> list = JpaUtil
    				.linq(DictionaryItem.class)
    				.isTrue("enabled")
    				.exists(Dictionary.class)
    					.equal("code", code)
    					.equalProperty("id", "dictionaryId")
    				.end()
    				.asc("order")
    				.list();
    		Map<String, List<DictionaryItem>> map = JpaUtil.classify(list, "parentId");
    		List<DictionaryItem> top = map.get(null);
    		if (top != null) {
    			for (DictionaryItem item : top) {
    				item.setChildren(map.get(item.getId()));
    			}
    		}
    		
    		return top;
    	}
  • 	public DictionaryItem getDictionaryItem(String key) {
    		return JpaUtil
    				.linq(DictionaryItem.class)
    				.isTrue("enabled")
    				.equal("key", key)
    				.findOne();
    	}