1 packageorg.konghao.sys.org.service.impl;2
3 importjava.lang.reflect.InvocationTargetException;4 importjava.lang.reflect.Method;5 importjava.util.Iterator;6 importjava.util.List;7
8 importjavax.inject.Inject;9 importjavax.persistence.criteria.CriteriaBuilder.In;10
11 importorg.apache.commons.beanutils.BeanUtils;12 importorg.dom4j.Attribute;13 importorg.dom4j.Document;14 importorg.dom4j.DocumentException;15 importorg.dom4j.Element;16 importorg.dom4j.io.SAXReader;17 importorg.konghao.sys.org.iservice.IInitService;18 importorg.konghao.sys.org.model.OrgType;19 importorg.konghao.sys.org.model.SysException;20 importorg.springframework.beans.factory.BeanFactory;21 importorg.springframework.stereotype.Service;22
23 @Service("initService")24 public class InitService extends AbstractBaseSevice implementsIInitService{25
26 privateDocument document;27 @Inject28 privateBeanFactory factory;29
30 @Override31 public voidinitEntityByXml(String filename) {32 Element root = this.readDocument(filename);33 String pname = root.attributeValue("package");34 List initEntitys = root.selectNodes("/entitys/initEntity");35 for(Element element : initEntitys) {36 String ipname = element.attributeValue("package");37 String cname = element.attributeValue("class");38 System.out.println(element.attributeValue("exist"));39 //如果为1 表示已经添加过了
40 if (element.attributeValue("exist") == "1" || element.attributeValue("exist").equals("1")) {41 continue;42 }if (ipname != null && !"".equals(ipname)) {43 cname = ipname + "." +cname;44 } else{45 cname = pname + "." +cname;46 }47 String method = element.attributeValue("method");48 List entitys = (List)element.selectNodes("entity");49 System.out.println(cname + " " +method );50 addElements(cname,method,entitys);51
52 }53 }54
55 privateElement readDocument(String filename){56 try{57 SAXReader saxReader = newSAXReader();58 Document d = saxReader.read( InitService.class.getClassLoader().getResourceAsStream("init/" +filename));59 returnd.getRootElement();60 } catch(DocumentException e) {61 e.printStackTrace();62 }63 return null;64
65 }66
67 private void addElements(String cname, String method, Listentitys) {68 for(Element element : entitys) {69 addElement(cname , method , element , null);70 }71 }72
73 private voidaddElement(String cname, String method, Element e,Object parent) {74 try{75 //获取所有的属性
76 List atts = (List)e.attributes();77 Object obj =Class.forName(cname).newInstance();78 String [] ms = method.split("\\.");79 if(ms.length!=2) throw new SysException("方法格式不正确");80 String sname = ms[0]; String mname = ms[1];81 for(Attribute att:atts) {82 String name =att.getName();83 String value =att.getValue();84 BeanUtils.copyProperty(obj, name, value);85 }86 if(parent!=null) {
87 BeanUtils.copyProperty(obj, "parent", parent);
88 }
89 Object service = factory.getBean(sname);
90 Method m = service.getClass().getMethod(mname,obj.getClass());
91 m.invoke(service, obj);
92 List es = e.selectNodes("entity");
93 for(Element ele:es) {
94 addElement(cname, method, ele, obj);
95 }96 } catch(ClassNotFoundException e1) {97 e1.printStackTrace();98 } catch(IllegalAccessException e1) {99 e1.printStackTrace();100 } catch(InvocationTargetException e1) {101 e1.printStackTrace();102 } catch(SecurityException e1) {103 e1.printStackTrace();104 } catch(NoSuchMethodException e1) {105 e1.printStackTrace();106 } catch(InstantiationException e1) {107 e1.printStackTrace();108 }109 }110
111 }