19
19
import static org .springframework .data .mongodb .core .query .Criteria .*;
20
20
import static org .springframework .data .mongodb .core .query .Query .*;
21
21
22
- import java .io .Serializable ;
23
22
import java .util .ArrayList ;
23
+ import java .util .Collections ;
24
+ import java .util .HashSet ;
24
25
import java .util .List ;
26
+ import java .util .Set ;
25
27
28
+ import org .bson .types .ObjectId ;
26
29
import org .springframework .dao .DataAccessException ;
27
30
import org .springframework .data .mongodb .core .script .CallableMongoScript ;
28
- import org .springframework .data .mongodb .core .script .MongoScript ;
31
+ import org .springframework .data .mongodb .core .script .ServerSideJavaScript ;
29
32
import org .springframework .util .Assert ;
33
+ import org .springframework .util .CollectionUtils ;
30
34
import org .springframework .util .ObjectUtils ;
31
35
import org .springframework .util .StringUtils ;
32
36
33
37
import com .mongodb .DB ;
34
38
import com .mongodb .MongoException ;
35
39
36
40
/**
37
- * Default implementation of {@link ScriptOperations} capable of saving and executing simple {@link MongoScript }.
41
+ * Default implementation of {@link ScriptOperations} capable of saving and executing {@link ServerSideJavaScript }.
38
42
*
39
43
* @author Christoph Strobl
40
44
* @since 1.7
@@ -53,6 +57,7 @@ public class DefaultScriptOperations implements ScriptOperations {
53
57
public DefaultScriptOperations (MongoOperations mongoOperations ) {
54
58
55
59
Assert .notNull (mongoOperations , "MongoOperations must not be null!" );
60
+
56
61
this .mongoOperations = mongoOperations ;
57
62
}
58
63
@@ -61,17 +66,12 @@ public DefaultScriptOperations(MongoOperations mongoOperations) {
61
66
* @see org.springframework.data.mongodb.core.ScriptOperations#save(org.springframework.data.mongodb.core.script.MongoScript)
62
67
*/
63
68
@ Override
64
- public CallableMongoScript save ( MongoScript script ) {
69
+ public CallableMongoScript register ( ServerSideJavaScript script ) {
65
70
66
71
Assert .notNull (script , "Script must not be null!" );
67
- CallableMongoScript callableScript = null ;
68
-
69
- if (script instanceof CallableMongoScript ) {
70
- callableScript = (CallableMongoScript ) script ;
71
- } else {
72
- callableScript = new CallableMongoScript (generateScriptName (), script );
73
- }
74
72
73
+ CallableMongoScript callableScript = (script instanceof CallableMongoScript ) ? (CallableMongoScript ) script
74
+ : new CallableMongoScript (generateScriptName (), script );
75
75
mongoOperations .save (callableScript , SCRIPT_COLLECTION_NAME );
76
76
return callableScript ;
77
77
}
@@ -81,41 +81,83 @@ public CallableMongoScript save(MongoScript script) {
81
81
* @see org.springframework.data.mongodb.core.ScriptOperations#execute(org.springframework.data.mongodb.core.script.MongoScript, java.lang.Object[])
82
82
*/
83
83
@ Override
84
- public Object execute (final MongoScript script , final Object ... args ) {
84
+ public Object execute (final ServerSideJavaScript script , final Object ... args ) {
85
85
86
86
Assert .notNull (script , "Script must not be null!" );
87
+
88
+ if (script instanceof CallableMongoScript ) {
89
+ return call (((CallableMongoScript ) script ).getName (), args );
90
+ }
91
+
87
92
return mongoOperations .execute (new DbCallback <Object >() {
88
93
89
94
@ Override
90
95
public Object doInDB (DB db ) throws MongoException , DataAccessException {
91
96
92
- if (script instanceof CallableMongoScript ) {
97
+ Assert .notNull (script .getCode (), "Script.code must not be null!" );
98
+
99
+ return db .eval (script .getCode (), convertScriptArgs (args ));
100
+ }
101
+ });
102
+ }
93
103
94
- String evalString = ((CallableMongoScript ) script ).getName () + "(" + convertAndJoinScriptArgs (args ) + ")" ;
95
- return db .eval (evalString );
96
- }
104
+ /*
105
+ * (non-Javadoc)
106
+ * @see org.springframework.data.mongodb.core.ScriptOperations#call(java.lang.String, java.lang.Object[])
107
+ */
108
+ @ Override
109
+ public Object call (final String scriptName , final Object ... args ) {
97
110
98
- Assert .notNull (script .getCode (), "Script.code must not be null!" );
99
- return db .eval (script .getCode ().toString (), convertScriptArgs (args ));
111
+ Assert .hasText (scriptName , "ScriptName must not be null or empty!" );
112
+
113
+ return mongoOperations .execute (new DbCallback <Object >() {
114
+
115
+ @ Override
116
+ public Object doInDB (DB db ) throws MongoException , DataAccessException {
117
+
118
+ String evalString = scriptName + "(" + convertAndJoinScriptArgs (args ) + ")" ;
119
+ return db .eval (evalString );
100
120
}
101
121
});
102
122
}
103
123
104
124
/*
105
125
* (non-Javadoc)
106
- * @see org.springframework.data.mongodb.core.ScriptOperations#load(java.io.Serializable)
126
+ * @see org.springframework.data.mongodb.core.ScriptOperations#exists(java.lang.String)
127
+ */
128
+ @ Override
129
+ public Boolean exists (String scriptName ) {
130
+
131
+ Assert .hasText (scriptName , "ScriptName must not be null or empty!" );
132
+
133
+ return mongoOperations .exists (query (where ("name" ).is (scriptName )), CallableMongoScript .class ,
134
+ SCRIPT_COLLECTION_NAME );
135
+ }
136
+
137
+ /*
138
+ * (non-Javadoc)
139
+ * @see org.springframework.data.mongodb.core.ScriptOperations#scriptNames()
107
140
*/
108
141
@ Override
109
- public CallableMongoScript load ( Serializable name ) {
142
+ public Set < String > scriptNames ( ) {
110
143
111
- Assert .notNull (name , "Name must not be null!" );
112
- return mongoOperations .findOne (query (where ("name" ).is (name )), CallableMongoScript .class , SCRIPT_COLLECTION_NAME );
144
+ List <CallableMongoScript > scripts = (mongoOperations .findAll (CallableMongoScript .class , SCRIPT_COLLECTION_NAME ));
145
+
146
+ if (CollectionUtils .isEmpty (scripts )) {
147
+ return Collections .emptySet ();
148
+ }
149
+
150
+ Set <String > scriptNames = new HashSet <String >();
151
+ for (CallableMongoScript script : scripts ) {
152
+ scriptNames .add (script .getName ());
153
+ }
154
+ return scriptNames ;
113
155
}
114
156
115
157
/**
116
- * Generate a valid name for the script . MongoDB requires a script id of type String. Calling scripts having ObjectId
117
- * as id fails. Therefore we create a random UUID without {@code -} (as this won't work) an prefix the result with
118
- * {@link #SCRIPT_NAME_PREFIX}.
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}.
119
161
*
120
162
* @return
121
163
*/
0 commit comments