public MongoDBQueryDescriptor build()

in mongodb/src/main/java/org/hibernate/ogm/datastore/mongodb/query/parsing/nativequery/impl/MongoDBQueryDescriptorBuilder.java [185:307]


	public MongoDBQueryDescriptor build() throws NativeQueryParseException {
		if ( !isQueryValid ) {
			// the input is CLI query: it starts with 'db'
			if ( isCliQuery ) {
				// db ???
				if ( collection == null ) {
					throw new NativeQueryParseException( "Cli query should match the format db.collection.operation(<arguments>)" );
				}
				// db . collection ???
				if ( operation == null && operationName == null ) {
					throw new NativeQueryParseException( "Cli query should match the format db.collection.operation(<arguments>)" );
				}
				// db . collection . UNSUPPORTED
				if ( operation == null ) {
					throw new NativeQueryParseException( "Operation '" + operationName + "' is unsupported" );
				}
				// db . collection . operation ( ???
				if ( invalidJsonParameter != null ) {
					try {
						Document.parse( invalidJsonParameter );
					}
					catch (JsonParseException e ) {
						throw new NativeQueryParseException( e );
					}
				}
				// db . collection . operation ( ???
				if ( !areParametersValid ) {
					throw new NativeQueryParseException( "Parameters are invalid for " + operationName );
				}
				else {
					throw new NativeQueryParseException( "Missing ')'" );
				}
			}
			// the input is not a json object: might be error in { ... } or does not start with '{'
			try {
				Document.parse( invalidJsonParameter );
			}
			catch (JsonParseException e ) {
				throw new NativeQueryParseException( e );
			}
		}

		//@todo refactor the spaghetti!
		if ( operation != Operation.AGGREGATE_PIPELINE ) {
			MongoDBQueryDescriptor descriptor = null;

			if ( operation == Operation.INSERTMANY ) {
				// must be array
				@SuppressWarnings("unchecked")
				List<Document> documents = (List<Document>) parseAsObject( updateOrInsert );
				descriptor = new MongoDBQueryDescriptor(
						collection,
						operation,
						parse( criteria ),
						parse( projection ),
						parse( orderBy ),
						parse( options ),
						null,
						documents,
						null,
						null,
						null,
						null
				);
			}
			else if ( operation == Operation.INSERT ) {
				//can be document or array
				Object anyDocs = parseAsObject( updateOrInsert );
				if ( anyDocs instanceof List ) {
					//this is array
					descriptor = new MongoDBQueryDescriptor(
							collection,
							operation,
							parse( criteria ),
							parse( projection ),
							parse( orderBy ),
							parse( options ),
							null,
							(List<Document>) anyDocs,
							null,
							null,
							null,
							null
					);
				}
				else {
					//this is one document
					descriptor = new MongoDBQueryDescriptor(
							collection,
							operation,
							parse( criteria ),
							parse( projection ),
							parse( orderBy ),
							parse( options ),
							(Document) anyDocs,
							null,
							null,
							null,
							null,
							null
					);
				}
			}
			else {
				descriptor = new MongoDBQueryDescriptor(
						collection,
						operation,
						parse( criteria ),
						parse( projection ),
						parse( orderBy ),
						parse( options ),
						parse( updateOrInsert ),
						null,
						null,
						distinctFieldName,
						mapFunction,
						reduceFunction
				);
			}
			return descriptor;
		}
		return new MongoDBQueryDescriptor( collection, operation, pipeline );
	}