123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- 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<MdpDataSourceImpl> 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<MdpConstantImpl> 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<TemplateImpl> 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<PropertyImpl> 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<MdpClassImpl> 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<MdpModuleImpl> 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<RelationImpl> 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<MdpAttributeImpl> 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);
- }
- }
|