Project

General

Profile

« Previous | Next » 

Revision 47884

[maven-release-plugin] copy for tag dnet-oai-utils-4.0.0

View differences:

modules/dnet-oai-utils/tags/dnet-oai-utils-4.0.0/deploy.info
1
{"type_source": "SVN", "goal": "package -U source:jar",
2
"url": "http://svn-public.driver.research-infrastructures.eu/driver/dnet45/modules/dnet-oai-utils/trunk/",
3
"deploy_repository": "dnet45-snapshots", "version": "4", "mail": "sandro.labruzzo@isti.cnr.it,michele.artini@isti.cnr.it, claudio.atzori@isti.cnr.it, alessia.bardi@isti.cnr.it",
4
"deploy_repository_url": "http://maven.research-infrastructures.eu/nexus/content/repositories/dnet45-snapshots", "name": "dnet-oai-utils"}
modules/dnet-oai-utils/tags/dnet-oai-utils-4.0.0/src/test/java/eu/dnetlib/data/information/oai/publisher/conf/OAIConfigurationParserTest.java
1
package eu.dnetlib.data.information.oai.publisher.conf;
2

  
3
import static org.junit.Assert.assertEquals;
4
import static org.junit.Assert.assertNotNull;
5
import static org.junit.Assert.assertTrue;
6

  
7
import java.io.IOException;
8
import java.nio.charset.Charset;
9
import java.util.List;
10
import java.util.Map;
11

  
12
import org.apache.commons.io.IOUtils;
13
import org.apache.commons.logging.Log;
14
import org.apache.commons.logging.LogFactory;
15
import org.junit.Before;
16
import org.junit.Test;
17
import org.springframework.core.io.ClassPathResource;
18
import org.springframework.core.io.Resource;
19

  
20
import com.google.common.collect.ArrayListMultimap;
21
import com.google.common.collect.Lists;
22
import com.google.common.collect.Maps;
23
import com.google.common.collect.Multimap;
24

  
25
import eu.dnetlib.data.information.oai.publisher.PublisherField;
26
import eu.dnetlib.data.information.oai.publisher.info.MDFInfo;
27
import eu.dnetlib.data.information.oai.publisher.info.SetInfo;
28

  
29
public class OAIConfigurationParserTest {
30

  
31
	private static final Log log = LogFactory.getLog(OAIConfigurationParserTest.class); // NOPMD by marko on 11/24/08 5:02 PM
32

  
33
	/** Resource under test **/
34
	private OAIConfigurationParser parser;
35
	/** Configuration profile to parse **/
36
	private String configurationProfile;
37
	/** File path where to find the XML configuration profile **/
38
	private String pathToProfile = "eu/dnetlib/data/information/oai/publisher/conf/OAIPublisherConfiguration-1.xml";
39

  
40
	/** Expected configuration based on the profile available at the given pathToProfile. **/
41
	private OAIConfiguration expectedConfiguration;
42

  
43
	@Before
44
	public void setUp() throws Exception {
45

  
46
		// preparing the expected configuration that we will use to perform asserts on the result of parsing
47
		expectedConfiguration = new OAIConfiguration();
48
		Map<String, SetInfo> setsMap = Maps.newHashMap();
49
		setsMap.put("set1", new SetInfo("set1", "set number 1", "test set", "(set =\"aSet\")", true));
50
		expectedConfiguration.setSetsMap(setsMap);
51
		expectedConfiguration.setFieldNames(Lists.newArrayList("title", "objIdentifier", "set"));
52
		Map<String, MDFInfo> formatsMap = Maps.newHashMap();
53
		formatsMap.put("oai_dc", new MDFInfo("oai_dc", "SCHEMA", "NAMESPACE", "oai_dc", "store", "driver", "", true));
54
		formatsMap.put("hope", new MDFInfo("hope", "SCHEMA", "NAMESPACE", "hope", "L", "I", "country = IT", "tdsRule", true));
55
		expectedConfiguration.setMdFormatsMap(formatsMap);
56
		expectedConfiguration.setIdNamespace("eagle");
57
		List<PublisherField> fields = Lists.newArrayList();
58
		String oaidcKey = "oai_dc-store-driver";
59
		String hopeKey = "hope-L-I";
60

  
61
		PublisherField titleField = new PublisherField();
62
		titleField.setFieldName("title");
63
		titleField.setRepeatable(true);
64
		Multimap<String, String> titleSources = ArrayListMultimap.create();
65
		titleSources.put(oaidcKey, "//*[local-name() = 'title']");
66
		titleSources.put(oaidcKey, "//*[local-name() ='alternative']");
67
		titleSources.put(hopeKey, "//title");
68
		titleSources.put(hopeKey, "//titleinfo");
69
		titleField.setSources(titleSources);
70

  
71
		fields.add(titleField);
72

  
73
		PublisherField objIdField = new PublisherField();
74
		objIdField.setFieldName("objIdentifier");
75
		objIdField.setRepeatable(false);
76
		Multimap<String, String> objIdSources = ArrayListMultimap.create();
77
		objIdSources.put(oaidcKey, "//*[local-name() ='objIdentifier']");
78
		objIdField.setSources(objIdSources);
79

  
80
		fields.add(objIdField);
81

  
82
		PublisherField setField = new PublisherField();
83
		setField.setFieldName("set");
84
		setField.setRepeatable(true);
85
		Multimap<String, String> setSources = ArrayListMultimap.create();
86
		setSources.put(oaidcKey, "//*[local-name() ='repositoryId']");
87
		setField.setSources(setSources);
88

  
89
		fields.add(setField);
90

  
91
		expectedConfiguration.setFields(fields);
92
		Resource resource = new ClassPathResource(pathToProfile);
93
		// setting up the parser and the profile as a string
94
		parser = new OAIConfigurationParser();
95
		configurationProfile = IOUtils.toString(resource.getInputStream(), Charset.forName("UTF-8"));
96
		log.info("setUp completed");
97
	}
98

  
99
	@Test
100
	public void testSchemeIdNamespace() throws IOException {
101
		OAIConfiguration result = parser.getConfiguration(configurationProfile);
102
		System.out.println(result.getIdScheme());
103
		System.out.println(result.getIdNamespace());
104
	}
105

  
106
	@Test
107
	public void testGetConfiguration() throws IOException {
108
		OAIConfiguration result = parser.getConfiguration(configurationProfile);
109
		assertNotNull(result);
110
		// asserts on sets
111
		assertTrue(result.getSetsMap().containsKey("set1"));
112
		SetInfo expected = expectedConfiguration.getSetsMap().get("set1");
113
		SetInfo parsedSet = result.getSetsMap().get("set1");
114
		assertEquals(expected.getQuery(), parsedSet.getQuery());
115
		assertEquals(expected.getSetDescription(), parsedSet.getSetDescription());
116
		assertEquals(expected.getSetName(), parsedSet.getSetName());
117
		assertEquals(expected.getSetSpec(), parsedSet.getSetSpec());
118
		// asserts on indices
119
		assertTrue(result.getFieldNames().containsAll(expectedConfiguration.getFieldNames()));
120
		assertEquals(3, result.getFieldsFor("oai_dc", "store", "driver").size());
121
		assertEquals(1, result.getFieldsFor("hope", "L", "I").size());
122

  
123
		// asserts on metadata formats
124
		assertTrue(result.getMdFormatsMap().containsKey("hope"));
125
		assertTrue(result.getMdFormatsMap().containsKey("oai_dc"));
126
		MDFInfo expectedFormat = expectedConfiguration.getMdFormatsMap().get("hope");
127
		MDFInfo parsedFormat = result.getMdFormatsMap().get("hope");
128
		assertEquals(expectedFormat.getBaseQuery(), parsedFormat.getBaseQuery());
129
		assertEquals(expectedFormat.getNamespace(), parsedFormat.getNamespace());
130
		assertEquals(expectedFormat.getPrefix(), parsedFormat.getPrefix());
131
		assertEquals(expectedFormat.getSchema(), parsedFormat.getSchema());
132

  
133
		assertEquals(expectedFormat.getSourceFormatInterpretation(), parsedFormat.getSourceFormatInterpretation());
134
		assertEquals(expectedFormat.getSourceFormatLayout(), parsedFormat.getSourceFormatLayout());
135
		assertEquals(expectedFormat.getSourceFormatName(), parsedFormat.getSourceFormatName());
136
		assertEquals(expectedFormat.getTransformationRuleID(), parsedFormat.getTransformationRuleID());
137
		assertEquals(expectedConfiguration.getIdNamespace(), result.getIdNamespace());
138
		assertEquals("oai", result.getIdScheme());
139

  
140
	}
141

  
142
	@Test
143
	public void splitRSToken() {
144
		String token = "1434158|dli||100|5551ed26e4b0f1f9ff635bab|false|";
145
		String[] splitted = token.split("\\|");
146
		System.out.println(splitted.length);
147
		for (String s : splitted) {
148
			System.out.println(s);
149
		}
150
	}
151
}
modules/dnet-oai-utils/tags/dnet-oai-utils-4.0.0/src/test/resources/eu/dnetlib/data/information/oai/publisher/conf/OAIPublisherConfiguration-1.xml
1
<RESOURCE_PROFILE>
2
	<HEADER>
