Session session = this.getSessionFactory().getCurrentSession();
session.beginTransaction();
// check parent authority
if (! (parentAuthority instanceof DbAuthority)) throw new StoreException(StoreException.KEY_NOTFOUND, "Parent authority is not from this store.");
// create authority
DbAuthority authority;
String authorityId = this.getUUID();
try {
authority = DbAuthority.ByAuthorityId(session, authorityId);
if (authority != null) {
throw new StoreException(StoreException.KEY_DUPNAME, "Authority exists already.");
}
authority = new DbAuthority();
authority.setAuthorityId(authorityId);
authority.setLocalAuthService(localAuthService);
for (Service service : services) authority.getServices().add(service);
for (Ref ref : refs) authority.getRefs().add(ref);
session.save(authority);
} catch (StoreException ex) {
session.getTransaction().rollback();
log.error(ex);
throw ex;
} catch (Exception ex) {
session.getTransaction().rollback();
log.error(ex);
throw new StoreException(ex, StoreException.KEY_DBERR, "Cannot access database.");
}
// create subsegment with this authority
DbSubSegment subSegment;
try {
subSegment = DbSubSegment.ByParentAndName(session, (DbAuthority) parentAuthority, subSegmentName);
if (subSegment != null) {
throw new StoreException(StoreException.KEY_DUPNAME, "Subsegment exists already.");
}
subSegment = new DbSubSegment();
subSegment.setParent((DbAuthority) parentAuthority);
subSegment.setName(subSegmentName);
subSegment.setAuthority(authority);
reattach(session, parentAuthority);
((DbAuthority) parentAuthority).getChildren().add(subSegment);
authority.getSubSegments().add(subSegment);
session.save(subSegment);
session.flush();
session.getTransaction().commit();
} catch (StoreException ex) {
session.getTransaction().rollback();
log.error(ex);
throw ex;
} catch (Exception ex) {
session.getTransaction().rollback();
log.error(ex);
throw new StoreException(ex, StoreException.KEY_DBERR, "Cannot access database.");
}
// done
log.trace("Done.");