Merge "AAI-1523 Tweak onap-java-formatter.xml"
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / util / AutoGenerateHtml.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  *  Modifications Copyright © 2018 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22 package org.onap.aai.util;
23
24 import java.io.File;
25 import java.io.IOException;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.ListIterator;
29
30 import org.onap.aai.setup.SchemaVersion;
31 import org.onap.aai.setup.SchemaVersions;
32 import org.onap.aai.util.swagger.GenerateSwagger;
33
34 import freemarker.template.TemplateException;
35 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
36
37 public class AutoGenerateHtml {
38     
39     private static final String AAI_GENERATE_VERSION = "aai.generate.version";
40     public static final String DEFAULT_SCHEMA_DIR = "../aai-schema";
41     //if the program is run from aai-common, use this directory as default"
42     public static final String ALT_SCHEMA_DIR = "aai-schema";
43     //used to check to see if program is run from aai-core
44     public static final String DEFAULT_RUN_DIR = "aai-core";
45
46     public static void main(String[] args) throws IOException, TemplateException {
47         String savedProperty = System.getProperty(AAI_GENERATE_VERSION);
48
49         AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
50             "org.onap.aai.config",
51             "org.onap.aai.setup"
52         );
53
54
55         SchemaVersions schemaVersions = ctx.getBean(SchemaVersions.class);
56         
57         List<SchemaVersion> versionsToGen = schemaVersions.getVersions();
58         Collections.sort(versionsToGen);
59         Collections.reverse(versionsToGen);
60         ListIterator<SchemaVersion> versionIterator = versionsToGen.listIterator();
61         String schemaDir;
62         if(System.getProperty("user.dir") != null && !System.getProperty("user.dir").contains(DEFAULT_RUN_DIR)) {
63             schemaDir = ALT_SCHEMA_DIR;
64           }
65           else {
66               schemaDir = DEFAULT_SCHEMA_DIR;
67           }
68           String release = System.getProperty("aai.release", "onap");
69         while (versionIterator.hasNext()) {
70             System.setProperty(AAI_GENERATE_VERSION, versionIterator.next().toString());
71             String yamlFile = schemaDir + "/src/main/resources/" + release + "/aai_swagger_yaml/aai_swagger_" + System.getProperty(AAI_GENERATE_VERSION)+ ".yaml";
72             File swaggerYamlFile = new File(yamlFile);
73             if(swaggerYamlFile.exists()) {
74                 GenerateSwagger.schemaVersions = schemaVersions;
75                 GenerateSwagger.main(args);
76             }
77         }
78         System.setProperty(AAI_GENERATE_VERSION, savedProperty);    
79     }
80 }