org.malagu.linq.JpaUtil Java Examples

The following examples show how to use org.malagu.linq.JpaUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DictionaryServiceImpl.java    From bdf3 with Apache License 2.0 6 votes vote down vote up
@Override
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;
}
 
Example #2
Source File: DataSourceInfoServiceImpl.java    From multitenant with Apache License 2.0 6 votes vote down vote up
@Override
public DataSourceInfo allocate(Organization organization) {
	if (StringUtils.isEmpty(organization.getDataSourceInfoId())) {
		List<DataSourceInfo> list = JpaUtil.linq(DataSourceInfo.class)
				.isTrue("enabled")
				.isTrue("shared")
				.addIfNot(organization.getDataSourceInfoId())
					.notExists(DataSourceInfo.class)
						.isTrue("enabled")
						.isTrue("shared")
						.lt("depletionIndex", "depletionIndex")
					.end()
				.endIf()
				.list(0, 1);
		Assert.notEmpty(list,"DataSourceInfo must contain elements");
		return list.get(0);
	} else {
		return JpaUtil.linq(DataSourceInfo.class)
				.isTrue("enabled")
				.isTrue("shared")
				.idEqual(organization.getDataSourceInfoId())
				.findOne();
	}
	
}
 
Example #3
Source File: MultitenantUserDetailsService.java    From bdf3 with Apache License 2.0 6 votes vote down vote up
@Override
public UserDetails loadUserByUsername(String username)
		throws UsernameNotFoundException {			
	try {
		Organization organization = loadOrganization();
		return MultitenantUtils.doQuery(organization.getId(), () ->  {
			User user = JpaUtil.getOne(User.class, username);
			user.setOrganization(organization);
			user.setAuthorities(grantedAuthorityService.getGrantedAuthorities(user));
			return user;
		});
	} catch (Exception e) {
		throw new UsernameNotFoundException(e.getMessage());
	}

}
 
Example #4
Source File: UrlServiceImpl.java    From bdf3 with Apache License 2.0 6 votes vote down vote up
@Override
public List<Url> findAll() {
	List<Url> urls = JpaUtil.linq(Url.class).list();
	List<Permission> permissions = JpaUtil.linq(Permission.class).equal("resourceType", Url.RESOURCE_TYPE).list();
	if (!permissions.isEmpty()) {
		Map<String, Url> urlMap = JpaUtil.index(urls);
		for (Permission permission : permissions) {
			Url url = urlMap.get(permission.getResourceId());
			List<ConfigAttribute> configAttributes = url.getAttributes();
			if (configAttributes == null) {
				configAttributes = new ArrayList<ConfigAttribute>();
				url.setAttributes(configAttributes);
			}
			configAttributes.add(permission);
		}
	}
	return urls;
}
 
Example #5
Source File: ComponentServiceImpl.java    From bdf3 with Apache License 2.0 6 votes vote down vote up
@Override
public List<Component> findAll() {
	List<Component> components = JpaUtil.linq(Component.class).list();
	List<Permission> permissions = JpaUtil.linq(Permission.class).equal("resourceType", Component.RESOURCE_TYPE).list();
	if (!permissions.isEmpty()) {
		Map<String, Component> componentMap = JpaUtil.index(components);
		for (Permission permission : permissions) {
			Component component = componentMap.get(permission.getResourceId());
			List<ConfigAttribute> configAttributes = component.getAttributes();
			if (configAttributes == null) {
				configAttributes = new ArrayList<ConfigAttribute>();
				component.setAttributes(configAttributes);
			}
			configAttributes.add(permission);
		}
	}
	return components;

}
 
Example #6
Source File: UserDetailsServiceImpl.java    From bdf3 with Apache License 2.0 5 votes vote down vote up
@Override
public UserDetails loadUserByUsername(String username)
		throws UsernameNotFoundException {
	try {
		
		User user = JpaUtil.getOne(User.class, username);
		user.setAuthorities(grantedAuthorityService.getGrantedAuthorities(user));
		return user;
	} catch (Exception e) {
		throw new UsernameNotFoundException("Not Found");
	}
	
	
}
 
Example #7
Source File: DictionaryServiceImpl.java    From bdf3 with Apache License 2.0 5 votes vote down vote up
@Override
public DictionaryItem getDictionaryItem(String key) {
	return JpaUtil
			.linq(DictionaryItem.class)
			.isTrue("enabled")
			.equal("key", key)
			.findOne();
}
 
