List of usage examples for org.apache.commons.lang StringUtils defaultIfBlank
public static String defaultIfBlank(String str, String defaultStr)
Returns either the passed in String, or if the String is whitespace, empty ("") or null
, the value of defaultStr
.
From source file:net.ymate.platform.persistence.jdbc.dialect.AbstractDialect.java
/** * @param fields ???/*from w ww .ja va 2s . co m*/ * @param suffix ???? * @param separator ?, ? * @return ?????separator */ protected String __doGenerateFieldsFormatStr(Fields fields, String suffix, String separator) { StringBuilder _fieldsSB = new StringBuilder(); Iterator<String> _fieldsIt = fields.fields().iterator(); suffix = StringUtils.defaultIfBlank(suffix, ""); separator = StringUtils.defaultIfBlank(separator, ", "); while (_fieldsIt.hasNext()) { _fieldsSB.append(this.wrapIdentifierQuote(_fieldsIt.next())).append(suffix); if (_fieldsIt.hasNext()) { _fieldsSB.append(separator); } } return _fieldsSB.toString(); }
From source file:net.ymate.platform.persistence.jdbc.dialect.AbstractDialect.java
public String buildTableName(String prefix, EntityMeta entityMeta, IShardingable shardingable) { String _entityName = entityMeta.getEntityName(); if (shardingable != null && entityMeta.getShardingRule() != null) { IShardingRule _rule = ClassUtils.impl(entityMeta.getShardingRule().value(), IShardingRule.class); if (_rule != null) { _entityName = _rule.getShardName(entityMeta.getEntityName(), shardingable.getShardingParam()); }// w w w . j av a 2 s.com } return this.wrapIdentifierQuote(StringUtils.defaultIfBlank(prefix, "").concat(_entityName)); }
From source file:net.ymate.platform.persistence.jdbc.impl.DefaultModuleCfg.java
public DefaultModuleCfg(YMP owner) throws Exception { Map<String, String> _moduleCfgs = owner.getConfig().getModuleConfigs(IDatabase.MODULE_NAME); ///*from w ww .j a va 2 s. c o m*/ this.dataSourceDefaultName = StringUtils.defaultIfBlank(_moduleCfgs.get("ds_default_name"), "default"); // this.dataSourceCfgMetas = new HashMap<String, DataSourceCfgMeta>(); String _dsNameStr = StringUtils.defaultIfBlank(_moduleCfgs.get("ds_name_list"), "default"); if (StringUtils.contains(_dsNameStr, this.dataSourceDefaultName)) { String[] _dsNameList = StringUtils.split(_dsNameStr, "|"); for (String _dsName : _dsNameList) { DataSourceCfgMeta _meta = __doParserDataSourceCfgMeta(_dsName, _moduleCfgs); if (_meta != null) { this.dataSourceCfgMetas.put(_dsName, _meta); } } } else { throw new IllegalArgumentException("The default datasource name does not match"); } }
From source file:net.ymate.platform.persistence.jdbc.impl.DefaultModuleCfg.java
/** * @param dsName ????/*from w ww . j a v a2 s . com*/ * @param _moduleCfgs ??? * @return ????? * @throws Exception ? */ @SuppressWarnings("unchecked") protected DataSourceCfgMeta __doParserDataSourceCfgMeta(String dsName, Map<String, String> _moduleCfgs) throws Exception { Map<String, String> _dataSourceCfgs = new HashMap<String, String>(); for (Map.Entry<String, String> _cfgEntry : _moduleCfgs.entrySet()) { String _key = _cfgEntry.getKey(); String _prefix = "ds." + dsName + "."; if (StringUtils.startsWith(_key, _prefix)) { String _cfgKey = StringUtils.substring(_key, _prefix.length()); _dataSourceCfgs.put(_cfgKey, _cfgEntry.getValue()); } } if (!_dataSourceCfgs.isEmpty()) { // DataSourceCfgMeta _meta = new DataSourceCfgMeta(); _meta.setName(dsName); _meta.setConnectionUrl(_dataSourceCfgs.get("connection_url")); _meta.setUsername(_dataSourceCfgs.get("username")); // ?? if (StringUtils.isNotBlank(_meta.getConnectionUrl()) && StringUtils.isNotBlank(_meta.getUsername())) { // ? _meta.setIsShowSQL(new BlurObject(_dataSourceCfgs.get("show_sql")).toBooleanValue()); _meta.setTablePrefix(_dataSourceCfgs.get("table_prefix")); // ??? String _adapterClassName = JDBC.DS_ADAPTERS .get(StringUtils.defaultIfBlank(_dataSourceCfgs.get("adapter_class"), "default")); _meta.setAdapterClass((Class<? extends IDataSourceAdapter>) ClassUtils.loadClass(_adapterClassName, this.getClass())); // // ? try { _meta.setType(JDBC.DATABASE .valueOf(StringUtils.defaultIfBlank(_dataSourceCfgs.get("type"), "").toUpperCase())); } catch (IllegalArgumentException e) { // ?? String _connUrl = URI.create(_meta.getConnectionUrl()).toString(); String[] _type = StringUtils.split(_connUrl, ":"); if (_type != null && _type.length > 0) { if (_type[1].equals("microsoft")) { _type[1] = "sqlserver"; } _meta.setType(JDBC.DATABASE.valueOf(_type[1].toUpperCase())); } } // _meta.setDialectClass(_dataSourceCfgs.get("dialect_class")); _meta.setDriverClass(StringUtils.defaultIfBlank(_dataSourceCfgs.get("driver_class"), JDBC.DB_DRIVERS.get(_meta.getType()))); _meta.setPassword(_dataSourceCfgs.get("password")); _meta.setIsPasswordEncrypted( new BlurObject(_dataSourceCfgs.get("password_encrypted")).toBooleanValue()); // if (_meta.isPasswordEncrypted() && StringUtils.isNotBlank(_meta.getPassword()) && StringUtils.isNotBlank(_dataSourceCfgs.get("password_class"))) { _meta.setPasswordClass((Class<? extends IPasswordProcessor>) ClassUtils .loadClass(_dataSourceCfgs.get("password_class"), this.getClass())); } // return _meta; } } return null; }
From source file:net.ymate.platform.persistence.jdbc.query.Insert.java
public static Insert create(String prefix, Class<? extends IEntity> entityClass) { return new Insert(StringUtils.defaultIfBlank(prefix, "") .concat(EntityMeta.createAndGet(entityClass).getEntityName())); }
From source file:net.ymate.platform.persistence.jdbc.scaffold.EntityGenerator.java
/** * ??/*from w ww . j a v a 2 s .co m*/ */ public void createEntityClassFiles() { Map<String, Object> _propMap = buildPropMap(); // boolean _isUseBaseEntity = BlurObject.bind(__owner.getConfig().getParam("jdbc.use_base_entity")) .toBooleanValue(); boolean _isUseClassSuffix = BlurObject.bind(__owner.getConfig().getParam("jdbc.use_class_suffix")) .toBooleanValue(); boolean _isUseChainMode = BlurObject.bind(__owner.getConfig().getParam("jdbc.use_chain_mode")) .toBooleanValue(); boolean _isUseStateSupport = BlurObject.bind(__owner.getConfig().getParam("jdbc.use_state_support")) .toBooleanValue(); _propMap.put("isUseBaseEntity", _isUseBaseEntity); _propMap.put("isUseClassSuffix", _isUseClassSuffix); _propMap.put("isUseChainMode", _isUseChainMode); _propMap.put("isUseStateSupport", _isUseStateSupport); if (_isUseBaseEntity) { buildTargetFile("/model/BaseEntity.java", "/BaseEntity.ftl", _propMap); } // List<String> _tableList = Arrays.asList(StringUtils .split(StringUtils.defaultIfBlank(__owner.getConfig().getParam("jdbc.table_list"), ""), "|")); if (_tableList.isEmpty()) { _tableList = getTableNames(); } // String _dbName = __owner.getConfig().getParam("jdbc.db_name"); String _dbUser = __owner.getConfig().getParam("jdbc.db_username"); String[] _prefixs = StringUtils .split(StringUtils.defaultIfBlank(__owner.getConfig().getParam("jdbc.table_prefix"), ""), '|'); boolean _isRemovePrefix = new BlurObject(__owner.getConfig().getParam("jdbc.remove_table_prefix")) .toBooleanValue(); List<String> _tableExcludeList = Arrays.asList(StringUtils.split(StringUtils .defaultIfBlank(__owner.getConfig().getParam("jdbc.table_exclude_list"), "").toLowerCase(), "|")); for (String _tableName : _tableList) { // ??? if (!_tableExcludeList.isEmpty()) { if (_tableExcludeList.contains(_tableName.toLowerCase())) { continue; } else { boolean _flag = false; for (String _excludedName : _tableExcludeList) { if (StringUtils.contains(_excludedName, "*") && StringUtils.startsWithIgnoreCase(_tableName, StringUtils.substringBefore(_excludedName, "*"))) { _flag = true; break; } } if (_flag) { continue; } } } TableMeta _tableMeta = getTableMeta(_dbName, _dbUser, _tableName); if (_tableMeta != null) { String _modelName = null; for (String _prefix : _prefixs) { if (_tableName.startsWith(_prefix)) { if (_isRemovePrefix) { _tableName = _tableName.substring(_prefix.length()); } _modelName = StringUtils.capitalize(EntityMeta.propertyNameToFieldName(_tableName)); break; } } if (StringUtils.isBlank(_modelName)) { _modelName = StringUtils.capitalize(EntityMeta.propertyNameToFieldName(_tableName)); } // _propMap.put("tableName", _tableName); _propMap.put("modelName", _modelName); List<Attr> _fieldList = new ArrayList<Attr>(); // List<Attr> _fieldListForNotNullable = new ArrayList<Attr>(); // ? List<Attr> _allFieldList = new ArrayList<Attr>(); // ???? if (_tableMeta.getPkSet().size() > 1) { _propMap.put("primaryKeyType", _modelName + "PK"); _propMap.put("primaryKeyName", StringUtils.uncapitalize((String) _propMap.get("primaryKeyType"))); List<Attr> _primaryKeyList = new ArrayList<Attr>(); _propMap.put("primaryKeyList", _primaryKeyList); Attr _pkAttr = new Attr((String) _propMap.get("primaryKeyType"), (String) _propMap.get("primaryKeyName"), null, false, false, 0, 0, 0, null, null); _fieldList.add(_pkAttr); _fieldListForNotNullable.add(_pkAttr); // for (String pkey : _tableMeta.getPkSet()) { ColumnInfo _ci = _tableMeta.getFieldMap().get(pkey); _primaryKeyList.add(_ci.toAttr()); _allFieldList.add(new Attr("String", _ci.getColumnName().toUpperCase(), _ci.getColumnName(), _ci.isAutoIncrement(), _ci.isSigned(), _ci.getPrecision(), _ci.getScale(), _ci.getNullable(), _ci.getDefaultValue(), _ci.getRemarks())); } for (String key : _tableMeta.getFieldMap().keySet()) { if (_tableMeta.getPkSet().contains(key)) { continue; } ColumnInfo _ci = _tableMeta.getFieldMap().get(key); Attr _attr = _ci.toAttr(); _fieldList.add(_attr); _fieldListForNotNullable.add(_attr); _allFieldList.add(new Attr("String", _ci.getColumnName().toUpperCase(), _ci.getColumnName(), _ci.isAutoIncrement(), _ci.isSigned(), _ci.getPrecision(), _ci.getScale(), _ci.getNullable(), _ci.getDefaultValue(), _ci.getRemarks())); } } else { _propMap.put("primaryKeyType", _tableMeta.getFieldMap().get(_tableMeta.getPkSet().get(0)).getColumnType()); _propMap.put("primaryKeyName", StringUtils .uncapitalize(EntityMeta.propertyNameToFieldName(_tableMeta.getPkSet().get(0)))); for (String key : _tableMeta.getFieldMap().keySet()) { ColumnInfo _ci = _tableMeta.getFieldMap().get(key); Attr _attr = _ci.toAttr(); _fieldList.add(_attr); if (_attr.getNullable() == 0) { _fieldListForNotNullable.add(_attr); } _allFieldList.add(new Attr("String", _ci.getColumnName().toUpperCase(), _ci.getColumnName(), _ci.isAutoIncrement(), _ci.isSigned(), _ci.getPrecision(), _ci.getScale(), _ci.getNullable(), _ci.getDefaultValue(), _ci.getRemarks())); } } _propMap.put("fieldList", _fieldList); // ?????? _propMap.put("notNullableFieldList", _fieldList.size() == _fieldListForNotNullable.size() ? Collections.emptyList() : _fieldListForNotNullable); _propMap.put("allFieldList", _allFieldList); // buildTargetFile("/model/" + _modelName + (_isUseClassSuffix ? "Model.java" : ".java"), "/Entity.ftl", _propMap); // if (_tableMeta.getPkSet().size() > 1) { _propMap.put("modelName", _modelName); if (_tableMeta.getPkSet().size() > 1) { List<Attr> _primaryKeyList = new ArrayList<Attr>(); _propMap.put("primaryKeyList", _primaryKeyList); // for (String pkey : _tableMeta.getPkSet()) { ColumnInfo _ci = _tableMeta.getFieldMap().get(pkey); _primaryKeyList.add(_ci.toAttr()); } } buildTargetFile("/model/" + _modelName + "PK.java", "/EntityPK.ftl", _propMap); } } } }
From source file:net.ymate.platform.persistence.jdbc.scaffold.EntityGenerator.java
private void buildTargetFile(String targetFileName, String tmplFile, Map<String, Object> propMap) { Writer _outWriter = null;/* w w w . j a v a2 s . c o m*/ try { File _outputFile = new File( RuntimeUtils.replaceEnvVariable(StringUtils .defaultIfBlank(__owner.getConfig().getParam("jdbc.output_path"), "${root}")), new File(((String) propMap.get("packageName")).replace('.', '/'), targetFileName).getPath()); File _path = _outputFile.getParentFile(); if (!_path.exists()) { _path.mkdirs(); } Template _template = __freemarkerConfig.getTemplate(__templateRootPath + tmplFile); _outWriter = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(_outputFile), StringUtils.defaultIfEmpty( __freemarkerConfig.getOutputEncoding(), __freemarkerConfig.getDefaultEncoding()))); _template.process(propMap, _outWriter); System.out.println("Output file \"" + _outputFile + "\"."); } catch (Exception e) { _LOG.warn("", e); } finally { if (_outWriter != null) { try { _outWriter.flush(); _outWriter.close(); } catch (IOException e) { _LOG.warn("", e); } } } }
From source file:net.ymate.platform.persistence.jdbc.scaffold.EntityGenerator.java
private Map<String, Object> buildPropMap() { Map<String, Object> _propMap = new HashMap<String, Object>(); _propMap.put("packageName", StringUtils.defaultIfBlank(__owner.getConfig().getParam("jdbc.package_name"), "packages")); _propMap.put("lastUpdateTime", new Date()); return _propMap; }
From source file:net.ymate.platform.persistence.mongodb.impl.MongoGridFSSession.java
public MongoGridFSSession(IMongoDataSourceAdapter dataSourceAdapter, String bucketName) throws Exception { this.__id = UUIDUtils.UUID(); this.__dataSourceHolder = dataSourceAdapter; this.__bucketName = StringUtils.defaultIfBlank(bucketName, GridFS.DEFAULT_BUCKET); ///* w ww . j a v a 2 s.co m*/ __gridFS = new GridFS(new DB(dataSourceAdapter.getMongoClient(), dataSourceAdapter.getDataSourceCfgMeta().getDatabaseName()), __bucketName); __dbCollection = __gridFS.getDB().getCollection(__bucketName.concat(".files")); }
From source file:net.ymate.platform.persistence.mongodb.impl.MongoModuleCfg.java
public MongoModuleCfg(YMP owner) throws Exception { Map<String, String> _moduleCfgs = owner.getConfig().getModuleConfigs(IMongo.MODULE_NAME); ////from w ww . j av a 2 s .c om this.dataSourceDefaultName = StringUtils.defaultIfBlank(_moduleCfgs.get("ds_default_name"), "default"); this.clientOptionsHandler = ClassUtils.impl(_moduleCfgs.get("ds_options_handler_class"), IMongoClientOptionsHandler.class, this.getClass()); // this.dataSourceCfgMetas = new HashMap<String, MongoDataSourceCfgMeta>(); String _dsNameStr = StringUtils.defaultIfBlank(_moduleCfgs.get("ds_name_list"), "default"); if (StringUtils.contains(_dsNameStr, this.dataSourceDefaultName)) { String[] _dsNameList = StringUtils.split(_dsNameStr, "|"); for (String _dsName : _dsNameList) { MongoDataSourceCfgMeta _meta = __doParserDataSourceCfgMeta(_dsName, _moduleCfgs); if (_meta != null) { this.dataSourceCfgMetas.put(_dsName, _meta); } } } else { throw new IllegalArgumentException("The default datasource name does not match"); } }