|
| 1 | +package com.marklogic.appdeployer.command.es; |
| 2 | + |
| 3 | +import com.marklogic.appdeployer.AppConfig; |
| 4 | +import com.marklogic.appdeployer.command.AbstractCommand; |
| 5 | +import com.marklogic.appdeployer.command.CommandContext; |
| 6 | +import com.marklogic.appdeployer.command.SortOrderConstants; |
| 7 | +import com.marklogic.client.DatabaseClient; |
| 8 | +import com.marklogic.client.es.CodeGenerationRequest; |
| 9 | +import com.marklogic.client.es.EntityServicesManager; |
| 10 | +import com.marklogic.client.es.GeneratedCode; |
| 11 | +import com.sun.org.apache.bcel.internal.classfile.Code; |
| 12 | +import org.springframework.util.FileCopyUtils; |
| 13 | + |
| 14 | +import java.io.File; |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +/** |
| 19 | + * Generates Entity Services model artifacts based on model definitions. This is implemented as a command, though it's |
| 20 | + * not likely to be invoked as part of e.g. an "mlDeploy" call in ml-gradle. But implementing it as a command does |
| 21 | + * allow for that possibility, and it's also easy to invoke by itself in ml-gradle too. |
| 22 | + */ |
| 23 | +public class GenerateModelArtifactsCommand extends AbstractCommand { |
| 24 | + |
| 25 | + // Should be very rare to need to override this, but just in case |
| 26 | + private String optionsPath = "options"; |
| 27 | + |
| 28 | + public GenerateModelArtifactsCommand() { |
| 29 | + int order = SortOrderConstants.LOAD_SCHEMAS - 1; |
| 30 | + if (SortOrderConstants.LOAD_SCHEMAS > SortOrderConstants.LOAD_MODULES) { |
| 31 | + order = SortOrderConstants.LOAD_MODULES - 1; |
| 32 | + } |
| 33 | + setExecuteSortOrder(order); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void execute(CommandContext context) { |
| 38 | + AppConfig appConfig = context.getAppConfig(); |
| 39 | + String modelsPath = appConfig.getModelsPath(); |
| 40 | + File modelsDir = new File(modelsPath); |
| 41 | + if (modelsDir.exists()) { |
| 42 | + DatabaseClient client = appConfig.newDatabaseClient(); |
| 43 | + EntityServicesManager mgr = new EntityServicesManager(client); |
| 44 | + for (File f : modelsDir.listFiles()) { |
| 45 | + GeneratedCode code = loadModelDefinition(appConfig, f, mgr); |
| 46 | + |
| 47 | + File modulesDir = selectModulesDir(appConfig); |
| 48 | + modulesDir.mkdirs(); |
| 49 | + |
| 50 | + generateInstanceConverter(appConfig, code, modulesDir); |
| 51 | + generateSearchOptions(code, modulesDir); |
| 52 | + generateDatabaseProperties(appConfig, code); |
| 53 | + generateSchema(appConfig, code); |
| 54 | + generateExtractionTemplate(appConfig, code); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + protected GeneratedCode loadModelDefinition(AppConfig appConfig, File f, EntityServicesManager mgr) { |
| 60 | + String name = f.getName(); |
| 61 | + String modelDefinition = null; |
| 62 | + try { |
| 63 | + modelDefinition = new String(FileCopyUtils.copyToByteArray(f)); |
| 64 | + } catch (IOException e) { |
| 65 | + throw new RuntimeException("Unable to read model definition from file: " + f.getAbsolutePath(), e); |
| 66 | + } |
| 67 | + String modelUri = mgr.loadModel(name, modelDefinition); |
| 68 | + return mgr.generateCode(modelUri, buildCodeGenerationRequest(appConfig)); |
| 69 | + } |
| 70 | + |
| 71 | + protected CodeGenerationRequest buildCodeGenerationRequest(AppConfig appConfig) { |
| 72 | + CodeGenerationRequest request = new CodeGenerationRequest(); |
| 73 | + request.setGenerateDatabaseProperties(appConfig.isGenerateDatabaseProperties()); |
| 74 | + request.setGenerateExtractionTemplate(appConfig.isGenerateExtractionTemplate()); |
| 75 | + request.setGenerateInstanceConverter(appConfig.isGenerateInstanceConverter()); |
| 76 | + request.setGenerateSchema(appConfig.isGenerateSchema()); |
| 77 | + request.setGenerateSearchOptions(appConfig.isGenerateSearchOptions()); |
| 78 | + return request; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Selects the last path from appConfig.getModulePaths, the assumption being that any paths before the last one |
| 83 | + * are for dependencies. |
| 84 | + * |
| 85 | + * @param appConfig |
| 86 | + * @return |
| 87 | + */ |
| 88 | + protected File selectModulesDir(AppConfig appConfig) { |
| 89 | + List<String> modulePaths = appConfig.getModulePaths(); |
| 90 | + String lastPath = modulePaths.get(modulePaths.size() - 1); |
| 91 | + return new File(lastPath); |
| 92 | + } |
| 93 | + |
| 94 | + protected void generateInstanceConverter(AppConfig appConfig, GeneratedCode code, File modulesDir) { |
| 95 | + String instanceConverter = code.getInstanceConverter(); |
| 96 | + if (instanceConverter != null) { |
| 97 | + File esDir = new File(modulesDir, appConfig.getInstanceConverterPath()); |
| 98 | + esDir.mkdirs(); |
| 99 | + File out = new File(esDir, code.getTitle() + "-" + code.getVersion() + ".xqy"); |
| 100 | + if (out.exists()) { |
| 101 | + out = new File(esDir, code.getTitle() + "-" + code.getVersion() + "-GENERATED.xqy"); |
| 102 | + } |
| 103 | + try { |
| 104 | + FileCopyUtils.copy(instanceConverter.getBytes(), out); |
| 105 | + if (logger.isInfoEnabled()) { |
| 106 | + logger.info("Wrote instance converter to: " + out.getAbsolutePath()); |
| 107 | + } |
| 108 | + } catch (IOException e) { |
| 109 | + throw new RuntimeException("Unable to write instance converter to: " + out.getAbsolutePath(), e); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + protected void generateSearchOptions(GeneratedCode code, File modulesDir) { |
| 115 | + String searchOptions = code.getSearchOptions(); |
| 116 | + if (searchOptions != null) { |
| 117 | + File optionsDir = new File(modulesDir, optionsPath); |
| 118 | + optionsDir.mkdirs(); |
| 119 | + File out = new File(optionsDir, code.getTitle() + ".xml"); |
| 120 | + if (out.exists()) { |
| 121 | + out = new File(optionsDir, code.getTitle() + "-GENERATED.xml"); |
| 122 | + } |
| 123 | + try { |
| 124 | + FileCopyUtils.copy(searchOptions.getBytes(), out); |
| 125 | + if (logger.isInfoEnabled()) { |
| 126 | + logger.info("Wrote search options to: " + out.getAbsolutePath()); |
| 127 | + } |
| 128 | + } catch (IOException e) { |
| 129 | + throw new RuntimeException("Unable to write search options to file: " + out.getAbsolutePath(), e); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + protected void generateDatabaseProperties(AppConfig appConfig, GeneratedCode code) { |
| 135 | + String props = code.getDatabaseProperties(); |
| 136 | + if (props != null) { |
| 137 | + File dir = appConfig.getConfigDir().getDatabasesDir(); |
| 138 | + dir.mkdirs(); |
| 139 | + File out = new File(dir, "content-database.json"); |
| 140 | + if (out.exists()) { |
| 141 | + out = new File(dir, "content-database-GENERATED.json"); |
| 142 | + } |
| 143 | + try { |
| 144 | + FileCopyUtils.copy(props.getBytes(), out); |
| 145 | + if (logger.isInfoEnabled()) { |
| 146 | + logger.info("Wrote database properties to: " + out.getAbsolutePath()); |
| 147 | + } |
| 148 | + } catch (IOException e) { |
| 149 | + throw new RuntimeException("Unable to write database properties to file: " + out.getAbsolutePath(), e); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + protected void generateSchema(AppConfig appConfig, GeneratedCode code) { |
| 155 | + String schema = code.getSchema(); |
| 156 | + if (schema != null) { |
| 157 | + File dir = new File(appConfig.getSchemasPath()); |
| 158 | + dir.mkdirs(); |
| 159 | + File out = new File(dir, code.getTitle() + "-" + code.getVersion() + ".xsd"); |
| 160 | + if (out.exists()) { |
| 161 | + out = new File(dir, code.getTitle() + "-" + code.getVersion() + "-GENERATED.xsd"); |
| 162 | + } |
| 163 | + try { |
| 164 | + FileCopyUtils.copy(schema.getBytes(), out); |
| 165 | + if (logger.isInfoEnabled()) { |
| 166 | + logger.info("Wrote schema to: " + out.getAbsolutePath()); |
| 167 | + } |
| 168 | + } catch (IOException e) { |
| 169 | + throw new RuntimeException("Unable to write schema to file: " + out.getAbsolutePath(), e); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + protected void generateExtractionTemplate(AppConfig appConfig, GeneratedCode code) { |
| 175 | + String template = code.getExtractionTemplate(); |
| 176 | + if (template != null) { |
| 177 | + File dir = new File(appConfig.getSchemasPath()); |
| 178 | + dir.mkdirs(); |
| 179 | + File out = new File(dir, code.getTitle() + "-" + code.getVersion() + ".tdex"); |
| 180 | + if (out.exists()) { |
| 181 | + out = new File(dir, code.getTitle() + "-" + code.getVersion() + "-GENERATED.tdex"); |
| 182 | + } |
| 183 | + try { |
| 184 | + FileCopyUtils.copy(template.getBytes(), out); |
| 185 | + if (logger.isInfoEnabled()) { |
| 186 | + logger.info("Wrote extraction template to: " + out.getAbsolutePath()); |
| 187 | + } |
| 188 | + } catch (IOException e) { |
| 189 | + throw new RuntimeException("Unable to write extraction template to file: " + out.getAbsolutePath(), e); |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + public void setOptionsPath(String optionsPath) { |
| 195 | + this.optionsPath = optionsPath; |
| 196 | + } |
| 197 | +} |
| 198 | + |
0 commit comments