11/*
2- * Copyright 2014 the original author or authors.
2+ * Copyright 2014-2015 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2727
2828import org .bson .types .ObjectId ;
2929import org .springframework .dao .DataAccessException ;
30- import org .springframework .data .mongodb .core .script .CallableMongoScript ;
31- import org .springframework .data .mongodb .core .script .ServerSideJavaScript ;
30+ import org .springframework .data .mongodb .core .script .ExecutableMongoScript ;
31+ import org .springframework .data .mongodb .core .script .NamedMongoScript ;
3232import org .springframework .util .Assert ;
3333import org .springframework .util .CollectionUtils ;
3434import org .springframework .util .ObjectUtils ;
4141 * Default implementation of {@link ScriptOperations} capable of saving and executing {@link ServerSideJavaScript}.
4242 *
4343 * @author Christoph Strobl
44+ * @author Oliver Gierke
4445 * @since 1.7
4546 */
46- public class DefaultScriptOperations implements ScriptOperations {
47+ class DefaultScriptOperations implements ScriptOperations {
4748
4849 private static final String SCRIPT_COLLECTION_NAME = "system.js" ;
4950 private static final String SCRIPT_NAME_PREFIX = "func_" ;
51+
5052 private final MongoOperations mongoOperations ;
5153
5254 /**
@@ -63,39 +65,39 @@ public DefaultScriptOperations(MongoOperations mongoOperations) {
6365
6466 /*
6567 * (non-Javadoc)
66- * @see org.springframework.data.mongodb.core.ScriptOperations#save(org.springframework.data.mongodb.core.script.MongoScript)
68+ * @see org.springframework.data.mongodb.core.ScriptOperations#register(org.springframework.data.mongodb.core.script.ExecutableMongoScript)
69+ */
70+ @ Override
71+ public NamedMongoScript register (ExecutableMongoScript script ) {
72+ return register (new NamedMongoScript (generateScriptName (), script ));
73+ }
74+
75+ /*
76+ * (non-Javadoc)
77+ * @see org.springframework.data.mongodb.core.ScriptOperations#register(org.springframework.data.mongodb.core.script.NamedMongoScript)
6778 */
6879 @ Override
69- public CallableMongoScript register (ServerSideJavaScript script ) {
80+ public NamedMongoScript register (NamedMongoScript script ) {
7081
7182 Assert .notNull (script , "Script must not be null!" );
7283
73- CallableMongoScript callableScript = (script instanceof CallableMongoScript ) ? (CallableMongoScript ) script
74- : new CallableMongoScript (generateScriptName (), script );
75- mongoOperations .save (callableScript , SCRIPT_COLLECTION_NAME );
76- return callableScript ;
84+ mongoOperations .save (script , SCRIPT_COLLECTION_NAME );
85+ return script ;
7786 }
7887
7988 /*
8089 * (non-Javadoc)
81- * @see org.springframework.data.mongodb.core.ScriptOperations#execute(org.springframework.data.mongodb.core.script.MongoScript , java.lang.Object[])
90+ * @see org.springframework.data.mongodb.core.ScriptOperations#execute(org.springframework.data.mongodb.core.script.ExecutableMongoScript , java.lang.Object[])
8291 */
8392 @ Override
84- public Object execute (final ServerSideJavaScript script , final Object ... args ) {
93+ public Object execute (final ExecutableMongoScript script , final Object ... args ) {
8594
8695 Assert .notNull (script , "Script must not be null!" );
8796
88- if (script instanceof CallableMongoScript ) {
89- return call (((CallableMongoScript ) script ).getName (), args );
90- }
91-
9297 return mongoOperations .execute (new DbCallback <Object >() {
9398
9499 @ Override
95100 public Object doInDB (DB db ) throws MongoException , DataAccessException {
96-
97- Assert .notNull (script .getCode (), "Script.code must not be null!" );
98-
99101 return db .eval (script .getCode (), convertScriptArgs (args ));
100102 }
101103 });
@@ -114,9 +116,7 @@ public Object call(final String scriptName, final Object... args) {
114116
115117 @ Override
116118 public Object doInDB (DB db ) throws MongoException , DataAccessException {
117-
118- String evalString = scriptName + "(" + convertAndJoinScriptArgs (args ) + ")" ;
119- return db .eval (evalString );
119+ return db .eval (String .format ("%s(%s)" , scriptName , convertAndJoinScriptArgs (args )));
120120 }
121121 });
122122 }
@@ -126,43 +126,33 @@ public Object doInDB(DB db) throws MongoException, DataAccessException {
126126 * @see org.springframework.data.mongodb.core.ScriptOperations#exists(java.lang.String)
127127 */
128128 @ Override
129- public Boolean exists (String scriptName ) {
129+ public boolean exists (String scriptName ) {
130130
131131 Assert .hasText (scriptName , "ScriptName must not be null or empty!" );
132132
133- return mongoOperations .exists (query (where ("name" ).is (scriptName )), CallableMongoScript .class ,
134- SCRIPT_COLLECTION_NAME );
133+ return mongoOperations .exists (query (where ("name" ).is (scriptName )), NamedMongoScript .class , SCRIPT_COLLECTION_NAME );
135134 }
136135
137136 /*
138137 * (non-Javadoc)
139- * @see org.springframework.data.mongodb.core.ScriptOperations#scriptNames ()
138+ * @see org.springframework.data.mongodb.core.ScriptOperations#getScriptNames ()
140139 */
141140 @ Override
142- public Set <String > scriptNames () {
141+ public Set <String > getScriptNames () {
143142
144- List <CallableMongoScript > scripts = ( mongoOperations .findAll (CallableMongoScript .class , SCRIPT_COLLECTION_NAME ) );
143+ List <NamedMongoScript > scripts = mongoOperations .findAll (NamedMongoScript .class , SCRIPT_COLLECTION_NAME );
145144
146145 if (CollectionUtils .isEmpty (scripts )) {
147146 return Collections .emptySet ();
148147 }
149148
150149 Set <String > scriptNames = new HashSet <String >();
151- for (CallableMongoScript script : scripts ) {
150+
151+ for (NamedMongoScript script : scripts ) {
152152 scriptNames .add (script .getName ());
153153 }
154- return scriptNames ;
155- }
156154
157- /**
158- * Generate a valid name for the {@literal JavaScript}. MongoDB requires an id of type String for scripts. Calling
159- * scripts having {@link ObjectId} as id fails. Therefore we create a random UUID without {@code -} (as this won't
160- * work) an prefix the result with {@link #SCRIPT_NAME_PREFIX}.
161- *
162- * @return
163- */
164- private String generateScriptName () {
165- return SCRIPT_NAME_PREFIX + randomUUID ().toString ().replaceAll ("-" , "" );
155+ return scriptNames ;
166156 }
167157
168158 private Object [] convertScriptArgs (Object ... args ) {
@@ -172,23 +162,27 @@ private Object[] convertScriptArgs(Object... args) {
172162 }
173163
174164 List <Object > convertedValues = new ArrayList <Object >(args .length );
165+
175166 for (Object arg : args ) {
176- if (arg instanceof String ) {
177- convertedValues .add ("'" + arg + "'" );
178- } else {
179- convertedValues .add (this .mongoOperations .getConverter ().convertToMongoType (arg ));
180- }
167+ convertedValues .add (arg instanceof String ? String .format ("'%s'" , arg ) : this .mongoOperations .getConverter ()
168+ .convertToMongoType (arg ));
181169 }
170+
182171 return convertedValues .toArray ();
183172 }
184173
185174 private String convertAndJoinScriptArgs (Object ... args ) {
186-
187- if (ObjectUtils .isEmpty (args )) {
188- return "" ;
189- }
190-
191- return StringUtils .arrayToCommaDelimitedString (convertScriptArgs (args ));
175+ return ObjectUtils .isEmpty (args ) ? "" : StringUtils .arrayToCommaDelimitedString (convertScriptArgs (args ));
192176 }
193177
178+ /**
179+ * Generate a valid name for the {@literal JavaScript}. MongoDB requires an id of type String for scripts. Calling
180+ * scripts having {@link ObjectId} as id fails. Therefore we create a random UUID without {@code -} (as this won't
181+ * work) an prefix the result with {@link #SCRIPT_NAME_PREFIX}.
182+ *
183+ * @return
184+ */
185+ private static String generateScriptName () {
186+ return SCRIPT_NAME_PREFIX + randomUUID ().toString ().replaceAll ("-" , "" );
187+ }
194188}
0 commit comments