Advertisement
Guest User

Untitled

a guest
Oct 8th, 2013
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. package com.smth.cool;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.HashMap;
  8. import java.util.HashSet;
  9. import java.util.Map;
  10. import java.util.Set;
  11.  
  12. import javax.sql.DataSource;
  13.  
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import org.springframework.dao.DataAccessException;
  17. import org.springframework.jdbc.core.ConnectionCallback;
  18. import org.springframework.jdbc.core.JdbcTemplate;
  19.  
  20. import com.smth.verycool.RPConfig;
  21. import com.smth.verycool.SimpleSqlBuilder;
  22. import com.smth.verycool.SqlStatement;
  23.  
  24. public class SecurityExchangeTool {
  25.  
  26.     private final Logger log = LoggerFactory.getLogger(getClass());
  27.  
  28.     private Map loadData(final SqlStatement coListSql) {
  29.         DataSource ds = RPConfig.getConfig().getSqlDataSource();
  30.         return (Map) new JdbcTemplate(ds).execute(new ConnectionCallback() {
  31.             @Override
  32.             public Object doInConnection(Connection con) throws SQLException,
  33.                     DataAccessException {
  34.                 Map totals = new HashMap();
  35.                 {
  36.                     SqlStatement sqt =
  37.                             new SimpleSqlBuilder().
  38.                                     line("select cs.pri_gmc, count(distinct cs.co_id) total").
  39.                                     line("from cs").
  40.                                     line("where cs.finish_date is null").
  41.                                     line("group by cs.pri_gmc").
  42.                                     line("having count(distinct cs.co_id) > 0").
  43.                                     toStatement();
  44.                     if (log.isDebugEnabled()) {
  45.                         log.debug(sqt.toString());
  46.                     }
  47.                     PreparedStatement st = sqt.prepareStatement(con);
  48.                     try {
  49.                         ResultSet rs = st.executeQuery();
  50.                         while (rs.next()) {
  51.                             totals.put(rs.getString(1), Integer.valueOf(rs.getInt(2)));
  52.                         }
  53.                     } catch (SQLException e) {
  54.                         log.warn(sqt.toString());
  55.                         throw e;
  56.                     } finally {
  57.                         st.close();
  58.                     }
  59.                 }
  60.  
  61.                 SimpleSqlBuilder sqb =
  62.                         new SimpleSqlBuilder().
  63.                                 append("select cs.pri_gmc, count(distinct cs.co_id) part").
  64.                                 line(" from").
  65.                                 subAlias("rel", coListSql).
  66.                                 line(", cs").
  67.                                 line("where cs.finish_date is null and rel.id = cs.co_id").
  68.                                 line("group by cs.pri_gmc").
  69.                                 line("union").
  70.                                 line("select -1 pri_gmc, count(*) part").
  71.                                 line(" from").
  72.                                 subAlias("rel", coListSql).
  73.                                 line(" where not exists (select 'x' from cs where cs.co_id = rel.id)");
  74.                 SqlStatement sq = sqb.toStatement();
  75.                 if (log.isDebugEnabled()) {
  76.                     log.debug(sq.toString());
  77.                 }
  78.                 PreparedStatement st = sq.prepareStatement(con);
  79.                 try {
  80.                     Map map = new HashMap();
  81.                     ResultSet rs = st.executeQuery();
  82.                     while (rs.next()) {
  83.                         String exch = rs.getString(1);
  84.                         Integer total = (Integer) totals.get(exch);
  85.                         map.put(exch, new PartAndTotal(rs.getInt(2), (total != null ? total.intValue() : 0)));
  86.                     }
  87.                     return map;
  88.                 } catch (SQLException e) {
  89.                     log.warn(sq.toString());
  90.                     throw e;
  91.                 } finally {
  92.                     st.close();
  93.                 }
  94.             }
  95.         });
  96.     }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement