001package org.nasdanika.html.model.app.gen.maven;
002
003import java.io.File;
004import java.io.IOException;
005import java.util.Collection;
006import java.util.Map;
007import java.util.Map.Entry;
008
009import org.apache.maven.plugins.annotations.LifecyclePhase;
010import org.apache.maven.plugins.annotations.Mojo;
011import org.apache.maven.plugins.annotations.Parameter;
012import org.eclipse.emf.common.util.DiagnosticException;
013import org.eclipse.emf.common.util.URI;
014import org.nasdanika.common.Context;
015import org.nasdanika.common.NasdanikaException;
016import org.nasdanika.common.ProgressMonitor;
017import org.nasdanika.html.model.app.gen.SemanticSiteGenerator;
018
019/**
020 * Generates semantic site.
021 */
022@Mojo(name = "generate-semantic-site", defaultPhase = LifecyclePhase.SITE)
023public class SemanticSiteGeneratorMojo extends AbstractSemanticGeneratorMojo {
024
025        /**
026         * Directory to output generated site
027         */
028        @Parameter(defaultValue = "target/semantic-site")       
029        private File outputDirectory;
030
031        /**
032         * Working directory for storing intermediate files
033         */
034        @Parameter(defaultValue = "target/semantic-site-work-dir")      
035        private File workDirectory;
036        
037        /**
038         * If true the working directory is cleaned before generation
039         */
040        @Parameter      
041        private boolean cleanWorkDir;   
042                
043        /**
044         * URI of the page template. Resolved relative to the project base directory
045         */
046        @Parameter(required = true)     
047        private String pageTemplate;
048        
049        /**
050         * Site map domain/URL to output to sitemap.xml. 
051         * Site map is not generated if this parameter is not set.
052         */
053        @Parameter      
054        private String siteMapDomain;
055        
056        /**
057         * URL of the Drawio viewer script for rendering Drawio diagrams.
058         * Set if you are hosting your own Drawio site.
059         */
060        @Parameter      
061        private String drawioViewer;    
062        
063        /**
064         * Number of known/expected errors. E.g. some blank pages or broken links. Build fails if the number of actual errors reported is different from this parameter. 
065         */
066        @Parameter(required = false)    
067        private int errors;
068        
069        /**
070         * URI of the root action from which to generate a site. Resolved relative to the project base directory.
071         * The root action may reference actions generated from the semantic model using <code>action-resource</code> token.
072         */
073        @Parameter      
074        private String rootAction;
075    
076    
077        @Override
078        protected void execute(Context context, ProgressMonitor progressMonitor) {              
079                SemanticSiteGenerator semanticSiteGenerator = createSemanticSiteGenerator(context, progressMonitor);
080                
081                File baseDir = project.getBasedir();
082                URI baseDirURI = URI.createFileURI(baseDir.getAbsolutePath()).appendSegment("");
083                
084                URI semanticModelURI = URI.createURI(model).resolve(baseDirURI);
085                URI pageTemplateURI = URI.createURI(pageTemplate).resolve(baseDirURI);
086                URI rootActionURI = org.nasdanika.common.Util.isBlank(rootAction) ? null : URI.createURI(rootAction).resolve(baseDirURI);
087
088                try {
089                        Map<String, Collection<String>> errors = semanticSiteGenerator.generate(
090                                        semanticModelURI, 
091                                        rootActionURI,
092                                        pageTemplateURI, 
093                                        siteMapDomain, 
094                                        outputDirectory, 
095                                        workDirectory, 
096                                        cleanWorkDir);
097                        
098                        int errorCount = 0;                     
099                        for (Entry<String, Collection<String>> ee: errors.entrySet()) {
100                                getLog().error(ee.getKey());
101                                for (String error: ee.getValue()) {
102                                        ++errorCount;
103                                        getLog().error("\t" + error);
104                                }
105                        }
106                        if (errorCount != this.errors) {
107                                String message = "There are " + errorCount + " site errors";
108                                getLog().error(message);
109                                throw new NasdanikaException(message);
110                        }
111                } catch (IOException | DiagnosticException ex) {
112                        throw new NasdanikaException(ex);
113                }
114        }
115    
116
117}