/** * * Creates an entry into the ODocument-Collection and create a new Class named "collectionName" * * @param collectionName * @return * @throws Throwable */ public ODocument create(String collectionName) throws Throwable { Logger.trace("Method Start"); try { if (existsCollection(collectionName)) throw new InvalidCollectionException("Collection " + collectionName + " already exists"); } catch (SqlInjectionException e) { throw new InvalidCollectionException(e); } ODocument doc = super.create(); doc.field("name", collectionName); save(doc); // create new class OClass documentClass = db.getMetadata().getSchema().getClass(CLASS_NODE_NAME); db.getMetadata().getSchema().createClass(collectionName, documentClass); // grants to the new class ORole registeredRole = RoleDao.getRole(DefaultRoles.REGISTERED_USER.toString()); ORole anonymousRole = RoleDao.getRole(DefaultRoles.ANONYMOUS_USER.toString()); registeredRole.addRule( ODatabaseSecurityResources.CLASS + "." + collectionName, ORole.PERMISSION_ALL); registeredRole.addRule( ODatabaseSecurityResources.CLUSTER + "." + collectionName, ORole.PERMISSION_ALL); anonymousRole.addRule( ODatabaseSecurityResources.CLASS + "." + collectionName, ORole.PERMISSION_READ); anonymousRole.addRule( ODatabaseSecurityResources.CLUSTER + "." + collectionName, ORole.PERMISSION_READ); PermissionsHelper.grantRead(doc, registeredRole); PermissionsHelper.grantRead(doc, anonymousRole); Logger.trace("Method End"); return doc; } // getNewModelInstance(String collectionName)
/** * * Creates an entry into the ODocument-Collection and create a new Class named "collectionName" * * @param collectionName * @return an ODocument instance representing the collection * @throws CollectionAlreadyExistsException * @throws OpenTransactionException, CollectionAlreadyExistsException, InvalidCollectionException, * InvalidModelException, Throwable */ public ODocument create(String collectionName) throws OpenTransactionException, CollectionAlreadyExistsException, InvalidCollectionException, InvalidModelException, Throwable { if (DbHelper.isInTransaction()) throw new OpenTransactionException("Cannot create a collection within an open transaction"); if (Logger.isTraceEnabled()) Logger.trace("Method Start"); if (Character.isDigit(collectionName.charAt(0))) { throw new InvalidCollectionException("Collection names cannot start by a digit"); } if (collectionName.contains(";")) { throw new InvalidCollectionException("Collection cannot contain ;"); } if (collectionName.contains(":")) { throw new InvalidCollectionException("Collection cannot contain :"); } if (collectionName.contains(".")) { throw new InvalidCollectionException("Collection cannot contain ."); } if (collectionName.contains("@")) { throw new InvalidCollectionException("Collection cannot contain @"); } if (collectionName.startsWith("_")) { throw new InvalidCollectionException("Collection cannot start with _"); } if (!collectionName.matches(COLLECTION_NAME_REGEX)) { throw new InvalidCollectionException( "Collection name must be complaint with the regular expression: " + COLLECTION_NAME_REGEX); } if (collectionName.toUpperCase().startsWith("_BB_")) { throw new InvalidCollectionException( "Collection name is not valid: it can't be prefixed with _BB_"); } try { if (existsCollection(collectionName)) throw new CollectionAlreadyExistsException( "Collection " + collectionName + " already exists"); } catch (SqlInjectionException e) { throw new InvalidCollectionException(e); } ODocument doc = super.create(); doc.field("name", collectionName); save(doc); // create new class OClass documentClass = db.getMetadata().getSchema().getClass(CLASS_NODE_NAME); db.getMetadata().getSchema().createClass(collectionName, documentClass); // grants to the new class ORole registeredRole = RoleDao.getRole(DefaultRoles.REGISTERED_USER.toString()); ORole anonymousRole = RoleDao.getRole(DefaultRoles.ANONYMOUS_USER.toString()); registeredRole.addRule( ODatabaseSecurityResources.CLASS + "." + collectionName, ORole.PERMISSION_ALL); registeredRole.addRule( ODatabaseSecurityResources.CLUSTER + "." + collectionName, ORole.PERMISSION_ALL); anonymousRole.addRule( ODatabaseSecurityResources.CLASS + "." + collectionName, ORole.PERMISSION_READ); anonymousRole.addRule( ODatabaseSecurityResources.CLUSTER + "." + collectionName, ORole.PERMISSION_READ); PermissionsHelper.grantRead(doc, registeredRole); PermissionsHelper.grantRead(doc, anonymousRole); if (Logger.isTraceEnabled()) Logger.trace("Method End"); return doc; } // getNewModelInstance(String collectionName)