44import com .marklogic .appdeployer .command .AbstractCommand ;
55import com .marklogic .appdeployer .command .CommandContext ;
66import com .marklogic .appdeployer .command .SortOrderConstants ;
7+ import com .marklogic .appdeployer .command .databases .DeploySchemasDatabaseCommand ;
78import com .marklogic .client .DatabaseClient ;
89import com .marklogic .client .es .CodeGenerationRequest ;
910import com .marklogic .client .es .EntityServicesManager ;
1011import com .marklogic .client .es .GeneratedCode ;
11- import com .sun .org .apache .bcel .internal .classfile .Code ;
1212import org .springframework .util .FileCopyUtils ;
1313
1414import java .io .File ;
@@ -97,13 +97,15 @@ protected void generateInstanceConverter(AppConfig appConfig, GeneratedCode code
9797 File esDir = new File (modulesDir , appConfig .getInstanceConverterPath ());
9898 esDir .mkdirs ();
9999 File out = new File (esDir , code .getTitle () + "-" + code .getVersion () + ".xqy" );
100+ String logMessage = "Wrote instance converter to: " ;
100101 if (out .exists ()) {
101102 out = new File (esDir , code .getTitle () + "-" + code .getVersion () + "-GENERATED.xqy" );
103+ logMessage = "Instance converter file already exists; writing new generated copy to: " ;
102104 }
103105 try {
104106 FileCopyUtils .copy (instanceConverter .getBytes (), out );
105107 if (logger .isInfoEnabled ()) {
106- logger .info ("Wrote instance converter to: " + out .getAbsolutePath ());
108+ logger .info (logMessage + out .getAbsolutePath ());
107109 }
108110 } catch (IOException e ) {
109111 throw new RuntimeException ("Unable to write instance converter to: " + out .getAbsolutePath (), e );
@@ -117,33 +119,55 @@ protected void generateSearchOptions(GeneratedCode code, File modulesDir) {
117119 File optionsDir = new File (modulesDir , optionsPath );
118120 optionsDir .mkdirs ();
119121 File out = new File (optionsDir , code .getTitle () + ".xml" );
122+ String logMessage = "Wrote search options to: " ;
120123 if (out .exists ()) {
121124 out = new File (optionsDir , code .getTitle () + "-GENERATED.xml" );
125+ logMessage = "Search options file already exists; writing new generated copy to: " ;
122126 }
123127 try {
124128 FileCopyUtils .copy (searchOptions .getBytes (), out );
125129 if (logger .isInfoEnabled ()) {
126- logger .info ("Wrote search options to: " + out .getAbsolutePath ());
130+ logger .info (logMessage + out .getAbsolutePath ());
127131 }
128132 } catch (IOException e ) {
129133 throw new RuntimeException ("Unable to write search options to file: " + out .getAbsolutePath (), e );
130134 }
131135 }
132136 }
133137
138+ /**
139+ * The content-database.json file generated by Entity Services has a reference to a schemas database. So if a config
140+ * file for a schemas database doesn't exist, one is generated so that the next deployment succeeds.
141+ *
142+ * @param appConfig
143+ * @param code
144+ */
134145 protected void generateDatabaseProperties (AppConfig appConfig , GeneratedCode code ) {
135146 String props = code .getDatabaseProperties ();
136147 if (props != null ) {
137- File dir = appConfig .getConfigDir ().getDatabasesDir ();
138- dir .mkdirs ();
139- File out = new File (dir , "content-database.json" );
148+ File dbDir = appConfig .getConfigDir ().getDatabasesDir ();
149+ dbDir .mkdirs ();
150+ File out = new File (dbDir , "content-database.json" );
151+ String logMessage = "Wrote database properties to: " ;
140152 if (out .exists ()) {
141- out = new File (dir , "content-database-GENERATED.json" );
153+ out = new File (dbDir , "content-database-GENERATED.json" );
154+ logMessage = "Database properties file already exists; writing new generated copy to: " ;
142155 }
143156 try {
144157 FileCopyUtils .copy (props .getBytes (), out );
145158 if (logger .isInfoEnabled ()) {
146- logger .info ("Wrote database properties to: " + out .getAbsolutePath ());
159+ logger .info (logMessage + out .getAbsolutePath ());
160+ }
161+
162+ // Makes some assumptions about the schemas file
163+ File schemasFile = new File (dbDir , DeploySchemasDatabaseCommand .DATABASE_FILENAME );
164+ if (!schemasFile .exists ()) {
165+ String payload = "{\" database-name\" : \" %%SCHEMAS_DATABASE%%\" }" ;
166+ try {
167+ FileCopyUtils .copy (payload .getBytes (), schemasFile );
168+ } catch (IOException ex ) {
169+ logger .warn ("Unable to write schemas database payload to file: " + schemasFile .getAbsolutePath (), ex );
170+ }
147171 }
148172 } catch (IOException e ) {
149173 throw new RuntimeException ("Unable to write database properties to file: " + out .getAbsolutePath (), e );
@@ -157,13 +181,15 @@ protected void generateSchema(AppConfig appConfig, GeneratedCode code) {
157181 File dir = new File (appConfig .getSchemasPath ());
158182 dir .mkdirs ();
159183 File out = new File (dir , code .getTitle () + "-" + code .getVersion () + ".xsd" );
184+ String logMessage = "Wrote schema to: " ;
160185 if (out .exists ()) {
161186 out = new File (dir , code .getTitle () + "-" + code .getVersion () + "-GENERATED.xsd" );
187+ logMessage = "Schema file already exists; writing new generated copy to: " ;
162188 }
163189 try {
164190 FileCopyUtils .copy (schema .getBytes (), out );
165191 if (logger .isInfoEnabled ()) {
166- logger .info ("Wrote schema to: " + out .getAbsolutePath ());
192+ logger .info (logMessage + out .getAbsolutePath ());
167193 }
168194 } catch (IOException e ) {
169195 throw new RuntimeException ("Unable to write schema to file: " + out .getAbsolutePath (), e );
@@ -177,13 +203,15 @@ protected void generateExtractionTemplate(AppConfig appConfig, GeneratedCode cod
177203 File dir = new File (appConfig .getSchemasPath ());
178204 dir .mkdirs ();
179205 File out = new File (dir , code .getTitle () + "-" + code .getVersion () + ".tdex" );
206+ String logMessage = "Wrote extraction template to: " ;
180207 if (out .exists ()) {
181208 out = new File (dir , code .getTitle () + "-" + code .getVersion () + "-GENERATED.tdex" );
209+ logMessage = "Extraction template already exists; writing new generated copy to: " ;
182210 }
183211 try {
184212 FileCopyUtils .copy (template .getBytes (), out );
185213 if (logger .isInfoEnabled ()) {
186- logger .info ("Wrote extraction template to: " + out .getAbsolutePath ());
214+ logger .info (logMessage + out .getAbsolutePath ());
187215 }
188216 } catch (IOException e ) {
189217 throw new RuntimeException ("Unable to write extraction template to file: " + out .getAbsolutePath (), e );
0 commit comments