3
		<RESOURCE_IDENTIFIER value="OAIPublisherConfiguration" />
4
		<RESOURCE_TYPE value="OAIPublisherConfigurationDSResourceType" />
5
		<RESOURCE_KIND value="OAIPublisherConfigurationDSResources" />
6
		<RESOURCE_URI value="" />
7
		<DATE_OF_CREATION value="2001-12-31T12:00:00" />
8
	</HEADER>
9
	<BODY>
10
		<CONFIGURATION>
11
			<IDSCHEME>oai</IDSCHEME>
12
			<IDNAMESPACE>eagle</IDNAMESPACE>
13
			<CURRENTDB>oaistore</CURRENTDB>
14
			<OAISETS>
15
				<OAISET enabled="true">
16
					<spec>set1</spec>
17
					<name>set number 1</name>
18
					<description>test set</description>
19
					<query>(set ="aSet")</query>
20
				</OAISET>
21
			</OAISETS>
22
			<METADATAFORMATS>
23
				<METADATAFORMAT exportable="true" metadataPrefix="oai_dc">
24
					<NAMESPACE>NAMESPACE</NAMESPACE>
25
					<SCHEMA>SCHEMA</SCHEMA>
26
					<SOURCE_METADATA_FORMAT interpretation="driver"
27
						layout="store" name="oai_dc" />
28
					<TRANSFORMATION_RULE />
29
					<BASE_QUERY />
30
				</METADATAFORMAT>
31
				<METADATAFORMAT exportable="true" metadataPrefix="hope">
32
					<NAMESPACE>NAMESPACE</NAMESPACE>
33
					<SCHEMA>SCHEMA</SCHEMA>
34
					<SOURCE_METADATA_FORMAT interpretation="I"
35
						layout="L" name="hope" />
36
					<TRANSFORMATION_RULE>tdsRule</TRANSFORMATION_RULE>
37
					<BASE_QUERY>country = IT</BASE_QUERY>
38
				</METADATAFORMAT>
39
			</METADATAFORMATS>
40
			<INDICES>
41
				<INDEX name="title">
42
					<SOURCE interpretation="I" layout="L" name="hope" path="//title" />
43
					<SOURCE interpretation="I" layout="L" name="hope" path="//titleinfo" />
44
					<SOURCE interpretation="driver" layout="store" name="oai_dc"
45
						path="//*[local-name() = 'title']" />
46
					<SOURCE interpretation="driver" layout="store" name="oai_dc"
47
						path="//*[local-name() ='alternative']" />
48
				</INDEX>
49
				<INDEX name="objIdentifier">
50
					<SOURCE interpretation="driver" layout="store" name="oai_dc"
51
						path="//*[local-name() ='objIdentifier']" />
52
				</INDEX>
53
				<INDEX name="set">
54
					<SOURCE interpretation="driver" layout="store" name="oai_dc"
55
						path="//*[local-name() ='repositoryId']" />
56
				</INDEX>
57
			</INDICES>
58
		</CONFIGURATION>
59
		<STATUS>
60
			<LAST_UPDATE value="2013-12-20T10:50:00" />
61
		</STATUS>
62
		<SECURITY_PARAMETERS>SECURITY_PARAMETERS</SECURITY_PARAMETERS>
63
	</BODY>
64
</RESOURCE_PROFILE>
modules/dnet-oai-utils/tags/dnet-oai-utils-4.0.0/src/main/java/eu/dnetlib/data/information/oai/utils/OAIParameterNames.java
1
package eu.dnetlib.data.information.oai.utils;
2

  
3
/**
4
 * Static class keeping track of env variable and job parameter names used by default by OAI Job nodes.
5
 * Created by alessia on 09/11/16.
6
 */
7
public class OAIParameterNames {
8

  
9
	public static String OAI_COLLECTON = "oai_collection";
10
	public static String OAI_DB = "oai_dbName";
11
	public static String OAI_FORMAT_NAME = "oai_format";
12
	public static String OAI_FORMAT_LAYOUT = "oai_layout";
13
	public static String OAI_FORMAT_INTERPRETATION = "oai_interpretation";
14
	public static String OAI_SOURCE = "oai_source";
15
	public static String OAI_INDEXES = "oai_indexFieldNames";
16
	public static String OAI_CONFIGURED_SETS_ONLY = "oai_configuredSetsOnly";
17
	public static String OAI_CONFIGURATION_PROFILE = "oai_configuration";
18
	public static String OAI_FEED_DATE = "oai_feedDate";
19
	public static String OAI_SKIP_DUPLICATES = "oai_skipDuplicates";
20
	public static String OAI_DUPLICATE_XPATH = "oai_duplicateXPath";
21
	public static String OAI_SYNC_EPR = "oai_syncEpr";
22
	public static String OAI_ALWAYS_NEW_RECORD= "oai_alwaysNewRecord";
23

  
24

  
25

  
26

  
27
}
modules/dnet-oai-utils/tags/dnet-oai-utils-4.0.0/src/main/java/eu/dnetlib/data/information/oai/publisher/CannotDisseminateFormatException.java
1
package eu.dnetlib.data.information.oai.publisher;
2

  
3
public class CannotDisseminateFormatException extends OaiPublisherException {
4

  
5
	private static final long serialVersionUID = -1395014964372657558L;
6

  
7
	public CannotDisseminateFormatException(final String message) {
8
		super(message);
9
	}
10

  
11
}
modules/dnet-oai-utils/tags/dnet-oai-utils-4.0.0/src/main/java/eu/dnetlib/data/information/oai/publisher/PublisherField.java
1
package eu.dnetlib.data.information.oai.publisher;
2

  
3
import com.google.common.collect.Multimap;
4

  
5
/**
6
 * Instances of this class represent fields to be explicitely managed by the OAI Publisher, for example for indexing purposes.
7
 * 
8
 * @author alessia
9
 * 
10
 */
11
public class PublisherField {
12

  
13
	/**
14
	 * Name of the field.
15
	 */
16
	private String fieldName;
17
	/**
18
	 * True if the value of the field is a list (i.e. repeatable values). False otherwise.
19
	 **/
20
	private boolean repeatable;
21
	/**
22
	 * Information about where to take values for this field.
23
	 * <p>
24
	 * Keys are metadat format: name-layout-interpretation; values are xpaths.
25
	 * </p>
26
	 */
27
	private Multimap<String, String> sources;
28

  
29
	public String getFieldName() {
30
		return fieldName;
31
	}
32

  
33
	public void setFieldName(final String fieldName) {
34
		this.fieldName = fieldName;
35
	}
36

  
37
	public boolean isRepeatable() {
38
		return repeatable;
39
	}
40

  
41
	public void setRepeatable(final boolean repeatable) {
42
		this.repeatable = repeatable;
43
	}
44

  
45
	public Multimap<String, String> getSources() {
46
		return sources;
47
	}
48

  
49
	public void setSources(final Multimap<String, String> sources) {
50
		this.sources = sources;
51
	}
52

  
53
	public PublisherField() {
54
		super();
55
		// TODO Auto-generated constructor stub
56
	}
57

  
58
	public PublisherField(final String fieldName, final boolean repeatable, final Multimap<String, String> sources) {
59
		super();
60
		this.fieldName = fieldName;
61
		this.repeatable = repeatable;
62
		this.sources = sources;
63
	}
64

  
65
}
modules/dnet-oai-utils/tags/dnet-oai-utils-4.0.0/src/main/java/eu/dnetlib/data/information/oai/publisher/OaiPublisherException.java
1
package eu.dnetlib.data.information.oai.publisher;
2

  
3
import javax.xml.ws.WebFault;
4

  
5
import eu.dnetlib.common.rmi.RMIException;
6

  
7
/**
8
 * OAI publisher specific exception.
9
 * 
10
 * @author michele
11
 * 
12
 */