Example #8
Source File: DictionaryServiceImpl.java    From bdf3 with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #9
Source File: UserServiceImpl.java    From bdf3 with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isAdministrator(String username) {
	Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
	if (principal instanceof User) {
		if (((User) principal).getUsername().equals(username)) {
			return ((User) principal).isAdministrator();
		} 
		if (username == null) {
			return ((User) principal).isAdministrator();
		}
	}
	User user = JpaUtil.linq(User.class).idEqual(username).findOne();
	return user.isAdministrator();
}
 
Example #10
Source File: LinqImpl.java    From linq with Apache License 2.0 5 votes vote down vote up
@Override
public <T> Page<T> paging(Pageable pageable) {
	if (parent != null) {
		applyPredicateToCriteria(sq);
		return parent.paging(pageable);
	}
	List<T> list;
	if (pageable == null) {
		list = list();
		return new PageImpl<T>(list);
	} else {
		Sort sort = pageable.getSort();
		if (sort != null) {
			orders.addAll(QueryUtils.toOrders(sort, root, cb));
		}
		applyPredicateToCriteria(criteria);
		TypedQuery<?> query = em.createQuery(criteria);
		Long offset = pageable.getOffset();
		query.setFirstResult(offset.intValue());
		query.setMaxResults(pageable.getPageSize());

		Long total = JpaUtil.count(criteria);
		List<T> content = Collections.<T> emptyList();
		if (total > pageable.getOffset()) {
			content = transform(query, false);
		}

		return new PageImpl<T>(content, pageable, total);
	}
}
 
Example #11
Source File: LinImpl.java    From linq with Apache License 2.0 5 votes vote down vote up
@Override
public T in(Class<?> domainClass) {
	if (!beforeMethodInvoke()) {
		return (T) this;
	}
	Expression<?> e = root.get(JpaUtil.getIdName(this.domainClass));
	T lin = createChild(domainClass);
	add(cb.in(e).value(lin.getSubquery()));
	return lin;
}
 
Example #12
Source File: UserController.java    From bdf3 with Apache License 2.0 5 votes vote down vote up
@RequestMapping(path = "/user/load", method = RequestMethod.GET)
public List<User> load(Pageable pageable, @RequestParam(name = "searchKey", required = false) String searchKey) {
	return JpaUtil
		.linq(User.class)
		.addIf(searchKey)
			.or()
				.like("username", "%" + searchKey + "%")
				.like("nickname", "%" + searchKey + "%")
			.end()
		.endIf()
		.list(pageable);
}
 
Example #13
Source File: LinImpl.java    From linq with Apache License 2.0 5 votes vote down vote up
public LinImpl(Class<?> domainClass, EntityManager entityManager) {
	this.domainClass = domainClass;
	em = entityManager;
	if (entityManager == null) {
		em = JpaUtil.getEntityManager(domainClass);
	}
	cb = em.getCriteriaBuilder();
	junction = new And();
	junctions.add(junction);
}
 
Example #14
Source File: OrganizationServiceImpl.java    From multitenant with Apache License 2.0 5 votes vote down vote up
@Override
@Transactional
public void register(Organization organization) {
	for (ResourceAllocator allocator : allocators) {
		allocator.allocate(organization);
	}
	JpaUtil.persist(organization);
}
 
Example #15
Source File: DataSourceInfoServiceImpl.java    From multitenant with Apache License 2.0 5 votes vote down vote up
@Override
public DataSourceInfo get(Organization organization) {
	return JpaUtil.linq(DataSourceInfo.class)
		.addIf(organization.getDataSourceInfoId())
			.idEqual(organization.getDataSourceInfoId())
		.endIf()
		.addIfNot(organization.getDataSourceInfoId())
			.exists(Organization.class)
				.equalProperty("dataSourceInfoId", "id")
				.idEqual(organization.getId())
			.end()
		.endIf()
		.findOne();
}
 
