@Override public void delete(String name) throws Exception { if (!existsCollection(name)) throw new InvalidCollectionException("Collection " + name + " does not exists"); // get the helper class GenericDao gdao = GenericDao.getInstance(); // begin transaction DbHelper.requestTransaction(); try { // delete all vertices linked to objects in this class String deleteVertices = "delete vertex _bb_nodevertex where _node.@class=?"; Object[] params = {name}; gdao.executeCommand(deleteVertices, params); // delete vertices linked to the collection entry in the _bb_collection class // note: the params are equals to the previous one (just the collection name) String deleteVertices2 = "delete vertex _bb_nodevertex where _node.@class='_bb_collection' and _node.name=?"; gdao.executeCommand(deleteVertices2, params); // delete this collection from the list of declared collections // note: the params are equals to the previous one (just the collection name) String deleteFromCollections = "delete from _bb_collection where name =?"; gdao.executeCommand(deleteFromCollections, params); // delete all records belonging to the dropping collection.... // it could be done dropping the class, but in this case we not should be able to perform a // rollback String deleteAllRecords = "delete from " + name; gdao.executeCommand(deleteAllRecords, new Object[] {}); // commit DbHelper.commitTransaction(); // drop the collection class outside collection String dropCollection = "drop class " + name; gdao.executeCommand(dropCollection, new Object[] {}); } catch (Exception e) { // rollback in case of error DbHelper.rollbackTransaction(); if (Logger.isDebugEnabled()) Logger.debug("An error occured deleting the collection " + name, e); throw e; } } // delete
public ODocument createLink(String sourceId, String destId, String edgeName) throws DocumentNotFoundException { DbHelper.requestTransaction(); OrientEdge edge = null; try { OrientVertex sourceVertex = StorageUtils.getNodeVertex(sourceId); OrientVertex destVertex = StorageUtils.getNodeVertex(destId); UUID token = UUID.randomUUID(); edge = (OrientEdge) sourceVertex.addEdge(edgeName, destVertex); edge.getRecord().field(BaasBoxPrivateFields.ID.toString(), token.toString()); edge.getRecord().field(BaasBoxPrivateFields.AUTHOR.toString(), DbHelper.currentUsername()); edge.getRecord().field(BaasBoxPrivateFields.CREATION_DATE.toString(), new Date()); edge.save(); DbHelper.commitTransaction(); } catch (DocumentNotFoundException e) { DbHelper.rollbackTransaction(); throw e; } // edge.getGraph().commit(); return edge.getRecord(); }
public static void removeSocialLoginTokens(ODocument user, String socialNetwork) throws ODatabaseException { DbHelper.requestTransaction(); try { ODocument systemProps = user.field(UserDao.ATTRIBUTES_SYSTEM); Map<String, ODocument> ssoTokens = systemProps.field(UserDao.SOCIAL_LOGIN_INFO); if (ssoTokens == null) { throw new ODatabaseException(socialNetwork + " is not linked with this account"); } else { ssoTokens.remove(socialNetwork); systemProps.field(UserDao.SOCIAL_LOGIN_INFO, ssoTokens); user.field(UserDao.ATTRIBUTES_SYSTEM, systemProps); systemProps.save(); user.save(); if (Logger.isDebugEnabled()) Logger.debug("saved tokens for user "); DbHelper.commitTransaction(); } } catch (Exception e) { e.printStackTrace(); DbHelper.rollbackTransaction(); throw new ODatabaseException("unable to add tokens"); } }
public static void addSocialLoginTokens(ODocument user, UserInfo userInfo) throws ODatabaseException { DbHelper.requestTransaction(); try { ODocument systemProps = user.field(UserDao.ATTRIBUTES_SYSTEM); Map<String, ODocument> ssoTokens = systemProps.field(UserDao.SOCIAL_LOGIN_INFO); if (ssoTokens == null) { ssoTokens = new HashMap<String, ODocument>(); } String jsonRep = userInfo.toJson(); ssoTokens.put(userInfo.getFrom(), (ODocument) new ODocument().fromJSON(jsonRep)); systemProps.field(UserDao.SOCIAL_LOGIN_INFO, ssoTokens); user.field(UserDao.ATTRIBUTES_SYSTEM, systemProps); systemProps.save(); user.save(); if (Logger.isDebugEnabled()) Logger.debug("saved tokens for user "); DbHelper.commitTransaction(); } catch (Exception e) { DbHelper.rollbackTransaction(); throw new ODatabaseException("unable to add tokens"); } }
public static ODocument signUp( String username, String password, Date signupDate, String role, JsonNode nonAppUserAttributes, JsonNode privateAttributes, JsonNode friendsAttributes, JsonNode appUsersAttributes, boolean generated) throws OSerializationException, Exception { ODatabaseRecordTx db = DbHelper.getConnection(); ODocument profile = null; UserDao dao = UserDao.getInstance(); try { // because we have to create an OUser record and a User Object, we need a transaction DbHelper.requestTransaction(); if (role == null) profile = dao.create(username, password); else profile = dao.create(username, password, role); ORID userRid = ((ODocument) profile.field("user")).getIdentity(); ORole friendRole = RoleDao.createFriendRole(username); friendRole.getDocument().field(RoleService.FIELD_ASSIGNABLE, true); friendRole.getDocument().field(RoleService.FIELD_MODIFIABLE, false); friendRole.getDocument().field(RoleService.FIELD_INTERNAL, true); friendRole .getDocument() .field(RoleService.FIELD_DESCRIPTION, "These are friends of " + username); /* these attributes are visible by: * Anonymous users * Registered user * Friends * User */ // anonymous { ODocument attrObj = new ODocument(dao.USER_ATTRIBUTES_CLASS); try { if (nonAppUserAttributes != null) attrObj.fromJSON(nonAppUserAttributes.toString()); else attrObj.fromJSON("{}"); } catch (OSerializationException e) { throw new OSerializationException( dao.ATTRIBUTES_VISIBLE_BY_ANONYMOUS_USER + " is not a valid JSON object", e); } PermissionsHelper.grantRead( attrObj, RoleDao.getRole(DefaultRoles.REGISTERED_USER.toString())); PermissionsHelper.grantRead( attrObj, RoleDao.getRole(DefaultRoles.ANONYMOUS_USER.toString())); PermissionsHelper.grantRead(attrObj, friendRole); PermissionsHelper.changeOwner(attrObj, userRid); profile.field(dao.ATTRIBUTES_VISIBLE_BY_ANONYMOUS_USER, attrObj); attrObj.save(); } /* these attributes are visible by: * User */ { ODocument attrObj = new ODocument(dao.USER_ATTRIBUTES_CLASS); try { if (privateAttributes != null) attrObj.fromJSON(privateAttributes.toString()); else attrObj.fromJSON("{}"); } catch (OSerializationException e) { throw new OSerializationException( dao.ATTRIBUTES_VISIBLE_ONLY_BY_THE_USER + " is not a valid JSON object", e); } profile.field(dao.ATTRIBUTES_VISIBLE_ONLY_BY_THE_USER, attrObj); PermissionsHelper.changeOwner(attrObj, userRid); attrObj.save(); } /* these attributes are visible by: * Friends * User */ { ODocument attrObj = new ODocument(dao.USER_ATTRIBUTES_CLASS); try { if (friendsAttributes != null) attrObj.fromJSON(friendsAttributes.toString()); else attrObj.fromJSON("{}"); } catch (OSerializationException e) { throw new OSerializationException( dao.ATTRIBUTES_VISIBLE_BY_FRIENDS_USER + " is not a valid JSON object", e); } PermissionsHelper.grantRead(attrObj, friendRole); PermissionsHelper.changeOwner(attrObj, userRid); profile.field(dao.ATTRIBUTES_VISIBLE_BY_FRIENDS_USER, attrObj); attrObj.save(); } /* these attributes are visible by: * Registered user * Friends * User */ { ODocument attrObj = new ODocument(dao.USER_ATTRIBUTES_CLASS); try { if (appUsersAttributes != null) attrObj.fromJSON(appUsersAttributes.toString()); else attrObj.fromJSON("{}"); } catch (OSerializationException e) { throw new OSerializationException( dao.ATTRIBUTES_VISIBLE_BY_REGISTERED_USER + " is not a valid JSON object", e); } PermissionsHelper.grantRead( attrObj, RoleDao.getRole(DefaultRoles.REGISTERED_USER.toString())); PermissionsHelper.changeOwner(attrObj, userRid); profile.field(dao.ATTRIBUTES_VISIBLE_BY_REGISTERED_USER, attrObj); attrObj.save(); } ODocument attrObj = new ODocument(dao.USER_ATTRIBUTES_CLASS); attrObj.field(dao.USER_LOGIN_INFO, new ArrayList()); attrObj.field(UserDao.GENERATED_USERNAME, generated); PermissionsHelper.grantRead( attrObj, RoleDao.getRole(DefaultRoles.REGISTERED_USER.toString())); PermissionsHelper.changeOwner(attrObj, userRid); profile.field(dao.ATTRIBUTES_SYSTEM, attrObj); PermissionsHelper.grantRead( profile, RoleDao.getRole(DefaultRoles.REGISTERED_USER.toString())); PermissionsHelper.grantRead(profile, RoleDao.getRole(DefaultRoles.ANONYMOUS_USER.toString())); PermissionsHelper.changeOwner(profile, userRid); profile.field(dao.USER_SIGNUP_DATE, signupDate == null ? new Date() : signupDate); profile.save(); DbHelper.commitTransaction(); } catch (OSerializationException e) { DbHelper.rollbackTransaction(); throw e; } catch (Exception e) { DbHelper.rollbackTransaction(); throw e; } return profile; } // signUp