home *** CD-ROM | disk | FTP | other *** search
Java Source | 2003-05-20 | 8.9 KB | 393 lines |
- package nl.pcactive.db;
-
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.Map;
- import java.util.TreeMap;
- import java.util.Vector;
-
- import nl.pcactive.bean.FamilyBean;
- import nl.pcactive.bean.IndividualBean;
-
- /**
- * <p>Description: GEDCOM 2 DATABASE</p>
- * <p>Copyright: Copyright (c) 2002</p>
- * <p>Company: Pc-Active</p>
- * @author Benny Lootens
- * @version 2.0
- */
- public class Relatives {
-
- /** if you use other tables name, you can change them here */
- public interface Tables {
-
- String INDIVIDUAL = "t_individual";
- String FAMILY = "t_family";
- String CHILD = "t_child";
- }
-
- /** @retrieves all relatives */
- public static Map getRelatives(
- boolean living,
- boolean death,
- String letter) {
- long s = System.currentTimeMillis();
-
- // my database: done 270ms in mysql, 231ms in postgresql -- 1638 individuals
-
- Connection con = null;
- PreparedStatement pstmt = null;
- ResultSet rs = null;
- TreeMap map = new TreeMap();
-
- try {
-
- String sql = " select * from " + Tables.INDIVIDUAL;
- if (living && death) {
- // do nothing
-
- if (letter != null && letter.length() == 1)
- sql += " WHERE lastname like '" + letter + "%'";
- } else {
- if (living) {
- sql += " WHERE length(deathdate) = 0 ";
- } else {
- sql += " WHERE length(deathdate) > 0 ";
- }
-
- if (letter != null && letter.length() == 1)
- sql += " AND lastname like '" + letter + "%'";
-
- }
-
- con = Jdbc.openConnection();
- pstmt = con.prepareStatement(sql);
- rs = pstmt.executeQuery();
- while (rs.next()) {
- IndividualBean bean = new IndividualBean();
- bean.populate(rs);
- map.put(
- bean.getLast()
- + " "
- + bean.getFirst()
- + " ("
- + bean.getId()
- + ")",
- bean);
- }
- } catch (Exception ex) {
- ex.printStackTrace(System.err);
- } finally {
- Jdbc.close(rs);
- Jdbc.close(pstmt);
- Jdbc.close(con);
-
- System.out.println(
- "getRelatives-elapsed time = "
- + (System.currentTimeMillis() - s)
- + "ms");
- }
- return map;
- }
-
- /** @return an individual by the given id */
- public static IndividualBean getIndividualById(int id) {
- Connection con = null;
- PreparedStatement pstmt = null;
- ResultSet rs = null;
- IndividualBean bean = new IndividualBean();
-
- try {
- con = Jdbc.openConnection();
- pstmt =
- con.prepareStatement(
- " select * from "
- + Tables.INDIVIDUAL
- + " where individual=?");
- pstmt.setInt(1, id);
- rs = pstmt.executeQuery();
- if (rs.next()) {
- bean.populate(rs);
- }
- } catch (Exception ex) {
- ex.printStackTrace(System.err);
- } finally {
- Jdbc.close(rs);
- Jdbc.close(pstmt);
- Jdbc.close(con);
- }
- return bean;
- }
-
- /** @retrieves all families */
- public static Map getFamilies(
- boolean living,
- boolean death,
- String letter) {
- long s = System.currentTimeMillis();
-
- // my database: done 10286ms in mysql, 3385ms in postgresql -- 578 families
-
- Connection con = null;
- PreparedStatement pstmt = null, pstmtIndiv = null;
- ResultSet rs = null, rs2 = null;
- TreeMap map = new TreeMap();
-
- try {
-
- String sql =
- " select * from "
- + Tables.FAMILY
- + ", "
- + Tables.INDIVIDUAL
- + " where "
- + Tables.FAMILY
- + ".husband = "
- + Tables.INDIVIDUAL
- + ".individual ";
-
- if (letter != null && letter.length() == 1)
- sql += " AND "
- + Tables.INDIVIDUAL
- + ".lastname like '"
- + letter
- + "%'";
-
- // if (living && death) {
- // // do nothing
- // }
- // else {
- // if (living) {
- // sql += " WHERE length(deathdate) = 0 ";
- // }
- // else {
- // sql += " WHERE length(deathdate) > 0 ";
- // }
- // }
-
- con = Jdbc.openConnection();
- pstmt = con.prepareStatement(sql);
-
- pstmtIndiv =
- con.prepareStatement(
- "select * from "
- + Tables.INDIVIDUAL
- + " where individual=?");
-
- rs = pstmt.executeQuery();
- while (rs.next()) {
- FamilyBean bean = new FamilyBean();
- bean.populate(rs);
-
- String husband = "";
-
- pstmtIndiv.setInt(1, Integer.parseInt(bean.getHusband()));
- rs2 = pstmtIndiv.executeQuery();
- if (rs2.next())
- husband =
- rs2.getString("lastname")
- + " "
- + rs2.getString("firstname");
- Jdbc.close(rs2);
-
- String wife = "";
-
- pstmtIndiv.setInt(1, Integer.parseInt(bean.getWife()));
- rs2 = pstmtIndiv.executeQuery();
- if (rs2.next())
- wife =
- rs2.getString("lastname")
- + " "
- + rs2.getString("firstname");
- Jdbc.close(rs2);
-
- map.put(
- husband + " x " + wife + " (" + bean.getId() + ")",
- bean);
- }
- } catch (Exception ex) {
- ex.printStackTrace(System.err);
- } finally {
- Jdbc.close(rs2);
- Jdbc.close(pstmtIndiv);
- Jdbc.close(rs);
- Jdbc.close(pstmt);
- Jdbc.close(con);
-
- System.out.println(
- "getFamilies-elapsed time = "
- + (System.currentTimeMillis() - s)
- + "ms");
- }
- return map;
- }
-
- /** @return an family by the given id */
- public static FamilyBean getFamilyById(int id) {
- Connection con = null;
- PreparedStatement pstmt = null;
- ResultSet rs = null;
- FamilyBean bean = new FamilyBean();
-
- try {
- con = Jdbc.openConnection();
- pstmt =
- con.prepareStatement(
- " select * from " + Tables.FAMILY + " where family=?");
- pstmt.setInt(1, id);
- rs = pstmt.executeQuery();
- if (rs.next()) {
- bean.populate(rs);
- }
- } catch (Exception ex) {
- ex.printStackTrace(System.err);
- } finally {
- Jdbc.close(rs);
- Jdbc.close(pstmt);
- Jdbc.close(con);
- }
- return bean;
- }
-
- /** @retrieves all children for a given family */
- public static Map getChildrenByFamily(int familyid) {
- Connection con = null;
- PreparedStatement pstmt = null;
- ResultSet rs = null;
- TreeMap map = new TreeMap();
-
- try {
- con = Jdbc.openConnection();
- pstmt =
- con.prepareStatement(
- " select child from " + Tables.CHILD + " where family=?");
- pstmt.setInt(1, familyid);
-
- rs = pstmt.executeQuery();
- while (rs.next()) {
- IndividualBean bean = getIndividualById(rs.getInt("child"));
- map.put(
- bean.getLast()
- + " "
- + bean.getFirst()
- + " ("
- + bean.getId()
- + ")",
- bean);
- }
- } catch (Exception ex) {
- ex.printStackTrace(System.err);
- } finally {
- Jdbc.close(rs);
- Jdbc.close(pstmt);
- Jdbc.close(con);
- }
- return map;
- }
-
- /** @return an family related to the given individual */
- public static Object[] getFamilyByIndividualId(int id) {
- Connection con = null;
- PreparedStatement pstmt = null;
- ResultSet rs = null;
- Vector v = new Vector();
-
- try {
- con = Jdbc.openConnection();
- pstmt =
- con.prepareStatement(
- " select * from "
- + Tables.FAMILY
- + " where husband=? or wife=? ");
- pstmt.setInt(1, id);
- pstmt.setInt(2, id);
- rs = pstmt.executeQuery();
- while (rs.next()) {
- FamilyBean bean = new FamilyBean();
- bean.populate(rs);
- v.addElement(bean);
- }
- } catch (Exception ex) {
- ex.printStackTrace(System.err);
- } finally {
- Jdbc.close(rs);
- Jdbc.close(pstmt);
- Jdbc.close(con);
- }
- return v.toArray();
- }
-
- public static IndividualBean getParentByChildId(int childid, String sex) {
-
- long s = System.currentTimeMillis();
-
- Connection con = null;
- PreparedStatement pstmt = null, pstmt2 = null;
- ResultSet rs = null;
- IndividualBean bean = new IndividualBean();
-
- try {
- int individualid = 0, familyid = 0;
-
- con = Jdbc.openConnection();
-
- pstmt =
- con.prepareStatement(
- " select * from "
- + Tables.INDIVIDUAL
- + " where individual=?");
-
- // get family to which the child belongs
- pstmt2 =
- con
- .prepareStatement(
- " select family from "
- + Tables.CHILD
- + " where child=? order by family desc" // latest family first
- );
- pstmt2.setInt(1, childid);
- rs = pstmt2.executeQuery();
- if (rs.next()) {
- familyid = rs.getInt("family");
- }
- Jdbc.close(rs);
- Jdbc.close(pstmt2);
-
- // get parent via the family
- pstmt2 =
- con.prepareStatement(
- " select "
- + ("F".equals(sex) ? "wife" : "husband")
- + " from "
- + Tables.FAMILY
- + " where family=? ");
- pstmt2.setInt(1, familyid);
- rs = pstmt2.executeQuery();
- if (rs.next()) {
- individualid = rs.getInt("F".equals(sex) ? "wife" : "husband");
- }
- Jdbc.close(rs);
-
- pstmt.setInt(1, individualid);
-
- rs = pstmt.executeQuery();
- if (rs.next()) {
- bean.populate(rs);
- }
- } catch (Exception ex) {
- ex.printStackTrace(System.err);
- } finally {
- Jdbc.close(rs);
- Jdbc.close(pstmt);
- Jdbc.close(pstmt2);
- Jdbc.close(con);
- System.out.println(
- "getParentByChild-elapsed time = "
- + (System.currentTimeMillis() - s)
- + "ms");
- }
- return bean;
- }
-
- }