Example #16
Source File: MasterDataSourceInitiator.java    From multitenant with Apache License 2.0 5 votes vote down vote up
@Override
@Transactional
public void afterPropertiesSet(ApplicationContext applicationContext) {
	boolean isExist = true;
	String master = databaseNameService.getDatabaseName(Constants.MASTER);
	DataSourceInfo dataSourceInfo = JpaUtil.getOne(DataSourceInfo.class, master);
	if (dataSourceInfo == null) {
		dataSourceInfo = new DataSourceInfo();
		isExist = false;
	}
	dataSourceInfo.setId(master);
	dataSourceInfo.setDriverClassName(properties.determineDriverClassName());
	dataSourceInfo.setEnabled(true);
	dataSourceInfo.setJndiName(properties.getJndiName());
	dataSourceInfo.setName("主公司数据源");
	dataSourceInfo.setUrl(properties.determineUrl());
	dataSourceInfo.setUsername(properties.determineUsername());
	dataSourceInfo.setPassword(properties.determinePassword());
	dataSourceInfo.setShared(true);
	dataSourceInfo.setDepletionIndex(1);
	dataSourceInfo.setType(properties.getType() != null ? properties.getType().getName() : null);
	if (isExist) {
		JpaUtil.merge(dataSourceInfo);
	} else {
		JpaUtil.persist(dataSourceInfo);
	}
	
}
 
Example #17
Source File: RegisterServiceImpl.java    From bdf3 with Apache License 2.0 4 votes vote down vote up
@Override
public Organization getOrganization(String organizationId) {
	return JpaUtil.getOne(Organization.class, organizationId);
}
 
Example #18
Source File: UserController.java    From bdf3 with Apache License 2.0 4 votes vote down vote up
@RequestMapping(path = "/user/remove/{id}", method = RequestMethod.DELETE)
@Transactional
public void remove(@PathVariable String id) {
	User user = JpaUtil.getOne(User.class, id);
	JpaUtil.remove(user);
}
 
Example #19
Source File: UserController.java    From bdf3 with Apache License 2.0 4 votes vote down vote up
@RequestMapping(path = "/user/add", method = RequestMethod.POST)
@Transactional
public void add(@RequestBody User user) throws Exception {
	JpaUtil.persist(user);
}
 
Example #20
Source File: UserController.java    From bdf3 with Apache License 2.0 4 votes vote down vote up
@RequestMapping(path = "/user/modify", method = RequestMethod.PUT)
@Transactional
public void modify(@RequestBody User user) throws Exception {
	JpaUtil.merge(user);
}
 
Example #21
Source File: UserGrantedAuthorityProvider.java    From bdf3 with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<? extends GrantedAuthority> provide(
		UserDetails userDetails) {
	List<GrantedAuthority> authorities = JpaUtil.linq(RoleGrantedAuthority.class).equal("actorId", userDetails.getUsername()).list();
	return authorities;
}
 
Example #22
Source File: DictionaryServiceImpl.java    From bdf3 with Apache License 2.0 4 votes vote down vote up
@Override
public Dictionary getDictionaryBy(String code) {
	return JpaUtil.linq(Dictionary.class).equal("code", code).findOne();
}
 
Example #23
Source File: MultitenantUtils.java    From multitenant with Apache License 2.0 4 votes vote down vote up
private static CommandService getCommandService() {
	if (commandService == null) {
		commandService = JpaUtil.getApplicationContext().getBean(CommandService.class);
	}
	return commandService;
}
 
Example #24
Source File: RegisterServiceImpl.java    From bdf3 with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isExistOrganization(String organizationId) {
	return JpaUtil.linq(Organization.class).equal("id", organizationId).exists();
}
 
Example #25
Source File: LinImpl.java    From linq with Apache License 2.0 4 votes vote down vote up
@Override
public T idEqual(Object id) {
	return equal(JpaUtil.getIdName(domainClass), id);
}
 
Example #26
Source File: LinImpl.java    From linq with Apache License 2.0 4 votes vote down vote up
@Override
public T selectId() {
	return select(JpaUtil.getIdName(domainClass));
}
 
Example #27
Source File: JpaUtilInitiator.java    From linq with Apache License 2.0 4 votes vote down vote up
@Override
public void initialize(ApplicationContext applicationContext) {
	JpaUtil.setGetEntityManagerFactoryStrategy(getEntityManagerFactoryStrategy);
	JpaUtil.setApplicationContext(applicationContext);
}
 
Example #28
Source File: OrganizationServiceImpl.java    From multitenant with Apache License 2.0 4 votes vote down vote up
@Override
public Organization get(String id) {
	return JpaUtil.getOne(Organization.class, id);
}
 
Example #29
Source File: MultitenantUtils.java    From multitenant with Apache License 2.0 4 votes vote down vote up
private static CurrentOrganizationStrategy getCurrentOrganizationStrategy() {
	if (currentOrganizationStrategy == null) {
		currentOrganizationStrategy = JpaUtil.getApplicationContext().getBean(CurrentOrganizationStrategy.class);
	}
	return currentOrganizationStrategy;
}