/** * Parameterized constructor with collection. * * @param collection * the collection to set */ public FeatureStoreMongoDB(MongoClient client) { this.mongoClient = client; this.featuresCollection = getFeaturesCollection(); }
/** * Parameterized constructor with collection. * * @param collection * the collection to set */ public FeatureStoreMongoDB(MongoClient client, String dbName, String collectionName) { this.mongoClient = client; this.collectionName = collectionName; this.dbName = dbName; this.featuresCollection = getFeaturesCollection(); }
/** {@inheritDoc} */ @Override public Set<String> readAllGroups() { Set<String> setOfGroups = new HashSet<String>(); for (DBObject dbObject : getFeaturesCollection().find()) { setOfGroups.add((String) dbObject.get(GROUPNAME)); } setOfGroups.remove(null); setOfGroups.remove(""); return setOfGroups; }
/** {@inheritDoc} */ @Override public boolean existGroup(String groupName) { if (groupName == null || groupName.isEmpty()) { throw new IllegalArgumentException(GROUPNAME_CANNOT_BE_NULL_NOR_EMPTY); } return getFeaturesCollection().count(BUILDER.getGroupName(groupName)) > 0; }
/** {@inheritDoc} */ @Override public Map<String, Feature> readAll() { LinkedHashMap<String, Feature> mapFP = new LinkedHashMap<String, Feature>(); for(DBObject dbObject : getFeaturesCollection().find()) { Feature feature = MAPPER.mapFeature(dbObject); mapFP.put(feature.getUid(), feature); } return mapFP; }
/** {@inheritDoc} */ @Override public void grantRoleOnFeature(String uid, String roleName) { if (uid == null || uid.isEmpty()) { throw new IllegalArgumentException(FEATURE_IDENTIFIER_CANNOT_BE_NULL_NOR_EMPTY); } if (roleName == null || roleName.isEmpty()) { throw new IllegalArgumentException("roleName cannot be null nor empty"); } if (!exist(uid)) { throw new FeatureNotFoundException(uid); } getFeaturesCollection().update(BUILDER.getFeatUid(uid), new BasicDBObject("$addToSet", BUILDER.getRoles(roleName))); }
/** {@inheritDoc} */ @Override public void removeRoleFromFeature(String uid, String roleName) { if (uid == null || uid.isEmpty()) { throw new IllegalArgumentException(FEATURE_IDENTIFIER_CANNOT_BE_NULL_NOR_EMPTY); } if (roleName == null || roleName.isEmpty()) { throw new IllegalArgumentException("roleName cannot be null nor empty"); } if (!exist(uid)) { throw new FeatureNotFoundException(uid); } getFeaturesCollection().update(BUILDER.getFeatUid(uid), new BasicDBObject("$pull", BUILDER.getRoles(roleName))); }
/** {@inheritDoc} */ @Override public void delete(String uid) { if (uid == null || uid.isEmpty()) { throw new IllegalArgumentException(FEATURE_IDENTIFIER_CANNOT_BE_NULL_NOR_EMPTY); } if (!exist(uid)) { throw new FeatureNotFoundException(uid); } getFeaturesCollection().remove(BUILDER.getFeatUid(uid)); }
/** {@inheritDoc} */ @Override public Feature read(String uid) { if (uid == null || uid.isEmpty()) { throw new IllegalArgumentException(FEATURE_IDENTIFIER_CANNOT_BE_NULL_NOR_EMPTY); } DBObject object = getFeaturesCollection().findOne(BUILDER.getFeatUid(uid)); if (object==null) { throw new FeatureNotFoundException(uid); } return MAPPER.mapFeature(object); }
/** {@inheritDoc} */ @Override public void update(Feature fp) { if (fp == null) { throw new IllegalArgumentException("Feature cannot be null nor empty"); } read(fp.getUid()); getFeaturesCollection().save(MAPPER.toDBObject(fp)); }
/** {@inheritDoc} */ @Override public void clear() { getFeaturesCollection().remove(BasicDBObjectBuilder.start().get()); }
/** {@inheritDoc} */ @Override public boolean exist(String featId) { Util.assertHasLength(featId); return 1 == getFeaturesCollection().count(BUILDER.getFeatUid(featId)); }
/** {@inheritDoc} */ @Override public Map<String, Feature> readGroup(String groupName) { if (groupName == null || groupName.isEmpty()) { throw new IllegalArgumentException(GROUPNAME_CANNOT_BE_NULL_NOR_EMPTY); } if (!existGroup(groupName)) { throw new GroupNotFoundException(groupName); } LinkedHashMap<String, Feature> mapFP = new LinkedHashMap<String, Feature>(); for (DBObject dbObject : getFeaturesCollection().find(BUILDER.getGroupName(groupName))) { Feature feature = MAPPER.mapFeature(dbObject); mapFP.put(feature.getUid(), feature); } return mapFP; }
/** {@inheritDoc} */ @Override public void enableGroup(String groupName) { if (groupName == null || groupName.isEmpty()) { throw new IllegalArgumentException(GROUPNAME_CANNOT_BE_NULL_NOR_EMPTY); } if (!existGroup(groupName)) { throw new GroupNotFoundException(groupName); } for (DBObject dbObject : getFeaturesCollection().find(BUILDER.getGroupName(groupName))) { Object enabledd = BUILDER.getEnable(true); getFeaturesCollection().update(dbObject, BasicDBObjectBuilder.start(MONGO_SET, enabledd).get()); } }
/** {@inheritDoc} */ @Override public void disableGroup(String groupName) { if (groupName == null || groupName.isEmpty()) { throw new IllegalArgumentException(GROUPNAME_CANNOT_BE_NULL_NOR_EMPTY); } if (!existGroup(groupName)) { throw new GroupNotFoundException(groupName); } for (DBObject dbObject : getFeaturesCollection().find(BUILDER.getGroupName(groupName))) { Object enabledd = BUILDER.getEnable(false); getFeaturesCollection().update(dbObject, BasicDBObjectBuilder.start(MONGO_SET, enabledd).get()); } }
/** {@inheritDoc} */ @Override public void addToGroup(String uid, String groupName) { if (uid == null || uid.isEmpty()) { throw new IllegalArgumentException(FEATURE_IDENTIFIER_CANNOT_BE_NULL_NOR_EMPTY); } if (groupName == null || groupName.isEmpty()) { throw new IllegalArgumentException(GROUPNAME_CANNOT_BE_NULL_NOR_EMPTY); } if (!exist(uid)) { throw new FeatureNotFoundException(uid); } DBObject target = BUILDER.getFeatUid(uid); DBObject nGroupName = BUILDER.getGroupName(groupName); getFeaturesCollection().update(target, BasicDBObjectBuilder.start(MONGO_SET, nGroupName).get()); }
/** {@inheritDoc} */ @Override public void create(Feature fp) { if (fp == null) { throw new IllegalArgumentException("Feature cannot be null nor empty"); } if (exist(fp.getUid())) { throw new FeatureAlreadyExistException(fp.getUid()); } getFeaturesCollection().save(MAPPER.toDBObject(fp)); }
/** {@inheritDoc} */ @Override public void removeFromGroup(String uid, String groupName) { if (uid == null || uid.isEmpty()) { throw new IllegalArgumentException(FEATURE_IDENTIFIER_CANNOT_BE_NULL_NOR_EMPTY); } if (groupName == null || groupName.isEmpty()) { throw new IllegalArgumentException(GROUPNAME_CANNOT_BE_NULL_NOR_EMPTY); } if (!exist(uid)) { throw new FeatureNotFoundException(uid); } if (!existGroup(groupName)) { throw new GroupNotFoundException(groupName); } DBObject target = BUILDER.getFeatUid(uid); DBObject nGroupName = BUILDER.getGroupName(""); getFeaturesCollection().update(target, BasicDBObjectBuilder.start(MONGO_SET, nGroupName).get()); }
/** * Update status of feature. * * @param uid * feature id * @param enable * enabler */ private void updateStatus(String uid, boolean enable) { Util.assertParamHasLength(uid, "uid (feature identifier"); if (!exist(uid)) { throw new FeatureNotFoundException(uid); } DBObject target = BUILDER.getFeatUid(uid); Object enabledd = BUILDER.getEnable(enable); getFeaturesCollection().update(target, BasicDBObjectBuilder.start(MONGO_SET, enabledd).get()); }