Example usage for com.mongodb.client.model CollationCaseFirst fromString

List of usage examples for com.mongodb.client.model CollationCaseFirst fromString

Introduction

In this page you can find the example usage for com.mongodb.client.model CollationCaseFirst fromString.

Prototype

public static CollationCaseFirst fromString(final String collationCaseFirst) 

Source Link

Document

Returns the CollationCaseFirst from the string value.

Usage

From source file:org.springframework.data.mongodb.core.Collation.java

License:Apache License

private static Converter<Collation, com.mongodb.client.model.Collation> toMongoCollationConverter() {

    return source -> {

        Builder builder = com.mongodb.client.model.Collation.builder();

        builder.locale(source.locale.asString());

        source.strength.ifPresent(strength -> {

            builder.collationStrength(CollationStrength.fromInt(strength.getLevel()));

            strength.getCaseLevel().ifPresent(builder::caseLevel);
            strength.getCaseFirst()//  w ww  .j  a  v a  2s. c  o m
                    .ifPresent(it -> builder.collationCaseFirst(CollationCaseFirst.fromString(it.state)));
        });

        source.numericOrdering.ifPresent(builder::numericOrdering);
        source.alternate.ifPresent(it -> {

            builder.collationAlternate(CollationAlternate.fromString(it.alternate));
            it.maxVariable.ifPresent(
                    maxVariable -> builder.collationMaxVariable(CollationMaxVariable.fromString(maxVariable)));
        });

        source.backwards.ifPresent(builder::backwards);
        source.normalization.ifPresent(builder::normalization);

        return builder.build();
    };
}

From source file:org.wso2.extension.siddhi.store.mongodb.util.MongoTableUtils.java

License:Open Source License

/**
 * Utility method which can be used to create an IndexModel.
 *
 * @param fieldName   the attribute on which the index is to be created.
 * @param sortOrder   the sort order of the index to be created.
 * @param indexOption json string of the options of the index to be created.
 * @return IndexModel.//from w  w w . j a v  a 2s.  c o m
 */
private static IndexModel createIndexModel(String fieldName, Integer sortOrder, String indexOption) {
    Document indexDocument = new Document(fieldName, sortOrder);
    if (indexOption == null) {
        return new IndexModel(indexDocument);
    } else {
        IndexOptions indexOptions = new IndexOptions();
        Document indexOptionDocument;
        try {
            indexOptionDocument = Document.parse(indexOption);
            for (Map.Entry<String, Object> indexEntry : indexOptionDocument.entrySet()) {
                Object value = indexEntry.getValue();
                switch (indexEntry.getKey()) {
                case "unique":
                    indexOptions.unique(Boolean.parseBoolean(value.toString()));
                    break;
                case "background":
                    indexOptions.background(Boolean.parseBoolean(value.toString()));
                    break;
                case "name":
                    indexOptions.name(value.toString());
                    break;
                case "sparse":
                    indexOptions.sparse(Boolean.parseBoolean(value.toString()));
                    break;
                case "expireAfterSeconds":
                    indexOptions.expireAfter(Long.parseLong(value.toString()), TimeUnit.SECONDS);
                    break;
                case "version":
                    indexOptions.version(Integer.parseInt(value.toString()));
                    break;
                case "weights":
                    indexOptions.weights((Bson) value);
                    break;
                case "languageOverride":
                    indexOptions.languageOverride(value.toString());
                    break;
                case "defaultLanguage":
                    indexOptions.defaultLanguage(value.toString());
                    break;
                case "textVersion":
                    indexOptions.textVersion(Integer.parseInt(value.toString()));
                    break;
                case "sphereVersion":
                    indexOptions.sphereVersion(Integer.parseInt(value.toString()));
                    break;
                case "bits":
                    indexOptions.bits(Integer.parseInt(value.toString()));
                    break;
                case "min":
                    indexOptions.min(Double.parseDouble(value.toString()));
                    break;
                case "max":
                    indexOptions.max(Double.parseDouble(value.toString()));
                    break;
                case "bucketSize":
                    indexOptions.bucketSize(Double.parseDouble(value.toString()));
                    break;
                case "partialFilterExpression":
                    indexOptions.partialFilterExpression((Bson) value);
                    break;
                case "collation":
                    DBObject collationOptions = (DBObject) value;
                    Collation.Builder builder = Collation.builder();
                    for (String collationKey : collationOptions.keySet()) {
                        String collationObj = value.toString();
                        switch (collationKey) {
                        case "locale":
                            builder.locale(collationObj);
                            break;
                        case "caseLevel":
                            builder.caseLevel(Boolean.parseBoolean(collationObj));
                            break;
                        case "caseFirst":
                            builder.collationCaseFirst(CollationCaseFirst.fromString(collationObj));
                            break;
                        case "strength":
                            builder.collationStrength(CollationStrength.valueOf(collationObj));
                            break;
                        case "numericOrdering":
                            builder.numericOrdering(Boolean.parseBoolean(collationObj));
                            break;
                        case "normalization":
                            builder.normalization(Boolean.parseBoolean(collationObj));
                            break;
                        case "backwards":
                            builder.backwards(Boolean.parseBoolean(collationObj));
                            break;
                        case "alternate":
                            builder.collationAlternate(CollationAlternate.fromString(collationObj));
                            break;
                        case "maxVariable":
                            builder.collationMaxVariable(CollationMaxVariable.fromString(collationObj));
                            break;
                        default:
                            log.warn("Annotation 'IndexBy' for the field '" + fieldName + "' contains "
                                    + "unknown 'Collation' Option key : '" + collationKey + "'. Please "
                                    + "check your query and try again.");
                            break;
                        }
                    }
                    if (builder.build().getLocale() != null) {
                        indexOptions.collation(builder.build());
                    } else {
                        throw new MongoTableException("Annotation 'IndexBy' for the field '" + fieldName + "'"
                                + " do not contain option for locale. Please check your query and try again.");
                    }
                    break;
                case "storageEngine":
                    indexOptions.storageEngine((Bson) value);
                    break;
                default:
                    log.warn("Annotation 'IndexBy' for the field '" + fieldName + "' contains unknown option "
                            + "key : '" + indexEntry.getKey() + "'. Please check your query and try again.");
                    break;
                }
            }
        } catch (JsonParseException | NumberFormatException e) {
            throw new MongoTableException("Annotation 'IndexBy' for the field '" + fieldName + "' contains "
                    + "illegal value(s) for index option. Please check your query and try again.", e);
        }
        return new IndexModel(indexDocument, indexOptions);
    }
}