Example #1
0
  @Override
  public void insertEntity(MongoIdentifiableEntity entity, MongoStoreInvocationContext context) {
    Class<? extends MongoEntity> clazz = entity.getClass();

    // Find annotations for ID, for all the properties and for the name of the collection.
    EntityInfo entityInfo = getEntityInfo(clazz);

    // Create instance of BasicDBObject and add all declared properties to it (properties with null
    // value probably should be skipped)
    BasicDBObject dbObject =
        mapperRegistry.convertApplicationObjectToDBObject(entity, BasicDBObject.class);

    DBCollection dbCollection = database.getCollection(entityInfo.getDbCollectionName());

    String currentId = entity.getId();

    // Generate random ID if not set already
    if (currentId == null) {
      currentId = KeycloakModelUtils.generateId();
      entity.setId(currentId);
    }

    // Adding "_id"
    dbObject.put("_id", currentId);

    try {
      dbCollection.insert(dbObject);
    } catch (MongoException e) {
      throw convertException(e);
    }

    // Treat object as created in this transaction (It is already submited to transaction)
    context.addCreatedEntity(entity);
  }
Example #2
0
  protected <T extends MongoIdentifiableEntity> T convertDBObjectToEntity(
      Class<T> type, DBObject dbObject, MongoStoreInvocationContext context) {
    // First look if we already have loaded object cached. If yes, we will use cached instance
    String id = dbObject.get("_id").toString();
    T object = context.getLoadedEntity(type, id);

    if (object == null) {
      // So convert and use fresh instance from DB
      MapperContext<Object, T> mapperContext = new MapperContext<Object, T>(dbObject, type, null);
      object = mapperRegistry.convertDBObjectToApplicationObject(mapperContext);
      context.addLoadedEntity(object);
    }
    return object;
  }
Example #3
0
  @Override
  public <T extends MongoIdentifiableEntity> T loadEntity(
      Class<T> type, String id, MongoStoreInvocationContext context) {
    // First look if we already read the object with this oid and type during this transaction. If
    // yes, use it instead of DB lookup
    T cached = context.getLoadedEntity(type, id);
    if (cached != null && type.isAssignableFrom(cached.getClass())) return cached;

    DBCollection dbCollection = getDBCollectionForType(type);

    BasicDBObject idQuery = new BasicDBObject("_id", id);
    DBObject dbObject = dbCollection.findOne(idQuery);

    if (dbObject == null) return null;

    MapperContext<Object, T> mapperContext = new MapperContext<Object, T>(dbObject, type, null);
    T converted = mapperRegistry.convertDBObjectToApplicationObject(mapperContext);

    // Now add it to loaded objects
    context.addLoadedEntity(converted);

    return converted;
  }
Example #4
0
  public MongoStoreImpl(
      DB database, boolean clearCollectionsOnStartup, Class<?>[] managedEntityTypes) {
    this.database = database;

    mapperRegistry = new MapperRegistry();

    for (Class<?> simpleMapperClass : SIMPLE_TYPES) {
      SimpleMapper mapper = new SimpleMapper(simpleMapperClass);
      mapperRegistry.addAppObjectMapper(mapper);
      mapperRegistry.addDBObjectMapper(mapper);
    }

    // Specific converter for ArrayList is added just for performance purposes to avoid recursive
    // converter lookup (most of list idm will be ArrayList)
    mapperRegistry.addAppObjectMapper(new ListMapper(mapperRegistry, ArrayList.class));
    mapperRegistry.addAppObjectMapper(new ListMapper(mapperRegistry, List.class));
    mapperRegistry.addDBObjectMapper(new BasicDBListMapper(mapperRegistry));

    mapperRegistry.addAppObjectMapper(new MapMapper(HashMap.class));
    mapperRegistry.addAppObjectMapper(new MapMapper(Map.class));
    mapperRegistry.addDBObjectMapper(new BasicDBObjectToMapMapper());

    // Enum converters
    mapperRegistry.addAppObjectMapper(new EnumToStringMapper());
    mapperRegistry.addDBObjectMapper(new StringToEnumMapper());

    for (Class<?> type : managedEntityTypes) {
      getEntityInfo(type);
      mapperRegistry.addAppObjectMapper(new MongoEntityMapper(this, mapperRegistry, type));
      mapperRegistry.addDBObjectMapper(new BasicDBObjectMapper(this, mapperRegistry, type));
    }

    if (clearCollectionsOnStartup) {
      // dropDatabase();
      clearManagedCollections(managedEntityTypes);
    }

    initManagedCollections(managedEntityTypes);
  }
Example #5
0
 public void addDBObjectConverter(Mapper<?, ?> mapper) {
   mapperRegistry.addDBObjectMapper(mapper);
 }