package com.sysmodel.datamodel.xmlmodel.impl; import java.io.File; import java.util.ArrayList; import java.util.Calendar; import java.util.Iterator; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.sysmodel.datamodel.xmlmanager.XMLLoader; import com.sysmodel.datamodel.xmlmanager.XMLSave; import com.sysmodel.datamodel.xmlmodel.ModelFactory; import com.sysmodel.datamodel.xmlmodel.Reference; import com.sysmodel.datamodel.xmlmodel.able.MdpAttribute; import com.sysmodel.datamodel.xmlmodel.able.MdpClass; import com.sysmodel.datamodel.xmlmodel.able.MdpConstant; import com.sysmodel.datamodel.xmlmodel.able.MdpDataSource; import com.sysmodel.datamodel.xmlmodel.able.MdpModule; import com.sysmodel.datamodel.xmlmodel.able.Relation; import com.sysmodel.datamodel.xmlmodel.able.SysModel; import com.sysmodel.datamodel.xmlmodel.able.Template; public class SysModelImpl implements SysModel{ private static Log log = LogFactory.getLog(SysModelImpl.class); private String author; private String version; private String unitcode; private String unitname; private ArrayList mdpModules = new ArrayList(); private ArrayList mdpDataSources = new ArrayList(); private ArrayList mdpConstants = new ArrayList(); private ArrayList mdpClasses = new ArrayList(); private ArrayList templates = new ArrayList(); private ArrayList listRelations = new ArrayList(); private static SysModelImpl sysmodel = null; public static String fileLoadPath = null; private SysModelImpl() { author = "nbs"; version = "1.0"; } /** * 获得应用单位编码 * * @return */ public String getUnitCode() { return unitcode; } public void setUnitcode(String unitcode) { this.unitcode = unitcode; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } public ArrayList getMdpModules() { return mdpModules; } public void setmdpModules(ArrayList mdpModules) { this.mdpModules = mdpModules; } public ArrayList getMdpDataSources() { return mdpDataSources; } public void setMdpDataSources(ArrayList mdpDataSources) { this.mdpDataSources = mdpDataSources; } public ArrayList getTemplates() { return templates; } public void setTemplates(ArrayList templates) { this.templates = templates; } public ArrayList getMdpConstants() { return mdpConstants; } public void setMdpConstants(ArrayList mdpConstants) { this.mdpConstants = mdpConstants; } public ArrayList getMdpClasses() { return mdpClasses; } public void setMdpClasses(ArrayList mdpClasses) { this.mdpClasses = mdpClasses; } public ArrayList getRelations() { return listRelations; } public void addRelations(List relations) { this.listRelations.addAll(relations); } public void setRelations(ArrayList relations) { this.listRelations = relations; } public Template getTemplateByName(String templateName) { if (templateName == null) return null; for (int i = 0; i < templates.size(); i++) { TemplateImpl temp = (TemplateImpl) templates.get(i); if (temp.getName().equals(templateName)) return temp; } return null; } public static SysModelImpl getInstance() { return sysmodel; } public static SysModelImpl getInstance(File[] files) { if (sysmodel == null) sysmodel = new SysModelImpl(); else sysmodel.clear(); if (null != files && 0 != files.length) { com.sysmodel.configLoader.XMLLoader loader = new com.sysmodel.configLoader.XMLLoader(); if (loader.loadXML(files)) log.info("文件加载成功"); } return sysmodel; } public static SysModelImpl getInstance(String fullpath) { if (sysmodel == null) { sysmodel = new SysModelImpl(); } else sysmodel.clear(); if (!fullpath.equals("")) { fileLoadPath = fullpath; XMLLoader loader = new XMLLoader(); if (loader.loadFromXML(fullpath)) { log.info("load success : " + fullpath); } } else log.info("full path is empty"); return sysmodel; } public boolean saveSysModelToXML() { XMLSave loader = new XMLSave(); return loader.saveToXMLFile(fileLoadPath); } public MdpModule getMdpModuleByCode(String code) { for (int i = 0; i < mdpModules.size(); i++) { MdpModuleImpl modSource = (MdpModuleImpl) mdpModules.get(i); if (modSource.getCode().equals(code)) return modSource; } return null; } public MdpDataSource getDataSourceByCode(int sourcecode) { for (int i = 0; i < mdpDataSources.size(); i++) { MdpDataSourceImpl dataSource = (MdpDataSourceImpl) mdpDataSources.get(i); if (dataSource.getDataSourceid() == sourcecode) return dataSource; } return null; } public MdpAttribute getMdpAttributeByName(int classid, String attributeName) { if (getMdpClassByClassID(classid) == null) { log.info("classid :" + classid + " 的表不存在"); return null; } else { return getMdpClassByClassID(classid).getMdpAttributeByName(attributeName); } } public MdpClass getMdpClassByClassID(int classid) { for (int i = 0; i < mdpClasses.size(); i++) { MdpClass mdpClass = (MdpClass) mdpClasses.get(i); if (mdpClass.getClassid() == classid) return mdpClass; } return null; } public ArrayList getMdpClassByType(String type) { ArrayList al = new ArrayList(); for (int i = 0; i < mdpClasses.size(); i++) { MdpClass mdpClass = (MdpClass) mdpClasses.get(i); if (mdpClass.getType().equals(type)) { al.add(mdpClass); } } return al; } public ArrayList getAllChildMdpClassByType(String type) { ArrayList al = new ArrayList(); for (int i = 0; i < mdpClasses.size(); i++) { MdpClass mdpClass = (MdpClass) mdpClasses.get(i); if (mdpClass.getType().startsWith(type)) { al.add(mdpClass); } } return al; } public MdpConstant getMdpConstantByName(String codeName) { for (int i = 0; i < mdpConstants.size(); i++) { MdpConstant constant = (MdpConstant) mdpConstants.get(i); if (constant.getCodeName().equals(codeName)) return constant; } return null; } public ArrayList getMdpConstantByType(String type) { ArrayList list = new ArrayList(); for (int i = 0; i < mdpConstants.size(); i++) { MdpConstantImpl MdpConstant = (MdpConstantImpl) mdpConstants.get(i); if (MdpConstant.getType().equals(type)) { list.add(MdpConstant); } } return list; } public String getMdpConstantDisplayString(String codeName, String uniqueCode) { MdpConstant constant = getMdpConstantByName(codeName); if (constant != null) { if (constant.isTree() && uniqueCode.length() > 4) { if (uniqueCode.substring(0, uniqueCode.length() - 2).equals("1200")) { return constant.getMdpConstantDisplayString(uniqueCode); } else { return constant.getMdpConstantDisplayString(uniqueCode.substring(0, uniqueCode.length() - 2)) + constant.getMdpConstantDisplayString(uniqueCode); } } else return constant.getMdpConstantDisplayString(uniqueCode); } return ""; } public boolean isChildTable(int classid) { for (int i = 0; i < listRelations.size(); i++) { Relation relation = (Relation) listRelations.get(i); if (relation.getGuestClassid() == classid && relation.getType() == Relation.REALTION_MASTER) return true; } return false; } public ArrayList getReferenceConstantMdpAttribute(MdpClass mdpClass, String codeName) { ArrayList arraylist = new ArrayList(); List attributes = mdpClass.getAllMdpAttributes(); Iterator it = attributes.iterator(); while (it.hasNext()) { MdpAttribute temp = (MdpAttribute) it.next(); if (temp.getReferenceType() == Reference.REF_CONSTANT && temp.getReference().getReferenceTable().equals(codeName)) arraylist.add(temp); } return arraylist; } public String getCalendarDayTime() { Calendar calendar = Calendar.getInstance(); java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyyMMddHHmmss"); return df.format(calendar.getTime()); } public String getFileLoadFolder() { return fileLoadPath.replaceAll("DataModel.xml", ""); } public ArrayList getMdpClassByDataSource(int dataSource) { SysModel sysmodel = ModelFactory.getSysmodel(); ArrayList mdpClassList = sysmodel.getMdpClasses(); ArrayList tables = new ArrayList(); for (int i = 0; i < mdpClassList.size(); i++) { MdpClass mdpClass = (MdpClass) mdpClassList.get(i); if (mdpClass.getDataSource() == dataSource) { tables.add(mdpClass); } } return tables; } public ArrayList getMdpConstantByDataSource(int dataSource) { SysModel sysmodel = ModelFactory.getSysmodel(); ArrayList MdpConstantList = sysmodel.getMdpConstants(); ArrayList mdpConstants = new ArrayList(); for (int i = 0; i < MdpConstantList.size(); i++) { MdpConstant mdpConstant = (MdpConstant) MdpConstantList.get(i); if (mdpConstant.getDataSource() == dataSource) { mdpConstants.add(mdpConstant); } } return mdpConstants; } private void clear() { mdpModules.clear(); mdpDataSources.clear(); templates.clear(); mdpConstants.clear(); mdpClasses.clear(); } public ArrayList getMdpModulesByPcode(String pCode) { ArrayList result = new ArrayList(); for (int i = 0; i < mdpModules.size(); i++) { MdpModuleImpl modSource = (MdpModuleImpl) mdpModules.get(i); // log.info("modSource.getPcode() = " + modSource.getPcode()); if (modSource.getPcode().equals(pCode)) result.add(modSource); } return result; } public String getUnitname() { return unitname; } public void setUnitname(String unitname) { this.unitname = unitname; } }