/src/gisdcl/schools/DistrictIdToZipDAO.java

https://bitbucket.org/iyusuf/schoolsearchgis · Java · 82 lines · 59 code · 7 blank · 16 comment · 4 complexity · b05b8d863d6279bf8b0542d6ff10934d MD5 · raw file

  1. package gisdcl.schools;
  2. import gisdcl.PMF;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.logging.Logger;
  6. import javax.jdo.PersistenceManager;
  7. import javax.jdo.Query;
  8. import javax.jdo.Transaction;
  9. /**
  10. * TODO: Make this DAO as Static or Singleton after further reading.
  11. *
  12. * @author iyusuf
  13. *
  14. */
  15. public class DistrictIdToZipDAO {
  16. private static final Logger log = Logger.getLogger(DistrictIdToZipDAO.class.getName());
  17. public String getDistrictsByAZip(String pZipId){
  18. log.info(" starting getDistrictsByAZip method, working with zip--> " + pZipId);
  19. StringBuffer districtIds = new StringBuffer();
  20. PersistenceManager pm = PMF.get().getPersistenceManager();
  21. Query query = pm.newQuery(DistrictIdToZip.class);
  22. query.setFilter("zip == pZipId");
  23. //query.setOrdering("hireDate desc");
  24. query.declareParameters("String pZipId");
  25. try {
  26. List<DistrictIdToZip> results = (List<DistrictIdToZip>) query.execute(pZipId);
  27. if (results.iterator().hasNext()) {
  28. for (DistrictIdToZip e : results) {
  29. log.info("..................");
  30. log.info("e.getSchoolid() : " + e.getSchoolid());
  31. log.info("e.getSchoolname() : " + e.getSchoolname());
  32. log.info("e.getZip() : " + e.getZip());
  33. districtIds.append(e.getSchoolid() + " , ");
  34. }
  35. return districtIds.toString();
  36. } else {
  37. log.info("No Result found for the given zipid == " + pZipId);
  38. return ("No Result found for the given zipid == " + pZipId);
  39. }
  40. } catch (Exception e){
  41. log.info("Exception at DAO DistrictIdToZip");
  42. return "Exception at DAO DistrictIdToZip";
  43. }finally {
  44. query.closeAll();
  45. pm.close();
  46. }
  47. }
  48. /**
  49. * Saves DistrictIdToZip to BigTable
  50. *
  51. * @param distToZipList
  52. * @return a descriptive text back to browser
  53. * @throws Exception
  54. */
  55. public String saveDisttoZipList(ArrayList<DistrictIdToZip> distToZipList) throws Exception{
  56. log.info("Starting persistence operation on DistrictIdToZip");
  57. log.info("Size of Collection: " + distToZipList.size());
  58. PersistenceManager pm = PMF.get().getPersistenceManager();
  59. try {
  60. //Save All DistrictToZip java object in List at once
  61. pm.makePersistentAll(distToZipList);
  62. log.info("Transaction committed");
  63. return "transaction committed for distToZipList";
  64. } catch(Exception e){
  65. log.warning("Error saving DistrictIdToZip collection");
  66. log.warning(e.getMessage());
  67. throw e;
  68. }finally {
  69. //Close Persistence Manager if it is not closed
  70. if(!pm.isClosed()){
  71. pm.close();
  72. }
  73. }
  74. }
  75. }