|
package com.ekexiu.portal.job;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.sql.DataSource;
import org.jfw.apt.annotation.Autowrie;
import org.jfw.apt.annotation.Bean;
import org.jfw.util.ListUtil;
import com.ekexiu.portal.pojo.DataDictCode;
import com.ekexiu.portal.service.DataDictService;
@Bean
public class DictTaskJobEntry implements Runnable{
private long delayTime = 600;
private int maxCount = 20;
@Autowrie("dataSource")
private DataSource dataSource;
@Autowrie
private DataDictService dataDictService;
public DataDictService getDataDictService() {
return dataDictService;
}
public void setDataDictService(DataDictService dataDictService) {
this.dataDictService = dataDictService;
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public long getDelayTime() {
return delayTime;
}
public void setDelayTime(long delayTime) {
this.delayTime = delayTime;
}
public int getMaxCount() {
return maxCount;
}
public void setMaxCount(int maxCount) {
this.maxCount = maxCount;
}
@Override
public void run() {
try {
Connection con =dataSource.getConnection();
try{
Items items = this.fillSetValue(con, "SELECT INDUSTRY FROM PROFESSOR");
List<Item> list = items.getItemByDescSort(this.maxCount);
List<DataDictCode> dicts = new ArrayList<DataDictCode>();
for(Item item:list){
DataDictCode d = new DataDictCode();
d.setCaption(item.getCaption());
dicts.add(d);
}
this.dataDictService.setHotIndustries(dicts);
items = this.fillSetValue(con, "SELECT SUBJECT FROM PROFESSOR");
list = items.getItemByDescSort(this.maxCount);
dicts = new ArrayList<DataDictCode>();
for(Item item:list){
DataDictCode d = new DataDictCode();
d.setCaption(item.getCaption());
dicts.add(d);
}
this.dataDictService.setHotSubjects(dicts);
}catch(Exception e){
try{con.rollback();
}catch(Exception e1){e1.printStackTrace();}
}finally{
try{con.close();
}catch(Exception e){e.printStackTrace();}
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected Items fillSetValue(Connection con, String sql) throws SQLException {
Items items = new Items();
Statement st = con.createStatement();
try {
ResultSet rs = st.executeQuery(sql);
try {
while (rs.next()) {
mergSet(rs.getString(1), items);
}
} finally {
try {
rs.close();
} catch (Exception e) {
}
}
} finally {
try {
st.close();
} catch (Exception e) {
}
}
return items;
}
private void mergSet(String valList, Items vals) {
if (valList == null)
return;
List<String> ss = ListUtil.splitTrimExcludeEmpty(valList, ',');
for(String str:ss){
if(str!=null && str.length()>0)
vals.add(str);
}
}
protected static class Items{
private List<Item> items = new ArrayList<Item>();
public void add(String itemCaption){
for(Item item:this.items){
if(itemCaption.equals(item.getCaption())){
item.incVal();
return;
}
}
this.items.add(new Item(itemCaption));
}
public List<Item> getItemByDescSort(int limit){
Collections.sort(this.items);
if(limit >= items.size()) return items;
return items.subList(0, limit);
}
public List<Item> getItemByDescSort(){
Collections.sort(this.items);
return items.subList(0, items.size());
}
}
protected static class Item implements Comparable<Item>{
private String caption;
private long value = 1;
public Item(String caption){
this.caption = caption;
this.value=1;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
public long getValue() {
return value;
}
public void incVal(){
++this.value;
}
@Override
public int compareTo(Item o) {
long anVal = o.value;
return this.value>anVal?-1:this.value==anVal?0:1;
}
}
}
|