13
@WebFault
14
public class OaiPublisherException extends RMIException {
15

  
16
	/**
17
	 * serialization.
18
	 */
19
	private static final long serialVersionUID = 6833698764790681184L;
20

  
21
	/**
22
	 * Creates an exception.
23
	 * 
24
	 * @param e
25
	 *            exception
26
	 */
27
	public OaiPublisherException(final Throwable e) {
28
		super(e);
29
	}
30

  
31
	/**
32
	 * Creates an exception.
33
	 * 
34
	 * @param message
35
	 *            message
36
	 * @param e
37
	 *            exception
38
	 */
39
	public OaiPublisherException(final String message, final Throwable e) {
40
		super(message, e);
41
	}
42

  
43
	/**
44
	 * Creates an exception.
45
	 * 
46
	 * @param message
47
	 *            message
48
	 */
49
	public OaiPublisherException(final String message) {
50
		super(message);
51
	}
52

  
53
}
modules/dnet-oai-utils/tags/dnet-oai-utils-4.0.0/src/main/java/eu/dnetlib/data/information/oai/publisher/conf/OAIConfiguration.java
1
package eu.dnetlib.data.information.oai.publisher.conf;
2

  
3
import java.util.Collection;
4
import java.util.List;
5
import java.util.Map;
6
import java.util.Set;
7

  
8
import com.google.common.base.Joiner;
9
import com.google.common.base.Predicate;
10
import com.google.common.collect.Collections2;
11
import com.google.common.collect.Sets;
12

  
13
import eu.dnetlib.data.information.oai.publisher.PublisherField;
14
import eu.dnetlib.data.information.oai.publisher.info.MDFInfo;
15
import eu.dnetlib.data.information.oai.publisher.info.SetInfo;
16

  
17
/**
18
 * An instance of this class represents an OAI Configuration profile.
19
 * 
20
 * @author alessia
21
 * 
22
 */
23
public class OAIConfiguration {
24

  
25
	/**
26
	 * Map for OAI sets. Keys: setSpec, values: SetInfo instances.
27
	 */
28
	private Map<String, SetInfo> setsMap;
29

  
30
	/**
31
	 * Map for metadata formats. Keys: metadata prefix, values: MDFInfo instances.
32
	 */
33
	private Map<String, MDFInfo> mdFormatsMap;
34

  
35
	private List<PublisherField> fields;
36

  
37
	private List<String> fieldNames;
38

  
39
	/**
40
	 * Used to generate the OAI identifiers compliant to the protocol. See
41
	 * http://www.openarchives.org/OAI/openarchivesprotocol.html#UniqueIdentifier. See property services.oai.publisher.id.scheme.
42
	 */
43
	private String idScheme;
44
	/**
45
	 * Used to generate the OAI identifiers compliant to the protocol. See
46
	 * http://www.openarchives.org/OAI/openarchivesprotocol.html#UniqueIdentifier. See property services.oai.publisher.id.namespace.
47
	 */
48
	private String idNamespace;
49

  
50
	private Set<MDFInfo> sourcesMDF = Sets.newHashSet();
51

  
52
	public Collection<PublisherField> getFieldsFor(final String format, final String layout, final String interpretation) {
53
		final String mdRef = Joiner.on("-").join(format, layout, interpretation);
54
		return Collections2.filter(this.getFields(), new Predicate<PublisherField>() {
55

  
56
			@Override
57
			public boolean apply(final PublisherField theField) {
58
				if (theField.getSources() == null) return false;
59
				return theField.getSources().containsKey(mdRef);
60
			}
61

  
62
		});
63
	}
64

  
65
	public Map<String, SetInfo> getSetsMap() {
66
		return setsMap;
67
	}
68

  
69
	public void setSetsMap(final Map<String, SetInfo> setsMap) {
70
		this.setsMap = setsMap;
71
	}
72

  
73
	public Map<String, MDFInfo> getMdFormatsMap() {
74
		return mdFormatsMap;
75
	}
76

  
77
	public void setMdFormatsMap(final Map<String, MDFInfo> mdFormatsMap) {
78
		this.mdFormatsMap = mdFormatsMap;
79
	}
80

  
81
	public List<PublisherField> getFields() {
82
		return fields;
83
	}
84

  
85
	public void setFields(final List<PublisherField> fields) {
86
		this.fields = fields;
87
	}
88

  
89
	public List<String> getFieldNames() {
90
		return fieldNames;
91
	}
92

  
93
	public void setFieldNames(final List<String> fieldNames) {
94
		this.fieldNames = fieldNames;
95
	}
96

  
97
	public Set<MDFInfo> getSourcesMDF() {
98
		return sourcesMDF;
99
	}
100

  
101
	public void setSourcesMDF(final Set<MDFInfo> sourcesMDF) {
102
		this.sourcesMDF = sourcesMDF;
103
	}
104

  
105
	public String getIdScheme() {
106
		return idScheme;
107
	}
108

  
109
	public void setIdScheme(final String idScheme) {
110
		this.idScheme = idScheme;
111
	}
112

  
113
	public String getIdNamespace() {
114
		return idNamespace;
115
	}
116

  
117
	public void setIdNamespace(final String idNamespace) {
118
		this.idNamespace = idNamespace;
119
	}
120

  
121
}
modules/dnet-oai-utils/tags/dnet-oai-utils-4.0.0/src/main/java/eu/dnetlib/data/information/oai/publisher/conf/OAIConfigurationExistReader.java
1
package eu.dnetlib.data.information.oai.publisher.conf;
2

  
3
import java.util.Iterator;
4
import java.util.List;
5
import java.util.Set;
6

  
7
import org.springframework.beans.factory.annotation.Autowired;
8

  
9
import com.google.common.collect.ArrayListMultimap;
10
import com.google.common.collect.Lists;
11
import com.google.common.collect.Multimap;
12
import com.google.common.collect.Sets;
13

  
14
import eu.dnetlib.common.rmi.UnimplementedException;
15
import eu.dnetlib.data.information.oai.publisher.OaiPublisherRuntimeException;
16
import eu.dnetlib.data.information.oai.publisher.PublisherField;
17
import eu.dnetlib.data.information.oai.publisher.info.MDFInfo;
18
import eu.dnetlib.data.information.oai.publisher.info.SetInfo;
19
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
20
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
21
import eu.dnetlib.enabling.tools.SplittedQueryExecutor;
22

  
23
/**
24
 * Instances of this class read the configuration of the OAI publisher from the IS, by performing xquery on the Exist database.
25
 * 
26
 * @author alessia
27
 * 
28
 */
