class SoapSecurityService extends AbstractService implements SecurityService {

	@Override
	public long storeGroup(String sid, WSGroup group) throws Exception {
		checkAdministrator(sid);

		try {
			GroupDAO dao = (GroupDAO) Context.get().getBean(GroupDAO.class);
			Group grp = group.toGroup();
			if (group.getId() != 0) {
				grp = dao.findById(group.getId());
				if (grp.getType() != Group.TYPE_DEFAULT) {
					throw new Exception(String.format("You cannot edit group with id %s because it is a system group",
							grp.getId()));
				}
				grp.setName(group.getName());
				grp.setDescription(group.getDescription());
				grp.setType(group.getType());
			}

			if (StringUtils.isEmpty(grp.getName()))
				throw new Exception("Missing mandatory value 'Name'");

			if (group.getUserIds() != null && group.getUserIds().length > 0) {
				UserDAO userDao = (UserDAO) Context.get().getBean(UserDAO.class);
				dao.initialize(grp);
				for (User usr : grp.getUsers()) {
					usr.removeGroup(grp.getId());
					userDao.store(usr);
				}

				for (long userId : group.getUserIds()) {
					User user = userDao.findById(userId);
					grp.getUsers().add(user);
					user.addGroup(grp);
					userDao.store(user);
				}
			}

			if (dao.store(grp)) {
				if (group.getInheritGroupId() != null && group.getInheritGroupId().longValue() > 0)
					dao.inheritACLs(grp, group.getInheritGroupId().longValue());
				return grp.getId();
			} else
				throw new Exception("Unable to store the group");
		} catch (Throwable t) {
			log.error(t.getMessage(), t);
			throw new Exception(t.getMessage());
		}
	}

}