public IndexOption getOptionForIndex(String indexName) { if ( StringHelper.isNullOrEmptyString( indexName ) ) { return new IndexOption(); } if ( !indexOptions.containsKey( indexName ) ) { return new IndexOption( indexName ); } return indexOptions.get( indexName ); }
private Boolean getAsBoolean() { Object configuredValue = getConfiguredValue(); if ( StringHelper.isNullOrEmptyString( configuredValue ) ) { return getDefaultValue() != null ? (Boolean) getDefaultValue() : null; } return ( configuredValue instanceof Boolean ) ? (Boolean) configuredValue : Boolean.valueOf( configuredValue.toString().trim() ); }
/** * parse JSON * @param json * @return * @see <a href="http://stackoverflow.com/questions/34436952/json-parse-equivalent-in-mongo-driver-3-x-for-java"> JSON.parse equivalent</a> */ private static Object parseAsObject(String json) { if ( StringHelper.isNullOrEmptyString( json ) ) { return null; } Document object = Document.parse( "{ 'json': " + json + "}" ); return object.get( "json" ); }
private String getAsString() { String stringValue = StringHelper.isNullOrEmptyString( getConfiguredValue() ) ? null : getConfiguredValue().toString().trim(); return stringValue == null ? (String) getDefaultValue() : stringValue; }
/** * parse JSON * @param json * @return * @see <a href="http://stackoverflow.com/questions/34436952/json-parse-equivalent-in-mongo-driver-3-x-for-java"> JSON.parse equivalent</a> */ private static Object parseAsObject(String json) { if ( StringHelper.isNullOrEmptyString( json ) ) { return null; } Document object = Document.parse( "{ 'json': " + json + "}" ); return object.get( "json" ); }
private Long getAsLong() { Object configuredValue = getConfiguredValue(); if ( StringHelper.isNullOrEmptyString( configuredValue ) ) { return (Long) getDefaultValue(); } else if ( configuredValue instanceof Number ) { return ( (Number) configuredValue ).longValue(); } else { try { String stringValue = configuredValue.toString().trim(); return Long.valueOf( stringValue ); } catch (NumberFormatException e) { throw log.notALong( getPropertyName(), configuredValue.toString() ); } } }
private Integer getAsInteger() { Object configuredValue = getConfiguredValue(); if ( StringHelper.isNullOrEmptyString( configuredValue ) ) { return (Integer) getDefaultValue(); } else if ( configuredValue instanceof Number ) { return ( (Number) configuredValue ).intValue(); } else { try { String stringValue = configuredValue.toString().trim(); return Integer.valueOf( stringValue ); } catch (NumberFormatException e) { throw log.notAnInteger( getPropertyName(), configuredValue.toString() ); } } }
@SuppressWarnings("unchecked") private <E extends Enum<E>> E getAsEnum() { Object configuredValue = getConfiguredValue(); Class<? extends Object> targetType = getTargetType(); if ( StringHelper.isNullOrEmptyString( configuredValue ) ) { return (E) getDefaultValue(); } else if ( configuredValue.getClass() == targetType ) { E asEnum = (E) configuredValue; return asEnum; } else { try { String stringValue = configuredValue.toString().trim().toUpperCase( Locale.ENGLISH ); return Enum.valueOf( (Class<E>) targetType, stringValue ); } catch (IllegalArgumentException e) { throw log.unknownEnumerationValue( getPropertyName(), configuredValue.toString(), Arrays.toString( targetType.getEnumConstants() ) ); } } }
@Override public T getValue() { if ( isRequired && StringHelper.isNullOrEmptyString( configuredValue ) ) { throw log.missingConfigurationProperty( propertyName ); } T typedValue = getTypedValue(); for ( PropertyValidator<T> validator : validators ) { validator.validate( typedValue ); } return typedValue; }
private URL getAsUrl() { Object value = getConfiguredValue(); if ( StringHelper.isNullOrEmptyString( value ) && getDefaultValue() != null ) { return (URL) getDefaultValue(); } else if ( value instanceof URL ) { return (URL) value; } boolean useDefault = false; if ( StringHelper.isNullOrEmptyString( value ) && getDefaultStringValue() != null ) { value = getDefaultStringValue(); useDefault = true; } if ( value == null ) { return null; } String stringValue = value.toString().trim(); URL resource = getFromClassPath( stringValue ); if ( resource == null ) { resource = getFromStringUrl( stringValue ); } if ( resource == null ) { resource = getFromFileSystemPath( stringValue ); } // if the resource is explicitly defined by the user, it mustn't be null if ( resource == null && !useDefault ) { throw log.invalidConfigurationUrl( getPropertyName(), value.toString() ); } return resource; }
private boolean validateIndexSpec(MongoDBIndexSpec indexSpec) { boolean valid = true; if ( StringHelper.isNullOrEmptyString( indexSpec.getIndexName() ) ) { log.indexNameIsEmpty( indexSpec.getCollection() ); valid = false; } if ( indexSpec.getIndexKeysDocument().keySet().isEmpty() ) { log.noValidKeysForIndex( indexSpec.getCollection(), indexSpec.getIndexName() ); valid = false; } return valid; }
private boolean validateIndexSpec(MongoDBIndexSpec indexSpec) { boolean valid = true; if ( StringHelper.isNullOrEmptyString( indexSpec.getIndexName() ) ) { log.indexNameIsEmpty( indexSpec.getCollection() ); valid = false; } if ( indexSpec.getIndexKeysDocument().keySet().isEmpty() ) { log.noValidKeysForIndex( indexSpec.getCollection(), indexSpec.getIndexName() ); valid = false; } return valid; }
private Document getIndexOptionDocument(Table table, IndexOption indexOption) { try { Document options; if ( StringHelper.isNullOrEmptyString( indexOption.getOptions() ) ) { options = new Document(); } else { options = Document.parse( indexOption.getOptions() ); } options.put( "name", indexOption.getTargetIndexName() ); return options; } catch (Exception e) { throw log.invalidOptionsFormatForIndex( table.getName(), indexOption.getTargetIndexName(), e ); } }
private Document getIndexOptionDocument(Table table, IndexOption indexOption) { try { Document options; if ( StringHelper.isNullOrEmptyString( indexOption.getOptions() ) ) { options = new Document(); } else { options = Document.parse( indexOption.getOptions() ); } options.put( "name", indexOption.getTargetIndexName() ); return options; } catch (Exception e) { throw log.invalidOptionsFormatForIndex( table.getName(), indexOption.getTargetIndexName(), e ); } }
public void createIndex(MongoDatabase database, MongoDBIndexSpec indexSpec) { MongoCollection<Document> collection = database.getCollection( indexSpec.getCollection() ); Map<String, Document> preexistingIndexes = getIndexes( collection ); String preexistingTextIndex = getPreexistingTextIndex( preexistingIndexes ); // if a text index already exists in the collection, MongoDB silently ignores the creation of the new text index // so we might as well log a warning about it if ( MongoDBIndexType.TEXT.equals( indexSpec.getIndexType() ) && preexistingTextIndex != null && !preexistingTextIndex.equalsIgnoreCase( indexSpec.getIndexName() ) ) { throw log.unableToCreateTextIndex( collection.getNamespace().getCollectionName(), indexSpec.getIndexName(), preexistingTextIndex ); } try { // if the index is already present and with the same definition, MongoDB simply ignores the call // if the definition is not the same, MongoDB throws an error, except in the case of a text index // where it silently ignores the creation collection.createIndex( indexSpec.getIndexKeysDocument(), indexSpec.getOptions() ); } catch (MongoException e) { String indexName = indexSpec.getIndexName(); if ( e.getCode() == INDEX_CREATION_ERROR_CODE && !StringHelper.isNullOrEmptyString( indexName ) && preexistingIndexes.containsKey( indexName ) ) { // The index already exists with a different definition and has a name: we drop it and we recreate it collection.dropIndex( indexName ); collection.createIndex( indexSpec.getIndexKeysDocument(), indexSpec.getOptions() ); } else { throw log.unableToCreateIndex( collection.getNamespace().getCollectionName(), indexName, e ); } } }
public void createIndex(MongoDatabase database, MongoDBIndexSpec indexSpec) { MongoCollection<Document> collection = database.getCollection( indexSpec.getCollection() ); Map<String, Document> preexistingIndexes = getIndexes( collection ); String preexistingTextIndex = getPreexistingTextIndex( preexistingIndexes ); // if a text index already exists in the collection, MongoDB silently ignores the creation of the new text index // so we might as well log a warning about it if ( MongoDBIndexType.TEXT.equals( indexSpec.getIndexType() ) && preexistingTextIndex != null && !preexistingTextIndex.equalsIgnoreCase( indexSpec.getIndexName() ) ) { throw log.unableToCreateTextIndex( collection.getNamespace().getCollectionName(), indexSpec.getIndexName(), preexistingTextIndex ); } try { // if the index is already present and with the same definition, MongoDB simply ignores the call // if the definition is not the same, MongoDB throws an error, except in the case of a text index // where it silently ignores the creation collection.createIndex( indexSpec.getIndexKeysDocument(), indexSpec.getOptions() ); } catch (MongoException e) { String indexName = indexSpec.getIndexName(); if ( e.getCode() == INDEX_CREATION_ERROR_CODE && !StringHelper.isNullOrEmptyString( indexName ) && preexistingIndexes.containsKey( indexName ) ) { // The index already exists with a different definition and has a name: we drop it and we recreate it collection.dropIndex( indexName ); collection.createIndex( indexSpec.getIndexKeysDocument(), indexSpec.getOptions() ); } else { throw log.unableToCreateIndex( collection.getNamespace().getCollectionName(), indexName, e ); } } }