29
public class OAIConfigurationExistReader implements OAIConfigurationReader {
30

  
31
	@Autowired
32
	private SplittedQueryExecutor queryExecutor;
33
	private String xpathToIdScheme = "//RESOURCE_PROFILE[.//RESOURCE_TYPE/@value = 'OAIPublisherConfigurationDSResourceType']//CONFIGURATION/IDSCHEME/text()";
34
	private String xpathToIdNamespace = "//RESOURCE_PROFILE[.//RESOURCE_TYPE/@value = 'OAIPublisherConfigurationDSResourceType']//CONFIGURATION/IDNAMESPACE/text()";
35

  
36
	@Override
37
	public List<SetInfo> getSets() {
38
		final String query = "for $x in collection('/db/DRIVER/OAIPublisherConfigurationDSResources/OAIPublisherConfigurationDSResourceType')//CONFIGURATION/OAISETS/OAISET "
39
				+ "return concat($x/spec, ':-:', $x/name, ':-:', $x//description , ':-:', $x//query, ':-:', $x/@enabled/string())";
40
		Iterable<SetInfo> sets = this.queryExecutor.query(SetInfo.class, query, ":-:");
41
		return Lists.newArrayList(sets);
42
	}
43

  
44
	@Override
45
	public List<SetInfo> getSets(final boolean onlyEnabled) {
46
		if (onlyEnabled) {
47
			final String query = "for $x in collection('/db/DRIVER/OAIPublisherConfigurationDSResources/OAIPublisherConfigurationDSResourceType')//CONFIGURATION/OAISETS/OAISET "
48
					+ "where $x/@enabled/string() = 'true' "
49
					+ "return concat($x/spec, ':-:', $x/name, ':-:', $x//description , ':-:', $x//query, ':-:', $x/@enabled/string())";
50
			Iterable<SetInfo> sets = this.queryExecutor.query(SetInfo.class, query, ":-:");
51
			return Lists.newArrayList(sets);
52
		} else return this.getSets();
53
	}
54

  
55
	@Override
56
	public List<String> getSetSpecs() {
57
		final String query = "for $x in collection('/db/DRIVER/OAIPublisherConfigurationDSResources/OAIPublisherConfigurationDSResourceType')//CONFIGURATION/OAISETS/OAISET "
58
				+ "return $x/spec/string() ";
59
		List<String> names = this.queryExecutor.performQuery(query);
60
		return names;
61
	}
62

  
63
	@Override
64
	public List<String> getSetSpecs(final boolean onlyEnabled) {
65
		final String query = "for $x in collection('/db/DRIVER/OAIPublisherConfigurationDSResources/OAIPublisherConfigurationDSResourceType')//CONFIGURATION/OAISETS/OAISET "
66
				+ "where $x/@enabled/string() = '" + onlyEnabled + "' " + "return $x/spec/string() ";
67
		List<String> names = this.queryExecutor.performQuery(query);
68
		return names;
69
	}
70

  
71
	@Override
72
	public SetInfo getSetInfo(final String setSpec) {
73
		final String query = "for $x in collection('/db/DRIVER/OAIPublisherConfigurationDSResources/OAIPublisherConfigurationDSResourceType')//CONFIGURATION/OAISETS/OAISET "
74
				+ "where $x/spec = '"
75
				+ setSpec
76
				+ "' "
77
				+ "return concat($x/spec, ':-:', $x/name, ':-:', $x//description , ':-:', $x//query, ':-:', $x/@enabled/string())";
78
		Iterable<SetInfo> sets = this.queryExecutor.query(SetInfo.class, query, ":-:");
79
		if (sets.iterator().hasNext()) return sets.iterator().next();
80
		else return null;
81
	}
82

  
83
	@Override
84
	public List<MDFInfo> getSourceMetadataFormats() {
85
		final String query = "for $x in collection('/db/DRIVER/OAIPublisherConfigurationDSResources/OAIPublisherConfigurationDSResourceType')//CONFIGURATION//METADATAFORMAT "
86
				+ "return concat($x//SOURCE_METADATA_FORMAT/@name/string(), ':-:',  $x//SOURCE_METADATA_FORMAT/@layout/string(), ':-:', $x//SOURCE_METADATA_FORMAT/@interpretation/string())";
87

  
88
		List<String> res;
89
		try {
90
			res = this.queryExecutor.getServiceLocator().getService(ISLookUpService.class).quickSearchProfile(query);
91
		} catch (ISLookUpException e) {
92
			throw new OaiPublisherRuntimeException(e);
93
		}
94
		Set<MDFInfo> sources = Sets.newHashSet();
95
		for (String src : res) {
96
			String[] splitted = src.split(":-:");
97
			MDFInfo mdfInfo = new MDFInfo();
98
			mdfInfo.setSourceFormatName(splitted[0]);
99
			mdfInfo.setSourceFormatLayout(splitted[1]);
100
			mdfInfo.setSourceFormatInterpretation(splitted[2]);
101
			sources.add(mdfInfo);
102
		}
103
		return Lists.newArrayList(sources);
104
	}
105

  
106
	@Override
107
	public List<MDFInfo> getMetadataFormatInfo() {
108
		final String query = "for $x in collection('/db/DRIVER/OAIPublisherConfigurationDSResources/OAIPublisherConfigurationDSResourceType')//CONFIGURATION//METADATAFORMAT "
109
				+ "return concat($x/@metadataPrefix/string(), ':-:', $x//SCHEMA , ':-:', $x//NAMESPACE,  "
110
				+ "':-:', $x//SOURCE_METADATA_FORMAT/@name/string(), ':-:',  $x//SOURCE_METADATA_FORMAT/@layout/string(), ':-:', $x//SOURCE_METADATA_FORMAT/@interpretation/string(), "
111
				+ "':-:', $x//BASE_QUERY, ':-:',  $x//TRANSFORMATION_RULE, ':-:', $x/@exportable/string() )";
112
		Iterable<MDFInfo> mdFormats = this.queryExecutor.query(MDFInfo.class, query, ":-:");
113
		return Lists.newArrayList(mdFormats);
114
	}
115

  
116
	@Override
117
	public List<MDFInfo> getMetadataFormatInfo(final boolean onlyEnabled) {
118
		if (onlyEnabled) {
119
			final String query = "for $x in collection('/db/DRIVER/OAIPublisherConfigurationDSResources/OAIPublisherConfigurationDSResourceType')//CONFIGURATION//METADATAFORMAT "
120
					+ "where $x/@exportable/string() = 'true' "
121
					+ "return concat($x/@metadataPrefix/string(), ':-:', $x//SCHEMA , ':-:', $x//NAMESPACE,  "
122
					+ "':-:', $x//SOURCE_METADATA_FORMAT/@name/string(), ':-:',  $x//SOURCE_METADATA_FORMAT/@layout/string(), ':-:', $x//SOURCE_METADATA_FORMAT/@interpretation/string(), "
123
					+ "':-:', $x//BASE_QUERY, ':-:',  $x//TRANSFORMATION_RULE, ':-:', $x/@exportable/string() )";
124
			Iterable<MDFInfo> mdFormats = this.queryExecutor.query(MDFInfo.class, query, ":-:");
125
			return Lists.newArrayList(mdFormats);
126
		} else return getMetadataFormatInfo();
127
	}
128

  
129
	@Override
130
	public MDFInfo getMetadataFormatInfo(final String mdPrefix) {
131
		final String query = "for $x in collection('/db/DRIVER/OAIPublisherConfigurationDSResources/OAIPublisherConfigurationDSResourceType')//CONFIGURATION//METADATAFORMAT "
132
				+ "where $x/@metadataPrefix/string()='"
133
				+ mdPrefix
134
				+ "' "
135
				+ "return concat($x/@metadataPrefix/string(), ':-:', $x//SCHEMA , ':-:', $x//NAMESPACE,  "
136
				+ "':-:', $x//SOURCE_METADATA_FORMAT/@name/string(), ':-:',  $x//SOURCE_METADATA_FORMAT/@layout/string(), ':-:', $x//SOURCE_METADATA_FORMAT/@interpretation/string(), "
137
				+ "':-:', $x//BASE_QUERY,':-:', $x//TRANSFORMATION_RULE, ':-:', $x/@exportable/string() )";
138
		Iterable<MDFInfo> mdFormats = this.queryExecutor.query(MDFInfo.class, query, ":-:");
139
		Iterator<MDFInfo> iterator = mdFormats.iterator();
140
		if (iterator.hasNext()) return iterator.next();
141
		else return null;
142
	}
143

  
144
	@Override
145
	public List<PublisherField> getFields() {
146
		throw new UnimplementedException();
147
	}
148

  
149
	@Override
150
	public List<PublisherField> getFields(final String format, final String interpretation, final String layout) {
151
		final String query = "for $x in collection('/db/DRIVER/OAIPublisherConfigurationDSResources/OAIPublisherConfigurationDSResourceType')//CONFIGURATION//INDICES/INDEX "
152
				+ "return concat($x/@name, ':-:', $x/@repeatable, ':-:', string-join($x/SOURCE[@name='"
153
				+ format
154
				+ "' and @interpretation='"
155
				+ interpretation
156
				+ "' and @layout='" + layout + "']/@path, ':-:'))";
157
		List<String> res;
158
		try {
159
			res = this.queryExecutor.getServiceLocator().getService(ISLookUpService.class).quickSearchProfile(query);
160
		} catch (ISLookUpException e) {
161
			throw new OaiPublisherRuntimeException(e);
162
		}
163
		List<PublisherField> fields = Lists.newArrayList();
164
		for (String index : res) {
165
			String[] splitted = index.split(":-:");
166
			String indexName = splitted[0];
167
			String repeatable = splitted[1];
168
			PublisherField field = new PublisherField();
169
			field.setFieldName(indexName);
170
			field.setRepeatable(Boolean.valueOf(repeatable));
171
			Multimap<String, String> sources = ArrayListMultimap.create();
172
			String mdFormat = format + "-" + layout + "-" + interpretation;
173
			for (int i = 2; i < splitted.length; i++) {
174
				sources.put(mdFormat, splitted[i]);
175
			}
176
			field.setSources(sources);
177
			fields.add(field);
178
		}
179
		return fields;
180
	}
181

  
182
	@Override
183
	public List<String> getFieldNames() {
184
		String query = "//RESOURCE_PROFILE[.//RESOURCE_TYPE/@value = 'OAIPublisherConfigurationDSResourceType']//CONFIGURATION//INDICES/INDEX/@name/string()";
185
		try {
186
			return this.queryExecutor.getServiceLocator().getService(ISLookUpService.class).quickSearchProfile(query);
187
		} catch (ISLookUpException e) {
188
			throw new OaiPublisherRuntimeException(e);
189
		}
190
	}
191

  
192
	@Override
193
	public List<MDFInfo> getFormatsServedBy(final String sourceFormatName, final String sourceFormatLayout, final String sourceFormatInterpretation) {
194
		final String query = "for $x in collection('/db/DRIVER/OAIPublisherConfigurationDSResources/OAIPublisherConfigurationDSResourceType')//CONFIGURATION//METADATAFORMAT[.//SOURCE_METADATA_FORMAT/@name = '"
195
				+ sourceFormatName
196
				+ "' and .//SOURCE_METADATA_FORMAT/@layout = '"
197
				+ sourceFormatLayout
198
				+ "' and .//SOURCE_METADATA_FORMAT/@interpretation = '"
199
				+ sourceFormatInterpretation
200
				+ "'] "
201
				+ "return concat($x/@metadataPrefix/string(), ':-:', $x//SCHEMA , ':-:', $x//NAMESPACE,  "
202
				+ "':-:', $x//SOURCE_METADATA_FORMAT/@name/string(), ':-:',  $x//SOURCE_METADATA_FORMAT/@layout/string(), ':-:', $x//SOURCE_METADATA_FORMAT/@interpretation/string(), "
203
				+ "':-:', $x//BASE_QUERY, ':-:',  $x//TRANSFORMATION_RULE, ':-:', $x/@exportable/string() )";
204
		Iterable<MDFInfo> mdFormats = this.queryExecutor.query(MDFInfo.class, query, ":-:");
205
		return Lists.newArrayList(mdFormats);
206
	}
207

  
208
	@Override
209
	public String getIdScheme() {
210
		try {
211
			return this.queryExecutor.getServiceLocator().getService(ISLookUpService.class).getResourceProfileByQuery(getXpathToIdScheme());
212
		} catch (ISLookUpException e) {
213
			throw new OaiPublisherRuntimeException(e);
214
		}
215
	}
216

  
217
	@Override
218
	public String getIdNamespace() {
219
		try {
220
			return this.queryExecutor.getServiceLocator().getService(ISLookUpService.class).getResourceProfileByQuery(getXpathToIdNamespace());
221
		} catch (ISLookUpException e) {
222
			throw new OaiPublisherRuntimeException(e);
223
		}
224
	}
225

  
226
	public SplittedQueryExecutor getQueryExecutor() {
227
		return queryExecutor;
228
	}
229

  
230
	public void setQueryExecutor(final SplittedQueryExecutor queryExecutor) {
231
		this.queryExecutor = queryExecutor;
232
	}
233

  
234
	public String getXpathToIdScheme() {
235
		return xpathToIdScheme;
236
	}
237

  
238
	public void setXpathToIdScheme(final String xpathToIdScheme) {
239
		this.xpathToIdScheme = xpathToIdScheme;
240
	}
241

  
242
	public String getXpathToIdNamespace() {
243
		return xpathToIdNamespace;
244
	}
245

  
246
	public void setXpathToIdNamespace(final String xpathToIdNamespace) {
247
		this.xpathToIdNamespace = xpathToIdNamespace;
248
	}
249

  
250
}
modules/dnet-oai-utils/tags/dnet-oai-utils-4.0.0/src/main/java/eu/dnetlib/data/information/oai/publisher/conf/OAIConfigurationReader.java
1
package eu.dnetlib.data.information.oai.publisher.conf;
2

  
3
import java.util.List;
4

  
5
import eu.dnetlib.data.information.oai.publisher.PublisherField;
6
import eu.dnetlib.data.information.oai.publisher.info.MDFInfo;
7
import eu.dnetlib.data.information.oai.publisher.info.SetInfo;
8

  
9
public interface OAIConfigurationReader {
10

  
11
	/**
12
	 * Constant names of fields used by the publisher to store records in the OAIStores. If you want an index over them, you need to specify
13
	 * it in the OAI Publisher Configuration profile.
14
	 */
15
	public final static String ID_FIELD = "objIdentifier";
16
	public final static String DATESTAMP_FIELD = "datestamp";
17
	public final static String SET_FIELD = "set";
18
	public final static String DELETED_FIELD = "deleted";
19
	public final static String BODY_FIELD = "body";
20
	public final static String UPDATED_FIELD = "updated";
21
	public final static String LAST_COLLECTION_DATE_FIELD = "lastCollectionDate";
22

  
23
	public List<SetInfo> getSets();
24

  
25
	public List<SetInfo> getSets(final boolean onlyEnabled);
26

  
27
	public List<String> getSetSpecs();
28

  
29
	public List<String> getSetSpecs(final boolean onlyEnabled);
30

  
31
	public SetInfo getSetInfo(final String setSpec);
32

  
33
	public List<MDFInfo> getMetadataFormatInfo();
34

  
35
	public List<MDFInfo> getMetadataFormatInfo(final boolean onlyEnabled);
36

  
37
	public MDFInfo getMetadataFormatInfo(final String mdPrefix);
38

  
39
	public List<String> getFieldNames();
40

  
41
	public List<PublisherField> getFields();
42

  
43
	public List<PublisherField> getFields(final String format, final String interpretation, final String layout);
44

  
45
	/**
46
	 * Used to generate the OAI identifiers compliant to the protocol. See
47
	 * http://www.openarchives.org/OAI/openarchivesprotocol.html#UniqueIdentifier.
48
	 */
49
	public String getIdScheme();
50

  
51
	/**
52
	 * Used to generate the OAI identifiers compliant to the protocol. See
53
	 * http://www.openarchives.org/OAI/openarchivesprotocol.html#UniqueIdentifier.
54
	 */
55
	public String getIdNamespace();
56

  
57
	public List<MDFInfo> getSourceMetadataFormats();
58

  
59
	public List<MDFInfo> getFormatsServedBy(String sourceFormatName, String sourceFormatLayout, String sourceFormatInterpretation);
60

  
61
}
modules/dnet-oai-utils/tags/dnet-oai-utils-4.0.0/src/main/java/eu/dnetlib/data/information/oai/publisher/conf/ISLookUpClient.java
1
package eu.dnetlib.data.information.oai.publisher.conf;
2

  
3
import java.util.List;
4
import javax.annotation.Resource;
5

  
6
import com.google.common.collect.Lists;
7
import eu.dnetlib.data.information.oai.publisher.OaiPublisherException;
8
import eu.dnetlib.data.information.oai.publisher.OaiPublisherRuntimeException;
9
import eu.dnetlib.data.information.oai.publisher.info.MDFInfo;
10
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpDocumentNotFoundException;
11
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
12
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
13
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
14
import eu.dnetlib.miscutils.functional.IdentityFunction;
15
import eu.dnetlib.miscutils.functional.UnaryFunction;
16
import eu.dnetlib.miscutils.functional.xml.ApplyXslt;
17
import org.apache.commons.lang.StringUtils;
18
import org.apache.commons.logging.Log;
19
import org.apache.commons.logging.LogFactory;
20
import org.dom4j.DocumentException;
21
import org.springframework.beans.factory.annotation.Value;
22
import org.springframework.cache.annotation.CacheEvict;
23
import org.springframework.cache.annotation.Cacheable;
24

  
25
/**
26
 * Helper class for common queries to the IS.
27
 * 
28
 * @author alessia
29
 * 
30
 */
