PageRenderTime 21ms CodeModel.GetById 15ms app.highlight 5ms RepoModel.GetById 0ms app.codeStats 0ms

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