All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aspire.nm.component.common.mobile.Strategy.Impl.MySqlLoader Maven / Gradle / Ivy

There is a newer version: 1.0.10
Show newest version
package com.aspire.nm.component.common.mobile.Strategy.Impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.aspire.nm.component.common.mobile.MobileUtil;
import com.aspire.nm.component.common.mobile.Strategy.LoadStrategy;
import com.aspire.nm.component.common.mobile.Strategy.Impl.db.black.Black;
import com.aspire.nm.component.common.mobile.Strategy.Impl.db.legalPerfix.LegalPerfix;
import com.aspire.nm.component.common.mobile.Strategy.Impl.db.segment.SegMent;
import com.aspire.nm.component.common.mobile.Strategy.Impl.db.switchs.Swithchs;
import com.aspire.nm.component.common.mobile.Strategy.Impl.db.update.UpdateTimes;
import com.aspire.nm.component.commonUtil.constants.ConstantConfig;
import com.aspire.nm.component.commonUtil.date.DateUtil;
import com.aspire.nm.component.commonUtil.db.DBConnectionManager;
import com.aspire.nm.component.commonUtil.db.Db;
import com.aspire.nm.component.commonUtil.log.BootLogger;


public class MySqlLoader implements LoadStrategy{
    
    private static DBConnectionManager dBConnectionManager = null;
    private static Db db = new Db();
    
    private static Logger logger = Logger.getLogger(MobileUtil.class);
    private static Logger bootLogger = Logger.getLogger(BootLogger.class);
    
    


    public MySqlLoader(String jdbcConfigPath){
        dBConnectionManager = new DBConnectionManager(jdbcConfigPath);
        Connection con = dBConnectionManager.getConnection("mysql");
        
        try{
            db.saveOrUpdateOrDelete(ConstantConfig.getContentValue("config/sql/create/updateMobile.sql"), null, con);
            bootLogger.debug("create updateMobile");
            
            db.saveOrUpdateOrDelete("insert common_update_mobile(table_name,update_time) values ('"+UpdateTimes.TABLE_NAME_BLACK+"',?)", new Object[]{DateUtil.getCurDateTime()}, con);
            db.saveOrUpdateOrDelete("insert common_update_mobile(table_name,update_time) values ('"+UpdateTimes.TABLE_NAME_LEGALPERFIX+"',?)", new Object[]{DateUtil.getCurDateTime()}, con);
            db.saveOrUpdateOrDelete("insert common_update_mobile(table_name,update_time) values ('"+UpdateTimes.TABLE_NAME_SEGMENT+"',?)", new Object[]{DateUtil.getCurDateTime()}, con);
            db.saveOrUpdateOrDelete("insert common_update_mobile(table_name,update_time) values ('"+UpdateTimes.TABLE_NAME_SWITCH+"',?)", new Object[]{DateUtil.getCurDateTime()}, con);
            
            bootLogger.debug("init updateMobile");
        }catch(Exception e){
            if(e.toString().indexOf("already exists") != -1){
                bootLogger.debug("exist update");
            }else{
                bootLogger.error(e);
                e.printStackTrace();
            }
        }
        try{
            db.saveOrUpdateOrDelete(ConstantConfig.getContentValue("config/sql/create/legalperfix.sql"), null, con);
            bootLogger.debug("create legalperfix");
            
            db.saveOrUpdateOrDelete("insert common_mobile_legalperfix(mobile,cmcc_mobile,ctcc_mobile,cucc_mobile) values (?,?,?,?)", 
                    new Object[]{"13,14,15,18","134,135,136,137,138,139,147,150,151,152,157,158,159,178,182,183,187,188","133,153,177,180,189,181","130,131,132,145,155,156,176,185,186"}, con);
            
            bootLogger.debug("init legalperfix");
        }catch(Exception e){
            if(e.toString().indexOf("already exists") != -1){
                bootLogger.debug("exist legalperfix");
            }else{
                bootLogger.error(e);
                e.printStackTrace();
            }
        }
        try{
            db.saveOrUpdateOrDelete(ConstantConfig.getContentValue("config/sql/create/segment.sql"), null, con);
            bootLogger.debug("create segment");
        }catch(Exception e){
            if(e.toString().indexOf("already exists") != -1){
                bootLogger.debug("exist segment");
            }else{
                bootLogger.error(e);
                e.printStackTrace();
            }
        }
        try{
            db.saveOrUpdateOrDelete(ConstantConfig.getContentValue("config/sql/create/black.sql"), null, con);
            bootLogger.debug("create black");
        }catch(Exception e){
            if(e.toString().indexOf("already exists") != -1){
                bootLogger.debug("exist black");
            }else{
                bootLogger.error(e);
                e.printStackTrace();
            }
        }
        try{
            db.saveOrUpdateOrDelete(ConstantConfig.getContentValue("config/sql/create/switch.sql"), null, con);
            bootLogger.debug("create switch");
        }catch(Exception e){
            if(e.toString().indexOf("already exists") != -1){
                bootLogger.debug("exist switch");
            }else{
            	logger.error(e);
                e.printStackTrace();
            }
        }
        dBConnectionManager.freeConnection("mysql", con);
    }
    
    

