public Organisation createChildOrg(String orgId, String title, Session session) { Organisation o = new Organisation(); o.setOrganisation(this); if (orgId != null) { orgId = orgId.trim(); if (orgId.length() == 0) { orgId = null; } } o.setOrgId(orgId); o.setTitle(title); o.setCreatedDate(new Date()); o.setModifiedDate(new Date()); if (this.getChildOrgs() == null) { this.setChildOrgs(new ArrayList<>()); } this.getChildOrgs().add(o); session.save(o); if (o.getOrgId() == null || o.getOrgId().trim().length() == 0) { findUniqueOrgId(o, session); session.save(o); } SubOrg.updateSubOrgs(o, session); return o; }
private void findUniqueOrgId(Organisation o, Session session) { String candidate = "O" + System.currentTimeMillis(); int i = 0; while (!isOrgIdUniqueWithinAdmin(session)) { candidate = "O" + System.currentTimeMillis() + i++; } o.setOrgId(candidate); // hack, todo, check for uniqueness within the account }