Foren

Find UserGroup with Exception and create it then?

thumbnail
Christian Schulze, geändert vor 12 Jahren.

Find UserGroup with Exception and create it then?

Junior Member Beiträge: 87 Beitrittsdatum: 19.11.09 Neueste Beiträge
Hello, I want to add UserGroups via UpgradeProcess with a hook.

Is it necessary to test if the UserGroup I want to add already exist with the UserGroupFinderUtil? Or is there another way without getting a Exception if it not exist?

like this
public boolean userGroupExist(String name, long companyId) {
		try {
			UserGroupFinderUtil.findByC_N(companyId, name);
			return true;
		}
		catch (Exception e) {
			return false;
		}
	}


regards
Chris
thumbnail
jelmer kuperus, geändert vor 12 Jahren.

RE: Find UserGroup with Exception and create it then?

Liferay Legend Beiträge: 1191 Beitrittsdatum: 10.03.10 Neueste Beiträge
Catching exception like that is really in poor style, what if the method throws a NullPointerException : your method will return false. You'll probably want to change that to catching NoSuchUserGroupException

Probably using the UserGroupFinderUtil directly wont work btw. Because that class is not wrapped in a transactional proxy.
You probably should be using UserGroupLocalServiceUtil.getUserGroup(long companyId, String name)

Alternatively you could use a dynamic query to determine if a usergroup already exists without raising an exception