/actions/src/main/java/org/ala/spatial/services/dao/ApplicationDAOImpl.java
Java | 143 lines | 106 code | 19 blank | 18 comment | 1 complexity | 320c5407092c1addf6bb11e064cfde0c MD5 | raw file
1/************************************************************************** 2 * Copyright (C) 2010 Atlas of Living Australia 3 * All Rights Reserved. 4 * 5 * The contents of this file are subject to the Mozilla Public 6 * License Version 1.1 (the "License"); you may not use this file 7 * except in compliance with the License. You may obtain a copy of 8 * the License at http://www.mozilla.org/MPL/ 9 * 10 * Software distributed under the License is distributed on an "AS 11 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 12 * implied. See the License for the specific language governing 13 * rights and limitations under the License. 14 ***************************************************************************/ 15package org.ala.spatial.services.dao; 16 17import java.io.UnsupportedEncodingException; 18import java.security.MessageDigest; 19import java.security.NoSuchAlgorithmException; 20import java.sql.ResultSet; 21import java.sql.SQLException; 22import java.sql.Timestamp; 23import java.util.Date; 24import java.util.List; 25import java.util.logging.Level; 26import javax.annotation.Resource; 27import javax.sql.DataSource; 28 29import org.ala.spatial.services.dto.Application; 30import org.apache.log4j.Logger; 31import org.springframework.jdbc.core.RowMapper; 32import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource; 33import org.springframework.jdbc.core.namedparam.SqlParameterSource; 34import org.springframework.jdbc.core.simple.SimpleJdbcInsert; 35import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; 36 37/** 38 * @author ajay 39 */ 40@org.springframework.stereotype.Service("applicationDao") 41public class ApplicationDAOImpl implements ApplicationDAO { 42 43 private static final Logger logger = Logger.getLogger(ApplicationDAOImpl.class); 44 private SimpleJdbcTemplate jdbcTemplate; 45 private SimpleJdbcInsert insertApplication; 46 47 @Resource(name = "dataSource") 48 public void setDataSource(DataSource dataSource) { 49 this.jdbcTemplate = new SimpleJdbcTemplate(dataSource); 50 this.insertApplication = new SimpleJdbcInsert(dataSource).withTableName("applications").usingGeneratedKeyColumns("id"); 51 } 52 53 @Override 54 public List<Application> findApplications() { 55 logger.info("Getting a list of all applications"); 56 String sql = "select * from applications"; 57 return jdbcTemplate.query(sql, new ApplicationMapper()); 58 } 59 60 @Override 61 public List<Application> findApplicationsByName(String name) { 62 logger.info("Getting a list of all applications by name"); 63 String sql = "select * from applications where name = ?"; 64 return jdbcTemplate.query(sql, new ApplicationMapper(), name); 65 } 66 67 @Override 68 public List<Application> findApplicationsByEmail(String email) { 69 logger.info("Getting a list of all applications by email"); 70 String sql = "select * from applications where email = ?"; 71 return jdbcTemplate.query(sql, new ApplicationMapper(), email); 72 } 73 74 @Override 75 public List<Application> findApplicationsByOrganisation(String org) { 76 logger.info("Getting a list of all applications by organisation"); 77 String sql = "select * from applications where organisation = ?"; 78 return jdbcTemplate.query(sql, new ApplicationMapper(), org); 79 } 80 81 @Override 82 public Application findApplicationByAppId(String appid) { 83 logger.info("Getting a list of all applications by appid"); 84 String sql = "select * from applications where appid = ?"; 85 return jdbcTemplate.query(sql, new ApplicationMapper(), appid).get(0); 86 } 87 88 @Override 89 public void addApplication(Application app) { 90 logger.info("Adding a new application " + app.getName() + " by " + app.getOrganisation() + " (" + app.getEmail() + ") "); 91 92 app.setRegtime(new Timestamp(new Date().getTime())); 93 try { 94 MessageDigest md = MessageDigest.getInstance("SHA-1"); 95 md.update((app.getName() + app.getEmail() + app.getRegtime()).getBytes()); 96 byte byteData[] = md.digest(); 97 98 //convert the byte to hex format method 1 99 StringBuffer sb = new StringBuffer(); 100 for (int i = 0; i < byteData.length; i++) { 101 sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1)); 102 } 103 app.setAppid(sb.toString()); 104 } catch (NoSuchAlgorithmException ex) { 105 java.util.logging.Logger.getLogger(ApplicationDAOImpl.class.getName()).log(Level.SEVERE, null, ex); 106 } 107 108 System.out.println(app.toString()); 109 110 SqlParameterSource parameters = new BeanPropertySqlParameterSource(app); 111 Number appUid = insertApplication.executeAndReturnKey(parameters); 112 app.setId(appUid.longValue()); 113 } 114 115 @Override 116 public void removeApplication(String appid) { 117 throw new UnsupportedOperationException("Not supported yet."); 118 } 119 120 @Override 121 public void updateApplication(Application app) { 122 throw new UnsupportedOperationException("Not supported yet."); 123 } 124 125 private static final class ApplicationMapper implements RowMapper<Application> { 126 127 public Application mapRow(ResultSet rs, int rowNum) throws SQLException { 128 Application app = new Application(); 129 app.setAppid(rs.getString("appid")); 130 app.setDescription(rs.getString("description")); 131 app.setEmail(rs.getString("email")); 132 app.setId(rs.getLong("id")); 133 app.setName(rs.getString("name")); 134 app.setOrganisation(rs.getString("organisation")); 135 app.setRegtime(rs.getTimestamp("regtime")); 136 app.setStatus(rs.getString("status")); 137 app.setUrl(rs.getString("url")); 138 app.setContact(rs.getString("contact")); 139 app.setClientip(rs.getString("clientip")); 140 return app; 141 } 142 } 143}