From 13caa265a9bc002a6f3ca0fef60f7556e2eb4f4c Mon Sep 17 00:00:00 2001 From: Austin Bookhart Date: Wed, 10 Jul 2019 11:30:28 -0400 Subject: [PATCH] Add support for Mapping data to shared fields - Adds a recursive structure similar to group parsing - SharedField xml is not contained in the data definition xml so we have to load it separately --- src/com/hannonhill/umt/api/SharedField.java | 16 +++++++ src/com/hannonhill/umt/service/RestApi.java | 17 +++++-- .../hannonhill/umt/service/XmlAnalyzer.java | 48 ++++++++++++------- 3 files changed, 59 insertions(+), 22 deletions(-) create mode 100644 src/com/hannonhill/umt/api/SharedField.java diff --git a/src/com/hannonhill/umt/api/SharedField.java b/src/com/hannonhill/umt/api/SharedField.java new file mode 100644 index 0000000..5d3add3 --- /dev/null +++ b/src/com/hannonhill/umt/api/SharedField.java @@ -0,0 +1,16 @@ +package com.hannonhill.umt.api; + +public class SharedField extends FolderContainedAsset +{ + private String xml; + + public String getXml() + { + return xml; + } + + public void setXml(String xml) + { + this.xml = xml; + } +} diff --git a/src/com/hannonhill/umt/service/RestApi.java b/src/com/hannonhill/umt/service/RestApi.java index cda46a9..445ba93 100644 --- a/src/com/hannonhill/umt/service/RestApi.java +++ b/src/com/hannonhill/umt/service/RestApi.java @@ -1,6 +1,6 @@ /* * Created on Jun 25, 2018 by tomusiaka - * + * * Copyright(c) 2000-2010 Hannon Hill Corporation. All rights reserved. */ package com.hannonhill.umt.service; @@ -18,6 +18,7 @@ import java.util.Map; import java.util.Set; +import com.hannonhill.umt.api.SharedField; import org.apache.commons.lang.xwork.StringUtils; import com.google.common.base.Charsets; @@ -283,7 +284,7 @@ private static String getCreatedAssetId(JsonObject createResult) /** * Gets all content types from given site - * + * * @param projectInformation * @return * @throws Exception @@ -330,7 +331,7 @@ public static Map getDataDefinitionFieldsForContent ContentType contentType) throws Exception { DataDefinition dataDefinition = readDataDefinition(projectInformation, contentType.getDataDefinitionId()); - return XmlAnalyzer.analyzeDataDefinitionXml(dataDefinition.getXml()); + return XmlAnalyzer.analyzeDataDefinitionXml(dataDefinition.getXml(), projectInformation); } /** @@ -427,6 +428,12 @@ public static XhtmlDataDefinitionBlock readXhtmlBlock(String id, ProjectInformat return getProperty(assetResult, "xhtmlDataDefinitionBlock", XhtmlDataDefinitionBlock.class); } + public static SharedField readSharedField(String id, ProjectInformation projectInformation) throws Exception + { + JsonObject assetResult = readAsset(projectInformation, new Identifier(id, "sharedField")); + return getProperty(assetResult, "sharedField", SharedField.class); + } + private static JsonObject readAsset(ProjectInformation projectInformation, Identifier identifier) throws Exception { return performApiRequest(projectInformation, "read", getJsonObjectWithIdentifier(identifier)).getAsJsonObject("asset"); @@ -452,13 +459,13 @@ private static JsonObject performApiRequest(String username, String password, St authentication.addProperty("password", password); data.add("authentication", authentication); - + DataOutputStream wr = new DataOutputStream(con.getOutputStream()); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(wr, "UTF-8")); writer.write(data.toString()); writer.close(); wr.close(); - + int responseCode = con.getResponseCode(); if (responseCode != 200) throw new Exception(responseCode + " response code"); diff --git a/src/com/hannonhill/umt/service/XmlAnalyzer.java b/src/com/hannonhill/umt/service/XmlAnalyzer.java index 06b680a..9750eba 100644 --- a/src/com/hannonhill/umt/service/XmlAnalyzer.java +++ b/src/com/hannonhill/umt/service/XmlAnalyzer.java @@ -1,6 +1,6 @@ /* * Created on Nov 20, 2009 by Artur Tomusiak - * + * * Copyright(c) 2000-2009 Hannon Hill Corporation. All rights reserved. */ package com.hannonhill.umt.service; @@ -14,6 +14,7 @@ import java.util.HashMap; import java.util.Map; +import com.hannonhill.umt.api.SharedField; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; @@ -27,7 +28,7 @@ /** * This class contains service methods for analyzing the xml file contents - * + * * @author Artur Tomusiak * @since 1.0 */ @@ -36,7 +37,7 @@ public class XmlAnalyzer /** * Analyzes a folder by going through each file in the folder and subfolders using * {@link #analyzeFile(File, ProjectInformation)}. - * + * * @param folder * @param assetTypes */ @@ -66,23 +67,23 @@ public static void analyzeFolder(Path folder, ProjectInformation projectInformat /** * Analyzes the data definition xml and returns a map of text fields and file chooser fields - * + * * @param xml * @return * @throws Exception */ - public static Map analyzeDataDefinitionXml(String xml) throws Exception + public static Map analyzeDataDefinitionXml(String xml, ProjectInformation projectInformation) throws Exception { Map returnMap = new HashMap<>(); Node rootNode = XmlUtil.convertXmlToNodeStructure(new InputSource(new StringReader(xml))); NodeList children = rootNode.getChildNodes(); - analyzeDataDefinitionGroup(children, "", "", returnMap); + analyzeDataDefinitionGroup(children, "", "", returnMap, projectInformation); return returnMap; } /** * Returns the value of the first "src" attribute found in given xml - * + * * @param xml * @return * @throws Exception @@ -98,7 +99,7 @@ public static String getFirstSrcAttribute(String xml) throws Exception /** * Checks if current node contains an src attribute and if not, then recursively checks all the ancestor * nodes and returns the values first one that contains. - * + * * @param node * @return */ @@ -120,14 +121,14 @@ private static String getFirstSrcAttribute(Node node) /** * Adds all the text fields to the returnMap recursively - * + * * @param children * @param identifierPrefix * @param labelPrefix * @param returnMap */ private static void analyzeDataDefinitionGroup(NodeList children, String identifierPrefix, String labelPrefix, - Map returnMap) + Map returnMap, ProjectInformation projectInformation) { for (int i = 0; i < children.getLength(); i++) { @@ -156,7 +157,7 @@ private static void analyzeDataDefinitionGroup(NodeList children, String identif // If group - go recursively, if text - add to the field list. Ignore asset choosers. if (node.getNodeName().equals("group")) - analyzeDataDefinitionGroup(node.getChildNodes(), identifierPrefix + identifier + "/", labelPrefix + label + "/", returnMap); + analyzeDataDefinitionGroup(node.getChildNodes(), identifierPrefix + identifier + "/", labelPrefix + label + "/", returnMap, projectInformation); else if (node.getNodeName().equals("text")) returnMap.put(newIdentifier, new DataDefinitionField(newIdentifier, newLabel, null, isMultiple(node), isWysiwyg(node))); else if (node.getNodeName().equals("asset") && node.getAttributes().getNamedItem("type") != null @@ -165,12 +166,25 @@ else if (node.getNodeName().equals("asset") && node.getAttributes().getNamedItem else if (node.getNodeName().equals("asset") && node.getAttributes().getNamedItem("type") != null && node.getAttributes().getNamedItem("type").getTextContent().equals("block")) returnMap.put(newIdentifier, new DataDefinitionField(newIdentifier, newLabel, ChooserType.BLOCK, isMultiple(node), false)); + else if (node.getNodeName().equals("shared-field")) + { + try { + SharedField sharedField = RestApi.readSharedField(node.getAttributes().getNamedItem("field-id").getNodeValue(), projectInformation); + Node sharedFieldNode = XmlUtil.convertXmlToNodeStructure(new InputSource(new StringReader(sharedField.getXml()))); + + analyzeDataDefinitionGroup(sharedFieldNode.getChildNodes(), identifierPrefix + identifier + "/", labelPrefix + label + "/", returnMap, projectInformation); + } + catch (Exception e) + { + //Failed to retrieve shared field + } + } } } /** * Returns true if provided node has a multiple="true" attribute - * + * * @param node * @return */ @@ -181,7 +195,7 @@ private static boolean isMultiple(Node node) /** * Returns true if provided node is a text node with wysiwyg="true" attribute - * + * * @param node * @return */ @@ -193,7 +207,7 @@ private static boolean isWysiwyg(Node node) /** * Adds the file to the list of files to process and collects the extension - * + * * @param file * @param projectInformation */ @@ -231,7 +245,7 @@ private static void analyzeFile(Path file, ProjectInformation projectInformation /** * Returns true if all the characters in a name are legal - * + * * @param name * @return */ @@ -250,7 +264,7 @@ public static final boolean allCharactersLegal(String name) /** * Removes illegal characters from provided string - * + * * @param name * @return */ @@ -267,7 +281,7 @@ public static final String removeIllegalCharacters(String name) /** * Wrapper for easily changing how we identify a "legal" character. - * + * * @param c * @return */