Project

General

Profile

Download (3.83 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2009 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * See LICENSE.TXT at the top of this package for the full license terms.
8
 */
9

    
10
package eu.etaxonomy.cdm.api.service.lsid.impl;
11

    
12
import java.util.HashMap;
13
import java.util.Map;
14
import java.util.Set;
15

    
16
import javax.annotation.PostConstruct;
17

    
18
import org.apache.commons.logging.Log;
19
import org.apache.commons.logging.LogFactory;
20
import org.springframework.beans.factory.annotation.Autowired;
21
import org.springframework.stereotype.Component;
22
import org.springframework.transaction.PlatformTransactionManager;
23
import org.springframework.transaction.TransactionStatus;
24
import org.springframework.transaction.support.DefaultTransactionDefinition;
25

    
26
import eu.etaxonomy.cdm.api.service.lsid.LSIDRegistry;
27
import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
28
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
29
import eu.etaxonomy.cdm.model.common.LSID;
30
import eu.etaxonomy.cdm.model.common.LSIDAuthority;
31
import eu.etaxonomy.cdm.persistence.dao.common.IIdentifiableDao;
32
import eu.etaxonomy.cdm.persistence.dao.common.ILsidAuthorityDao;
33

    
34
@Component
35
public class LsidRegistryImpl implements LSIDRegistry {
36
	private static Log log = LogFactory.getLog(LsidRegistryImpl.class);
37
    //	 the main registry, stores all pattern mappings.
38
	private Map<String,IIdentifiableDao<? extends IdentifiableEntity>> registry = new HashMap<>();
39

    
40
	private Set<IIdentifiableDao> identifiableDaos;
41

    
42
	private ILsidAuthorityDao lsidAuthorityDao;
43

    
44
	protected PlatformTransactionManager transactionManager;
45

    
46
	protected DefaultTransactionDefinition txDefinition = new DefaultTransactionDefinition();
47

    
48
	@Autowired
49
	public void setIdentifiableDaos(Set<IIdentifiableDao> identifiableDaos) {
50
		this.identifiableDaos = identifiableDaos;
51
	}
52

    
53
	@Autowired
54
	public void setLsidAuthorityDao(ILsidAuthorityDao lsidAuthorityDao) {
55
		this.lsidAuthorityDao = lsidAuthorityDao;
56
	}
57

    
58
	@Autowired
59
	public void setTransactionManager(PlatformTransactionManager transactionManager) {
60
		this.transactionManager = transactionManager;
61
	}
62

    
63
	@PostConstruct
64
	public void init() {
65
		registry = new HashMap<String,IIdentifiableDao<? extends IdentifiableEntity>>();
66
		TransactionStatus txStatus = transactionManager.getTransaction(txDefinition);
67
		for(LSIDAuthority lsidAuthority : lsidAuthorityDao.list(null, null)) {
68
			for(String namespace : lsidAuthority.getNamespaces().keySet()) {
69
				Class<? extends IIdentifiableEntity> clazz = lsidAuthority.getNamespaces().get(namespace);
70
				boolean foundDao = false;
71
				for(IIdentifiableDao identifiableDao : identifiableDaos) {
72
					if(clazz.equals(identifiableDao.getType())) {
73
						foundDao = true;
74
						registry.put(lsidAuthority.getAuthority() + ":" + namespace, identifiableDao);
75
						break;
76
					}
77
				}
78

    
79
				if(!foundDao) {
80
					log.warn("Did not find DAO serving classes of type " + clazz + " for authority " + lsidAuthority.getAuthority() + " with namespace " + namespace);
81
				}
82
			}
83
		}
84

    
85
		transactionManager.commit(txStatus);
86
	}
87

    
88
	@Override
89
    public IIdentifiableDao<? extends IdentifiableEntity> lookupDAO(LSID lsid) {
90
        //		 if the LSID is null, then we return whatever is registered with the no lsid pattern
91

    
92
		if (lsid == null) {
93
			IIdentifiableDao<? extends IdentifiableEntity> identifiableDAO = registry.get(Pattern.NO_LSID_PATTERN.toString());
94
			return identifiableDAO;
95
		}
96

    
97
		Pattern lookup = new Pattern();
98
		lookup.setAuthority(lsid.getAuthority().toString());
99
		lookup.setNamespace(lsid.getNamespace());
100

    
101
		IIdentifiableDao<? extends IdentifiableEntity> identifiableDAO = registry.get(lookup.toString());
102
		if (identifiableDAO != null) {
103
			log.info("Found DAO " + identifiableDAO.getClass().getSimpleName());
104
			return identifiableDAO;
105
		}
106
		log.info("Didn't find dao, returning null");
107
		return null;
108
	}
109
}
(4-4/9)