X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Faai%2Fmodelloader%2Fentity%2Fmodel%2FNamedQueryArtifactParser.java;h=5b9488e02565ac355cf3f2e399094c2291967c5d;hb=refs%2Fchanges%2F49%2F40749%2F1;hp=8188b28564a367631d098e533a92245ff1cda6df;hpb=4dd316529148d07059d844197cdb676806bdc0c6;p=aai%2Fmodel-loader.git diff --git a/src/main/java/org/onap/aai/modelloader/entity/model/NamedQueryArtifactParser.java b/src/main/java/org/onap/aai/modelloader/entity/model/NamedQueryArtifactParser.java index 8188b28..5b9488e 100644 --- a/src/main/java/org/onap/aai/modelloader/entity/model/NamedQueryArtifactParser.java +++ b/src/main/java/org/onap/aai/modelloader/entity/model/NamedQueryArtifactParser.java @@ -20,114 +20,86 @@ */ package org.onap.aai.modelloader.entity.model; +import java.util.List; + import org.onap.aai.modelloader.entity.Artifact; import org.onap.aai.modelloader.service.ModelLoaderMsgs; import org.onap.aai.cl.api.Logger; import org.onap.aai.cl.eelf.LoggerFactory; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; -import org.xml.sax.InputSource; - -import java.io.StringReader; -import java.util.ArrayList; -import java.util.List; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; +import org.w3c.dom.Node; -public class NamedQueryArtifactParser implements IModelParser { +public class NamedQueryArtifactParser extends AbstractModelArtifactParser { - private static String NAMED_QUERY_VERSION_ID = "named-query-uuid"; - private static String RELATIONSHIP_DATA = "relationship-data"; - private static String RELATIONSHIP_KEY = "relationship-key"; - private static String RELATIONSHIP_VALUE = "relationship-value"; - private static String MODEL_ELEMENT_RELATIONSHIP_KEY = "model.model-invariant-id"; + private static final String NAMED_QUERY_VERSION_ID = "named-query-uuid"; + private static final String MODEL_ELEMENT_RELATIONSHIP_KEY = "model.model-invariant-id"; - - private static Logger logger = LoggerFactory.getInstance().getLogger(NamedQueryArtifactParser.class.getName()); - - public List parse(byte[] artifactPayload, String artifactName) { - String payload = new String(artifactPayload); - List modelList = new ArrayList(); + private static Logger logger = LoggerFactory.getInstance().getLogger(NamedQueryArtifactParser.class.getName()); - try { - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - DocumentBuilder builder = factory.newDocumentBuilder(); - InputSource is = new InputSource(new StringReader(payload)); - Document doc = builder.parse(is); + /** + * {@inheritDoc} + */ + @Override + boolean processParsedModel(List modelList, String artifactName, IModelArtifact model) { + boolean valid = false; - NamedQueryArtifact model = parseModel(doc.getDocumentElement(), payload); + if (model != null) { + logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, + "Named-Query parsed =====>>>> " + "Named-Query-UUID: " + ((NamedQueryArtifact) model) + .getNamedQueryUuid()); + modelList.add((NamedQueryArtifact) model); - if (model != null) { - logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Named-Query parsed =====>>>> " + "Named-Query-UUID: "+ model.getNamedQueryUuid()); - modelList.add(model); - } - else { - logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse named-query artifact " + artifactName); - return null; - } - } - catch (Exception ex) { - logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse named-query artifact " + artifactName + ": " + ex.getLocalizedMessage()); - } + valid = true; + } else { + logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, + "Unable to parse named-query artifact " + artifactName); + } - return modelList; - } + return valid; + } - private NamedQueryArtifact parseModel(Node modelNode, String payload) { - NamedQueryArtifact model = new NamedQueryArtifact(); - model.setPayload(payload); + /** + * {@inheritDoc} + */ + @Override + String buildArtifactParseExceptionMessage(String artifactName, String localisedMessage) { + return "Unable to parse named-query artifact " + artifactName + ": " + localisedMessage; + } - Element e = (Element)modelNode; - model.setModelNamespace(e.getAttribute("xmlns")); + @Override + String getModelElementRelationshipKey() { + return MODEL_ELEMENT_RELATIONSHIP_KEY; + } - parseNode(modelNode, model); + /** + * {@inheritDoc} + */ + @Override + String getVersionIdNodeName() { + return NAMED_QUERY_VERSION_ID; + } - if (model.getNamedQueryUuid() == null) { - return null; + /** + * {@inheritDoc} + */ + @Override + void setVersionId(IModelArtifact model, Node node) { + ((NamedQueryArtifact) model).setNamedQueryUuid(node.getTextContent().trim()); } - return model; - } - - private void parseNode(Node node, NamedQueryArtifact model) { - if (node.getNodeName().equalsIgnoreCase(NAMED_QUERY_VERSION_ID)) { - model.setNamedQueryUuid(node.getTextContent().trim()); - } - else if (node.getNodeName().equalsIgnoreCase(RELATIONSHIP_DATA)) { - parseRelationshipNode(node, model); - } - else { - NodeList nodeList = node.getChildNodes(); - for (int i = 0; i < nodeList.getLength(); i++) { - Node childNode = nodeList.item(i); - parseNode(childNode, model); - } - } - } - - private void parseRelationshipNode(Node node, NamedQueryArtifact model) { - String key = null; - String value = null; - - NodeList nodeList = node.getChildNodes(); - for (int i = 0; i < nodeList.getLength(); i++) { - Node childNode = nodeList.item(i); - if (childNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_KEY)) { - key = childNode.getTextContent().trim(); - } - else if (childNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_VALUE)) { - value = childNode.getTextContent().trim(); - } + /** + * {@inheritDoc} + */ + @Override + IModelArtifact createModelArtifactInstance() { + return new NamedQueryArtifact(); } - - if ( (key != null) && (key.equalsIgnoreCase(MODEL_ELEMENT_RELATIONSHIP_KEY )) ) { - if (value != null) { - model.addDependentModelId(value); - } + + /** + * {@inheritDoc} + */ + @Override + boolean modelIsValid(IModelArtifact model) { + return ((NamedQueryArtifact) model).getNamedQueryUuid() != null; } - } }