11package com .marklogic .client .ext .schemasloader .impl ;
22
33import com .marklogic .client .DatabaseClient ;
4+ import com .marklogic .client .eval .ServerEvaluationCall ;
45import com .marklogic .client .ext .batch .BatchWriter ;
56import com .marklogic .client .ext .batch .RestBatchWriter ;
67import com .marklogic .client .ext .file .DocumentFile ;
78import com .marklogic .client .ext .file .GenericFileLoader ;
9+ import com .marklogic .client .ext .helper .ClientHelper ;
810import com .marklogic .client .ext .modulesloader .impl .DefaultFileFilter ;
911import com .marklogic .client .ext .schemasloader .SchemasLoader ;
12+ import com .marklogic .client .io .DocumentMetadataHandle ;
1013
14+ import java .util .ArrayList ;
1115import java .util .List ;
16+ import java .util .Set ;
1217import java .util .function .Supplier ;
18+ import java .util .stream .Collectors ;
1319
1420public class DefaultSchemasLoader extends GenericFileLoader implements SchemasLoader {
1521
1622 private DatabaseClient schemasDatabaseClient ;
1723 private String tdeValidationDatabase ;
1824
1925 /**
20- * Simplest constructor for using this class. Just provide a DatabaseClient, and this will use sensible defaults
21- * for how documents are read and written. Note that the DatabaseClient will not be released after this class is
22- * done with it, as this class wasn't the one that created it.
26+ * Simplest constructor for using this class. Just provide a DatabaseClient, and this will use sensible defaults for
27+ * how documents are read and written. Note that the DatabaseClient will not be released after this class is done
28+ * with it, as this class wasn't the one that created it.
2329 *
2430 * @param schemasDatabaseClient
2531 */
@@ -70,14 +76,21 @@ protected void initializeDefaultSchemasLoader() {
7076 }
7177
7278 /**
73- * Run the given paths through the DocumentFileReader, and then send the result to the BatchWriter, and then
74- * return the result.
79+ * Run the given paths through the DocumentFileReader, and then send the result to the BatchWriter, and then return
80+ * the result.
7581 *
7682 * @param paths
7783 * @return a DocumentFile for each file that was loaded as a schema
7884 */
7985 @ Override
8086 public List <DocumentFile > loadSchemas (String ... paths ) {
87+ ClientHelper helper = new ClientHelper (schemasDatabaseClient );
88+ if (helper .getMLEffectiveVersion () >= 10000900 && tdeValidationDatabase != null && !tdeValidationDatabase .isEmpty ()) {
89+ logger .info ("Installing tde's using tde.templateBatchInsert" );
90+ List <DocumentFile > documentFiles = super .getDocumentFiles (paths );
91+ buildTemplateBatchInsertCall (documentFiles ).eval ().close ();
92+ return documentFiles ;
93+ }
8194 return super .loadFiles (paths );
8295 }
8396
@@ -88,4 +101,51 @@ public String getTdeValidationDatabase() {
88101 public void setTdeValidationDatabase (String tdeValidationDatabase ) {
89102 this .tdeValidationDatabase = tdeValidationDatabase ;
90103 }
104+
105+ protected ServerEvaluationCall buildTemplateBatchInsertCall (List <DocumentFile > documentFiles ) {
106+ String tdeTemplate = getTdeBatchInsertQuery (documentFiles );
107+ StringBuilder script = new StringBuilder ("declareUpdate(); xdmp.invokeFunction(function() {var tde = require('/MarkLogic/tde.xqy');" );
108+ script .append (tdeTemplate );
109+ script .append (format ("}, {database: xdmp.database('%s')})" , tdeValidationDatabase ));
110+ return schemasDatabaseClient .newServerEval ().javascript (script .toString ());
111+ }
112+
113+ private String getTdeBatchInsertQuery (List <DocumentFile > documentFiles ) {
114+ List <String > templateInfoList = new ArrayList <>();
115+ for (DocumentFile doc : documentFiles ) {
116+ String uri = doc .getUri ();
117+ String content = doc .getContent ().toString ();
118+
119+ // Permissions
120+ DocumentMetadataHandle .DocumentPermissions documentPermissions = doc .getDocumentMetadata ().getPermissions ();
121+ List <String > permissionList = new ArrayList <>();
122+ documentPermissions .keySet ().forEach (key -> {
123+ Set <DocumentMetadataHandle .Capability > values = documentPermissions .get (key );
124+ values .forEach (value -> permissionList .add (String .format ("xdmp.permission('%s', '%s')" , key , value )));
125+ });
126+ String permissions = "[" .concat (permissionList .stream ().collect (Collectors .joining (", " ))).concat ("]" );
127+
128+ // Collections
129+ List <String > collectionsList = new ArrayList <>();
130+ doc .getDocumentMetadata ().getCollections ().forEach (collection -> collectionsList .add (collection ));
131+ String collections = collectionsList .stream ().map (coll -> '"' + coll + '"' ).collect (Collectors .joining (", " ));
132+ collections = "[" .concat (collections ).concat ("]" );
133+
134+ // Template info
135+ String templateFormat = "" ;
136+ if (doc .getFormat ().toString ().equals ("XML" )) {
137+ templateFormat = String .format ("tde.templateInfo('%s', xdmp.unquote(`%s`), %s, %s)" , uri , content , permissions , collections );
138+ } else if (doc .getFormat ().toString ().equals ("JSON" )) {
139+ templateFormat = String .format ("tde.templateInfo('%s', xdmp.toJSON(%s), %s, %s)" , uri , content , permissions , collections );
140+ } else {
141+ templateFormat = String .format ("tde.templateInfo('%s',%s, %s, %s)" , uri , content , permissions , collections );
142+ }
143+ templateInfoList .add (templateFormat );
144+ }
145+
146+ String templateString = "tde.templateBatchInsert(["
147+ .concat (templateInfoList .stream ().collect (Collectors .joining ("," )))
148+ .concat ("]);" );
149+ return templateString ;
150+ }
91151}
0 commit comments