31
public class ISLookUpClient {
32

  
33
	private static final Log log = LogFactory.getLog(ISLookUpClient.class); // NOPMD by marko on 11/24/08 5:02 PM
34

  
35
	@Value("${services.oai.publisher.db.xquery}")
36
	private String xpathToCurrentDB;
37

  
38
	/**
39
	 * lookup service.
40
	 */
41
	@Resource
42
	private UniqueServiceLocator serviceLocator;
43

  
44
	@Resource
45
	private OAIConfigurationExistReader configuration;
46

  
47
	public List<MDFInfo> listMetadataFormats(final boolean onlyEnabled) throws ISLookUpException, DocumentException, OaiPublisherException {
48
		return Lists.newArrayList(this.configuration.getMetadataFormatInfo(onlyEnabled));
49
	}
50

  
51
	@Cacheable(value = "unaryFunctions", key = "#tdsRuleId")
52
	public UnaryFunction<String, String> getUnaryFunctionFromTDSRule(final String tdsRuleId) {
53
		log.fatal("Not using cache to get TDSRule " + tdsRuleId);
54
		final String queryCodeRule = "//RESOURCE_PROFILE[.//RESOURCE_IDENTIFIER/@value='" + tdsRuleId
55
				+ "']/BODY/CONFIGURATION/SCRIPT/CODE/*[local-name()='stylesheet']";
56
		String xsltCode = null;
57
		try {
58
			xsltCode = this.serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(queryCodeRule);
59
		} catch (ISLookUpDocumentNotFoundException e) {
60
			log.fatal("Unexisting TDSRule profile with identifier " + tdsRuleId);
61
			throw new OaiPublisherRuntimeException(e);
62
		} catch (ISLookUpException e) {
63
			throw new OaiPublisherRuntimeException(e);
64
		}
65
		if (StringUtils.isBlank(xsltCode)) {
66
			log.warn("Unexpected blank stylesheet in TDSRule profile with id: " + tdsRuleId + ". Returning identity function.");
67
			return new IdentityFunction<String>();
68
		} else return new ApplyXslt(xsltCode);
69
	}
70

  
71
	@Cacheable("oaiDB")
72
	public String getCurrentDB() throws ISLookUpDocumentNotFoundException, ISLookUpException {
73
		log.fatal("Not using cache to get the current OAI db");
74
		String currentDBName = this.serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(xpathToCurrentDB);
75
		log.info("Current OAI DB:" + currentDBName);
76
		return currentDBName;
77
	}
78

  
79
	@CacheEvict(value = { "unaryFunctions", "oaiDB" }, allEntries = true, beforeInvocation = true)
80
	public void evictCaches() {
81
		log.info("Evicted caches oaiDB and unaryFunctions");
82
	}
83

  
84
	public OAIConfigurationExistReader getConfiguration() {
85
		return configuration;
86
	}
87

  
88
	public void setConfiguration(final OAIConfigurationExistReader configuration) {
89
		this.configuration = configuration;
90
	}
91

  
92
}
modules/dnet-oai-utils/tags/dnet-oai-utils-4.0.0/src/main/java/eu/dnetlib/data/information/oai/publisher/conf/OAIConfigurationStringReader.java
1
package eu.dnetlib.data.information.oai.publisher.conf;
2

  
3
import java.io.IOException;
4
import java.util.Collection;
5
import java.util.List;
6

  
7
import javax.annotation.Resource;
8

  
9
import org.apache.commons.logging.Log;
10
import org.apache.commons.logging.LogFactory;
11

  
12
import com.google.common.base.Function;
13
import com.google.common.base.Predicate;
14
import com.google.common.collect.Collections2;
15
import com.google.common.collect.Iterables;
16
import com.google.common.collect.Lists;
17

  
18
import eu.dnetlib.data.information.oai.publisher.OaiPublisherRuntimeException;
19
import eu.dnetlib.data.information.oai.publisher.PublisherField;
20
import eu.dnetlib.data.information.oai.publisher.info.MDFInfo;
21
import eu.dnetlib.data.information.oai.publisher.info.SetInfo;
22

  
23
/**
24
 * Instances of this class reads the OAI configuration from a string, which is the configuration profile passed in as a string.
25
 * 
26
 * @author alessia
27
 * 
28
 */
