public String getIdOrName(){ return Strings.firstNotEmpty(id,name); }
public static String getTitleOrName(MetaObjNamed named) { return Strings.firstNotEmpty(named.getTitle(), named.getName()); }
private static String title(JsonObject o, String defaults) { String s = Strings.firstNotEmpty(o.getString(TITLE), o.getString("x-" + TITLE)); if(Strings.isEmpty(s)) { s = Strings.firstNotEmpty(o.getString(SUMMARY), o.getString("x-" + SUMMARY)); } if(Strings.isEmpty(s)) { s = defaults; } return s; }
@Override public EntityMapping tryGetEntityMapping(Class<?> entityClass) { Args.notNull(entityClass,"entity class"); EntityMapping em = classToEntityMappings.get(entityClass); if(null == em && Model.class.isAssignableFrom(entityClass)) { em = modelToEntityMappings.get(entityClass); } if(null == em) { Entity entity = entityClass.getAnnotation(Entity.class); if(null != entity) { String entityName = Strings.firstNotEmpty(entity.name(), entity.value()); if(!Strings.isEmpty(entityName)) { em = tryGetEntityMapping(entityName); if(null != em) { classToEntityMappings.put(entityClass, em); } } } } if(null == em && config.isMappingClassSimpleName()){ em = tryGetEntityMapping(entityClass.getSimpleName()); } return em; }
protected OrmContext lookupOrmContext(RestdConfig c) { OrmContext oc = c.getOrmContext(); if(null == oc) { String dataSourceName = Strings.firstNotEmpty(c.getDataSourceName(), DataSourceManager.DEFAULT_DATASOURCE_NAME); oc = ormRegistry.findContext(dataSourceName); if (null == oc) { throw new ApiConfigException("Can't create restd api , orm context '" + dataSourceName + "' has not been registered!"); } } return oc; }
private static String titleOrDesc(JsonObject o, String defaults) { String s = Strings.firstNotEmpty(o.getString(TITLE), o.getString("x-" + TITLE)); if(Strings.isEmpty(s)) { s = Strings.firstNotEmpty(o.getString(SUMMARY), o.getString("x-" + SUMMARY)); } if(Strings.isEmpty(s)) { s = o.getString(DESCRIPTION); } if(Strings.isEmpty(s)) { s = defaults; } return s; }
this.jdbcUrl = Strings.firstNotEmpty(Strings.trimToNull(properties.get(JDBC_URL)),Strings.trimToNull(properties.get(URL))); this.username = properties.get(USERNAME); this.password = properties.get(PASSWORD);
protected boolean mappingFieldColumnByMetaName(MetadataContext context,EntityMappingBuilder emb,FieldMappingBuilder fmb,MetaName a){ String metaFieldName = null; if(null != a){ metaFieldName = Strings.firstNotEmpty(a.value(),a.reserved().getFieldName()); }else{ Annotation metaNameAnnotation = Classes.getAnnotationByMetaType(fmb.getBeanProperty().getAnnotations(),MetaName.class); if(null != metaNameAnnotation){ MetaName metaDomain = metaNameAnnotation.annotationType().getAnnotation(MetaName.class); metaFieldName = Strings.firstNotEmpty(metaDomain.value(),metaDomain.reserved().getFieldName()); } } if(!Strings.isEmpty(metaFieldName)) { fmb.setMetaFieldName(metaFieldName); fmb.setReservedMetaFieldName(ReservedMetaFieldName.tryForName(metaFieldName)); return true; } return false; }
protected KeyAndDataSource resolveSqlIdentity(BeanDefinition bd, ReflectValued v, Inject inject) { String key = Strings.firstNotEmpty(inject.name(), inject.value()); SqlKey sk = v.getAnnotation(SqlKey.class); if (null != sk) { return resolveSqlIdentity(bd, v, sk); } if (Strings.isEmpty(key)) { key = v.getName(); } return new KeyAndDataSource(key, null); }
@Override public Object newInstance(ConvertContext context, Class<?> type, Type genericType, Map<String, Object> map) { JsonType jsonType = type.getAnnotation(JsonType.class); if(null == jsonType) { return null; } String propertyName = Strings.firstNotEmpty(jsonType.property(), jsonType.meta().getDefaultPropertyName()); Object propertyValue = map.get(propertyName); if(null == propertyValue) { return null; } String name = (String)propertyValue; if(jsonType.meta() == JsonType.MetaType.CLASS_NAME) { return Reflection.newInstance(Classes.forName(name)); }else { for(JsonType.SubType subType : jsonType.types()) { if(subType.name().equals(name)) { return Reflection.newInstance(subType.type()); } } } return null; }
protected String modelName(ApiMetadataContext context, MComplexType ct) { if(null != ct.getJavaType()) { ApiModel a = ct.getJavaType().getAnnotation(ApiModel.class); if(null != a) { String name = Strings.firstNotEmpty(a.name(),a.value()); if(!Strings.isEmpty(name)) { return name; } } ModelConfig mc = context.getConfig().getModelConfig(ct.getJavaType()); if(null != mc && !Strings.isEmpty(mc.getName())) { return mc.getName(); } } return ct.getName(); }
static MetaEntity convert(DbConvertContext context, DbTable table) { NamingStrategy ns = context.getNamingStrategy(); MetaEntity entity = new MetaEntity(); entity.setName(ns.tableToEntityName(table.getName().toLowerCase())); entity.setTitle(Strings.firstNotEmpty(table.getComment(), entity.getName())); entity.setTable(table.getName()); for(DbColumn column : table.getColumns()) { entity.addField(convert(context, table, column)); } // for(DbIndex idx : table.getIndexes()) { // entity.addTableIndex(convert(context, table, idx)); // } return entity; }
protected void resolveDoc(ApiMetadataContext context, MNamedWithDescBuilder o, Doc a) { String summary = Strings.firstNotEmpty(a.summary(), a.value()); if(Strings.isEmpty(o.getSummary()) && !Strings.isEmpty(summary)) { o.setSummary(docResolver.resolveDesc(summary)); } String desc = a.desc(); if(Strings.isEmpty(o.getDescription()) && !Strings.isEmpty(desc)) { o.setDescription(docResolver.resolveDesc(desc)); } } }
private void compileFilter(MetaQueryFilterSet qfs, MetaQueryFilter filter) { try { if (!Strings.isEmpty(filter.getCond())) { JsModule module = engine.createModule(Strings.firstNotEmpty(qfs.getSource(), qfs.getName()), filter.getCond()); module.compile(); filter.setCondExpr(new JsModuleExpr(module)); } String where = Strings.trim(filter.getWhere()); if (Strings.isEmpty(where)) { filter.setWhereExpr(new ValuedExpression(null)); } else { if (Strings.startsWithIgnoreCase(where, "eval:")) { String script = where.substring(5).trim(); JsModule module = engine.createModule(qfs.getSource(), script); module.compile(); filter.setWhereExpr(new JsModuleExpr(module)); } else { filter.setWhereExpr(new ValuedExpression<>(where)); } } } catch (ScriptException e) { throw new IllegalStateException("Error compile query filter", e); } } }
protected void mappingEntityByAnnotation(MetadataContext context, EntityMappingBuilder emb, Entity a){ if(null != a){ emb.setEntityName(Strings.firstNotEmpty(a.name(), a.value())); emb.setTableSchema(a.schema()); emb.setTableName(a.table()); emb.setTableNameDeclared(true); if(!Strings.isEmpty(a.secondaryTable())) { emb.setSecondaryTable(new DbTableBuilder(a.secondaryTable())); } mappingListenerByAnnotations(context, emb, a.listeners()); } }
static MetaField convert(DbConvertContext context, DbTable table, DbColumn column) { NamingStrategy ns = context.getNamingStrategy(); MetaField field = new MetaField(); field.setName(ns.columnToFieldName(column.getName().toLowerCase())); field.setTitle(Strings.firstNotEmpty(column.getComment(), field.getName())); field.setDescription(column.getComment()); field.setIdentity(column.isPrimaryKey()); field.setIncrement(column.isAutoIncrement()); field.setUnique(column.isUnique()); field.setRequired(!column.isNullable()); field.setColumn(column.getName()); field.setType(column.getTypeName()); field.setLength(column.getLength() >= 0 ? column.getLength() : null); field.setPrecision(column.getPrecision() >= 0 ? column.getPrecision() : null); field.setScale(column.getScale() >= 0 ? column.getScale() : null); field.setDefaults(column.getDefaultValue()); return field; }
@Override public void postAppInit(App app) throws Throwable { if(enabled) { if(!sc.config().isEnabled()) { sc.enable(true); } this.hasDataSources = dsm.hasDataSources(); if(hasDataSources) { String dataSourceName = Strings.firstNotEmpty(jdbcDataSourceName, DataSourceManager.DEFAULT_DATASOURCE_NAME); if(getTokenStore() instanceof JdbcStore) { ((JdbcStore) getTokenStore()).setDataSourceName(dataSourceName); } if(getCodeStore() instanceof JdbcStore) { ((JdbcStore)getCodeStore()).setDataSourceName(dataSourceName); } if(getClientStore() instanceof JdbcStore) { ((JdbcStore)getClientStore()).setDataSourceName(dataSourceName); } if(getSSOStore() instanceof JdbcStore) { ((JdbcStore) getSSOStore()).setDataSourceName(dataSourceName); } } if(isCleanupEnabled()) { schedulerManager .newFixedThreadPoolScheduler("auth-cleanup") .scheduleAtFixedRate(() -> cleanup(), getCleanupInterval() * 1000l); } } }
protected void writeParameter(WriteContext context, ApiMetadata m, JsonWriter w, MApiParameter p) { w.startObject(); w.property(NAME, p.getName()) .propertyOptional(SUMMARY, p.getSummary()) .property(DESCRIPTION, nullToEmpty(Strings.firstNotEmpty(p.getDescription(),p.getSummary(), p.getTitle()))) .property(IN, SwaggerMappings.in(p.getLocation())); w.propertyOptional(REQUIRED, p.getRequired()); writeExtension(w, p.getExtension()); try{ if(Location.BODY == p.getLocation()) { w.property(SCHEMA, () -> { w.startObject(); writeParameterType(context, m, w, p); w.endObject(); }); }else{ writeParameterType(context, m, w, p); } }catch(RuntimeException e) { throw e; } w.endObject(); }
protected void setSequenceProperties(MetadataContext context, EntityMappingBuilder emb,FieldMappingBuilder fmb,SequenceMappingBuilder seq){ Sequence a = fmb.getBeanProperty() != null ? fmb.getBeanProperty().getAnnotation(Sequence.class) : null; if(null != a){ seq.setName(Strings.firstNotEmpty(a.name(),a.value())); if(a.start() != Long.MAX_VALUE){ seq.setStart(a.start()); } if(a.increment() != Integer.MIN_VALUE){ seq.setIncrement(a.increment()); } if(a.cache() != Integer.MIN_VALUE){ seq.setCache(a.cache()); } } seq.setSchema(emb.getTableSchema()); if(Strings.isEmpty(seq.getName())){ seq.setName(context.getNamingStrategy().generateSequenceName(emb.getTableName(), fmb.getColumn().getName())); } }
w.property(DESCRIPTION, nullToEmpty(Strings.firstNotEmpty(r.getDescription(), r.getSummary())));