{
boolean permissionExists = false;
if (StringUtils.isEmpty(permission.getName()))
{
throw new DataBackendException("Could not create "
+ "a permission with empty name!");
}
try
{
lockExclusive();
permissionExists = checkExists(permission);
if (!permissionExists)
{
// add a row to the table
Criteria criteria = PermissionPeer.buildCriteria(permission);
PermissionPeer.doInsert(criteria);
// try to get the object back using the name as key.
criteria = new Criteria();
criteria.add(PermissionPeer.NAME,
permission.getName());
List results = PermissionPeer.doSelect(criteria);
if (results.size() != 1)
{
throw new DataBackendException(
"Internal error - query returned "
+ results.size() + " rows");
}
Permission newPermission = (Permission) results.get(0);
// add the permission to system-wide cache
getAllPermissions().add(newPermission);
// return the object with correct id
return newPermission;
}
}
catch (Exception e)
{
throw new DataBackendException(
"addPermission(Permission) failed", e);
}
finally
{
unlockExclusive();