向数据库加入大量数据

InitSystemData
java 代码
 
  1. package com.bjsxt.oa.manager.impl;   
  2.   
  3. import java.io.BufferedReader;   
  4. import java.io.InputStream;   
  5. import java.io.InputStreamReader;   
  6. import java.text.SimpleDateFormat;   
  7. import java.util.List;   
  8.   
  9. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;   
  10.   
  11. import com.bjsxt.oa.manager.AclManager;   
  12. import com.bjsxt.oa.manager.DataDictionaryManager;   
  13. import com.bjsxt.oa.manager.InitSystemData;   
  14. import com.bjsxt.oa.manager.ModuleManager;   
  15. import com.bjsxt.oa.manager.PartyManager;   
  16. import com.bjsxt.oa.manager.PersonManager;   
  17. import com.bjsxt.oa.manager.RoleManager;   
  18. import com.bjsxt.oa.manager.UserManager;   
  19. import com.bjsxt.oa.model.DataDictionary;   
  20. import com.bjsxt.oa.model.Module;   
  21. import com.bjsxt.oa.model.Party;   
  22. import com.bjsxt.oa.model.Person;   
  23. import com.bjsxt.oa.model.Role;   
  24. import com.bjsxt.oa.model.User;   
  25.   
  26. public class InitSystemDataImpl extends HibernateDaoSupport implements InitSystemData{   
  27.   
  28.     static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");   
  29.        
  30.     private ModuleManager moduleManager;   
  31.     private RoleManager roleManager;   
  32.     private AclManager aclManager;   
  33.     private PersonManager personManager;   
  34.     private PartyManager partyManager;   
  35.     private UserManager userManager;   
  36.     private DataDictionaryManager dataDictionaryManager;   
  37.   
  38.     public void addInitSystemData(){   
  39.         addInitModuleData();   
  40.         addInitRoleData();   
  41.         addInitPartyData();   
  42.         addInitPersonData();   
  43.         addInitDictionaryData();   
  44.     }   
  45.        
  46.     //初始化模块信息   
  47.     public void addInitModuleData(){   
  48.         try {   
  49.             InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("module.txt");   
  50.                
  51.             BufferedReader reader = new BufferedReader(new InputStreamReader(is));   
  52.             String line = null;   
  53.             Module parent = null;   
  54.             int order = 1;   
  55.             while((line = reader.readLine()) != null){   
  56.                 if(line.trim().equals("")){   
  57.                     continue;   
  58.                 }   
  59.                    
  60.                 //submodule   
  61.                 if(line.startsWith("\t")){   
  62.                     String[] ss = line.trim().split(",");   
  63.                     Module sub = new Module();   
  64.                     sub.setName(ss[0].trim());   
  65.                     sub.setSn(ss[1].trim());   
  66.                     sub.setUrl(ss[2].trim());   
  67.                     sub.setOrderNo(order++);   
  68.                     moduleManager.addModule(sub, parent.getId());   
  69.                 }else//parentmodule   
  70.                     String[] ss = line.trim().split(",");   
  71.                     parent = new Module();   
  72.                     parent.setName(ss[0].trim());   
  73.                     parent.setSn(ss[1].trim());   
  74.                     parent.setOrderNo(order++);   
  75.                     moduleManager.addModule(parent, 0);   
  76.                 }   
  77.             }   
  78.         } catch (Exception e) {   
  79.             e.printStackTrace();   
  80.             throw new RuntimeException(e);   
  81.         }   
  82.     }   
  83.        
  84.     //初始化角色信息以及角色的访问权限   
  85.     public void addInitRoleData(){   
  86.         try {   
  87.             InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("role.txt");   
  88.                
  89.             BufferedReader reader = new BufferedReader(new InputStreamReader(is));   
  90.             String line = null;   
  91.             while((line = reader.readLine()) != null){   
  92.                 if(line.trim().equals("")){   
  93.                     continue;   
  94.                 }   
  95.                 String[] ss = line.trim().split(":");   
  96.                    
  97.                 Role role = new Role();   
  98.                 role.setName(ss[0].trim());   
  99.                 roleManager.addRole(role);   
  100.                    
  101.                 //获取被分配的模块列表   
  102.                 String moduleString = ss[1].trim();   
  103.                 String[] moduleNames = moduleString.split(",");   
  104.                 int[] moduleIds = new int[moduleNames.length];   
  105.                 for(int i=0; i
  106.                     List list = getHibernateTemplate().find("from Module where name = ?",moduleNames[i]);   
  107.                     if(list.isEmpty()){   
  108.                         throw new RuntimeException("找不到相应模块["+moduleNames[i]+"]");   
  109.                     }   
  110.                     if(list.size() > 1){   
  111.                         throw new RuntimeException("存在同名模块["+moduleNames[i]+"]");   
  112.                     }   
  113.                     Module m = (Module)list.get(0);   
  114.                     moduleIds[i] = m.getId();   
  115.                 }   
  116.                 aclManager.addAcls(role.getId(), moduleIds);   
  117.             }   
  118.         } catch (Exception e) {   
  119.             e.printStackTrace();   
  120.             throw new RuntimeException(e);   
  121.         }   
  122.     }   
  123.        
  124.     //初始化机构信息   
  125.     public void addInitPartyData(){   
  126.         try {   
  127.             InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("party.txt");   
  128.                
  129.             BufferedReader reader = new BufferedReader(new InputStreamReader(is));   
  130.             String line = null;   
  131.             Party parent = null;   
  132.             Party sub = null;   
  133.             while((line = reader.readLine()) != null){   
  134.                 if(line.trim().equals("")){   
  135.                     continue;   
  136.                 }   
  137.                 String name = line.trim();   
  138.                 //第三级机构信息   
  139.                 if(line.startsWith("\t\t")){   
  140.                     Party subsub = new Party();   
  141.                     subsub.setName(name);   
  142.                     subsub.setParent(sub);   
  143.                     getHibernateTemplate().save(subsub);   
  144.                     subsub.setSn(sub.getSn()+"_"+subsub.getId()); //设置SN的值   
  145.                     getHibernateTemplate().update(subsub);   
  146.                 }   
  147.                 //第二级机构信息   
  148.                 else if(line.startsWith("\t")){   
  149.                     sub = new Party();   
  150.                     sub.setName(name);   
  151.                     sub.setParent(parent);   
  152.                     getHibernateTemplate().save(sub);   
  153.                     sub.setSn(parent.getSn()+"_"+sub.getId());   
  154.                     getHibernateTemplate().update(sub);   
  155.                 }else//parentmodule   
  156.                     parent = new Party();   
  157.                     parent.setName(name);   
  158.                     getHibernateTemplate().save(parent);   
  159.                     parent.setSn(parent.getId()+"");   
  160.                     getHibernateTemplate().update(parent);   
  161.                 }   
  162.             }   
  163.         } catch (Exception e) {   
  164.             e.printStackTrace();   
  165.             throw new RuntimeException(e);   
  166.         }   
  167.     }   
  168.        
  169.     //初始化人员信息   
  170.     public void addInitPersonData(){   
  171.         try {   
  172.             InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("person.txt");   
  173.                
  174.             BufferedReader reader = new BufferedReader(new InputStreamReader(is));   
  175.             String line = null;   
  176.             while((line = reader.readLine()) != null){   
  177.                 if(line.trim().equals("")){   
  178.                     continue;   
  179.                 }   
  180.                 String[] ss = line.trim().split(",");   
  181.                 String personName = ss[0];   
  182.                 String userAccount = ss[1];   
  183.                 String partyName = ss[2]; //从index==3起,是用户具有的角色   
  184.                    
  185.                 //根据名称获取机构信息   
  186.                 Party party = null;   
  187.                 List list = getHibernateTemplate().find("from Party where name = ?",partyName);   
  188.                 if(list.isEmpty() || list.size() > 1){   
  189.                     throw new RuntimeException("机构不存在或机构名称重复!");   
  190.                 }   
  191.                 party = (Party)list.get(0);   
  192.                    
  193.                 //根据角色名称,获取角色的ID列表   
  194.                 int roleslength = ss.length - 3;   
  195.                 int[] roleIds = new int[roleslength];   
  196.                 for(int i=0; i
  197.                     String roleName = ss[3 + i];   
  198.                     List rolelist = getHibernateTemplate().find("from Role where name = ?",roleName);   
  199.                     if(rolelist.isEmpty() || rolelist.size() > 1){   
  200.                         throw new RuntimeException("无法找到被分配的角色名称或角色名称有重复");   
  201.                     }   
  202.                     roleIds[i] = ((Role)rolelist.get(0)).getId();   
  203.                 }   
  204.                    
  205.                 //创建Person对象   
  206.                 Person person = new Person();   
  207.                 person.setName(personName);   
  208.                 person.setParty(party);   
  209.                 person.setSex("1");   
  210.                 person.setAddress("北京");   
  211.                 person.setPhone("12345678");   
  212.                 getHibernateTemplate().save(person);   
  213.                    
  214.                 //创建User对象   
  215.                 User user = new User();   
  216.                 user.setAccount(userAccount);   
  217.                 user.setPassword(userAccount);   
  218.                 user.setExpireTime(format.parse("2008-11-11"));   
  219.                 userManager.addUser(user, person.getId(), roleIds);   
  220.             }   
  221.         } catch (Exception e) {   
  222.             e.printStackTrace();   
  223.             throw new RuntimeException(e);   
  224.         }   
  225.     }   
  226.        
  227.     //初始化代码表   
  228.     public void addInitDictionaryData(){   
  229.         try {   
  230.             InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("code.txt");   
  231.                
  232.             BufferedReader reader = new BufferedReader(new InputStreamReader(is));   
  233.             String line = null;   
  234.             String category = null;   
  235.             while((line = reader.readLine()) != null){   
  236.                 if(line.trim().equals("")){   
  237.                     continue;   
  238.                 }   
  239.                    
  240.                 //添加字典条目   
  241.                 if(line.startsWith("\t")){   
  242.                     String[] ss = line.trim().split(",");   
  243.                     DataDictionary dict = new DataDictionary();   
  244.                     dict.setCategory(category);   
  245.                     dict.setCode(ss[0].trim());   
  246.                     dict.setText(ss[1].trim());   
  247.                     dataDictionaryManager.addDictionary(dict);   
  248.                 }else{   
  249.                     category = line.trim();   
  250.                 }   
  251.                    
  252.             }   
  253.         } catch (Exception e) {   
  254.             e.printStackTrace();   
  255.             throw new RuntimeException(e);   
  256.         }   
  257.     }   
  258.        
  259.        
  260.     public void setDataDictionaryManager(DataDictionaryManager dataDictionaryManager) {   
  261.         this.dataDictionaryManager = dataDictionaryManager;   
  262.     }   
  263.   
  264.     public void setModuleManager(ModuleManager moduleManager) {   
  265.         this.moduleManager = moduleManager;   
  266.     }   
  267.   
  268.     public void setPartyManager(PartyManager partyManager) {   
  269.         this.partyManager = partyManager;   
  270.     }   
  271.   
  272.     public void setPersonManager(PersonManager personManager) {   
  273.         this.personManager = personManager;   
  274.     }   
  275.   
  276.     public void setRoleManager(RoleManager roleManager) {   
  277.         this.roleManager = roleManager;   
  278.     }   
  279.   
  280.     public void setUserManager(UserManager userManager) {   
  281.         this.userManager = userManager;   
  282.     }   
  283.   
  284.     public void setAclManager(AclManager aclManager) {   
  285.         this.aclManager = aclManager;   
  286.     }      
  287. }   

 

Java代码 复制代码
  1. package com.bjsxt.orm;   
  2.   
  3. import junit.framework.TestCase;   
  4.   
  5. import org.springframework.beans.factory.BeanFactory;   
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  7.   
  8. import com.bjsxt.orm.manager.InitSystemData;   
  9.   
  10. public class InitSystemDataTest extends TestCase {   
  11.     static BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext-*.xml");   
  12.        
  13.     public void testAddInitSystemData() {   
  14.         InitSystemData init = (InitSystemData)factory.getBean("initSystemData");   
  15.         init.addInitSystemData();   
  16.     }   
  17.   
  18. }  
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值