29
public class OAIConfigurationStringReader implements OAIConfigurationReader {
30

  
31
	private static final Log log = LogFactory.getLog(OAIConfigurationStringReader.class); // NOPMD by marko on 11/24/08 5:02 PM
32

  
33
	private OAIConfiguration oaiConfiguration;
34
	@Resource
35
	private OAIConfigurationParser configurationParser;
36

  
37
	public OAIConfigurationStringReader() {
38
		oaiConfiguration = null;
39
	}
40

  
41
	public OAIConfigurationStringReader(final String profile) {
42
		this.readConfiguration(profile);
43
	}
44

  
45
	public void readConfiguration(final String profile) {
46
		log.debug(profile);
47
		try {
48
			oaiConfiguration = configurationParser.getConfiguration(profile);
49
		} catch (IOException e) {
50
			throw new OaiPublisherRuntimeException("Could not read OAI configuration profile", e);
51
		}
52
	}
53

  
54
	public boolean isConfigurationLoaded() {
55
		return this.oaiConfiguration != null;
56
	}
57

  
58
	@Override
59
	public List<SetInfo> getSets() {
60
		if (isConfigurationLoaded()) return Lists.newArrayList(this.oaiConfiguration.getSetsMap().values());
61
		else throw new OaiPublisherRuntimeException("Configuration is not loaded");
62
	}
63

  
64
	@Override
65
	public List<SetInfo> getSets(final boolean onlyEnabled) {
66
		if (isConfigurationLoaded()) return Lists.newArrayList(Iterables.filter(this.oaiConfiguration.getSetsMap().values(), new Predicate<SetInfo>() {
67

  
68
			/**
69
			 * {@inheritDoc}
70
			 * 
71
			 * @see com.google.common.base.Predicate#apply(java.lang.Object)
72
			 */
73
			@Override
74
			public boolean apply(final SetInfo setInfo) {
75
				if (onlyEnabled) return setInfo.isEnabled();
76
				else return true;
77
			}
78

  
79
		}));
80
		else throw new OaiPublisherRuntimeException("Configuration is not loaded");
81
	}
82

  
83
	@Override
84
	public List<String> getSetSpecs() {
85
		if (isConfigurationLoaded()) return Lists.newArrayList(this.oaiConfiguration.getSetsMap().keySet());
86
		else throw new OaiPublisherRuntimeException("Configuration is not loaded");
87
	}
88

  
89
	@Override
90
	public List<String> getSetSpecs(final boolean onlyEnabled) {
91
		if (isConfigurationLoaded()) {
92
			if (!onlyEnabled) return this.getSetSpecs();
93
			List<SetInfo> enabled = this.getSets(true);
94
			return Lists.newArrayList(Iterables.transform(enabled, new Function<SetInfo, String>() {
95

  
96
				@Override
97
				public String apply(final SetInfo setInfo) {
98
					return setInfo.getSetSpec();
99
				}
100

  
101
			}));
102
		} else throw new OaiPublisherRuntimeException("Configuration is not loaded");
103
	}
104

  
105
	@Override
106
	public List<MDFInfo> getSourceMetadataFormats() {
107
		if (isConfigurationLoaded()) return Lists.newArrayList(this.oaiConfiguration.getSourcesMDF());
108
		else throw new OaiPublisherRuntimeException("Configuration is not loaded");
109
	}
110

  
111
	@Override
112
	public SetInfo getSetInfo(final String setSpec) {
113
		if (isConfigurationLoaded()) return this.oaiConfiguration.getSetsMap().get(setSpec);
114
		else throw new OaiPublisherRuntimeException("Configuration is not loaded");
115
	}
116

  
117
	@Override
118
	public List<MDFInfo> getMetadataFormatInfo() {
119
		if (isConfigurationLoaded()) return Lists.newArrayList(this.oaiConfiguration.getMdFormatsMap().values());
120
		else throw new OaiPublisherRuntimeException("Configuration is not loaded");
121
	}
122

  
123
	@Override
124
	public List<MDFInfo> getMetadataFormatInfo(final boolean onlyEnabled) {
125
		if (isConfigurationLoaded()) {
126
			if (!onlyEnabled) return this.getMetadataFormatInfo();
127
			else return Lists.newArrayList(Iterables.filter(this.oaiConfiguration.getMdFormatsMap().values(), new Predicate<MDFInfo>() {
128

  
129
				/**
130
				 * {@inheritDoc}
131
				 * 
132
				 * @see com.google.common.base.Predicate#apply(java.lang.Object)
133
				 */
134
				@Override
135
				public boolean apply(final MDFInfo mdfInfo) {
136
					if (onlyEnabled) return mdfInfo.isEnabled();
137
					else return true;
138
				}
139

  
140
			}));
141

  
142
		} else throw new OaiPublisherRuntimeException("Configuration is not loaded");
143
	}
144

  
145
	@Override
146
	public MDFInfo getMetadataFormatInfo(final String mdPrefix) {
147
		if (isConfigurationLoaded()) return this.oaiConfiguration.getMdFormatsMap().get(mdPrefix);
148
		else throw new OaiPublisherRuntimeException("Configuration is not loaded");
149
	}
150

  
151
	@Override
152
	public List<PublisherField> getFields() {
153
		if (isConfigurationLoaded()) return this.oaiConfiguration.getFields();
154
		else throw new OaiPublisherRuntimeException("Configuration is not loaded");
155
	}
156

  
157
	@Override
158
	public List<PublisherField> getFields(final String format, final String interpretation, final String layout) {
159
		if (isConfigurationLoaded()) return Lists.newArrayList(this.oaiConfiguration.getFieldsFor(format, layout, interpretation));
160
		else throw new OaiPublisherRuntimeException("Configuration is not loaded");
161
	}
162

  
163
	@Override
164
	public List<String> getFieldNames() {
165
		if (isConfigurationLoaded()) return this.oaiConfiguration.getFieldNames();
166
		else throw new OaiPublisherRuntimeException("Configuration is not loaded");
167
	}
168

  
169
	@Override
170
	public List<MDFInfo> getFormatsServedBy(final String sourceFormatName, final String sourceFormatLayout, final String sourceFormatInterpretation) {
171
		if (isConfigurationLoaded()) {
172
			Collection<MDFInfo> unfiltered = this.oaiConfiguration.getMdFormatsMap().values();
173
			return Lists.newArrayList(Collections2.filter(unfiltered, new Predicate<MDFInfo>() {
174

  
175
				@Override
176
				public boolean apply(final MDFInfo mdf) {
177
					return (mdf.getSourceFormatName() == sourceFormatName) && (mdf.getSourceFormatLayout() == sourceFormatLayout)
178
							&& (mdf.getSourceFormatInterpretation() == sourceFormatInterpretation);
179
				}
180

  
181
			}));
182

  
183
		} else throw new OaiPublisherRuntimeException("Configuration is not loaded");
184
	}
185

  
186
	@Override
187
	public String getIdScheme() {
188
		if (isConfigurationLoaded()) return this.oaiConfiguration.getIdScheme();
189
		else throw new OaiPublisherRuntimeException("Configuration is not loaded");
190
	}
191

  
192
	@Override
193
	public String getIdNamespace() {
194
		if (isConfigurationLoaded()) return this.oaiConfiguration.getIdNamespace();
195
		else throw new OaiPublisherRuntimeException("Configuration is not loaded");
196
	}
197

  
198
	public OAIConfiguration getOaiConfiguration() {
199
		return oaiConfiguration;
200
	}
201

  
202
	public void setOaiConfiguration(final OAIConfiguration oaiConfiguration) {
203
		this.oaiConfiguration = oaiConfiguration;
204
	}
205

  
206
	public OAIConfigurationParser getConfigurationParser() {
207
		return configurationParser;
208
	}
209

  
210
	public void setConfigurationParser(final OAIConfigurationParser configurationParser) {
211
		this.configurationParser = configurationParser;
212
	}
213

  
214
}
modules/dnet-oai-utils/tags/dnet-oai-utils-4.0.0/src/main/java/eu/dnetlib/data/information/oai/publisher/conf/OAIConfigurationParser.java
1
package eu.dnetlib.data.information.oai.publisher.conf;
2

  
3
import java.io.IOException;
4
import java.io.StringReader;
5
import java.util.List;
6
import java.util.Map;
7

  
8
import javax.xml.stream.XMLInputFactory;
9
import javax.xml.stream.XMLStreamConstants;
10
import javax.xml.stream.XMLStreamException;
11
import javax.xml.stream.XMLStreamReader;
12
import javax.xml.transform.stream.StreamSource;
13

  
14
import org.apache.commons.logging.Log;
15
import org.apache.commons.logging.LogFactory;
16

  
17
import com.google.common.collect.ArrayListMultimap;
18
import com.google.common.collect.Lists;
19
import com.google.common.collect.Maps;
20
import com.google.common.collect.Multimap;
21

  
22
import eu.dnetlib.data.information.oai.publisher.OaiPublisherRuntimeException;
23
import eu.dnetlib.data.information.oai.publisher.PublisherField;
24
import eu.dnetlib.data.information.oai.publisher.info.MDFInfo;
25
import eu.dnetlib.data.information.oai.publisher.info.SetInfo;
26

  
27
/**
28
 * Parses an XML document representing the OAI configuration profile and creates the corresponding OAIConfiguration object.
29
 * 
30
 * @author alessia
31
 * 
32
 */
