package com.sysmodel.datamodel.xmlmanager; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import com.sysmodel.datamodel.xmlmodel.Reference; import com.sysmodel.datamodel.xmlmodel.impl.MdpAttributeImpl; import com.sysmodel.datamodel.xmlmodel.impl.MdpClassImpl; import com.sysmodel.datamodel.xmlmodel.impl.MdpConstantImpl; import com.sysmodel.datamodel.xmlmodel.impl.MdpDataSourceImpl; import com.sysmodel.datamodel.xmlmodel.impl.MdpModuleImpl; import com.sysmodel.datamodel.xmlmodel.impl.PropertyImpl; import com.sysmodel.datamodel.xmlmodel.impl.RelationImpl; import com.sysmodel.datamodel.xmlmodel.impl.SysModelImpl; import com.sysmodel.datamodel.xmlmodel.impl.TemplateImpl; import com.sysmodel.xformmodel.able.Validate; import com.toolkit.file.FileOperate; public class XMLSave{ private Log log = LogFactory.getLog(XMLSave.class); public boolean saveToXMLFile(String filePath) { boolean bsuccess = true; log.info("DataModel.xml filePath = " + filePath); try { DocumentBuilderFactory domfactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = domfactory.newDocumentBuilder(); Document doc = builder.newDocument(); createXMLDoc(doc); FileOperate FileOperate = new FileOperate(); bsuccess = FileOperate.saveDocumentFromDomToXMLFile(doc, filePath); } catch (Exception e) { bsuccess = false; log.error(e); } return bsuccess; } private void createXMLDoc(Document doc) { Element root = doc.createElement("sysmodel"); doc.appendChild(root); /** * create about */ creatAbout(doc); /** * create datasource */ createDataSource(doc); /** * create MdpModule */ createMdpModule(doc); /** * create constant */ createConstant(doc); /** * create mdpClass */ createMdpClass(doc); } // create about private void creatAbout(Document doc) { Element root = doc.getDocumentElement(); Element author = doc.createElement("author"); author.appendChild(doc.createTextNode(SysModelImpl.getInstance().getAuthor())); Element version = doc.createElement("version"); version.appendChild(doc.createTextNode(SysModelImpl.getInstance().getVersion())); root.appendChild(author); root.appendChild(version); } // create datasources private void createDataSource(Document doc) { Element root = doc.getDocumentElement(); Element listElement = doc.createElement("mdpDataSources"); List dataSource = SysModelImpl.getInstance().getMdpDataSources(); for (int i = 0; i < dataSource.size(); i++) { MdpDataSourceImpl datasource = dataSource.get(i); Element element = doc.createElement("mdpDataSource"); element.setAttribute("dataSourceid", datasource.getDataSourceid() + ""); element.setAttribute("description", datasource.getDescription() == null ? "" : datasource.getDescription()); element.setAttribute("dataBase", datasource.getDataBase() + ""); element.setAttribute("url", datasource.getURL() == null ? "" : datasource.getURL()); element.setAttribute("user", datasource.getUser() == null ? "" : datasource.getUser()); element.setAttribute("password", datasource.getPassword() == null ? "" : datasource.getPassword()); element.setAttribute("jndi", datasource.getJNDI() == null ? "" : datasource.getJNDI()); // element.setAttribute("dataSourceType",datasource.getDataSourceType()==null // ? "":datasource.getDataSourceType()); listElement.appendChild(element); } root.appendChild(listElement); } // create constant private void createConstant(Document doc) { Element root = doc.getDocumentElement(); Element listElement = doc.createElement("mdpConstants"); // create template createTemplate(listElement, doc); List listConstant = SysModelImpl.getInstance().getMdpConstants(); for (int i = 0; i < listConstant.size(); i++) { MdpConstantImpl constant = listConstant.get(i); Element constantElement = doc.createElement("mdpConstant"); // create self attribute constantElement.setAttribute("type", constant.getType() == null ? "" : constant.getType()); constantElement.setAttribute("codeName", constant.getCodeName() == null ? "" : constant.getCodeName()); constantElement.setAttribute("ngoogleEn", constant.getNgoogleEn() == null ? "" : constant.getNgoogleEn()); constantElement.setAttribute("description", constant.getDescription() == null ? "" : constant.getDescription()); constantElement.setAttribute("templateName", constant.getTemplateName() == null ? "" : constant.getTemplateName()); constantElement.setAttribute("rootFrom", constant.getRootFrom() == null ? "" : constant.getRootFrom()); constantElement.setAttribute("dataSource", constant.getDataSource() + ""); constantElement.setAttribute("keyCodeAuto", String.valueOf(constant.isKeyCodeAuto())); constantElement.setAttribute("orderProperty", constant.getOrderProperty() == null ? "" : constant.getOrderProperty()); constantElement.setAttribute("orderMethod", constant.getOrderMethod() == null ? "" : constant.getOrderMethod()); // add child listElement.appendChild(constantElement); } root.appendChild(listElement); } // create template private void createTemplate(Element listElement, Document doc) { Element listTemplatesElement = doc.createElement("templates"); List listTemplates = SysModelImpl.getInstance().getTemplates(); for (int i = 0; i < listTemplates.size(); i++) { TemplateImpl template = listTemplates.get(i); Element templateElement = doc.createElement("template"); templateElement.setAttribute("name", template.getName() == null ? "" : template.getName()); templateElement.setAttribute("tableName", template.getTableName() == null ? "" : template.getTableName()); templateElement.setAttribute("dataSource", template.getDataSource() + ""); templateElement.setAttribute("description", template.getDescription() == null ? "" : template.getDescription()); // create property createProperty(templateElement, template.getListproperty(), doc); // add templates element listTemplatesElement.appendChild(templateElement); } // add mdpConstants element listElement.appendChild(listTemplatesElement); } // create property private void createProperty(Element constantElement, List listProperty, Document doc) { for (int i = 0; i < listProperty.size(); i++) { Element propertyElement = doc.createElement("property"); PropertyImpl property = listProperty.get(i); propertyElement.setAttribute("name", property.getName() == null ? "" : property.getName()); propertyElement.setAttribute("description", property.getDescription() == null ? "" : property.getDescription()); propertyElement.setAttribute("dataType", property.getDataType()); propertyElement.setAttribute("length", property.getLength() + ""); propertyElement.setAttribute("refConstant", property.getRefConstant() + ""); propertyElement.setAttribute("scale", property.getScale() == null ? "" : property.getScale()); propertyElement.setAttribute( "referenceParentName", property.getReferenceParentName() == null ? "" : property .getReferenceParentName()); propertyElement.setAttribute("display", String.valueOf(property.isDisplay())); propertyElement.setAttribute("unique", String.valueOf(property.isUnique())); creatvalidates(propertyElement, doc, property); constantElement.appendChild(propertyElement); } } private void creatvalidates(Element pagereportField, Document doc, PropertyImpl property) { Element validatesField = doc.createElement("validates"); Validate Validate = property.getValidate(); if (Validate.isNeed()) { Element validateNode = doc.createElement("validate"); validateNode.setAttribute("type", "need"); validatesField.appendChild(validateNode); } if (Validate.isTrim()) { Element validateNode = doc.createElement("validate"); validateNode.setAttribute("type", "trim"); validatesField.appendChild(validateNode); } if (!Validate.getMaxLength().equals("")) { Element validateNode = doc.createElement("validate"); validateNode.setAttribute("type", "length"); validateNode.setAttribute("maxLength", Validate.getMaxLength()); validatesField.appendChild(validateNode); } if (!Validate.getRegxValue().equals("")) { Element validateNode = doc.createElement("validate"); validateNode.setAttribute("type", "regx"); validateNode.setAttribute("regxValue", Validate.getRegxValue()); validatesField.appendChild(validateNode); } if (!Validate.getDojoType().equals("")) { Element validateNode = doc.createElement("validate"); validateNode.setAttribute("type", "dojoType"); validateNode.setAttribute("typeValue", Validate.getDojoType()); validatesField.appendChild(validateNode); } if (!Validate.getMaxValue().equals("") || !Validate.getMinValue().equals("")) { Element validateNode = doc.createElement("validate"); validateNode.setAttribute("type", "range"); validateNode.setAttribute("maxValue", Validate.getMaxValue()); validateNode.setAttribute("minValue", Validate.getMinValue()); validatesField.appendChild(validateNode); } if (!Validate.getInvalidMessage().equals("") || !Validate.getPromptMessage().equals("")) { Element validateNode = doc.createElement("validate"); validateNode.setAttribute("type", "message"); validateNode.setAttribute("promptMessage", Validate.getPromptMessage()); validateNode.setAttribute("invalidMessage", Validate.getInvalidMessage()); validatesField.appendChild(validateNode); } pagereportField.appendChild(validatesField); } // create MdpClass private void createMdpClass(Document doc) { Element root = doc.getDocumentElement(); Element listElement = doc.createElement("mdpClasses"); List listMdpClass = SysModelImpl.getInstance().getMdpClasses(); for (int i = 0; i < listMdpClass.size(); i++) { MdpClassImpl mdpclass = listMdpClass.get(i); Element classElement = doc.createElement("mdpClass"); // create self attribute classElement.setAttribute("classid", mdpclass.getClassid() + ""); classElement.setAttribute("type", mdpclass.getType() == null ? "" : mdpclass.getType()); classElement.setAttribute("validate", String.valueOf(mdpclass.isValidate())); classElement.setAttribute("dataSource", mdpclass.getDataSource() + ""); classElement.setAttribute("name", mdpclass.getName() == null ? "" : mdpclass.getName()); /** * classElement.setAttribute("ngoogleEn",mdpclass.getNgoogleEn()== * null ? "":mdpclass.getNgoogleEn()); * classElement.setAttribute("oldName",mdpclass.getOldName() == null * ? "":mdpclass.getOldName()); */ classElement.setAttribute("description", mdpclass.getDescription() == null ? "" : mdpclass.getDescription()); classElement.setAttribute("exist", String.valueOf(mdpclass.isExist())); classElement.setAttribute("catche", String.valueOf(mdpclass.isCatche())); // create MdpAttribute createMdpAttribute(classElement, mdpclass.getAllMdpAttributes(), doc); createRelation(classElement, mdpclass.getRelations(), doc); classElement.setAttribute("primaryKey", mdpclass.getPrimaryKey() == null ? "" : mdpclass.getPrimaryKey()); // add child listElement.appendChild(classElement); } root.appendChild(listElement); } // create MdpModule private void createMdpModule(Document doc) { Element root = doc.getDocumentElement(); Element listElement = doc.createElement("mdpModules"); List listmdpmodule = SysModelImpl.getInstance().getMdpModules(); for (int i = 0; i < listmdpmodule.size(); i++) { MdpModuleImpl mdpmodule = listmdpmodule.get(i); Element classElement = doc.createElement("module"); // create self attribute classElement.setAttribute("pcode", mdpmodule.getPcode() + ""); classElement.setAttribute("code", mdpmodule.getCode() + ""); classElement.setAttribute("name", mdpmodule.getName()); classElement.setAttribute("ngoogleEn", mdpmodule.getNgoogleEn()); classElement.setAttribute("tPrefix", mdpmodule.getTPrefix()); classElement.setAttribute("description", mdpmodule.getDescription() + ""); classElement.setAttribute("packageName", mdpmodule.getPackageName() + ""); // add child listElement.appendChild(classElement); } root.appendChild(listElement); } // create relation private void createRelation(Element classElement, List listRelations, Document doc) { for (int i = 0; i < listRelations.size(); i++) { Element realtionElement = doc.createElement("relation"); RelationImpl relation = listRelations.get(i); realtionElement.setAttribute("type", relation.getType() + ""); realtionElement.setAttribute("guestClassid", relation.getGuestClassid() + ""); classElement.appendChild(realtionElement); } } // create MdpAttribute private void createMdpAttribute(Element classElement, List listMdpAttributes, Document doc) { for (int i = 0; i < listMdpAttributes.size(); i++) { Element mdpAttributeElement = doc.createElement("mdpAttribute"); MdpAttributeImpl attribute = listMdpAttributes.get(i); mdpAttributeElement.setAttribute("name", attribute.getName() == null ? "" : attribute.getName()); /** * mdpAttributeElement.setAttribute("ngoogleEn",attribute. * getNgoogleEn()==null ? "":attribute.getNgoogleEn()); * mdpAttributeElement * .setAttribute("oldName",attribute.getOldName()==null ? * "":attribute.getOldName()); */ mdpAttributeElement.setAttribute("description", attribute.getDescription() == null ? "" : attribute.getDescription()); mdpAttributeElement.setAttribute("unit", attribute.getUnit() == null ? "" : attribute.getUnit()); mdpAttributeElement.setAttribute("dataType", String.valueOf(attribute.getDataType())); mdpAttributeElement.setAttribute("precision", String.valueOf(attribute.getPrecision())); mdpAttributeElement.setAttribute("scale", attribute.getScale() == null ? "" : attribute.getScale()); mdpAttributeElement.setAttribute("defaultValue", attribute.getDefaultValue() == null ? "" : attribute.getDefaultValue()); mdpAttributeElement.setAttribute("referenceType", String.valueOf(attribute.getReferenceType())); mdpAttributeElement.setAttribute("logicPrimaryKey", String.valueOf(attribute.isLogicPrimaryKey())); mdpAttributeElement.setAttribute("validate", String.valueOf(attribute.isValidate())); mdpAttributeElement.setAttribute("index", String.valueOf(attribute.isIndex())); mdpAttributeElement.setAttribute("indexType", attribute.getIndexType() == null ? "" : attribute.getIndexType()); mdpAttributeElement.setAttribute("notNull", String.valueOf(attribute.isNotNull())); mdpAttributeElement.setAttribute("isAutoIncrement", String.valueOf(attribute.isAutoIncrement())); mdpAttributeElement.setAttribute("isReadOnly", String.valueOf(attribute.isReadOnly())); mdpAttributeElement.setAttribute("isSearchable", String.valueOf(attribute.isSearchable())); mdpAttributeElement.setAttribute("isSigned", String.valueOf(attribute.isSigned())); mdpAttributeElement.setAttribute("columnDisplaySize", String.valueOf(attribute.getColumnDisplaySize())); mdpAttributeElement.setAttribute("fieldType", attribute.getFieldType() == null ? "" : attribute.getFieldType()); // create reference if (attribute.getReferenceType() != Reference.REF_NONE) { createReference(mdpAttributeElement, attribute, doc); } classElement.appendChild(mdpAttributeElement); } } // create reference private void createReference(Element mdpAttributeElement, MdpAttributeImpl attribute, Document doc) { Element referenceElement = doc.createElement("reference"); Reference reference = attribute.getReference(); if (reference != null) { referenceElement.setAttribute("referenceTable", reference.getReferenceTable() == null ? "" : reference.getReferenceTable()); referenceElement.setAttribute("storeName", reference.getStoreName() == null ? "" : reference.getStoreName()); referenceElement.setAttribute("displayName", reference.getDisplayName() == null ? "" : reference.getDisplayName()); } mdpAttributeElement.appendChild(referenceElement); } }