Project

General

Profile

1
package eu.dnetlib.enabling.is.registry;
2

    
3
import java.util.Date;
4
import javax.xml.xpath.XPathExpressionException;
5

    
6
import eu.dnetlib.enabling.is.registry.schema.OpaqueResourceValidator;
7
import eu.dnetlib.enabling.is.registry.schema.ValidationException;
8
import eu.dnetlib.enabling.is.registry.validation.ProfileValidationStrategy;
9
import eu.dnetlib.enabling.is.registry.validation.RegistrationPhase;
10
import eu.dnetlib.enabling.is.store.ISStore;
11
import eu.dnetlib.enabling.is.store.ISStoreException;
12
import eu.dnetlib.enabling.tools.*;
13
import eu.dnetlib.rmi.enabling.ISRegistryException;
14
import eu.dnetlib.rmi.enabling.ISRegistryService;
15
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.beans.factory.annotation.Required;
17

    
18
public class IdPreservingPendingResourceManagerImpl implements PendingResourceManager, ResourceKindResolver {
19

    
20
	/**
21
	 * is registry
22
	 */
23
	private ISRegistryService isRegistry;
24

    
25
	/**
26
	 * is store
27
	 */
28
	private ISStore isStore;
29

    
30
	/**
31
	 * This bean resolver
32
	 */
33
	private ResourceKindResolver resourceKindResolver;
34

    
35
	/**
36
	 * used to validate resources.
37
	 */
38
	private OpaqueResourceValidator resourceValidator;
39

    
40
	/**
41
	 * used to get the root collection.
42
	 */
43
	private XQueryUtils xqueryUtils;
44

    
45
	/**
46
	 * manages resource identifier mappings with the abstract xmldb namespace (files/collections).
47
	 */
48
	@Autowired
49
	private ResourceIdentifierResolver resIdResolver = new CompatResourceIdentifierResolverImpl();
50

    
51
	/**
52
	 * used to create a new resource id.
53
	 */
54
	@Autowired
55
	private ResourceIdentifierComposer resIdComposer = new CompatResourceIdentifierResolverImpl();
56

    
57
	/**
58
	 * used to validate resources w.r.t. a set of defined properties.
59
	 */
60
	@Autowired
61
	private ProfileValidationStrategy profileValidationStrategy;
62

    
63
	/**
64
	 * {@inheritDoc}
65
	 * 
66
	 * @see eu.dnetlib.enabling.is.registry.PendingResourceManager#setPending(eu.dnetlib.enabling.tools.OpaqueResource)
67
	 */
68
	@Override
69
	public void setPending(final OpaqueResource resource, final boolean local) {
70
		try {
71
			resource.setResourceKind(getPendingKindForType(resource.getResourceType()));
72

    
73
			if (!local) {
74
				isRegistry.deleteProfile(resource.getResourceId());
75

    
76
				registerProfile(resource);
77
			}
78

    
79
		} catch (XPathExpressionException e) {
80
			throw new IllegalStateException(e);
81
		} catch (ISRegistryException e) {
82
			throw new IllegalStateException(e);
83
		}
84
	}
85

    
86
	/**
87
	 * {@inheritDoc}
88
	 * 
89
	 * @see eu.dnetlib.enabling.is.registry.PendingResourceManager#setPending(eu.dnetlib.enabling.tools.OpaqueResource)
90
	 */
91
	@Override
92
	public void setPending(final OpaqueResource resource) {
93
		setPending(resource, false);
94
	}
95

    
96
	/**
97
	 * {@inheritDoc}
98
	 * 
99
	 * @see eu.dnetlib.enabling.is.registry.PendingResourceManager#setValid(eu.dnetlib.enabling.tools.OpaqueResource)
100
	 */
101
	@Override
102
	public void setValid(final OpaqueResource resource) {
103
		try {
104
			if (resource.getResourceKind() != null && resource.getResourceKind().equals(getNormalKindForType(resource.getResourceType()))) { throw new IllegalArgumentException(
105
					"trying to validate an already valid resource"); }
106

    
107
			profileValidationStrategy.accept(resource, RegistrationPhase.Validate);
108

    
109
			resource.setResourceKind(getNormalKindForType(resource.getResourceType()));
110

    
111
			isRegistry.deleteProfile(resource.getResourceId());
112

    
113
			registerProfile(resource);
114

    
115
		} catch (XPathExpressionException e) {
116
			throw new IllegalStateException(e);
117
		} catch (ISRegistryException e) {
118
			throw new IllegalStateException(e);
119
		} catch (ValidationException e) {
120
			throw new IllegalStateException(e);
121
		}
122
	}
123

    
124
	public String registerProfile(final OpaqueResource resource) throws ISRegistryException {
125
		try {
126
			final String oldId = resource.getResourceId();
127
			final String fileName = resIdResolver.getFileName(oldId);
128
			final String newColl = xqueryUtils.getCollectionPath(resource);
129

    
130
			final String newId = resIdComposer.createResourceId(fileName, newColl);
131

    
132
			resource.setResourceId(newId);
133

    
134
			resource.setModificationDate(new Date());
135

    
136
			// log.info("validating to xml schema: " + resource.asString());
137
			resourceValidator.validate(resource);
138

    
139
			// TODO: factor out ResourceType class
140
			isStore.insertXML(fileName, xqueryUtils.getRootCollection() + newColl, resource.asString());
141

    
142
			return resource.getResourceId();
143
		} catch (ISStoreException e) {
144
			throw new ISRegistryException(e);
145
		} catch (ValidationException e) {
146
			throw new ISRegistryException("profile is not conforming to the schema: " + e.getMessage(), e);
147
		}
148
	}
149

    
150
	@Override
151
	public String getNormalKindForType(final String resourceType) throws XPathExpressionException {
152
		return resourceKindResolver.getNormalKindForType(resourceType);
153
	}
154

    
155
	@Override
156
	public String getPendingKindForType(final String resourceType) throws XPathExpressionException {
157
		return resourceKindResolver.getPendingKindForType(resourceType);
158
	}
159

    
160
	// /////////////////////////////
161

    
162
	public ResourceKindResolver getResourceKindResolver() {
163
		return resourceKindResolver;
164
	}
165

    
166
	@Required
167
	public void setResourceKindResolver(final ResourceKindResolver resourceKindResolver) {
168
		this.resourceKindResolver = resourceKindResolver;
169
	}
170

    
171
	@Required
172
	public void setXqueryUtils(final XQueryUtils xqueryUtils) {
173
		this.xqueryUtils = xqueryUtils;
174
	}
175

    
176
	public OpaqueResourceValidator getResourceValidator() {
177
		return resourceValidator;
178
	}
179

    
180
	@Required
181
	public void setResourceValidator(final OpaqueResourceValidator resourceValidator) {
182
		this.resourceValidator = resourceValidator;
183
	}
184

    
185
	public ISRegistryService getIsRegistry() {
186
		return isRegistry;
187
	}
188

    
189
	@Required
190
	public void setIsRegistry(final ISRegistryService isRegistry) {
191
		this.isRegistry = isRegistry;
192
	}
193

    
194
	public ISStore getIsStore() {
195
		return isStore;
196
	}
197

    
198
	@Required
199
	public void setIsStore(final ISStore isStore) {
200
		this.isStore = isStore;
201
	}
202

    
203
}
(4-4/11)