protected void resolveSqlCommands(Dao dao, OrmMetadata md) { //TODO : cleanupAccessTokensCommand = md.tryGetSqlCommand(CLEANUP_ACCESS_TOKENS_SQL_KEY); cleanupRefreshTokensCommand = md.tryGetSqlCommand(CLEANUP_REFRESH_TOKENS_SQL_KEY); } }
protected EntityMapping em(Class<?> type) throws MappingNotFoundException { return metadata().getEntityMapping(type); }
protected boolean isEntityClass(Class<?> clzz){ return metadata().tryGetEntityMapping(clzz)!=null; }
protected void loadSqls(SqlReaderContext context, Map<String,DefaultSqlCommand> sqls) { final OrmMetadata md = context.getConfigContext().getMetadata(); sqls.forEach((key,sql) -> { if(!matchContext(context, sql)) { return; } //Removes if exists. md.removeSqlCommand(key); //Clones a new one for current orm context. md.addSqlCommand(key, sql.clone()); }); }
@Override public <T> EntityQuery<T> createNamedQuery(Class<T> entityClass,String queryName) { Args.notNull(entityClass,"entity class"); Args.notEmpty(queryName,"query name"); EntityMapping em = metadata().getEntityMapping(entityClass); SqlCommand command = metadata().tryGetSqlCommand(queryName); if(null == command) { command = metadata().tryGetSqlCommand(em.getEntityName(),queryName); } if(null == command){ throw new SqlNotFoundException("Query '" + queryName + "' not found for entity class '" + entityClass.getName() + "'"); } final SqlCommand c=command; return runInWrapperContext(em, (context)->{ return queryFactory().createEntityQuery(context.getDao(), context.getEntityMapping(), entityClass, c); }); }
protected void createMetadata(OrmContext oc, MetaApi api) { mappingDatabase(oc.getDb(), api); MappingSchema ms = ormConverter.toMappingSchema(oc, api); for(EntityMapping em : ms.getEntityMappings()) { oc.getMetadataManager().createEntity(oc, em); } //load sqls oc.getMetadataManager().loadSqls(oc); //convert sqls Map<String, SqlCommand> commands = ormConverter.toSqlCommands(oc, api); commands.forEach((k,c) -> { SqlCommand old = oc.getMetadata().tryGetSqlCommand(k); if(null != old) { log.info("The sql command '{}' from '{}' will be replaced by '{}'", k, old.getSource(), c.getSource()); oc.getMetadata().removeSqlCommand(k); } oc.getMetadata().addSqlCommand(k, c); }); }
protected void resolveTableSource(OrmMetadata metadata,SqlTableSource table) { if(table instanceof SqlTableName){ SqlTableName tableName = (SqlTableName)table; String lastName = tableName.getLastName(); EntityMapping em = metadata.tryGetEntityMapping(lastName); if(null == em){ em = metadata.tryGetEntityMappingByTableName(lastName); if(null == em) { em = metadata.tryGetEntityMappingByTableName(lastName); } } if(null != em){ if(tableName.getSecondaryOrFirstName() == null || Strings.equalsIgnoreCase(em.getTable().getSchema(), tableName.getSecondaryOrFirstName())){ tableName.setEntityMapping(em); } } } String alias = table.getAlias(); if(null != alias){ aliases.put(alias, table); } }
log.debug("Load {} entities used {}ms",context.getMetadata().getEntityMappingSize(),sw.getElapsedMilliseconds()); for(EntityMapping em : context.getMetadata().getEntityMappingSnapshotList()){ if(em.isRemote()){ continue; for(SqlCommand command : context.getMetadata().getSqlCommandSnapshotList()) { command.prepare(context);
protected List<DbSchema> createDbSchemas() { Db db = ormContext.getDb(); List<EntityMapping> entityMappings = ormContext.getMetadata().getEntityMappingSnapshotList(); List<SequenceMapping> sequenceMappings = ormContext.getMetadata().getSequenceMappingSnapshotList();
protected Set<EntityMapping> computeOrmModels(RestdConfig c, OrmMetadata om) { List<EntityMapping> ems = om.getEntityMappingSnapshotList();
protected void tryAddSqlCommand(MetadataContext context, EntityMapping em, String name, SqlCommand command) { if(null != command) { context.getMetadata().addSqlCommand(em, name, command); } }
@Override public <T> EntityQuery<T> createNamedQuery(String entityName, Class<T> resultClass, String queryName) { Args.notEmpty(entityName,"entity name"); Args.notEmpty(queryName,"query name"); Args.notNull(resultClass,"result class"); EntityMapping em = metadata().getEntityMapping(entityName); SqlCommand command = metadata().tryGetSqlCommand(queryName); if(null == command) { command = metadata().tryGetSqlCommand(em.getEntityName(), queryName); } if(null == command){ throw new SqlNotFoundException("Query '" + queryName + "' not found for entity '" + entityName + "'"); } final SqlCommand c=command; return runInWrapperContext(em, (context)->{ return queryFactory().createEntityQuery(context.getDao(), context.getEntityMapping(), resultClass, c); }); }
@Override public void postInitialize(OrmContext context) throws Exception { Dao dao = context.getDao(); Dmo dmo = context.getDmo(); for (EntityMapping em : context.getMetadata().getEntityMappingSnapshotList()) { Class<? extends Model> cls = em.getModelClass(); if (null != cls) { ModelContext modelContext = ModelRegistry.tryGetModelContext(cls.getName()); if (null == modelContext || (modelContext != null && modelContext.getEntityMapping().isRemote())) { registerModel(context, em, dao, dmo); } } } }
protected EntityMapping em(String name) throws MappingNotFoundException { return ormContext.getMetadata().getEntityMapping(name); }
protected void resolveSqlCommands(Dao dao, OrmMetadata md) { loadAuthorizationCodeCommand = md.tryGetSqlCommand(LOAD_AUTHORIZATION_CODE_SQL_KEY); deleteAuthorizationCodeCommand = md.tryGetSqlCommand(DELETE_AUTHORIZATION_CODE_SQL_KEY); cleanupAuthorizationCodesCommand = md.tryGetSqlCommand(CLEANUP_AUTHORIZATION_CODES_SQL_KEY); }
public static OrmContext context(Class<?> entityClass) { OrmRegistry registry = AppContext.factory().getBean(OrmRegistry.class); for(OrmContext context : registry.contexts()) { if(context.getMetadata().tryGetEntityMapping(entityClass) != null) { return context; } } throw new IllegalStateException("Orm context not found for entity class '" + entityClass + "'"); }
protected EntityMapping em(Class<?> type) throws MappingNotFoundException { return ormContext.getMetadata().getEntityMapping(type); }
protected void resolveSqlCommands(Dao dao, OrmMetadata md) { loadSessionByTokenCommand = md.tryGetSqlCommand(LOAD_SESSION_BY_TOKEN_SQL_KEY); loadSessionByIdCommand = md.tryGetSqlCommand(LOAD_SESSION_BY_ID_SQL_KEY); loadLoginsInSessionCommand = md.tryGetSqlCommand(LOAD_LOGINS_IN_SESSION_SQL_KEY); cleanupLoginsCommand = md.tryGetSqlCommand(CLEANUP_SSO_LOGINS_SQL_KEY); cleanupSessionsCommand = md.tryGetSqlCommand(CLEANUP_SSO_SESSIONS_SQL_KEY); } }
@Override public EntityModule require(Class<?> c) throws EntityNotFoundException { try { Api api = Api.current(); if(null == api) { throw new IllegalStateException("Can't require entity module '" + c.getName() + "', no api deployed!"); } EntityMapping em = api.getOrmContext().getMetadata().tryGetEntityMapping(c); if(null == em) { throw new EntityNotFoundException("Entity '" + c.getName() + "' not found!"); } return dao(engine.getMainModule(), api).entity(em.getEntityName()); }catch (ScriptException e) { throw new IllegalStateException("Error require entity module by class '" + c.getName() + "', " + e.getMessage(), e); } }
protected EntityMapping em(String name) throws MappingNotFoundException { return metadata().getEntityMapping(name); }