SiteManager.java 12.3 KB
Newer Older
/*
 * Copyright 2020 ETSI
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 * 3. Neither the name of the copyright holder nor the names of its contributors 
 *    may be used to endorse or promote products derived from this software without 
 *    specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package fr.emse.gitlab.saref.managers;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.jena.atlas.io.IndentedWriter;
import org.apache.jena.graph.NodeFactory;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.sparql.core.Var;
import org.apache.jena.sparql.engine.binding.Binding;
import org.apache.jena.sparql.engine.binding.BindingHashMap;
import org.apache.jena.sparql.util.Context;
import org.eclipse.jgit.api.Git;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fr.emse.ci.sparqlext.SPARQLExt;
import fr.emse.ci.sparqlext.engine.PlanFactory;
import fr.emse.ci.sparqlext.engine.RootPlan;
import fr.emse.ci.sparqlext.stream.LookUpRequest;
import fr.emse.ci.sparqlext.stream.SPARQLExtStreamManager;
import fr.emse.ci.sparqlext.utils.ContextUtils;
import fr.emse.ci.sparqlext.utils.VarUtils;
import fr.emse.gitlab.saref.SAREF;
import fr.emse.gitlab.saref.SAREFErrorLogger;
import fr.emse.gitlab.saref.SAREFPipeline;
import fr.emse.gitlab.saref.SAREFPipeline.Mode;
import fr.emse.gitlab.saref.SAREFPipelineException;
import fr.emse.gitlab.saref.utils.StreamManagerFactory;

public class SiteManager extends SAREFErrorLogger {

	private static final Logger LOG = LoggerFactory.getLogger(SiteManager.class);

	private static final String DOC_BASE = SAREF.BASE + "documentation/";
	private static final String TERM_QUERY = DOC_BASE + "term/main.rqg";
	private static final Var VAR_TERM = VarUtils.allocVar("term");
	private static final String ONTO_QUERY = DOC_BASE + "ontology/main.rqg";
	private static final Var VAR_VERSION_IRI = VarUtils.allocVar("versionIRI");
	private static final String EXAMPLE_QUERY = DOC_BASE + "example/main.rqg";
	private static final Var VAR_EXAMPLE = VarUtils.allocVar("example");

	private static final String NAME_SITE = "site";
	private static final String NAME_STATIC = "static";
	private static final String NAME_REPORT_HTML = "report.html";

	private static final SPARQLExtStreamManager STREAM_MANAGER_BASE = StreamManagerFactory.get();
	private final File siteDir;
	private final File staticDir;
	private final File reportFileHtml;
	private final RootPlan planForTerm;
	private final RootPlan planForOntologyVersion;
	private final RootPlan planForExample;

	private static enum MESSAGE {
		prepare_error;
	}

	public SiteManager(SAREFPipeline pipeline, Logger errorLogger) throws IOException {
		super(pipeline, errorLogger);
		siteDir = new File(pipeline.targetDir, NAME_SITE);
		staticDir = new File(siteDir, NAME_STATIC);
		reportFileHtml = new File(siteDir, NAME_REPORT_HTML);

		String query = IOUtils.toString(STREAM_MANAGER_BASE.open(new LookUpRequest(TERM_QUERY, SPARQLExt.MEDIA_TYPE)),
				StandardCharsets.UTF_8);
		planForTerm = PlanFactory.create(query, DOC_BASE);

		query = IOUtils.toString(STREAM_MANAGER_BASE.open(new LookUpRequest(ONTO_QUERY, SPARQLExt.MEDIA_TYPE)),
				StandardCharsets.UTF_8);
		planForOntologyVersion = PlanFactory.create(query, DOC_BASE);

		query = IOUtils.toString(STREAM_MANAGER_BASE.open(new LookUpRequest(TERM_QUERY, SPARQLExt.MEDIA_TYPE)),
				StandardCharsets.UTF_8);
//		planForExample = PlanFactory.create(query, DOC_BASE);
		planForExample = null;
	}

	/**
	 * Prepare the site folder and clone the saref-portal-static project repository.
	 */
	public void prepareSite() throws SAREFPipelineException {
		if (pipeline.ignoreSite) {
			return;
		}
		try (Git git = Git.cloneRepository().setURI(SAREF.SAREF_PORTAL_STATIC_GIT).setDirectory(siteDir).call()) {
		} catch (Exception ex) {
			try (Git git = Git.open(siteDir)) {
				git.pull().call();
			} catch (Exception e) {
				String msg = getMessage(MESSAGE.prepare_error, SAREF.SAREF_PORTAL_STATIC_GIT);
				log(msg, e, Mode.DEVELOP, Mode.RELEASE, Mode.PORTAL);
				throw new SAREFPipelineException(msg, e);
			}
		}
	}

	public boolean writeReport(StringWriter sw) throws IOException {
		if (pipeline.ignoreSite) {
			return false;
		}
		File reportHTML = new File(siteDir, "report.html");
		try (IndentedWriter writer = new IndentedWriter(new FileOutputStream(reportHTML));) {
			boolean debugTemplate = pipeline.mode == Mode.DEVELOP;
			Context context = ContextUtils.build(writer).setBase(SAREF.BASE).setDebugTemplate(debugTemplate)
					.setStreamManager(STREAM_MANAGER_BASE).build();
			BindingHashMap binding = new BindingHashMap();
			Var varTestSuites = VarUtils.allocVar("testsuites");
			binding.add(varTestSuites, NodeFactory.createLiteral(sw.toString()));
			List<Binding> bindings = new ArrayList<>();
			bindings.add(binding);
			String query = IOUtils.toString(
					STREAM_MANAGER_BASE.open(new LookUpRequest("documentation/report/main.rqg", SPARQLExt.MEDIA_TYPE)),
					StandardCharsets.UTF_8);
			RootPlan reportPlan = PlanFactory.create(query, DOC_BASE);
			reportPlan.execTemplateStream(bindings, context);
		}
	}

	public void generateSite() {
		if (pipeline.ignoreSite) {
			return;
		}
		LOG.info("Generate static files for the portal");
		
//		DatasetManager datasetManager = pipeline.getDatasetManager();
//		Dataset dataset = datasetManager.getDataset();
//		dataset.begin(ReadWrite.READ);
//		Model config = datasetManager.getConfigModel();
//
//		File configFile = new File(siteDir, "config.ttl");
//		try (FileOutputStream fos = new FileOutputStream(configFile)) {
//			config.write(fos, "TTL");
//		} catch (IOException ex) {
//			errorLogger.error("Unexpected exception while writing configuration file", ex);
//		}
//
//		for (ResIterator it = config.listResourcesWithProperty(RDF.type, EX.OntologyVersion); it.hasNext();) {
//			Resource resource = it.next();// https://saref.etsi.org/core/master/
//			String uri = resource.getURI();
//			String ontoDirPath = uri.substring(SAREF.BASE.length(), uri.length() - 1); // core/master
//			try {
//				File ontoDir = new File(siteDir, ontoDirPath);
//				FileUtils.forceMkdir(ontoDir);
//				String repoName = resource.getRequiredProperty(EX.repositoryName).getString(); // saref-core
//				String ontologyFileName = repoName.equals("saref-core") ? "saref" : repoName; // saref
//				Model model = dataset.getNamedModel(resource.getURI());
//				for (Languages l : Languages.values()) {
//					File file = new File(ontoDir, String.format("%s.%s", ontologyFileName, l.getExt()));
//					try (FileOutputStream fos = new FileOutputStream(file)) {
//						model.write(fos, l.getLang());
//					}
//				}
//				File htmlFile = new File(ontoDir, String.format("%s.html", ontologyFileName));
//				
//				
//				// attention, le directory change selon le graph à generer. 
//				StreamManager streamManager = StreamManagerFactory.get(directory);
//
//				
//				try (IndentedWriter writer = new IndentedWriter(new FileOutputStream(htmlFile));) {
//					Context context = ContextUtils.build(writer).setBase(REGEX.BASE)
//							.setDebugTemplate(pipeline.mode == Mode.DEVELOP).setInputDataset(dataset)
//							.setStreamManager(streamManager).build();
//					generateHTML(planForOntologyVersion, VAR_VERSION_IRI, resource, context);
//				}
//				
//				
//				
//			} catch (Exception ex) {
//				errorLogger.error(String.format("Unexpected exception while writing static files for %s", resource),
//						ex);
//			}
//			try {
//				File diagramDir = new File(siteDir, ontoDirPath + File.separator + "diagrams");
//				FileUtils.copyDirectory(new File(directory, "diagrams"), diagramDir);
//			} catch (IOException ex) {
//				errorLogger.warn("Could not copy diagrams directory", ex);
//			}
//		}
//
//		if (!ignoreTerms) {
//			for (ResIterator it = config.listResourcesWithProperty(RDF.type, RDFP.Resource); it.hasNext();) {
//				Resource resource = it.next();// https://saref.etsi.org/core/master/ --
//												// https://saref.etsi.org/core/Command
//				String uri = resource.getURI();
//				try {
//					String termPath = uri.substring(SAREF.BASE.length()); // core/Command
//					File termDir = new File(siteDir, termPath).getParentFile(); // core
//					FileUtils.forceMkdir(termDir);
//					String termName = resource.getRequiredProperty(EX.localName).getString(); // Command
//					Model model = dataset.getNamedModel(resource.getURI());
//					for (Languages l : Languages.values()) {
//						File file = new File(termDir, String.format("%s.%s", termName, l.getExt()));
//						try (FileOutputStream fos = new FileOutputStream(file)) {
//							model.write(fos, l.getLang());
//						}
//					}
//					File htmlFile = new File(termDir, String.format("%s.html", termName));
//					try (IndentedWriter writer = new IndentedWriter(new FileOutputStream(htmlFile));) {
//						Context context = ContextUtils.build(writer).setBase(REGEX.BASE).setDebugTemplate(verbose)
//								.setInputDataset(dataset).setStreamManager(streamManager).build();
//						generateHTML(planForTerm, VAR_TERM, resource, context);
//					}
//				} catch (Exception ex) {
//					errorLogger.error(String.format("Unexpected exception while writing static files for %s", resource),
//							ex);
//				}
//			}
//		}
//
//		if (!ignoreExamples) {
//			for (ResIterator it = config.listResourcesWithProperty(RDF.type, EX.Example); it.hasNext();) {
//				Resource resource = it.next();// https://saref.etsi.org/core/master/example/doorswitch
//				String uri = resource.getURI();
//				try {
//					String exPath = uri.substring(REGEX.BASE.length()); // core/master/example/doorswitch
//					File exDir = new File(siteDir, exPath).getParentFile(); // core/master/example
//					FileUtils.forceMkdir(exDir);
//					String exName = resource.getRequiredProperty(EX.localName).getString(); // doorswitch
//					Model model = dataset.getNamedModel(resource.getURI());
//					for (Languages l : Languages.values()) {
//						File file = new File(exDir, String.format("%s.%s", exName, l.getExt()));
//						try (FileOutputStream fos = new FileOutputStream(file)) {
//							model.write(fos, l.getLang());
//						}
//					}
//					// File htmlFile = new File(exDir, String.format("%s.html", exName));
//					// try (IndentedWriter writer = new IndentedWriter(new
//					// FileOutputStream(htmlFile));) {
//					// Context context = ContextUtils.build(writer).setBase(Constants.BASE)
//					// .setDebugTemplate(!Constants.PRODUCTION).setInputDataset(dataset)
//					// .setStreamManager(streamManager).build();
//					// generateHTML(planForExample, VAR_EXAMPLE, resource, context);
//					// }
//				} catch (Exception ex) {
//					errorLogger.error(String.format("Unexpected exception while writing static files for %s", resource),
//							ex);
//				}
//			}
//		}
//
//		dataset.end();
	}

	private void generateHTML(RootPlan plan, Var var, Resource resource, Context context) {
		BindingHashMap binding = new BindingHashMap();
		binding.add(var, resource.asNode());
		List<Binding> bindings = new ArrayList<>();
		bindings.add(binding);
		plan.execTemplateStream(bindings, context);
	}
}