View Javadoc

1   /***
2    * 
3    */
4   package hu.elte.tribus.sf;
5   
6   import hu.elte.tribus.model.Preference;
7   import hu.elte.tribus.model.QA;
8   import hu.elte.tribus.model.Topic;
9   import hu.elte.tribus.model.User;
10  
11  import java.sql.SQLException;
12  
13  import org.hibernate.HibernateException;
14  import org.hibernate.Session;
15  import org.springframework.orm.hibernate3.HibernateCallback;
16  import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
17  import org.springframework.transaction.PlatformTransactionManager;
18  import org.springframework.transaction.TransactionStatus;
19  import org.springframework.transaction.support.TransactionCallback;
20  import org.springframework.transaction.support.TransactionTemplate;
21  
22  /***
23   * Yes, this should be a bean with hibernate support.
24   * 
25   * @author kocka
26   * 
27   */
28  public class Seeder extends HibernateDaoSupport {
29  
30  	PlatformTransactionManager txManager = null;
31  
32  	public final static String PREF_KEY_DB_PATCH_VERSION = "db_patch_version";
33  
34  	public void seed() {
35  		// this is intentionally a programmed transaction
36  		// all the other should be decalred
37  
38  		new TransactionTemplate(txManager).execute(new TransactionCallback() {
39  
40  			public Object doInTransaction(TransactionStatus arg0) {
41  				return getHibernateTemplate().execute(new HibernateCallback() {
42  
43  					public Object doInHibernate(Session session)
44  							throws HibernateException, SQLException {
45  
46  						Preference dbPatchVersion;
47  							dbPatchVersion = (Preference) session.get(
48  									Preference.class, PREF_KEY_DB_PATCH_VERSION);
49  						int iDbPatchVersion = 0;
50  						if (dbPatchVersion == null) {
51  							dbPatchVersion = new Preference();
52  							dbPatchVersion.setKey(PREF_KEY_DB_PATCH_VERSION);
53  							dbPatchVersion.setValue("0");
54  						} else {
55  							iDbPatchVersion = Integer.parseInt(dbPatchVersion.getValue());
56  						}
57  
58  						if (iDbPatchVersion < 1) {
59  							User admin = new User();
60  							admin.setUsername("admin");
61  							admin.setPasswd("yikulju");
62  							admin.setFirstname("Yikulju");
63  							admin.setLastname("Administrator");
64  							admin.setEmail("admin@yikulju.com");
65  							admin.setStatus(User.ENUM_STATUS_REG_ACCEPTED);
66  							admin.setAdmin(true);
67  							session.save(admin);
68  							User user = new User();
69  							user.setUsername("user");
70  							user.setPasswd("user");
71  							user.setFirstname("yikulju");
72  							user.setLastname("user");
73  							user.setEmail("user@yikulju.com");
74  							user.setStatus(User.ENUM_STATUS_REG_ACCEPTED);
75  							session.save(user);
76  							Topic topic = new Topic();
77  							topic.setName("DB2");
78  							topic
79  									.setDesc("All about the ancient daemon of unix");
80  
81  							Topic topic2 = new Topic();
82  							topic2.setName("Websphere");
83  							topic2.setDesc("Errr.... Fooo...");
84  
85  							Topic topic3 = new Topic();
86  							topic3.setName("Chicks");
87  							topic3.setDesc("All about them");
88  
89  							session.save(topic);
90  							session.save(topic2);
91  							session.save(topic3);
92  
93  							QA qa = new QA();
94  							qa
95  									.setAnswer("Maybe 12, but have to write documentation and more testcases!");
96  							qa
97  									.setQuestion("How much time do we have until the end of the contest?");
98  							qa.setTopic(topic3);
99  							session.save(qa);
100 
101 							QA qa2 = new QA();
102 							qa2.setQuestion("How can I get home from here?");
103 							qa2.setAnswer("Walk, that simple it is.");
104 							qa2.setTopic(topic3);
105 							session.save(qa2);
106 
107 							QA qa3 = new QA();
108 							qa3.setQuestion("How is the weather outside?");
109 							qa3
110 									.setAnswer("Optimal for a night trip. Don`t leave your headlamp home!");
111 							qa3.setTopic(topic3);
112 							session.save(qa3);
113 
114 							QA qa4 = new QA();
115 							qa4 = new QA();
116 							qa4.setQuestion("How is the weather?");
117 							qa4
118 									.setAnswer("However the weather is, DB2 is your friend.");
119 							qa4.setTopic(topic);
120 							session.save(qa4);
121 
122 							QA qa5 = new QA();
123 							qa5.setQuestion("How did you like the contest?");
124 							qa5
125 									.setAnswer("It was very good, I felt great and my team did a good work.");
126 							qa5.setTopic(topic);
127 							session.save(qa5);
128 
129 							QA qa6 = new QA();
130 							qa6
131 									.setQuestion("How can one create a db2 instance?");
132 							qa6
133 									.setAnswer("To create a db2 instance, run db2icrt as root user.");
134 							qa6.setTopic(topic);
135 							session.save(qa6);
136 
137 							QA qa7 = new QA();
138 							qa7.setQuestion("What platform does DB2 run on?");
139 							qa7
140 									.setAnswer("DB2 runs on nearly anything from handheld to mainframe.");
141 							qa7.setTopic(topic);
142 							session.save(qa7);
143 							iDbPatchVersion = 1;
144 						}
145 						/*
146 						 * Add more db data patching by release here comes more
147 						 * db patch.
148 						 */
149 						dbPatchVersion.setValue(Integer
150 								.toString(iDbPatchVersion));
151 						session.save(dbPatchVersion);
152 
153 						return null;
154 					}
155 				});
156 			}
157 		});
158 	}
159 
160 	public PlatformTransactionManager getTxManager() {
161 		return txManager;
162 	}
163 
164 	public void setTxManager(PlatformTransactionManager txManager) {
165 		this.txManager = txManager;
166 	}
167 }