public static Model transform(Document doc, JavaScriptObject aTarget) throws Exception { try { Model model = new Model(AppClient.getInstance()); XmlDom2Model transformer = new XmlDom2Model(doc, aTarget); model.accept(transformer); return model; } catch (Exception ex) { Logger.getLogger(Model.class.getName()).log(Level.SEVERE, null, ex); throw ex; } }
protected void readModel(Model aModel) throws Exception { Element el = DATAMODEL_TAG_NAME.equals(doc.getDocumentElement().getNodeName()) ? doc.getDocumentElement() : Utils.getElementByTagName(doc.getDocumentElement(), DATAMODEL_TAG_NAME); if (el != null && aModel != null) { model = aModel; try { currentTag = el; NodeList nl = currentTag.getChildNodes(); if (nl != null && nl.getLength() > 0) { Element lcurrentTag = currentTag; try { for (int i = 0; i < nl.getLength(); i++) { if (ENTITY_TAG_NAME.equals(nl.item(i).getNodeName())) { currentTag = (Element) nl.item(i); Entity entity = new Entity(); entity.accept(this); } else if (RELATION_TAG_NAME.equals(nl.item(i).getNodeName())) { currentTag = (Element) nl.item(i); Relation relation = new Relation(); relation.accept(this); } else if (REFERENCE_RELATION_TAG_NAME.equals(nl.item(i).getNodeName())) { currentTag = (Element) nl.item(i); Relation relation = new ReferenceRelation(); relation.accept(this); } } } finally { currentTag = lcurrentTag; } } model.validateQueries(); for (Runnable resolver : relationsResolvers) { resolver.run(); } model.checkRelationsIntegrity(); } finally { relationsResolvers = null; model = null; } } }
@Override public void visit(final Relation relation) { if (relation != null && model != null) { NamedNodeMap attrs = currentTag.getAttributes(); Node lefna = attrs.getNamedItem(LEFT_ENTITY_FIELD_ATTR_NAME); Node lepna = attrs.getNamedItem(LEFT_ENTITY_PARAMETER_ATTR_NAME); Node refna = attrs.getNamedItem(RIGHT_ENTITY_FIELD_ATTR_NAME); Node repna = attrs.getNamedItem(RIGHT_ENTITY_PARAMETER_ATTR_NAME); final String leftEntityId = readAttribute(LEFT_ENTITY_ID_ATTR_NAME, null); final String leftFieldName = lefna != null ? lefna.getNodeValue() : null; final String leftParameterName = lepna != null ? lepna.getNodeValue() : null; final String rightEntityId = readAttribute(RIGHT_ENTITY_ID_ATTR_NAME, null); final String rightFieldName = refna != null ? refna.getNodeValue() : null; final String rightParameterName = repna != null ? repna.getNodeValue() : null; model.addRelation(relation); Runnable resolver = new Runnable() { @Override public void run() { try { Entity lEntity = model.getEntityById(leftEntityId); if (lEntity != null) { if (leftParameterName != null && !leftParameterName.isEmpty()) { relation.setLeftField( lEntity.getQuery().getParameters().get(leftParameterName)); } else if (leftFieldName != null && !leftFieldName.isEmpty()) { relation.setLeftField(lEntity.getFields().get(leftFieldName)); } } relation.setLeftEntity(lEntity); lEntity.addOutRelation(relation); Entity rEntity = model.getEntityById(rightEntityId); if (rEntity != null) { if (rightParameterName != null && !rightParameterName.isEmpty()) { relation.setRightField( rEntity.getQuery().getParameters().get(rightParameterName)); } else if (rightFieldName != null && !rightFieldName.isEmpty()) { relation.setRightField(rEntity.getFields().get(rightFieldName)); } } relation.setRightEntity(rEntity); rEntity.addInRelation(relation); } catch (Exception ex) { Logger.getLogger(XmlDom2Model.class.getName()).log(Level.SEVERE, null, ex); } } }; relationsResolvers.add(resolver); } }
protected void readEntity(Entity entity) { if (entity != null) { String entityId = readAttribute(ENTITY_ID_ATTR_NAME, null); if ("null".equals(entityId)) { entityId = null; } assert entityId != null : "Entity id must be provided"; entity.setEntityId(entityId); String queryId = readAttribute(QUERY_ID_ATTR_NAME, null); if ("null".equals(queryId)) { queryId = null; } assert queryId != null : "Query id must be provided"; entity.setQueryName(queryId); assert model != null; entity.setModel(model); model.addEntity(entity); } }