package com.github.zhangkaitao.shiro.chapter21.web.taglib;
import com.github.zhangkaitao.shiro.chapter21.entity.Organization;
import com.github.zhangkaitao.shiro.chapter21.entity.Resource;
import com.github.zhangkaitao.shiro.chapter21.entity.Role;
import com.github.zhangkaitao.shiro.chapter21.entity.User;
import com.github.zhangkaitao.shiro.chapter21.service.OrganizationService;
import com.github.zhangkaitao.shiro.chapter21.service.ResourceService;
import com.github.zhangkaitao.shiro.chapter21.service.RoleService;
import com.github.zhangkaitao.shiro.chapter21.service.UserService;
import com.github.zhangkaitao.shiro.spring.SpringUtils;
import org.springframework.util.CollectionUtils;
import java.util.Collection;
/**
* <p>User: Zhang Kaitao
* <p>Date: 14-2-15
* <p>Version: 1.0
*/
public class Functions {
public static boolean in(Iterable iterable, Object element) {
if(iterable == null) {
return false;
}
return CollectionUtils.contains(iterable.iterator(), element);
}
public static String organizationName(Long organizationId) {
Organization organization = getOrganizationService().findOne(organizationId);
if(organization == null) {
return "";
}
return organization.getName();
}
public static String organizationNames(Collection<Long> organizationIds) {
if(CollectionUtils.isEmpty(organizationIds)) {
return "";
}
StringBuilder s = new StringBuilder();
for(Long organizationId : organizationIds) {
Organization organization = getOrganizationService().findOne(organizationId);
if(organization == null) {
return "";
}
s.append(organization.getName());
s.append(",");
}
if(s.length() > 0) {
s.deleteCharAt(s.length() - 1);
}
return s.toString();
}
public static String roleName(Long roleId) {
Role role = getRoleService().findOne(roleId);
if(role == null) {
return "";
}
return role.getDescription();
}
public static String roleNames(Collection<Long> roleIds) {
if(CollectionUtils.isEmpty(roleIds)) {
return "";
}
StringBuilder s = new StringBuilder();
for(Long roleId : roleIds) {
Role role = getRoleService().findOne(roleId);
if(role == null) {
return "";
}
s.append(role.getDescription());
s.append(",");
}
if(s.length() > 0) {
s.deleteCharAt(s.length() - 1);
}
return s.toString();
}
public static String resourceName(Long resourceId) {
Resource resource = getResourceService().findOne(resourceId);
if(resource == null) {
return "";
}
return resource.getName();
}
public static String resourceNames(Collection<Long> resourceIds) {
if(CollectionUtils.isEmpty(resourceIds)) {
return "";
}
StringBuilder s = new StringBuilder();
for(Long resourceId : resourceIds) {
Resource resource = getResourceService().findOne(resourceId);
if(resource == null) {
return "";
}
s.append(resource.getName());
s.append(",");
}
if(s.length() > 0) {
s.deleteCharAt(s.length() - 1);
}
return s.toString();
}
public static String username(Long userId) {
User user = getUserService().findOne(userId);
if(user == null) {
return "";
}
return user.getUsername();
}
private static OrganizationService organizationService;
private static RoleService roleService;
private static ResourceService resourceService;
private static UserService userService;
public static OrganizationService getOrganizationService() {
if(organizationService == null) {
organizationService = SpringUtils.getBean(OrganizationService.class);
}
return organizationService;
}
public static RoleService getRoleService() {
if(roleService == null) {
roleService = SpringUtils.getBean(RoleService.class);
}
return roleService;
}
public static ResourceService getResourceService() {
if(resourceService == null) {
resourceService = SpringUtils.getBean(ResourceService.class);
}
return resourceService;
}
public static UserService getUserService() {
if(userService == null) {
userService = SpringUtils.getBean(UserService.class);
}
return userService;
}
}