61b9c2c89f759c6ea713e4f42f07e981c06d6beb.svn-base 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. package com.sysmodel.datamodel.xmlmanager;
  2. import java.util.List;
  3. import javax.xml.parsers.DocumentBuilder;
  4. import javax.xml.parsers.DocumentBuilderFactory;
  5. import org.apache.commons.logging.Log;
  6. import org.apache.commons.logging.LogFactory;
  7. import org.w3c.dom.Document;
  8. import org.w3c.dom.Element;
  9. import com.sysmodel.datamodel.xmlmodel.Reference;
  10. import com.sysmodel.datamodel.xmlmodel.impl.MdpAttributeImpl;
  11. import com.sysmodel.datamodel.xmlmodel.impl.MdpClassImpl;
  12. import com.sysmodel.datamodel.xmlmodel.impl.MdpConstantImpl;
  13. import com.sysmodel.datamodel.xmlmodel.impl.MdpDataSourceImpl;
  14. import com.sysmodel.datamodel.xmlmodel.impl.MdpModuleImpl;
  15. import com.sysmodel.datamodel.xmlmodel.impl.PropertyImpl;
  16. import com.sysmodel.datamodel.xmlmodel.impl.RelationImpl;
  17. import com.sysmodel.datamodel.xmlmodel.impl.SysModelImpl;
  18. import com.sysmodel.datamodel.xmlmodel.impl.TemplateImpl;
  19. import com.sysmodel.xformmodel.able.Validate;
  20. import com.toolkit.file.FileOperate;
  21. public class XMLSave{
  22. private Log log = LogFactory.getLog(XMLSave.class);
  23. public boolean saveToXMLFile(String filePath) {
  24. boolean bsuccess = true;
  25. log.info("DataModel.xml filePath = " + filePath);
  26. try {
  27. DocumentBuilderFactory domfactory = DocumentBuilderFactory.newInstance();
  28. DocumentBuilder builder = domfactory.newDocumentBuilder();
  29. Document doc = builder.newDocument();
  30. createXMLDoc(doc);
  31. FileOperate FileOperate = new FileOperate();
  32. bsuccess = FileOperate.saveDocumentFromDomToXMLFile(doc, filePath);
  33. } catch (Exception e) {
  34. bsuccess = false;
  35. log.error(e);
  36. }
  37. return bsuccess;
  38. }
  39. private void createXMLDoc(Document doc) {
  40. Element root = doc.createElement("sysmodel");
  41. doc.appendChild(root);
  42. /**
  43. * create about
  44. */
  45. creatAbout(doc);
  46. /**
  47. * create datasource
  48. */
  49. createDataSource(doc);
  50. /**
  51. * create MdpModule
  52. */
  53. createMdpModule(doc);
  54. /**
  55. * create constant
  56. */
  57. createConstant(doc);
  58. /**
  59. * create mdpClass
  60. */
  61. createMdpClass(doc);
  62. }
  63. // create about
  64. private void creatAbout(Document doc) {
  65. Element root = doc.getDocumentElement();
  66. Element author = doc.createElement("author");
  67. author.appendChild(doc.createTextNode(SysModelImpl.getInstance().getAuthor()));
  68. Element version = doc.createElement("version");
  69. version.appendChild(doc.createTextNode(SysModelImpl.getInstance().getVersion()));
  70. root.appendChild(author);
  71. root.appendChild(version);
  72. }
  73. // create datasources
  74. private void createDataSource(Document doc) {
  75. Element root = doc.getDocumentElement();
  76. Element listElement = doc.createElement("mdpDataSources");
  77. List<MdpDataSourceImpl> dataSource = SysModelImpl.getInstance().getMdpDataSources();
  78. for (int i = 0; i < dataSource.size(); i++) {
  79. MdpDataSourceImpl datasource = dataSource.get(i);
  80. Element element = doc.createElement("mdpDataSource");
  81. element.setAttribute("dataSourceid", datasource.getDataSourceid() + "");
  82. element.setAttribute("description", datasource.getDescription() == null ? ""
  83. : datasource.getDescription());
  84. element.setAttribute("dataBase", datasource.getDataBase() + "");
  85. element.setAttribute("url", datasource.getURL() == null ? "" : datasource.getURL());
  86. element.setAttribute("user", datasource.getUser() == null ? "" : datasource.getUser());
  87. element.setAttribute("password",
  88. datasource.getPassword() == null ? "" : datasource.getPassword());
  89. element.setAttribute("jndi", datasource.getJNDI() == null ? "" : datasource.getJNDI());
  90. // element.setAttribute("dataSourceType",datasource.getDataSourceType()==null
  91. // ? "":datasource.getDataSourceType());
  92. listElement.appendChild(element);
  93. }
  94. root.appendChild(listElement);
  95. }
  96. // create constant
  97. private void createConstant(Document doc) {
  98. Element root = doc.getDocumentElement();
  99. Element listElement = doc.createElement("mdpConstants");
  100. // create template
  101. createTemplate(listElement, doc);
  102. List<MdpConstantImpl> listConstant = SysModelImpl.getInstance().getMdpConstants();
  103. for (int i = 0; i < listConstant.size(); i++) {
  104. MdpConstantImpl constant = listConstant.get(i);
  105. Element constantElement = doc.createElement("mdpConstant");
  106. // create self attribute
  107. constantElement.setAttribute("type",
  108. constant.getType() == null ? "" : constant.getType());
  109. constantElement.setAttribute("codeName",
  110. constant.getCodeName() == null ? "" : constant.getCodeName());
  111. constantElement.setAttribute("ngoogleEn", constant.getNgoogleEn() == null ? ""
  112. : constant.getNgoogleEn());
  113. constantElement.setAttribute("description", constant.getDescription() == null ? ""
  114. : constant.getDescription());
  115. constantElement.setAttribute("templateName", constant.getTemplateName() == null ? ""
  116. : constant.getTemplateName());
  117. constantElement.setAttribute("rootFrom",
  118. constant.getRootFrom() == null ? "" : constant.getRootFrom());
  119. constantElement.setAttribute("dataSource", constant.getDataSource() + "");
  120. constantElement.setAttribute("keyCodeAuto", String.valueOf(constant.isKeyCodeAuto()));
  121. constantElement.setAttribute("orderProperty", constant.getOrderProperty() == null ? ""
  122. : constant.getOrderProperty());
  123. constantElement.setAttribute("orderMethod", constant.getOrderMethod() == null ? ""
  124. : constant.getOrderMethod());
  125. // add child
  126. listElement.appendChild(constantElement);
  127. }
  128. root.appendChild(listElement);
  129. }
  130. // create template
  131. private void createTemplate(Element listElement, Document doc) {
  132. Element listTemplatesElement = doc.createElement("templates");
  133. List<TemplateImpl> listTemplates = SysModelImpl.getInstance().getTemplates();
  134. for (int i = 0; i < listTemplates.size(); i++) {
  135. TemplateImpl template = listTemplates.get(i);
  136. Element templateElement = doc.createElement("template");
  137. templateElement.setAttribute("name",
  138. template.getName() == null ? "" : template.getName());
  139. templateElement.setAttribute("tableName", template.getTableName() == null ? ""
  140. : template.getTableName());
  141. templateElement.setAttribute("dataSource", template.getDataSource() + "");
  142. templateElement.setAttribute("description", template.getDescription() == null ? ""
  143. : template.getDescription());
  144. // create property
  145. createProperty(templateElement, template.getListproperty(), doc);
  146. // add templates element
  147. listTemplatesElement.appendChild(templateElement);
  148. }
  149. // add mdpConstants element
  150. listElement.appendChild(listTemplatesElement);
  151. }
  152. // create property
  153. private void createProperty(Element constantElement, List<PropertyImpl> listProperty,
  154. Document doc) {
  155. for (int i = 0; i < listProperty.size(); i++) {
  156. Element propertyElement = doc.createElement("property");
  157. PropertyImpl property = listProperty.get(i);
  158. propertyElement.setAttribute("name",
  159. property.getName() == null ? "" : property.getName());
  160. propertyElement.setAttribute("description", property.getDescription() == null ? ""
  161. : property.getDescription());
  162. propertyElement.setAttribute("dataType", property.getDataType());
  163. propertyElement.setAttribute("length", property.getLength() + "");
  164. propertyElement.setAttribute("refConstant", property.getRefConstant() + "");
  165. propertyElement.setAttribute("scale",
  166. property.getScale() == null ? "" : property.getScale());
  167. propertyElement.setAttribute(
  168. "referenceParentName",
  169. property.getReferenceParentName() == null ? "" : property
  170. .getReferenceParentName());
  171. propertyElement.setAttribute("display", String.valueOf(property.isDisplay()));
  172. propertyElement.setAttribute("unique", String.valueOf(property.isUnique()));
  173. creatvalidates(propertyElement, doc, property);
  174. constantElement.appendChild(propertyElement);
  175. }
  176. }
  177. private void creatvalidates(Element pagereportField, Document doc, PropertyImpl property) {
  178. Element validatesField = doc.createElement("validates");
  179. Validate Validate = property.getValidate();
  180. if (Validate.isNeed()) {
  181. Element validateNode = doc.createElement("validate");
  182. validateNode.setAttribute("type", "need");
  183. validatesField.appendChild(validateNode);
  184. }
  185. if (Validate.isTrim()) {
  186. Element validateNode = doc.createElement("validate");
  187. validateNode.setAttribute("type", "trim");
  188. validatesField.appendChild(validateNode);
  189. }
  190. if (!Validate.getMaxLength().equals("")) {
  191. Element validateNode = doc.createElement("validate");
  192. validateNode.setAttribute("type", "length");
  193. validateNode.setAttribute("maxLength", Validate.getMaxLength());
  194. validatesField.appendChild(validateNode);
  195. }
  196. if (!Validate.getRegxValue().equals("")) {
  197. Element validateNode = doc.createElement("validate");
  198. validateNode.setAttribute("type", "regx");
  199. validateNode.setAttribute("regxValue", Validate.getRegxValue());
  200. validatesField.appendChild(validateNode);
  201. }
  202. if (!Validate.getDojoType().equals("")) {
  203. Element validateNode = doc.createElement("validate");
  204. validateNode.setAttribute("type", "dojoType");
  205. validateNode.setAttribute("typeValue", Validate.getDojoType());
  206. validatesField.appendChild(validateNode);
  207. }
  208. if (!Validate.getMaxValue().equals("") || !Validate.getMinValue().equals("")) {
  209. Element validateNode = doc.createElement("validate");
  210. validateNode.setAttribute("type", "range");
  211. validateNode.setAttribute("maxValue", Validate.getMaxValue());
  212. validateNode.setAttribute("minValue", Validate.getMinValue());
  213. validatesField.appendChild(validateNode);
  214. }
  215. if (!Validate.getInvalidMessage().equals("") || !Validate.getPromptMessage().equals("")) {
  216. Element validateNode = doc.createElement("validate");
  217. validateNode.setAttribute("type", "message");
  218. validateNode.setAttribute("promptMessage", Validate.getPromptMessage());
  219. validateNode.setAttribute("invalidMessage", Validate.getInvalidMessage());
  220. validatesField.appendChild(validateNode);
  221. }
  222. pagereportField.appendChild(validatesField);
  223. }
  224. // create MdpClass
  225. private void createMdpClass(Document doc) {
  226. Element root = doc.getDocumentElement();
  227. Element listElement = doc.createElement("mdpClasses");
  228. List<MdpClassImpl> listMdpClass = SysModelImpl.getInstance().getMdpClasses();
  229. for (int i = 0; i < listMdpClass.size(); i++) {
  230. MdpClassImpl mdpclass = listMdpClass.get(i);
  231. Element classElement = doc.createElement("mdpClass");
  232. // create self attribute
  233. classElement.setAttribute("classid", mdpclass.getClassid() + "");
  234. classElement.setAttribute("type", mdpclass.getType() == null ? "" : mdpclass.getType());
  235. classElement.setAttribute("validate", String.valueOf(mdpclass.isValidate()));
  236. classElement.setAttribute("dataSource", mdpclass.getDataSource() + "");
  237. classElement.setAttribute("name", mdpclass.getName() == null ? "" : mdpclass.getName());
  238. /**
  239. * classElement.setAttribute("ngoogleEn",mdpclass.getNgoogleEn()==
  240. * null ? "":mdpclass.getNgoogleEn());
  241. * classElement.setAttribute("oldName",mdpclass.getOldName() == null
  242. * ? "":mdpclass.getOldName());
  243. */
  244. classElement.setAttribute("description", mdpclass.getDescription() == null ? ""
  245. : mdpclass.getDescription());
  246. classElement.setAttribute("exist", String.valueOf(mdpclass.isExist()));
  247. classElement.setAttribute("catche", String.valueOf(mdpclass.isCatche()));
  248. // create MdpAttribute
  249. createMdpAttribute(classElement, mdpclass.getAllMdpAttributes(), doc);
  250. createRelation(classElement, mdpclass.getRelations(), doc);
  251. classElement.setAttribute("primaryKey", mdpclass.getPrimaryKey() == null ? ""
  252. : mdpclass.getPrimaryKey());
  253. // add child
  254. listElement.appendChild(classElement);
  255. }
  256. root.appendChild(listElement);
  257. }
  258. // create MdpModule
  259. private void createMdpModule(Document doc) {
  260. Element root = doc.getDocumentElement();
  261. Element listElement = doc.createElement("mdpModules");
  262. List<MdpModuleImpl> listmdpmodule = SysModelImpl.getInstance().getMdpModules();
  263. for (int i = 0; i < listmdpmodule.size(); i++) {
  264. MdpModuleImpl mdpmodule = listmdpmodule.get(i);
  265. Element classElement = doc.createElement("module");
  266. // create self attribute
  267. classElement.setAttribute("pcode", mdpmodule.getPcode() + "");
  268. classElement.setAttribute("code", mdpmodule.getCode() + "");
  269. classElement.setAttribute("name", mdpmodule.getName());
  270. classElement.setAttribute("ngoogleEn", mdpmodule.getNgoogleEn());
  271. classElement.setAttribute("tPrefix", mdpmodule.getTPrefix());
  272. classElement.setAttribute("description", mdpmodule.getDescription() + "");
  273. classElement.setAttribute("packageName", mdpmodule.getPackageName() + "");
  274. // add child
  275. listElement.appendChild(classElement);
  276. }
  277. root.appendChild(listElement);
  278. }
  279. // create relation
  280. private void createRelation(Element classElement, List<RelationImpl> listRelations, Document doc) {
  281. for (int i = 0; i < listRelations.size(); i++) {
  282. Element realtionElement = doc.createElement("relation");
  283. RelationImpl relation = listRelations.get(i);
  284. realtionElement.setAttribute("type", relation.getType() + "");
  285. realtionElement.setAttribute("guestClassid", relation.getGuestClassid() + "");
  286. classElement.appendChild(realtionElement);
  287. }
  288. }
  289. // create MdpAttribute
  290. private void createMdpAttribute(Element classElement, List<MdpAttributeImpl> listMdpAttributes,
  291. Document doc) {
  292. for (int i = 0; i < listMdpAttributes.size(); i++) {
  293. Element mdpAttributeElement = doc.createElement("mdpAttribute");
  294. MdpAttributeImpl attribute = listMdpAttributes.get(i);
  295. mdpAttributeElement.setAttribute("name",
  296. attribute.getName() == null ? "" : attribute.getName());
  297. /**
  298. * mdpAttributeElement.setAttribute("ngoogleEn",attribute.
  299. * getNgoogleEn()==null ? "":attribute.getNgoogleEn());
  300. * mdpAttributeElement
  301. * .setAttribute("oldName",attribute.getOldName()==null ?
  302. * "":attribute.getOldName());
  303. */
  304. mdpAttributeElement.setAttribute("description", attribute.getDescription() == null ? ""
  305. : attribute.getDescription());
  306. mdpAttributeElement.setAttribute("unit",
  307. attribute.getUnit() == null ? "" : attribute.getUnit());
  308. mdpAttributeElement.setAttribute("dataType", String.valueOf(attribute.getDataType()));
  309. mdpAttributeElement.setAttribute("precision", String.valueOf(attribute.getPrecision()));
  310. mdpAttributeElement.setAttribute("scale",
  311. attribute.getScale() == null ? "" : attribute.getScale());
  312. mdpAttributeElement.setAttribute("defaultValue",
  313. attribute.getDefaultValue() == null ? "" : attribute.getDefaultValue());
  314. mdpAttributeElement.setAttribute("referenceType",
  315. String.valueOf(attribute.getReferenceType()));
  316. mdpAttributeElement.setAttribute("logicPrimaryKey",
  317. String.valueOf(attribute.isLogicPrimaryKey()));
  318. mdpAttributeElement.setAttribute("validate", String.valueOf(attribute.isValidate()));
  319. mdpAttributeElement.setAttribute("index", String.valueOf(attribute.isIndex()));
  320. mdpAttributeElement.setAttribute("indexType", attribute.getIndexType() == null ? ""
  321. : attribute.getIndexType());
  322. mdpAttributeElement.setAttribute("notNull", String.valueOf(attribute.isNotNull()));
  323. mdpAttributeElement.setAttribute("isAutoIncrement",
  324. String.valueOf(attribute.isAutoIncrement()));
  325. mdpAttributeElement.setAttribute("isReadOnly", String.valueOf(attribute.isReadOnly()));
  326. mdpAttributeElement.setAttribute("isSearchable",
  327. String.valueOf(attribute.isSearchable()));
  328. mdpAttributeElement.setAttribute("isSigned", String.valueOf(attribute.isSigned()));
  329. mdpAttributeElement.setAttribute("columnDisplaySize",
  330. String.valueOf(attribute.getColumnDisplaySize()));
  331. mdpAttributeElement.setAttribute("fieldType", attribute.getFieldType() == null ? ""
  332. : attribute.getFieldType());
  333. // create reference
  334. if (attribute.getReferenceType() != Reference.REF_NONE) {
  335. createReference(mdpAttributeElement, attribute, doc);
  336. }
  337. classElement.appendChild(mdpAttributeElement);
  338. }
  339. }
  340. // create reference
  341. private void createReference(Element mdpAttributeElement, MdpAttributeImpl attribute,
  342. Document doc) {
  343. Element referenceElement = doc.createElement("reference");
  344. Reference reference = attribute.getReference();
  345. if (reference != null) {
  346. referenceElement.setAttribute("referenceTable",
  347. reference.getReferenceTable() == null ? "" : reference.getReferenceTable());
  348. referenceElement.setAttribute("storeName", reference.getStoreName() == null ? ""
  349. : reference.getStoreName());
  350. referenceElement.setAttribute("displayName", reference.getDisplayName() == null ? ""
  351. : reference.getDisplayName());
  352. }
  353. mdpAttributeElement.appendChild(referenceElement);
  354. }
  355. }