Commit 0541cf50 authored by Brian Long's avatar Brian Long
Browse files

added dynamic normalizer and reusable crawler

parent 89a43457
Loading
Loading
Loading
Loading
+1 −3
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@
							<goal>unpack-app</goal>
						</goals>
						<configuration>
							<apsAppName>${aps.app}</apsAppName>
							<normalize>true</normalize>
						</configuration>
					</execution>
					<execution>
@@ -41,8 +41,6 @@
							<goal>pack-app</goal>
						</goals>
						<configuration>
							<apsAppName>${aps.app}</apsAppName>
							<targetDirectory>${basedir}/src/main/app</targetDirectory>
						</configuration>
					</execution>
				</executions>
+1 −0
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@
						</goals>
						<configuration>
							<normalize>true</normalize>
							<diffFriendly>true</diffFriendly>
						</configuration>
					</execution>
					<execution>
+13 −0
Original line number Diff line number Diff line
package com.inteligr8.maven.aps.modeling.crawler;

public interface ApsAppCrawlable {

	ApsFileTransformer getAppJsonTransformer();
	
	ApsFileTransformer getProcessJsonTransformer();

	ApsFileTransformer getProcessBpmnTransformer();

	ApsFileTransformer getFormJsonTransformer();

}
+105 −0
Original line number Diff line number Diff line
package com.inteligr8.maven.aps.modeling.crawler;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ApsAppCrawler {

	private final Logger logger = LoggerFactory.getLogger(ApsAppCrawler.class);
	private final Pattern filenamePattern = Pattern.compile("([^/]+)-([0-9]+)\\.(json|bpmn20\\.xml)");
	private final String appName;
	private final File appDirectory;
	private final File appDescriptor;
	private final boolean failOnIntegrityViolation;
	
	public ApsAppCrawler(String apsAppName, File apsAppDirectory, boolean failOnIntegrityViolation) {
		this.appName = apsAppName;
		this.appDirectory = apsAppDirectory;
		this.failOnIntegrityViolation = failOnIntegrityViolation;

		this.appDescriptor = this.validateDescriptor(appDirectory);
		if (this.logger.isDebugEnabled())
			this.logger.debug("APS App descriptor found: " + this.appDescriptor);
	}
	
	protected File validateDescriptor(File appDirectory) {
		File appDescriptor = new File(appDirectory, this.appName + ".json");
		
		if (!appDescriptor.exists())
			throw new IllegalStateException("The APS App descriptor could not be found: " + appDescriptor);
		if (!appDescriptor.isFile())
			throw new IllegalStateException("The APS App descriptor is not a file: " + appDescriptor);
		
		return appDescriptor;
	}
	
	public void execute(ApsAppCrawlable crawlable) throws IOException {
		this.logger.info("Crawling APS App ...");
		
		Map<String, ApsFileTransformer> processTransformers = new HashMap<>();
		processTransformers.put("json", crawlable.getProcessJsonTransformer());
		processTransformers.put("bpmn20.xml", crawlable.getProcessBpmnTransformer());
		processTransformers.put("bpmn", crawlable.getProcessBpmnTransformer());

		this.transform(crawlable.getAppJsonTransformer(), this.appDescriptor, this.appName, null);
		this.crawlModels("form-models", crawlable.getFormJsonTransformer());
		this.crawlModels("bpmn-models", processTransformers);
		this.crawlModels("bpmn-subprocess-models", crawlable.getProcessJsonTransformer());
	}
	
	protected void crawlModels(String modelsDirectoryName, ApsFileTransformer transformer) throws IOException {
		this.crawlModels(modelsDirectoryName, Collections.singletonMap("json", transformer));
	}
	
	protected void crawlModels(String modelsDirectoryName, Map<String, ApsFileTransformer> transformers) throws IOException {
		File modelsDirectory = new File(this.appDirectory, modelsDirectoryName);
		if (!modelsDirectory.exists()) {
			this.logger.debug("APS model directory doesn't exist; no models to transform: {}", modelsDirectory);
			return;
		}
		
		for (File modelFile : modelsDirectory.listFiles()) {
			this.logger.trace("Transforming model: {}", modelFile);
			
			Matcher matcher = this.filenamePattern.matcher(modelFile.getName());
			if (!matcher.find()) {
				this.logger.warn("The '{}' folder has a file with an unexpected filename format; skipping: {}", modelsDirectoryName, modelFile);
				continue;
			}
			
			String ext = matcher.group(3);
			ApsFileTransformer transformer = transformers.get(ext.toLowerCase());
			if (transformer == null) {
				this.logger.warn("The '{}' folder has a file with an unexpected extension; skipping: {}", modelsDirectoryName, modelFile);
				continue;
			}
			
			String modelName = matcher.group(1);
			Long modelId = new Long(matcher.group(2));
			this.logger.trace("Transforming model {} ID: {}", modelName, modelId);
			
			this.transform(transformer, modelFile, modelName, modelId);
		}
	}
	
	private void transform(ApsFileTransformer transformer, File modelFile, String modelName, Long modelId) throws IOException {
		try {
			transformer.transformFile(modelFile, modelName, modelId);
		} catch (IllegalArgumentException | IllegalStateException ie) {
			if (this.failOnIntegrityViolation) {
				throw ie;
			} else {
				this.logger.warn(ie.getMessage());
			}
		}
	}

}
+10 −0
Original line number Diff line number Diff line
package com.inteligr8.maven.aps.modeling.crawler;

import java.io.File;
import java.io.IOException;

public interface ApsFileTransformer {

	void transformFile(File file, String modelName, Long modelId) throws IOException;

}
Loading