1 package com.proalpha.pds.generator;
4 import java.util.ArrayList;
6 import java.util.ListIterator;
7 import java.util.regex.Pattern;
9 import org.eclipse.core.resources.IFile;
10 import org.eclipse.core.resources.ResourceAttributes;
11 import org.eclipse.jface.dialogs.MessageDialog;
12 import org.eclipse.swt.widgets.Display;
13 import org.eclipse.ui.PlatformUI;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
17 import com.proalpha.pds.paconnector.parameter.StartparameterReader;
18 import com.proalpha.pds.paconnector.userexit.UserExitDelegate;
19 import com.proalpha.pds.templates.data.IGeneratorDataContainer;
20 import com.proalpha.pds.templates.helper.FileSystemUtils;
21 import com.proalpha.pds.templates.jaxb.JaxUnmarshallerGeneric;
22 import com.proalpha.pds.templates.jaxb.TtBAlpLine;
23 import com.proalpha.pds.templates.jaxb.TtBAlpLine.TtBAlpLineRow;
37 private static final Logger logger = LoggerFactory.getLogger(BalpGenerator.class);
39 private static final String CUSTOM_LEVEL_CUSTOMER =
"Y";
40 private static final String CUSTOM_LEVEL_PARTNER =
"X";
41 private static final String CUSTOM_LEVEL_COUNTRY =
"C";
42 private static final String CUSTOM_LEVEL_INDUSTRY =
"U";
43 private static final String CUSTOM_LEVEL_COMPONENT =
"Q";
45 private static final String B_ALP95_CDF =
"b__alp95.cdf";
46 private static final String B_ALP94_CDF =
"b__alp94.cdf";
47 private static final String B_ALP93_CDF =
"b__alp93.cdf";
48 private static final String B_ALP92_CDF =
"b__alp92.cdf";
49 private static final String B_ALP91_CDF =
"b__alp91.cdf";
51 private final IGeneratorDataContainer dataContainer;
52 private final UserExitDelegate userExitDelegate;
53 private final StartparameterReader startparameterReader;
64 public BalpGenerator(IGeneratorDataContainer datacontainer, UserExitDelegate userExitDelegate,
65 StartparameterReader startparameterReader)
throws Exception {
67 this.dataContainer = datacontainer;
68 this.userExitDelegate = userExitDelegate;
69 this.startparameterReader = startparameterReader;
71 initializeGenerator();
75 private void readXmlBalpFile(File xmlFile)
throws Exception {
77 if (!xmlFile.exists()) {
78 throw new Exception(
"File " + xmlFile.getAbsolutePath() +
" not exits.");
81 JaxUnmarshallerGeneric<TtBAlpLine> jaxb =
new JaxUnmarshallerGeneric<>(TtBAlpLine.class);
83 TtBAlpLine lines = jaxb.readXMLFile(xmlFile);
85 List<TtBAlpLine.TtBAlpLineRow> elementList = lines.getTtBAlpLineRow();
87 List<String> userExitList =
new ArrayList<>();
89 List<String> balpFileHeader =
new ArrayList<>();
91 ListIterator<TtBAlpLineRow> iterator = elementList.listIterator();
94 Boolean isHeaderFound = Boolean.FALSE;
97 while (iterator.hasNext()) {
99 line = (iterator.next()).getContent();
101 balpFileHeader.add(line);
103 if (line.contains(
"_MAIN-BLOCK")) {
104 isHeaderFound = Boolean.TRUE;
109 if (!isHeaderFound) {
110 MessageDialog.openInformation(Display.getCurrent().getActiveShell(),
111 xmlFile.getAbsolutePath() +
" has the wrong format", xmlFile.getAbsolutePath()
112 +
" file has the wrong format. The file has been regenerated. Please check the file.");
113 iterator = elementList.listIterator();
114 balpFileHeader.clear();
115 balpFileHeader.add(
"&ANALYZE-SUSPEND _UIB-CODE-BLOCK _CUSTOM _MAIN-BLOCK Include");
119 while (iterator.hasNext()) {
121 line = (iterator.next()).getContent();
122 if (isLineUserExitDefinition(line)) {
123 userExitList.add(line);
127 dataContainer.setUserExitFiles(userExitList);
128 dataContainer.setBALPTemplateList(balpFileHeader);
130 xmlFile.deleteOnExit();
133 private boolean isLineUserExitDefinition(String line) {
135 Pattern patternUserExit = Pattern.compile(
"^&GLOB.+");
136 Pattern patternInheritsClass = Pattern.compile(
"^&GLOB.+_INHERITS.+");
138 return (patternUserExit.matcher(line).find() || patternInheritsClass.matcher(line).find());
142 private void loadBalpValues() throws Exception {
144 String balpFile = getBAlpFile(dataContainer.getCustomLevel());
146 File xmlFile = userExitDelegate.extractBalpFileToXMLFile(balpFile);
148 readXmlBalpFile(xmlFile);
152 private String getBAlpFile(String customLevel) {
154 switch (customLevel) {
155 case CUSTOM_LEVEL_COUNTRY:
157 case CUSTOM_LEVEL_PARTNER:
159 case CUSTOM_LEVEL_CUSTOMER:
161 case CUSTOM_LEVEL_INDUSTRY:
163 case CUSTOM_LEVEL_COMPONENT:
171 private IFile getGeneratorOutputFile() {
173 IFile outputFile =
null;
174 String bAlpFile = getBAlpFile(dataContainer.getCustomLevel());
178 outputFile = FileSystemUtils.createFileInWorkingDirectory(startparameterReader.getWorkingDirectory(),
179 "basis/incl", bAlpFile);
184 if (outputFile.exists()) {
185 ResourceAttributes resourceAttributes =
new ResourceAttributes();
186 resourceAttributes.setReadOnly(
false);
187 outputFile.setResourceAttributes(resourceAttributes);
190 }
catch (Exception e) {
191 MessageDialog.openInformation(PlatformUI.getWorkbench().getDisplay().getActiveShell(),
192 "Error create output file",
193 "Could not create generator output file: " + bAlpFile +
"\n" + e.getMessage());
194 logger.error(e.getMessage(), e);
200 private void initializeGenerator() throws Exception {
204 dataContainer.setOutputFile(getGeneratorOutputFile());
214 public void generate() throws Exception {
216 ContentGenerator generator =
new ContentGenerator(
"b__alp Generator", dataContainer,
"pABalpxxcdf");
217 generator.disableOpenFileInEditor();
218 generator.schedule();