33
public class OAIConfigurationParser {
34

  
35
	private static final Log log = LogFactory.getLog(OAIConfigurationParser.class); // NOPMD by marko on 11/24/08 5:02 PM
36

  
37
	private final ThreadLocal<XMLInputFactory> factory = new ThreadLocal<XMLInputFactory>() {
38

  
39
		@Override
40
		protected XMLInputFactory initialValue() {
41
			return XMLInputFactory.newInstance();
42
		}
43
	};
44

  
45
	public OAIConfiguration getConfiguration(final String configurationProfile) throws IOException {
46
		log.debug(configurationProfile);
47
		OAIConfiguration config = new OAIConfiguration();
48
		Map<String, SetInfo> setsMap = Maps.newHashMap();
49
		Map<String, MDFInfo> mdFormatsMap = Maps.newHashMap();
50
		List<String> indexNames = Lists.newArrayList();
51
		List<PublisherField> fields = Lists.newArrayList();
52
		try {
53
			final XMLStreamReader parser = factory.get().createXMLStreamReader(new StreamSource(new StringReader(configurationProfile)));
54
			while (parser.hasNext()) {
55
				int event = parser.next();
56
				if (event == XMLStreamConstants.START_ELEMENT) {
57
					final String localName = parser.getLocalName();
58
					if (localName.equals("IDSCHEME")) {
59
						config.setIdScheme(parser.getElementText());
60
					} else if (localName.equals("IDNAMESPACE")) {
61
						config.setIdNamespace(parser.getElementText());
62
					} else if (localName.equals("OAISET")) {
63
						boolean inSet = true;
64
						SetInfo setInfo = new SetInfo();
65
						String enabled = parser.getAttributeValue(null, "enabled");
66
						setInfo.setEnabled(Boolean.parseBoolean(enabled));
67
						while (parser.hasNext() && inSet) {
68
							event = parser.next();
69
							if (event == XMLStreamConstants.START_ELEMENT) {
70
								String setElementName = parser.getLocalName();
71
								String setElementValue = parser.getElementText();
72
								this.handleSetInfo(setInfo, setElementName, setElementValue);
73
							}
74
							if ((event == XMLStreamConstants.END_ELEMENT) && parser.getLocalName().equals("OAISET")) {
75
								inSet = false;
76
								setsMap.put(setInfo.getSetSpec(), setInfo);
77
							}
78
						}
79
					} else {
80
						if (localName.equals("METADATAFORMAT")) {
81
							boolean inMetadata = true;
82
							MDFInfo mdfInfo = new MDFInfo();
83
							String exportable = parser.getAttributeValue(null, "exportable");
84
							mdfInfo.setEnabled(Boolean.parseBoolean(exportable));
85
							String mdPrefix = parser.getAttributeValue(null, "metadataPrefix");
86
							mdfInfo.setPrefix(mdPrefix);
87
							while (parser.hasNext() && inMetadata) {
88
								event = parser.next();
89
								if (event == XMLStreamConstants.START_ELEMENT) {
90
									String mdfElementName = parser.getLocalName();
91
									if (mdfElementName.equals("SOURCE_METADATA_FORMAT")) {
92
										this.handleSourceMDF(mdfInfo, parser);
93
										config.getSourcesMDF().add(mdfInfo);
94
									} else {
95
										String mdfElementValue = parser.getElementText();
96
										this.handleMDFInfo(mdfInfo, mdfElementName, mdfElementValue);
97
									}
98
								}
99
								if ((event == XMLStreamConstants.END_ELEMENT) && parser.getLocalName().equals("METADATAFORMAT")) {
100
									inMetadata = false;
101
									mdFormatsMap.put(mdPrefix, mdfInfo);
102
								}
103
							}
104
						} else {
105
							// INDICES
106
							if (localName.equals("INDEX")) {
107
								boolean inIndex = true;
108
								PublisherField publisherField = new PublisherField();
109
								String indexName = parser.getAttributeValue(null, "name");
110
								String repeatable = parser.getAttributeValue(null, "repeatable");
111
								boolean isRepeatable = Boolean.valueOf(repeatable);
112
								indexNames.add(indexName);
113
								publisherField.setFieldName(indexName);
114
								publisherField.setRepeatable(isRepeatable);
115
								// now let's set the SOURCES
116
								Multimap<String, String> fieldSources = ArrayListMultimap.create();
117
								while (parser.hasNext() && inIndex) {
118
									event = parser.next();
119
									if (event == XMLStreamConstants.START_ELEMENT) {
120
										String currentElementName = parser.getLocalName();
121
										this.handleIndex(fieldSources, indexName, parser, currentElementName);
122
									}
123
									if ((event == XMLStreamConstants.END_ELEMENT) && parser.getLocalName().equals("INDEX")) {
124
										inIndex = false;
125
									}
126
								}
127
								publisherField.setSources(fieldSources);
128
								fields.add(publisherField);
129

  
130
							}
131
						}
132
					}
133
				}
134
			}
135
			config.setFields(fields);
136
			config.setFieldNames(indexNames);
137
			config.setMdFormatsMap(mdFormatsMap);
138
			config.setSetsMap(setsMap);
139
			return config;
140
		} catch (final XMLStreamException e) {
141
			throw new OaiPublisherRuntimeException(e);
142
		}
143

  
144
	}
145

  
146
	/**
147
	 * Sets information about the indices.
148
	 * 
149
	 * @param fieldSources
150
	 * @param indexName
151
	 * @param parser
152
	 * @param currentLocalName
153
	 */
154
	private void handleIndex(final Multimap<String, String> fieldSources, final String indexName, final XMLStreamReader parser, final String currentLocalName) {
155
		if (currentLocalName.equals("SOURCE")) {
156
			MDFInfo indexSource = new MDFInfo();
157
			this.handleSourceMDF(indexSource, parser);
158
			String key = indexSource.getSourceFormatName() + "-" + indexSource.getSourceFormatLayout() + "-" + indexSource.getSourceFormatInterpretation();
159
			fieldSources.put(key, parser.getAttributeValue(null, "path"));
160
		} else {
161
			log.warn("I do not know how to handle INDEX element with name " + currentLocalName);
162
		}
163
	}
164

  
165
	/**
166
	 * Sets information about the source metadata format in the given instance of MDFInfo.
167
	 * 
168
	 * @param mdfInfo
169
	 * @param parser
170
	 */
171
	private void handleSourceMDF(final MDFInfo mdfInfo, final XMLStreamReader parser) {
172
		String interpretation = parser.getAttributeValue(null, "interpretation");
173
		String layout = parser.getAttributeValue(null, "layout");
174
		String name = parser.getAttributeValue(null, "name");
175
		mdfInfo.setSourceFormatInterpretation(interpretation);
176
		mdfInfo.setSourceFormatLayout(layout);
177
		mdfInfo.setSourceFormatName(name);
178
	}
179

  
180
	/**
181
	 * Sets information in the MDFInfo instance based on the element name.
182
	 * 
183
	 * @param mdfInfo
184
	 * @param mdfElementName
185
	 * @param mdfElementValue
186
	 */
187
	private void handleMDFInfo(final MDFInfo mdfInfo, final String elementName, final String elementValue) {
188
		if (elementName.equals("NAMESPACE")) {
189
			mdfInfo.setNamespace(elementValue);
190
		} else {
191
			if (elementName.equals("SCHEMA")) {
192
				mdfInfo.setSchema(elementValue);
193
			} else {
194
				if (elementName.equals("TRANSFORMATION_RULE")) {
195
					mdfInfo.setTransformationRuleID(elementValue);
196
				} else {
197
					if (elementName.equals("BASE_QUERY")) {
198
						mdfInfo.setBaseQuery(elementValue);
199
					} else {
200
						log.warn("I do not know how to handle Metadata Format element with name " + elementName + " and value " + elementValue);
201
					}
202
				}
203
			}
204
		}
205

  
206
	}
207

  
208
	/**
209
	 * Sets information in the SetInfo instance based on the element name.
210
	 * 
211
	 * @param setInfo
212
	 * @param elementName
213
	 * @param elementValue
214
	 */
215
	private void handleSetInfo(final SetInfo setInfo, final String elementName, final String elementValue) {
216
		if (elementName.equals("spec")) {
217
			setInfo.setSetSpec(elementValue);
218
		} else {
219
			if (elementName.equals("name")) {
220
				setInfo.setSetName(elementValue);
221
			} else {
222
				if (elementName.equals("description")) {
223
					setInfo.setSetDescription(elementValue);
224
				} else {
225
					if (elementName.equals("query")) {
226
						setInfo.setQuery(elementValue);
227
					} else {
228
						log.warn("I do not know how to handle Set element with name " + elementName + " and value " + elementValue);
229
					}
230
				}
231
			}
232
		}
233
	}
234
}
modules/dnet-oai-utils/tags/dnet-oai-utils-4.0.0/src/main/java/eu/dnetlib/data/information/oai/publisher/conf/OAIConfigurationWriter.java
1
package eu.dnetlib.data.information.oai.publisher.conf;
2

  
3
import java.util.Collection;
4
import java.util.List;
5

  
6
import javax.annotation.Resource;
7

  
8
import org.apache.commons.logging.Log;
9
import org.apache.commons.logging.LogFactory;
10

  
11
import eu.dnetlib.data.information.oai.publisher.PublisherField;
12
import eu.dnetlib.data.information.oai.publisher.info.MDFInfo;
13
import eu.dnetlib.data.information.oai.publisher.info.SetInfo;
14
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
15
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryException;
16
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
17
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
18
import eu.dnetlib.miscutils.functional.string.EscapeXml;
19

  
20
public class OAIConfigurationWriter {
21

  
22
	private static final Log log = LogFactory.getLog(OAIConfigurationWriter.class); // NOPMD by marko on 11/24/08 5:02 PM
23

  
24
	@Resource
25
	private UniqueServiceLocator serviceLocator;
26

  
27
	@Resource(name = "oaiConfigurationExistReader")
28
	private OAIConfigurationReader configuration;
29

  
30
	private boolean execute(final String xUpdate) throws ISRegistryException {
31
		log.debug("Running XUpdate:\n" + xUpdate);
32
		boolean done = this.serviceLocator.getService(ISRegistryService.class).executeXUpdate(xUpdate);
33
		if (!done) {
34
			log.fatal("Could not perform the following Xupdate:\n" + xUpdate);
35
		}
36
		return done;
37
	}
38

  
39
	public boolean updateMetadataFormat(final String mdPrefix, final MDFInfo newInfo) throws ISRegistryException {
40

  
41
		final String xUpdate = "update replace //RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='OAIPublisherConfigurationDSResourceType']"
42
				+ "//CONFIGURATION//METADATAFORMAT[./@metadataPrefix/string()='" + mdPrefix + "'] with <METADATAFORMAT exportable=\"" + newInfo.isEnabled()
43
				+ "\" metadataPrefix=\"" + newInfo.getPrefix() + "\">" + "<NAMESPACE>" + newInfo.getNamespace() + "</NAMESPACE><SCHEMA>" + newInfo.getSchema()
44
				+ "</SCHEMA><SOURCE_METADATA_FORMAT interpretation=\"" + newInfo.getSourceFormatInterpretation() + "\" layout=\""
45
				+ newInfo.getSourceFormatLayout() + "\" name=\"" + newInfo.getSourceFormatName() + "\"/><TRANSFORMATION_RULE>"
46
				+ newInfo.getTransformationRuleID() + "</TRANSFORMATION_RULE><BASE_QUERY>" + new EscapeXml().evaluate(newInfo.getBaseQuery())
47
				+ "</BASE_QUERY></METADATAFORMAT>";
48
		return this.execute(xUpdate);
49
	}
50

  
51
	public boolean addMetadataFormat(final MDFInfo newInfo) throws ISRegistryException {
52

  
53
		String action = "update insert ";
54
		String newNode = "<METADATAFORMAT metadataPrefix='" + newInfo.getPrefix() + "' exportable='" + newInfo.isEnabled() + "' ><NAMESPACE>"
55
				+ newInfo.getNamespace() + "</NAMESPACE><SCHEMA>" + newInfo.getSchema() + "</SCHEMA>" + "<SOURCE_METADATA_FORMAT interpretation='"
56
				+ newInfo.getSourceFormatInterpretation() + "' layout='" + newInfo.getSourceFormatLayout() + "' name='" + newInfo.getSourceFormatName()
57
				+ "' /><TRANSFORMATION_RULE>" + newInfo.getTransformationRuleID() + "</TRANSFORMATION_RULE><BASE_QUERY>"
58
				+ new EscapeXml().evaluate(newInfo.getBaseQuery()) + "</BASE_QUERY></METADATAFORMAT>";
59
		String targetNode = "//RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='OAIPublisherConfigurationDSResourceType']//CONFIGURATION/METADATAFORMATS";
60

  
61
		String xUpdate = action + newNode + " into " + targetNode;
62
		return this.execute(xUpdate);
63
	}
64

  
65
	public boolean deleteMetadataFormat(final String mdPrefix) throws ISRegistryException {
66
		String xUpdate =
67
				"update delete //RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='OAIPublisherConfigurationDSResourceType']//CONFIGURATION//METADATAFORMATS/METADATAFORMAT[./@metadataPrefix/string()='"
68
						+ mdPrefix + "']";
69
		return this.execute(xUpdate);
70
	}
71

  
72
	public boolean updateOAISet(final String setSpec, final SetInfo newInfo) throws ISRegistryException {
73
		final String xUpdate = "update replace //RESOURCE_PROFILE[.//RESOURCE_TYPE/@value='OAIPublisherConfigurationDSResourceType']"
74
				+ "//CONFIGURATION//OAISET[spec/text()='" + setSpec + "'] with " + "<OAISET enabled=\"" + newInfo.isEnabled() + "\">" + "<spec>"
75
				+ newInfo.getSetSpec() + "</spec>" + "<name>" + newInfo.getSetName() + "</name>" + "<description>" + newInfo.getSetDescription()
76
				+ "</description>" + "<query>" + new EscapeXml().evaluate(newInfo.getQuery()) + "</query></OAISET>";
77

  
78
		return this.execute(xUpdate);
79
	}
80

  
81
	public boolean addOAISet(final SetInfo newInfo) throws ISRegistryException {
82
		String action = "update insert ";
83
		String newNode = "<OAISET enabled=\"" + newInfo.isEnabled() + "\">" + "<spec>" + newInfo.getSetSpec() + "</spec>" + "<name>" + newInfo.getSetName()
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff