/** * add new user * * @param user * @param nodes * @return * @throws DuplicateBookException */ public Node addUser(User user, List<String> nodes) throws DuplicateBookException { SessionProvider sProvider = SessionProvider.createSystemProvider(); if (isExistUserName(user.getUsername(), sProvider)) { throw new DuplicateBookException(String.format("User %s is existed", user.getUsername())); } user.setUserId(Utils.userId++); try { Node parentNode = getNodeByPath(DEFAULT_PARENT_PATH + DEFAULT_PARENT_USER_PATH, sProvider); Node userNode = parentNode.addNode("" + user.getUserId(), BookNodeTypes.EXO_USER); userNode.setProperty(BookNodeTypes.EXO_USER_NAME, user.getUsername()); userNode.setProperty(BookNodeTypes.EXO_USER_PASSWORD, user.getPassword()); userNode.setProperty(BookNodeTypes.EXO_USER_FULLNAME, user.getFullname()); userNode.setProperty(BookNodeTypes.EXO_USER_ADDRESS, user.getAddress()); userNode.setProperty(BookNodeTypes.EXO_USER_PHONE, user.getPhone()); List<Value> values = new ArrayList<Value>(); for (String string : nodes) { Value val = parentNode.getSession().getValueFactory().createValue(getNodeByPath(string, sProvider)); values.add(val); } userNode.setProperty(BookNodeTypes.EXO_BOOK, values.toArray(new Value[values.size()])); parentNode.getSession().save(); return userNode; } catch (RepositoryException re) { log.error("Failed to add user", re); return null; } finally { sProvider.close(); } }
/** * Create user by node data * * @param node * @return * @throws RepositoryException */ private User createUserByNode(Node node) throws RepositoryException { if (node != null) { User user = new User(); try { user.setUserId(Integer.valueOf(node.getName())); } catch (RepositoryException re) { log.error("Error in convert integer", re); return null; } PropertyReader reader = new PropertyReader(node); user.setUsername(reader.string(BookNodeTypes.EXO_USER_NAME)); user.setPassword(reader.string(BookNodeTypes.EXO_USER_PASSWORD)); user.setFullname(reader.string(BookNodeTypes.EXO_USER_FULLNAME)); user.setAddress(reader.string(BookNodeTypes.EXO_USER_ADDRESS)); user.setPhone(reader.string(BookNodeTypes.EXO_USER_PHONE)); return user; } return null; }