import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Session;
/**
* Loads reader.general and reader.tech and uses them to construct People and
* Subscription objects. Then saves them via Hibernate.
*
* @author bob
*
*/
public class FileLoader {
public static void main(String[] args) throws IOException {
Logger.global.setLevel(Level.WARNING);
BufferedReader br = new BufferedReader(new InputStreamReader(
FileLoader.class.getResourceAsStream("readers.general")));
String reader = null;
HashMap<String, Person> peeps = new HashMap<String, Person>();
while ((reader = br.readLine()) != null && reader.trim().length() != 0) {
reader = reader.trim();
Person p = new Person();
peeps.put(reader, p);
p.email = reader;
p.fullname = reader;
p.subscriptions = new ArrayList<Subscription>();
Subscription s = new Subscription();
s.email = reader;
s.subject = "general";
p.subscriptions.add(s);
}
br = new BufferedReader(new InputStreamReader(FileLoader.class
.getResourceAsStream("readers.tech")));
while ((reader = br.readLine()) != null && reader.trim().length() != 0) {
reader = reader.trim();
Subscription s = new Subscription();
s.email = reader;
s.subject = "tech";
Person p = peeps.get(reader);
p.subscriptions.add(s);
}
Session sess = SessionFactoryFascade.getSessionCreate();
sess.beginTransaction();
for (Iterator iter = peeps.values().iterator(); iter.hasNext();) {
Person p = (Person) iter.next();
sess.persist(p);
}
sess.getTransaction().commit();
sess.close();
}
}
|