InstitutionsRepository.java 3.4 KB
Newer Older
M
v3.2.1  
MaxKey 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 

M
MaxKey 已提交
18
package org.maxkey.persistence.repository;
M
v3.2.1  
MaxKey 已提交
19 20 21 22

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
M
MaxKey 已提交
23
import java.util.concurrent.ConcurrentHashMap;
M
v3.2.1  
MaxKey 已提交
24 25 26 27 28 29 30 31 32 33 34
import java.util.concurrent.TimeUnit;

import org.maxkey.entity.Institutions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

M
MaxKey 已提交
35 36
public class InstitutionsRepository {
    private static Logger _logger = LoggerFactory.getLogger(InstitutionsRepository.class);
M
v3.2.1  
MaxKey 已提交
37
    
M
MaxKey 已提交
38
    private static final String SELECT_STATEMENT = 
M
sso  
MaxKey 已提交
39
    						"select * from  mxk_institutions where id = ? or domain = ? " ;
M
v3.2.1  
MaxKey 已提交
40 41 42

    protected static final Cache<String, Institutions> institutionsStore = 
            Caffeine.newBuilder()
M
v 3.3.0  
MaxKey 已提交
43 44
                	.expireAfterWrite(60, TimeUnit.MINUTES)
                	.build();
M
v3.2.1  
MaxKey 已提交
45
    
M
MaxKey 已提交
46 47 48
    //id domain mapping
    protected static final  ConcurrentHashMap<String,String> mapper = new ConcurrentHashMap<String,String>();
    
M
v3.2.1  
MaxKey 已提交
49 50
    protected JdbcTemplate jdbcTemplate;
    
M
MaxKey 已提交
51
    public InstitutionsRepository(JdbcTemplate jdbcTemplate) {
M
v3.2.1  
MaxKey 已提交
52 53
        this.jdbcTemplate = jdbcTemplate;
    }
M
MaxKey 已提交
54
    
M
sso  
MaxKey 已提交
55 56 57
    public Institutions get(String instIdOrDomain) {
        _logger.trace(" instId {}" , instIdOrDomain);
        Institutions inst = institutionsStore.getIfPresent(mapper.get(instIdOrDomain)==null ? "1" : mapper.get(instIdOrDomain) );
M
MaxKey 已提交
58 59
        if(inst == null) {
	        List<Institutions> institutions = 
M
sso  
MaxKey 已提交
60
	        		jdbcTemplate.query(SELECT_STATEMENT,new InstitutionsRowMapper(),instIdOrDomain,instIdOrDomain);
M
MaxKey 已提交
61 62 63 64 65 66
	        
	        if (institutions != null && institutions.size() > 0) {
	        	inst = institutions.get(0);
	        }
	        institutionsStore.put(inst.getDomain(), inst);
	        mapper.put(inst.getId(), inst.getDomain());
M
v3.2.1  
MaxKey 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80
        }
        
        return inst;
    }
    
    public class InstitutionsRowMapper implements RowMapper<Institutions> {
        @Override
        public Institutions mapRow(ResultSet rs, int rowNum) throws SQLException {
        	Institutions institution = new Institutions();
        	institution.setId(rs.getString("id"));
        	institution.setName(rs.getString("name"));
        	institution.setFullName(rs.getString("fullname"));
        	institution.setLogo(rs.getString("logo"));
        	institution.setDomain(rs.getString("domain"));
M
MaxKey 已提交
81
        	institution.setFrontTitle(rs.getString("fronttitle"));
M
MaxKey 已提交
82
        	institution.setConsoleTitle(rs.getString("consoletitle"));
M
MaxKey 已提交
83 84 85
        	institution.setCaptchaType(rs.getString("captchatype"));
        	institution.setCaptchaSupport(rs.getString("captchasupport"));
        	institution.setDefaultUri(rs.getString("defaultUri"));
M
v3.2.1  
MaxKey 已提交
86 87 88 89
            return institution;
        }
    }
}