public Thing create (String type) { Thing thing = new Thing (type); db.save (thing); return thing; } public Thing get (long id) {
/** * @param id status id and optional name (used when create=true) * @param create if true and status doesn't exist, a new status with an optional name would be created. */ public Status getStatus (String id, boolean create) throws HibernateException, SQLException { String name = ""; int sp = id.indexOf (" "); if (sp > 0 && id.length() > sp) { name = id.substring (sp + 1); id = id.substring (0, sp); } Status s = (Status) db.session().get(Status.class, id); if (s == null && create) { s = new Status (); s.setId (id); s.setName (name.length() > 0 ? name : id); s.setTimeoutState (Status.OFF); s.setGroupName ("Unfiled"); db.save (s); } return s; }
/** * factory method used to create a Revision associated with this user. * * @param author the revision author * @param ref associated with this revision * @param info information * @return a revision entry */ public Revision createRevision (User author, String ref, String info) { Revision re = new Revision(); re.setDate (new Date()); re.setInfo (info); re.setRef (ref); re.setAuthor (author); db.save (re); return re; } }
private Role createRole (DB db, Realm realm, String name, String... permissions) { Role role = new Role(realm, name); Set<Permission> perms = role.getPermissions(); for (String p : permissions) perms.add(Permission.valueOf(p)); db.save(role); return role; } }
@Override public void exec(CLIContext cli, String[] args) throws Exception { if (args.length < 2) { cli.println("Usage: addpermission <role> <permission 1> <permission 2> ... <permission n>"); return; } try (DB db = new DB()) { db.open(); db.beginTransaction(); RoleManager rm = new RoleManager(db); Role role = null; if (rm.getRoleByName(args[1]) == null) { role = new Role(args[1]); Set<Permission> perms = role.getPermissions(); for (int i = 2; i < args.length; i++) perms.add(Permission.valueOf(args[i])); db.save(role); } db.commit(); cli.println (role != null ? "Role created " + role.getName() : " Role already exists"); } catch (Exception e) { cli.println (e.getMessage()); } } }
@Override public void exec(CLIContext cli, String[] args) throws Exception { if (args.length < 2) { cli.println("Usage: addrole <role> <permission 1> <permission 2> ... <permission n>"); return; } try (DB db = new DB()) { db.open(); db.beginTransaction(); RoleManager rm = new RoleManager(db); Role role = null; if (rm.getRoleByName(args[1]) == null) { role = new Role(args[1]); Set<Permission> perms = role.getPermissions(); for (int i = 2; i < args.length; i++) perms.add(Permission.valueOf(args[i])); db.save(role); } db.commit(); cli.println (role != null ? "Role created " + role.getName() : " Role already exists"); } catch (Exception e) { cli.println (e.getMessage()); } } }
public boolean saveUser (Binder binder, String clearPass) throws BLException { User u = (User) getOriginalEntity(); if (binder.writeBeanIfValid(getOriginalEntity())) { try { return (boolean) DB.execWithTransaction((db) -> { db.save(u); if (clearPass != null && !clearPass.isEmpty()) { UserManager mgr = new UserManager(db); try { mgr.setPassword(u, clearPass); } catch (BLException e) { return false; } addRevisionCreated(db, getEntityName(), u.getId().toString()); u.setForcePasswordChange(true); db.session().update(u); return true; } return false; }); } catch (Exception e) { getApp().getLog().error(e); return false; } } else { throw new BLException("Invalid user"); } }