Java Code Examples for org.malagu.linq.JpaUtil

The following examples show how to use org.malagu.linq.JpaUtil. These examples are extracted from open source projects. 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 Project: multitenant   Source File: DataSourceInfoServiceImpl.java    License: 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 2
Source Project: bdf3   Source File: MultitenantUserDetailsService.java    License: 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 3
Source Project: bdf3   Source File: DictionaryServiceImpl.java    License: 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 4
Source Project: bdf3   Source File: UrlServiceImpl.java    License: 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 Project: bdf3   Source File: ComponentServiceImpl.java    License: 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 Project: multitenant   Source File: MasterDataSourceInitiator.java    License: 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 7
Source Project: multitenant   Source File: DataSourceInfoServiceImpl.java    License: 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 8
Source Project: multitenant   Source File: OrganizationServiceImpl.java    License: 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 9
Source Project: linq   Source File: LinImpl.java    License: 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 10
Source Project: linq   Source File: LinImpl.java    License: 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 11
Source Project: linq   Source File: LinqImpl.java    License: 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 12
Source Project: bdf3   Source File: DictionaryServiceImpl.java    License: 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 13
Source Project: bdf3   Source File: DictionaryServiceImpl.java    License: 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 14
Source Project: bdf3   Source File: UserServiceImpl.java    License: 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 15
Source Project: bdf3   Source File: UserDetailsServiceImpl.java    License: 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 16
Source Project: bdf3   Source File: UserController.java    License: 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 17
Source Project: multitenant   Source File: MultitenantUtils.java    License: 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 18
Source Project: multitenant   Source File: MultitenantUtils.java    License: 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;
}
 
Example 19
Source Project: multitenant   Source File: OrganizationServiceImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Organization get(String id) {
	return JpaUtil.getOne(Organization.class, id);
}
 
Example 20
Source Project: linq   Source File: JpaUtilInitiator.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void initialize(ApplicationContext applicationContext) {
	JpaUtil.setGetEntityManagerFactoryStrategy(getEntityManagerFactoryStrategy);
	JpaUtil.setApplicationContext(applicationContext);
}
 
Example 21
Source Project: linq   Source File: LinImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public T selectId() {
	return select(JpaUtil.getIdName(domainClass));
}
 
Example 22
Source Project: linq   Source File: LinImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public T idEqual(Object id) {
	return equal(JpaUtil.getIdName(domainClass), id);
}
 
Example 23
Source Project: bdf3   Source File: RegisterServiceImpl.java    License: 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 24
Source Project: bdf3   Source File: RegisterServiceImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Organization getOrganization(String organizationId) {
	return JpaUtil.getOne(Organization.class, organizationId);
}
 
Example 25
Source Project: bdf3   Source File: DictionaryServiceImpl.java    License: 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 26
Source Project: bdf3   Source File: UserGrantedAuthorityProvider.java    License: 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 27
Source Project: bdf3   Source File: UserController.java    License: 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 28
Source Project: bdf3   Source File: UserController.java    License: 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 29
Source Project: bdf3   Source File: UserController.java    License: 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);
}