addToTag(tagMembership);
}
public void addToTag(final TagMembership tagMembership) throws SimpleMessageException {
if (tagMembership == null) {
throw new SimpleMessageException("TagMembership not provided.");
}
if (tagMembership.getTag() == null) {
throw new SimpleMessageException("Tag not provided.");
}
if (tagMembership.getPermissibleObject() == null) {
throw new SimpleMessageException("PermissibleObject not provided.");
}
User authUser = getAuthenticatedUser(session.get());
if (authUser == null) {
throw new SimpleMessageException("User is not authenticated.");
}
// assumption is that the membership does not exist but the category / permissible object do
// they must be loaded
Tag hibTag = ((Tag) session.get().load(Tag.class, tagMembership.getTag().getId()));
if (hibTag == null) {
throw new SimpleMessageException("Tag not found: " + tagMembership.getTag().getId());
}
PermissibleObject hibPermissibleObject = ((PermissibleObject) session.get().load(PermissibleObject.class, tagMembership.getPermissibleObject().getId()));
if (hibPermissibleObject == null) {
throw new SimpleMessageException("PermissibleObject not found: " + tagMembership.getPermissibleObject());
}
Transaction tx = session.get().beginTransaction();
try {
tagMembership.setTag(hibTag);
tagMembership.setPermissibleObject(hibPermissibleObject);
session.get().save(tagMembership);
tx.commit();
} catch (Throwable t) {
Logger.log(t);
try {
tx.rollback();
} catch (Throwable tt) {
}
throw new SimpleMessageException(t.getMessage());
}
}