Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for Mapping data to Shared Fields #30 #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/com/hannonhill/umt/api/SharedField.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
17 changes: 12 additions & 5 deletions src/com/hannonhill/umt/service/RestApi.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,6 +14,7 @@
import java.nio.file.Files;
import java.util.*;

import com.hannonhill.umt.api.SharedField;
import org.apache.commons.lang.xwork.StringUtils;

import com.google.common.base.Charsets;
Expand Down Expand Up @@ -279,7 +280,7 @@ private static String getCreatedAssetId(JsonObject createResult)

/**
* Gets all content types from given site
*
*
* @param projectInformation
* @return
* @throws Exception
Expand Down Expand Up @@ -326,7 +327,7 @@ public static Map<String, DataDefinitionField> getDataDefinitionFieldsForContent
ContentType contentType) throws Exception
{
DataDefinition dataDefinition = readDataDefinition(projectInformation, contentType.getDataDefinitionId());
return XmlAnalyzer.analyzeDataDefinitionXml(dataDefinition.getXml());
return XmlAnalyzer.analyzeDataDefinitionXml(dataDefinition.getXml(), projectInformation);
}

/**
Expand Down Expand Up @@ -423,6 +424,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");
Expand All @@ -449,13 +456,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");
Expand Down
47 changes: 30 additions & 17 deletions src/com/hannonhill/umt/service/XmlAnalyzer.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,6 +12,7 @@
import java.nio.file.Path;
import java.util.*;

import com.hannonhill.umt.api.SharedField;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
Expand All @@ -25,7 +26,7 @@

/**
* This class contains service methods for analyzing the xml file contents
*
*
* @author Artur Tomusiak
* @since 1.0
*/
Expand All @@ -34,7 +35,6 @@ public class XmlAnalyzer
/**
* Analyzes a folder by going through each file in the folder and subfolders using
* {@link XmlAnalyzer#analyzeFile(Path, ProjectInformation)}.
*
* @param folder
* @param projectInformation
*/
Expand Down Expand Up @@ -64,23 +64,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<String, DataDefinitionField> analyzeDataDefinitionXml(String xml) throws Exception
public static Map<String, DataDefinitionField> analyzeDataDefinitionXml(String xml, ProjectInformation projectInformation) throws Exception
{
Map<String, DataDefinitionField> returnMap = new LinkedHashMap<>();
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
Expand All @@ -96,7 +96,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
*/
Expand All @@ -118,14 +118,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<String, DataDefinitionField> returnMap)
Map<String, DataDefinitionField> returnMap, ProjectInformation projectInformation)
{
for (int i = 0; i < children.getLength(); i++)
{
Expand Down Expand Up @@ -154,7 +154,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
Expand All @@ -163,12 +163,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
*/
Expand All @@ -179,7 +192,7 @@ private static boolean isMultiple(Node node)

/**
* Returns true if provided node is a text node with wysiwyg="true" attribute
*
*
* @param node
* @return
*/
Expand All @@ -191,7 +204,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
*/
Expand Down Expand Up @@ -229,7 +242,7 @@ private static void analyzeFile(Path file, ProjectInformation projectInformation

/**
* Returns true if all the characters in a name are legal
*
*
* @param name
* @return
*/
Expand All @@ -248,7 +261,7 @@ public static final boolean allCharactersLegal(String name)

/**
* Removes illegal characters from provided string
*
*
* @param name
* @return
*/
Expand All @@ -265,7 +278,7 @@ public static final String removeIllegalCharacters(String name)

/**
* Wrapper for easily changing how we identify a "legal" character.
*
*
* @param c
* @return
*/
Expand Down