Project

General

Profile

1
package gr.uoa.di.driver.enabling.islookup;
2

    
3
import java.util.ArrayList;
4
import java.util.HashMap;
5
import java.util.List;
6
import java.util.Map;
7

    
8
import org.apache.log4j.Logger;
9

    
10
import eu.dnetlib.domain.EPR;
11
import eu.dnetlib.domain.SearchCriteria;
12
import eu.dnetlib.domain.SecureDriverResource;
13
import eu.dnetlib.domain.enabling.SecurityProfile;
14
import eu.dnetlib.domain.enabling.SecurityProfileSearchCriteria;
15
import gr.uoa.di.driver.enabling.ISLookUp;
16
import gr.uoa.di.driver.enabling.ISLookUpException;
17

    
18
public class SecurityAwareLookupImpl<R extends SecureDriverResource> implements
19
		ISLookUp<R> {
20

    
21
	private static Logger logger = Logger.getLogger(SecurityAwareLookupImpl.class);
22
	
23
	private ISLookUp<R> lookUp = null;
24
	
25
	private ISLookUp<SecurityProfile> securityLookUp = null;
26

    
27
	@Override
28
	public List<R> fetch(SearchCriteria criteria) throws ISLookUpException {
29
		logger.debug("fetching secure profiles");
30
		List<R> list = lookUp.fetch(criteria);
31

    
32
		logger.debug("Loading and assigning security profiles");
33
		Map<String, SecurityProfile> secProfiles = this.getSecurityProfiles(list);
34
		
35
		for (R r : list) {
36
			SecurityProfile secProfile = secProfiles.get(r.getResourceId());
37
			
38
			r.setSecurityProfile(secProfile);
39
		}
40

    
41
		return list;
42
	}
43

    
44
	@Override
45
	public List<String> fetch(String XQuery) throws ISLookUpException {
46
		return lookUp.fetch(XQuery);
47
	}
48

    
49
	@Override
50
	public R getById(String id) throws ISLookUpException {
51
		R r = lookUp.getById(id);
52
		
53
		r.setSecurityProfile(this.getSecurityProfile(id));
54
		
55
		return r;
56
	}
57

    
58
	@Override
59
	public List<R> getByid(String[] ids) throws ISLookUpException {
60
		List<R> list = new ArrayList<R>();
61
		
62
		for (String id:ids)
63
			list.add(lookUp.getById(id));
64
		
65
		logger.debug("Loading and assigning security profiles");
66
		Map<String, SecurityProfile> secProfiles = this.getSecurityProfiles(list);
67
		
68
		for (R r : list) {
69
			SecurityProfile secProfile = secProfiles.get(r.getResourceId());
70
			
71
			r.setSecurityProfile(secProfile);
72
		}
73
		
74
		return list;
75
	}
76

    
77
	@Override
78
	public R getUniqueResult(SearchCriteria criteria) throws ISLookUpException {
79
		R r = lookUp.getUniqueResult(criteria);
80
		
81
		if (r != null)
82
			r.setSecurityProfile(this.getSecurityProfile(r.getResourceId()));
83
		
84
		return r;
85
	}
86

    
87
	@Override
88
	public List<R> performQuickSearch(SearchCriteria criteria)
89
			throws ISLookUpException {
90
		List<R> list = this.lookUp.performQuickSearch(criteria);
91
		
92
		logger.debug("Loading and assigning security profiles");
93
		Map<String, SecurityProfile> secProfiles = this.getSecurityProfiles(list);
94
		
95
		for (R r : list) {
96
			SecurityProfile secProfile = secProfiles.get(r.getResourceId());
97
			
98
			r.setSecurityProfile(secProfile);
99
		}
100

    
101
		return list;
102
	}
103

    
104
	public void setLookUp(ISLookUp<R> lookUp) {
105
		this.lookUp = lookUp;
106
	}
107
	
108
	public void setSecurityLookUp(ISLookUp<SecurityProfile> securityLookUp) {
109
		this.securityLookUp = securityLookUp;
110
	}
111

    
112
	private Map<String, SecurityProfile> getSecurityProfiles(List<R> profiles)
113
			throws ISLookUpException {
114
		SecurityProfileSearchCriteria crit = new SecurityProfileSearchCriteria();
115
		Map<String, SecurityProfile> map = new HashMap<String, SecurityProfile>();
116

    
117
		for (R r : profiles)
118
			crit.getDriverResourceIds().add(r.getResourceId());
119
		
120
		for (SecurityProfile secProfile: securityLookUp.fetch(crit))
121
			map.put(secProfile.getDriverResourceId(), secProfile);
122
		
123
		return map;
124
	}
125
	
126
	private SecurityProfile getSecurityProfile(String resourceId)
127
			throws ISLookUpException {
128
		SecurityProfileSearchCriteria crit = new SecurityProfileSearchCriteria();
129

    
130
		crit.getDriverResourceIds().add(resourceId);
131

    
132
		return this.securityLookUp.getUniqueResult(crit);
133
	}
134
}
(4-4/4)