Changes for checkstyle 8.32
[policy/apex-pdp.git] / model / basic-model / src / main / java / org / onap / policy / apex / model / basicmodel / handling / ApexModelFileWriter.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.model.basicmodel.handling;
22
23 import java.io.File;
24 import java.io.FileOutputStream;
25 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
26 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
27 import org.slf4j.ext.XLogger;
28 import org.slf4j.ext.XLoggerFactory;
29
30 /**
31  * This class writes an Apex model to a file.
32  *
33  * @author Liam Fallon (liam.fallon@ericsson.com)
34  * @param <M> the type of Apex model to write to file, must be a sub class of {@link AxModel}
35  */
36 public class ApexModelFileWriter<M extends AxModel> {
37     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexModelFileWriter.class);
38
39     // Should models being written to files be valid
40     private boolean validateFlag;
41
42     /**
43      * Constructor, set the validation flag.
44      *
45      * @param validateFlag indicates if validation be performed prior to output
46      */
47     public ApexModelFileWriter(final boolean validateFlag) {
48         this.validateFlag = validateFlag;
49     }
50
51     /**
52      * Write a model to an XML file.
53      *
54      * @param model The model to write
55      * @param rootModelClass The concept class
56      * @param modelFileName The name of the file to write to
57      * @throws ApexException thrown on errors
58      */
59     public void apexModelWriteXmlFile(final M model, final Class<M> rootModelClass, final String modelFileName)
60                     throws ApexException {
61         LOGGER.debug("running apexModelWriteXMLFile . . .");
62
63         final ApexModelWriter<M> modelWriter = new ApexModelWriter<>(rootModelClass);
64         modelWriter.setValidateFlag(validateFlag);
65         modelWriter.getCDataFieldSet().add("description");
66         modelWriter.getCDataFieldSet().add("logic");
67         modelWriter.getCDataFieldSet().add("uiLogic");
68
69         writeModelFile(model, modelWriter, modelFileName);
70
71         LOGGER.debug("ran apexModelWriteXMLFile");
72     }
73
74     /**
75      * Write a model to an JSON file.
76      *
77      * @param model The model to write
78      * @param rootModelClass The concept class
79      * @param modelFileName The name of the file to write to
80      * @throws ApexException thrown on errors
81      */
82     public void apexModelWriteJsonFile(final M model, final Class<M> rootModelClass, final String modelFileName)
83                     throws ApexException {
84         LOGGER.debug("running apexModelWriteJSONFile . . .");
85
86         final ApexModelWriter<M> modelWriter = new ApexModelWriter<>(rootModelClass);
87         modelWriter.setJsonOutput(true);
88         modelWriter.setValidateFlag(validateFlag);
89
90         writeModelFile(model, modelWriter, modelFileName);
91
92         LOGGER.debug("ran apexModelWriteJSONFile");
93     }
94
95     /**
96      * Checks if the validation flag is set.
97      *
98      * @return true, the validation flag is set
99      */
100     public boolean isValidateFlag() {
101         return validateFlag;
102     }
103
104     /**
105      * Sets the validate flag.
106      *
107      * @param validateFlag the validate flag value
108      */
109     public void setValidateFlag(final boolean validateFlag) {
110         this.validateFlag = validateFlag;
111     }
112
113     /**
114      * Write a model to a file using a model writer.
115      *
116      * @param model The model to write
117      * @param modelWriter the model writer to use to write the model to the file
118      * @param modelFileName the file name of the file to write to
119      * @throws ApexException on exceptions writing the model
120      */
121     private void writeModelFile(final M model, final ApexModelWriter<M> modelWriter, final String modelFileName)
122                     throws ApexException {
123         final File modelFile = new File(modelFileName);
124         if (!modelFile.getParentFile().exists() && !modelFile.getParentFile().mkdirs()) {
125             LOGGER.warn("could not create directory  " + modelFile.getParentFile());
126             throw new ApexException("could not create directory  " + modelFile.getParentFile());
127         }
128
129         try {
130             final FileOutputStream fileOutputStream = new FileOutputStream(modelFile);
131             modelWriter.write(model, fileOutputStream);
132             fileOutputStream.close();
133         } catch (final Exception e) {
134             LOGGER.warn("error processing file " + modelFile.getAbsolutePath(), e);
135             throw new ApexException("error processing file " + modelFile.getAbsolutePath(), e);
136         }
137     }
138 }