Commit a7b591e6 by Owen

modify xml generation, added ordering. fixed general apperance of xml.

parent 574e5618
......@@ -15,6 +15,7 @@ let newOption;
export let schema;
export async function generateFields(inputSchema, containerId) {
window.populateFields = populateFields;
schema = inputSchema;
let divContainer = document.getElementById(containerId);
......@@ -197,10 +198,8 @@ export async function generateFields(inputSchema, containerId) {
$(document).ready(function() {
$('form:first *:input[type!=hidden]:first').focus(); // Run code
});
}
}
......@@ -242,6 +241,32 @@ const noValidation = (key) => {
* @returns
* created input field element
*/
const inputHidden = (key, validation) => {
try {
const input = document.createElement('textarea');
input.setAttribute('rows', 5);
input.setAttribute('id', key);
input.setAttribute('name', key);
input.setAttribute('cols', 30);
input.setAttribute('autocomplete', 'off');
input.setAttribute('inputMode', validation.collection === 'email' ? 'email' : 'text');
input.addEventListener('focusout', handleInput);
return input;
} catch (err) {
throw err;
}
}
/**
*
* @param {*} key
* will serve as id of input field
* @param {*} validation
* validation of field from schema
* @returns
* created input field element
*/
const inputString = (key, validation) => {
try {
const {
......@@ -1829,21 +1854,37 @@ const deconstruct = async (section, container, classAttribute) => {
fieldLabel, hidden
} = section[key]
// Create hidden input for fields with hidden="y"
// if (hidden && hidden.toLowerCase() === "y") {
// const hiddenInput = document.createElement('input');
// hiddenInput.setAttribute('type', 'hidden');
// hiddenInput.setAttribute('name', key);
// hiddenInput.setAttribute('value', ''); // Set the value as needed
const validation = getValidation(key, schema)
// // Append the hidden input to the container
// container.appendChild(hiddenInput);
if (hidden) {
const hiddenField = document.createElement('div') // will contain input field and label
hiddenField.setAttribute('class', 'fieldContainer ' + classAttribute)
container.appendChild(hiddenField)
// // Skip creating other visible input elements for hidden fields
// continue;
// }
const labelContainer = document.createElement('div') // name beside input field
labelContainer.setAttribute('class', 'labelContainer ' + classAttribute)
hiddenField.appendChild(labelContainer)
const validation = getValidation(key, schema)
const label = document.createElement('label')
label.textContent = fieldLabel ? fieldLabel : `Missing label`
label.style.color = fieldLabel ? '#000000' : '#ff3333'
labelContainer.appendChild(label)
const inputContainer = document.createElement('div') // input field
inputContainer.setAttribute('class', 'inputContainer ' + classAttribute)
hiddenField.appendChild(inputContainer)
// default hiddeninputs to String
const hiddenInput = inputHidden(key, schema);
hiddenInput.setAttribute('class', classAttribute)
inputContainer.appendChild(hiddenInput);
hiddenField.style.display = 'none'; // Hide the input
hiddenField.classList.add('hidden'); // Add 'hidden' class
continue;
}
const newField = document.createElement('div') // will contain input field and label
newField.setAttribute('class', 'fieldContainer ' + classAttribute)
......@@ -1924,10 +1965,6 @@ const deconstruct = async (section, container, classAttribute) => {
}
input.setAttribute('class', classAttribute)
inputContainer.appendChild(input)
if (hidden && hidden !== undefined) {
newField.style.display = 'none'; // Hide the input
newField.classList.add('hidden'); // Add 'hidden' class
}
}
//keys na drop down
......@@ -2226,10 +2263,15 @@ export async function populateFields() {
for (let key of Object.keys(schema[doctype][section])) {
if (schema[doctype][section][key].aka == "field" + k) {
//console.log("key = " + key);
console.log("key = " + key + +", value ="+v);
//console.log("validation: " + schema[doctype][section][key].validation.collection);
fields["field" + k] = v;
if (schema[doctype][section][key].hidden){
document.getElementById(key).value = v;
continue;
}
if(schema[doctype][section][key].validation.collection === 'radiolist'){
//retrieve the radio button value from the XML
......
......@@ -22,6 +22,10 @@ export const validateInput = (fieldID, value) => {
if(!validation) return { valid: false, error: [`FieldName: '${fieldID}' not in schema`] }
// Skip validation for hidden fields
if (isFieldHidden(fieldID)){
return { valid: true };
}
switch(validation.collection) {
case 'email':
return validateEmail(validation, value)
......@@ -429,4 +433,15 @@ export const checkValidValues = (fieldID, value) => {
} catch(err) {
return { isValidValue: false, errMsg: [err]}
}
}
\ No newline at end of file
}
function isFieldHidden(key) {
for (const docType in schema) {
for (const section in schema[docType]) {
if (schema[docType][section][key] && schema[docType][section][key].hidden) {
return true;
}
}
}
return false;
}
\ No newline at end of file
......@@ -15,7 +15,7 @@ export async function WriteForm(e,metrics,doctype,section) {
// localStorage.setItem("submit", "1");
let fields = {};
for (var i=0;i<Nodes.length;i++) {
for (var i=0;i<Nodes.length;i++) {
if (Nodes[i].style.display === 'none' && !Nodes[i].classList.contains('hidden')) continue
let fid = Nodes[i].id;
if (fid == 'DocType' || fid == 'Section' || fid == '' || fid == "submitButton") continue
......@@ -73,8 +73,11 @@ export async function WriteForm(e,metrics,doctype,section) {
}
fields[Object.keys(lookup[fid]).includes('aka') ? lookup[fid].aka.replace("field", "") : fid] = Nodes[i].value;
}
let fieldOrder = extractAkaValues(schema, doctype, section);
console.log(fieldOrder);
let response = await createOutputXml(fields, myArray, doctype, section);
let response = await createOutputXml(fields, myArray, doctype, section, fieldOrder);
return response;
}
catch(Err) {
......@@ -83,17 +86,36 @@ export async function WriteForm(e,metrics,doctype,section) {
return false;
}
async function createOutputXml(fields, metrics, doctype, section) {
function extractAkaValues(json, doctype, section) {
const akaValues = [];
function recursiveExtract(obj) {
for (const key in obj) {
if (typeof obj[key] === 'object') {
recursiveExtract(obj[key]);
} else if (key === 'aka') {
akaValues.push(obj[key].replace('field', ''));
}
}
}
recursiveExtract(json[doctype][section]);
return akaValues.join('|');
}
async function createOutputXml(fields, metrics, doctype, section, fieldOrder) {
let response = null;
if (IS_RETRIEVE_FROM_BPO == "Y") {
response = await createBPOXML(fields, metrics, doctype, section);
response = await createBPOXML(fields, metrics, doctype, section, fieldOrder);
} else{
response = await createNonBPOXML(fields, metrics, doctype, section);
response = await createNonBPOXML(fields, metrics, doctype, section, fieldOrder);
}
return response;
}
async function createNonBPOXML(fields, metrics, doctype, section){
async function createNonBPOXML(fields, metrics, doctype, section, fieldOrder){
console.log(fields);
let fileExt = "";
switch(ENCODING_PASS){
case "PASS1":
......@@ -137,7 +159,8 @@ async function createNonBPOXML(fields, metrics, doctype, section){
"fields": fields,
"outputDir": TEMPORARY_FOLDER + "/" + fileNameOnly + "/" + fileName,
"doctype": doctype,
"section": section
"section": section,
"fieldOrder" : fieldOrder
}
let response = await fetch(urlWriteXml, {
......@@ -151,7 +174,7 @@ async function createNonBPOXML(fields, metrics, doctype, section){
return response;
}
async function createBPOXML(fields, metrics, doctype, section){
async function createBPOXML(fields, metrics, doctype, section, fieldOrder){
let elementId = sessionStorage.getItem("element_id");
let filePaths = JSON.parse(sessionStorage.getItem("dir_files"));
......@@ -176,7 +199,8 @@ async function createBPOXML(fields, metrics, doctype, section){
"fields": fields,
"outputDir": sessionStorage.getItem("element_file_loc") + "/" + (ENCODING_PASS == "PASS1" ? elementId + ".DTA" : elementId + ".DTB"),
"doctype": doctype,
"section": section
"section": section,
"fieldOrder" : fieldOrder
}
let response = await fetch(urlWriteXml, {
......
......@@ -2,5 +2,5 @@ export const PROJECT_CODE = "PROJCODE01";
export const ENCODING_PASS = "PASS1";
export const GFS_URL = "http://107.20.193.188/gfs-explorer-ws/svc/gfs-rest/";
export const TEMPORARY_FOLDER = "C:/Users/oang/Desktop/Mobile GDE/Elements";
export const TEMPORARY_FOLDER = "E:/Coding/Mobile GDE Elements";
export const GFS_ROOT_FOLDER = "/Users";
\ No newline at end of file
......@@ -2001,9 +2001,9 @@
"mandatory" : true
}
},
"ver_question3" : {
"fieldLabel" : "Verification Question 3",
"aka" : "field172",
"ver_question4" : {
"fieldLabel" : "Verification Question 4",
"aka" : "field173",
"validation" : {
"fieldLength" : 3.0,
"collection" : "radiolist",
......@@ -2011,9 +2011,9 @@
"mandatory" : true
}
},
"ver_question4" : {
"fieldLabel" : "Verification Question 4",
"aka" : "field173",
"ver_question3" : {
"fieldLabel" : "Verification Question 3",
"aka" : "field172",
"validation" : {
"fieldLength" : 3.0,
"collection" : "radiolist",
......
......@@ -24,4 +24,8 @@ public class Field {
this.no = no;
this.value = value;
}
public String getNo() {
return this.no;
}
}
......@@ -36,4 +36,8 @@ public class Record {
this.section = xml.getSection();
this.subRecord = new SubRecord(xml);
}
public SubRecord getSubRecord() {
return this.subRecord;
}
}
package com.svi.webgde.restservice.object;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import com.svi.webgde.restservice.utils.XMLUtil;
import com.thoughtworks.xstream.annotations.XStreamAlias;
......@@ -31,4 +33,37 @@ public class SubRecord {
this.eor = xml.getEor();
this.fields = XMLUtil.generateFields(xml);
}
/**
* Arrange the contents of the fields list based on the order specified in the pipe-delimited string.
*
* @param orderPipeDelimited Pipe-delimited string specifying the order.
*/
public void arrangeFields(String orderPipeDelimited) {
List<Field> sortedFields = fields.stream()
.sorted(Comparator.comparingInt(f -> getOrder(f.getNo(), orderPipeDelimited)))
.collect(Collectors.toList());
// Update the fields list with the sorted order
fields.clear();
fields.addAll(sortedFields);
}
/**
* Helper method to get the order of a field based on the pipe-delimited string.
*
* @param fieldNo The field number to get the order for.
* @param orderPipeDelimited The pipe-delimited string specifying the order.
* @return The order of the field.
*/
private int getOrder(String fieldNo, String orderPipeDelimited) {
String[] orderArray = orderPipeDelimited.split("\\|");
for (int i = 0; i < orderArray.length; i++) {
if (orderArray[i].equals(fieldNo)) {
return i;
}
}
// Default to a high value if not found (end of the list)
return orderArray.length;
}
}
package com.svi.webgde.restservice.object;
import java.util.LinkedHashMap;
import java.util.Map;
public class XMLContents {
......@@ -21,10 +22,19 @@ public class XMLContents {
private String imageName;
private int subRecordNo;
private String eor;
private Map<String, String> fields;
private Map<String, String> fields = new LinkedHashMap<>();;
private String outputDir;
private String doctype;
private String section;
private String fieldOrder;
public String getFieldOrder() {
return fieldOrder;
}
public void setFieldOrder(String fieldOrder) {
this.fieldOrder = fieldOrder;
}
public String getProjCode() {
return projCode;
......@@ -193,4 +203,5 @@ public class XMLContents {
public void setSection(String section) {
this.section = section;
}
}
......@@ -20,7 +20,10 @@ import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
......@@ -67,29 +70,40 @@ public class XMLUtil {
*/
public static String generateXML(XMLContents xml) throws TransformerException, ParserConfigurationException,
SAXException, IOException, XPathExpressionException {
XStream xs = new XStream(new DomDriver("ISO-8859-1", new XmlFriendlyNameCoder("_-", "_")));
XStream xs = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("_-", "_")));
xs.alias(xml.getProjCode(), OutputXML.class);
xs.processAnnotations(Record.class);
Header header = new Header(xml);
Record record = new Record(xml);
record.getSubRecord().arrangeFields(xml.getFieldOrder());
OutputXML output = new OutputXML(header, record);
// SAXParserFactory spf = SAXParserFactory.newInstance();
// SAXParser sp = spf.newSAXParser();
// Source source = new SAXSource(sp.getXMLReader(),
// new InputSource(new StringReader(xs.toXML(output).replaceAll("(?<![^\\W_])[ ](?![^\\W_])", " "))));
//SAXParserFactory spf = SAXParserFactory.newInstance();
//SAXParser sp = spf.newSAXParser();
//Source source = new SAXSource(sp.getXMLReader(),
//new InputSource(new StringReader(xs.toXML(output).replaceAll("(?<![^\\W_])[ ](?![^\\W_])", " "))));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(
new InputSource(new StringReader(xs.toXML(output).replaceAll("(?<![^\\W_])[ ](?![^\\W_])", " "))));
// Generate list of all empty Nodes, them remove them
XPath xp = XPathFactory.newInstance().newXPath();
NodeList nl = (NodeList) xp.evaluate("//text()[normalize-space(.)='']", document, XPathConstants.NODESET);
for (int i = 0; i < nl.getLength(); ++i) { // note the position of the '++'
Node node = nl.item(i);
node.getParentNode().removeChild(node);
}
DOMSource source = new DOMSource(document);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE, "no");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment