2323import com .marklogic .client .ext .modulesloader .impl .DefaultFileFilter ;
2424import com .marklogic .client .ext .schemasloader .SchemasLoader ;
2525import com .marklogic .client .io .DocumentMetadataHandle ;
26- import org .springframework .util .StringUtils ;
2726
2827import java .util .ArrayList ;
2928import java .util .List ;
3332
3433public class DefaultSchemasLoader extends GenericFileLoader implements SchemasLoader {
3534
36- private DatabaseClient schemasDatabaseClient ;
37- private String tdeValidationDatabase ;
38- protected QbvDocumentFileProcessor qbvDocumentFileProcessor ;
35+ private final DatabaseClient schemasDatabaseClient ;
36+ private final DatabaseClient contentDatabaseClient ;
37+ private final boolean validateTdeTemplates ;
38+ private QbvDocumentFileProcessor qbvDocumentFileProcessor ;
3939
4040 /**
41- * Simplest constructor for using this class. Just provide a DatabaseClient, and this will use sensible defaults for
42- * how documents are read and written. Note that the DatabaseClient will not be released after this class is done
43- * with it, as this class wasn't the one that created it.
44- *
45- * @param schemasDatabaseClient
41+ * @param schemasDatabaseClient for loading files into an application's schemas database
42+ * @param contentDatabaseClient for validating TDEs and generating QBVs
4643 */
47- public DefaultSchemasLoader (DatabaseClient schemasDatabaseClient ) {
48- this (schemasDatabaseClient , null );
44+ public DefaultSchemasLoader (DatabaseClient schemasDatabaseClient , DatabaseClient contentDatabaseClient ) {
45+ this (schemasDatabaseClient , contentDatabaseClient , true );
4946 }
5047
5148 /**
52- * If you want to validate TDE templates before they're loaded, you need to provide a second DatabaseClient that
53- * connects to the content database associated with the schemas database that schemas will be loaded into. This is
54- * because the "tde.validate" function must run against the content database.
55- *
56- * @param schemasDatabaseClient
57- * @param tdeValidationDatabase
49+ * @param schemasDatabaseClient for loading files into an application's schemas database
50+ * @param contentDatabaseClient for validating TDEs and generating QBVs
51+ * @param validateTdeTemplates if false, TDEs will not be validated nor loaded via tde.templateBatchInsert
5852 */
59- public DefaultSchemasLoader (DatabaseClient schemasDatabaseClient , String tdeValidationDatabase ) {
53+ public DefaultSchemasLoader (DatabaseClient schemasDatabaseClient , DatabaseClient contentDatabaseClient , boolean validateTdeTemplates ) {
6054 super (((Supplier <BatchWriter >) () -> {
6155 RestBatchWriter writer = new RestBatchWriter (schemasDatabaseClient );
6256 // Default this to 1, as it's not typical to have such a large number of schemas to load that multiple threads
@@ -67,31 +61,20 @@ public DefaultSchemasLoader(DatabaseClient schemasDatabaseClient, String tdeVali
6761 }).get ());
6862
6963 this .schemasDatabaseClient = schemasDatabaseClient ;
70- this .tdeValidationDatabase = tdeValidationDatabase ;
71- initializeDefaultSchemasLoader ();
72- }
64+ this .contentDatabaseClient = contentDatabaseClient ;
65+ this .validateTdeTemplates = validateTdeTemplates ;
7366
74- /**
75- * Assumes that the BatchWriter has already been initialized.
76- *
77- * @param batchWriter
78- * @deprecated Since 4.6.0; this class needs a DatabaseClient for the schemas database passed to it so that it can
79- * pass that client on to specific file processors.
80- */
81- @ Deprecated
82- public DefaultSchemasLoader (BatchWriter batchWriter ) {
83- super (batchWriter );
84- initializeDefaultSchemasLoader ();
85- }
67+ if (this .contentDatabaseClient != null ) {
68+ this .qbvDocumentFileProcessor = new QbvDocumentFileProcessor (this .schemasDatabaseClient , this .contentDatabaseClient );
69+ addDocumentFileProcessor (this .qbvDocumentFileProcessor );
70+ }
71+
72+ if (this .validateTdeTemplates && this .contentDatabaseClient != null ) {
73+ addDocumentFileProcessor (new TdeDocumentFileProcessor (this .contentDatabaseClient ));
74+ } else {
75+ addDocumentFileProcessor (new TdeDocumentFileProcessor (null ));
76+ }
8677
87- /**
88- * Adds the DocumentFileProcessors and FileFilters specific to loading schemas, which will then be used to construct
89- * a DocumentFileReader by the parent class.
90- */
91- protected void initializeDefaultSchemasLoader () {
92- this .qbvDocumentFileProcessor = new QbvDocumentFileProcessor (this .schemasDatabaseClient , this .tdeValidationDatabase );
93- addDocumentFileProcessor (new TdeDocumentFileProcessor (this .schemasDatabaseClient , this .tdeValidationDatabase ));
94- addDocumentFileProcessor (this .qbvDocumentFileProcessor );
9578 addFileFilter (new DefaultFileFilter ());
9679 }
9780
@@ -107,7 +90,7 @@ public List<DocumentFile> loadSchemas(String... paths) {
10790 final List <DocumentFile > documentFiles = super .getDocumentFiles (paths );
10891
10992 if (!documentFiles .isEmpty ()) {
110- if (TdeUtil .templateBatchInsertSupported (schemasDatabaseClient ) && StringUtils . hasText ( tdeValidationDatabase ) ) {
93+ if (this . validateTdeTemplates && TdeUtil .templateBatchInsertSupported (schemasDatabaseClient ) && contentDatabaseClient != null ) {
11194 SchemaFiles schemaFiles = readSchemaFiles (documentFiles );
11295 if (!schemaFiles .tdeFiles .isEmpty ()) {
11396 loadTdeTemplatesViaBatchInsert (schemaFiles .tdeFiles );
@@ -122,7 +105,10 @@ public List<DocumentFile> loadSchemas(String... paths) {
122105 writeDocumentFiles (documentFiles );
123106 }
124107 }
125- this .qbvDocumentFileProcessor .processQbvFiles ();
108+
109+ if (this .qbvDocumentFileProcessor != null ) {
110+ this .qbvDocumentFileProcessor .processQbvFiles ();
111+ }
126112
127113 return documentFiles ;
128114 }
@@ -152,11 +138,10 @@ private void loadTdeTemplatesViaBatchInsert(List<DocumentFile> tdeFiles) {
152138 tdeFiles .stream ().map (documentFile -> documentFile .getFile ().getName ()).collect (Collectors .toList ()));
153139
154140 String query = buildTdeBatchInsertQuery (tdeFiles );
155- StringBuilder script = new StringBuilder ("declareUpdate(); xdmp.invokeFunction(function() {var tde = require('/MarkLogic/tde.xqy');" );
141+ StringBuilder script = new StringBuilder ("declareUpdate(); const tde = require('/MarkLogic/tde.xqy'); " );
156142 script .append (query );
157- script .append (format ("}, {database: xdmp.database('%s')})" , tdeValidationDatabase ));
158143 try {
159- schemasDatabaseClient .newServerEval ().javascript (script .toString ()).eval ().close ();
144+ contentDatabaseClient .newServerEval ().javascript (script .toString ()).eval ().close ();
160145 } catch (Exception ex ) {
161146 throw new RuntimeException ("Unable to load and validate TDE templates via tde.templateBatchInsert; " +
162147 "cause: " + ex .getMessage () + "; the following script can be run in Query Console against your content " +
@@ -216,17 +201,4 @@ public SchemaFiles(List<DocumentFile> tdeFiles, List<DocumentFile> nonTdeFiles)
216201 this .nonTdeFiles = nonTdeFiles ;
217202 }
218203 }
219-
220- public String getTdeValidationDatabase () {
221- return tdeValidationDatabase ;
222- }
223-
224- /**
225- * @param tdeValidationDatabase
226- * @deprecated Should be set via the constructor and not modified.
227- */
228- @ Deprecated
229- public void setTdeValidationDatabase (String tdeValidationDatabase ) {
230- this .tdeValidationDatabase = tdeValidationDatabase ;
231- }
232204}
0 commit comments