    @Override
    public UpdateTimes getUpdateTimes() {
    	
        UpdateTimes updateTimes = new UpdateTimes();
        
        Connection con = dBConnectionManager.getConnection("mysql");
        List> result;
        try {
            result = db.query(ConstantConfig.getContentValue("config/sql/query/updateMobile.sql"), null, con);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
        for(Map map : result){
        	if(((String)map.get("table_name")).equals(UpdateTimes.TABLE_NAME_BLACK)){
        		updateTimes.setUpdateBlackTime((String)map.get("update_time"));
        	}
        	if(((String)map.get("table_name")).equals(UpdateTimes.TABLE_NAME_LEGALPERFIX)){
        		updateTimes.setUpdateLegalPerfixTime((String)map.get("update_time"));
        	}
        	if(((String)map.get("table_name")).equals(UpdateTimes.TABLE_NAME_SEGMENT)){
        		updateTimes.setUpdateSegmentTime((String)map.get("update_time"));
        	}
        	if(((String)map.get("table_name")).equals(UpdateTimes.TABLE_NAME_SWITCH)){
        		updateTimes.setUpdateSwitchTime((String)map.get("update_time"));
        	}
        }
        dBConnectionManager.freeConnection("mysql", con);
        return updateTimes;
    }

    @Override
    public List loadSegMents() {
    	
        List list = new ArrayList();
        
        Connection con = dBConnectionManager.getConnection("mysql");
        List> result;
        try {
            result = db.query(ConstantConfig.getContentValue("config/sql/query/segment.sql"), null, con);
        } catch (SQLException e) {
        	logger.error(e);
            e.printStackTrace();
            return null;
        }
        for(Map map : result){
            SegMent segMent = new SegMent(
                    (String)map.get("segment"),
                    (String)map.get("province"),
                    (String)map.get("province_code"),
                    (String)map.get("city"),
                    (String)map.get("city_code"),
                    (String)map.get("mmsc_id"));
            list.add(segMent);
        }
        dBConnectionManager.freeConnection("mysql", con);
        return list;
    }

    
    
    
    
    
    
    @Override
    public List loadBlacks() {

        List list = new ArrayList();
        Connection con = dBConnectionManager.getConnection("mysql");
        List> result;
        try {
            result = db.query(ConstantConfig.getContentValue("config/sql/query/black.sql"), null, con);
        } catch (SQLException e) {
        	logger.error(e);
            e.printStackTrace();
            return null;
        }
        
        for(Map map : result){
            Black black = new Black((String)map.get("terminal"),(String)map.get("desc"));
            list.add(black);
        }
        dBConnectionManager.freeConnection("mysql", con);
        return list;
    }



    @Override
    public List loadSwithchs() {

        List list = new ArrayList();
        Connection con = dBConnectionManager.getConnection("mysql");
        List> result;
        try {
            result = db.query(ConstantConfig.getContentValue("config/sql/query/switch.sql"), null, con);
        } catch (SQLException e) {
        	logger.error(e);
            e.printStackTrace();
            return null;
        }
        for(Map map : result){
            Swithchs swithchs = new Swithchs((String)map.get("terminal"),(Integer)map.get("type"));
            list.add(swithchs);
        }
        dBConnectionManager.freeConnection("mysql", con);
        return list;
    }

    
    @Override
    public LegalPerfix loadLegalPerfix() {

    	LegalPerfix legalPerfix = new LegalPerfix();
        Connection con = dBConnectionManager.getConnection("mysql");
        List> result;
        try {
            result = db.query(ConstantConfig.getContentValue("config/sql/query/legalperfix.sql"), null, con);
        } catch (SQLException e) {
        	logger.error(e);
            e.printStackTrace();
            return null;
        }
        for(Map map : result){
        	legalPerfix.setPerfix_legal_mobile(((String)map.get("mobile")).split(","));
        	legalPerfix.setPerfix_legal_cmcc_mobile(((String)map.get("cmcc_mobile")).split(","));
        	legalPerfix.setPerfix_legal_ctcc_mobile(((String)map.get("ctcc_mobile")).split(","));
        	legalPerfix.setPerfix_legal_cucc_mobile(((String)map.get("cucc_mobile")).split(","));
        }
        dBConnectionManager.freeConnection("mysql", con);
        return legalPerfix;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy