Revision 29687
Added by Sandro La Bruzzo almost 10 years ago
modules/dnet-modular-index-service/trunk/src/main/java/eu/dnetlib/functionality/index/resultset/ResultsCache.java | ||
---|---|---|
1 |
package eu.dnetlib.functionality.index.resultset; |
|
2 |
|
|
3 |
import java.util.List; |
|
4 |
|
|
5 |
public class ResultsCache { |
|
6 |
|
|
7 |
private List<String> cache; |
|
8 |
|
|
9 |
private int from; |
|
10 |
|
|
11 |
private int to; |
|
12 |
|
|
13 |
public ResultsCache(List<String> cache, int from, int to) { |
|
14 |
this.cache = cache; |
|
15 |
this.from = from; |
|
16 |
this.to = to; |
|
17 |
} |
|
18 |
|
|
19 |
public boolean resultInCache(int from, int to) { |
|
20 |
return from >= this.from && to <= this.to; |
|
21 |
} |
|
22 |
|
|
23 |
public List<String> get(int from, int to) { |
|
24 |
return cache.subList(from - 1, to); |
|
25 |
} |
|
26 |
|
|
27 |
} |
modules/dnet-modular-index-service/trunk/src/main/java/eu/dnetlib/functionality/index/resultset/factory/LookupResultSetListenerFactory.java | ||
---|---|---|
1 |
package eu.dnetlib.functionality.index.resultset.factory; |
|
2 |
|
|
3 |
import java.util.List; |
|
4 |
|
|
5 |
import eu.dnetlib.data.provision.index.rmi.IndexServiceException; |
|
6 |
import eu.dnetlib.enabling.resultset.ResultSetListener; |
|
7 |
import eu.dnetlib.functionality.index.IndexServerDAO; |
|
8 |
import eu.dnetlib.functionality.index.query.IndexQuery; |
|
9 |
import eu.dnetlib.functionality.index.query.QueryLanguage; |
|
10 |
import eu.dnetlib.functionality.index.resultset.LookupResultSetListener; |
|
11 |
import eu.dnetlib.functionality.index.utils.MetadataReference; |
|
12 |
|
|
13 |
/** |
|
14 |
* A factory for creating LookupResultSetListener objects. |
|
15 |
* |
|
16 |
* @author claudio |
|
17 |
* @author Sandro |
|
18 |
*/ |
|
19 |
public class LookupResultSetListenerFactory extends IndexResultSetListenerFactory { |
|
20 |
|
|
21 |
/** |
|
22 |
* {@inheritDoc} |
|
23 |
* |
|
24 |
* @see eu.dnetlib.functionality.index.resultset.factory.IndexResultSetListenerFactory#getListener(eu.dnetlib.functionality.index.query.QueryLanguage, |
|
25 |
* java.lang.String, eu.dnetlib.functionality.index.utils.MetadataReference, java.lang.String[]) |
|
26 |
*/ |
|
27 |
@Override |
|
28 |
public ResultSetListener getListener(final IndexServerDAO dao, |
|
29 |
final QueryLanguage lang, |
|
30 |
final String query, |
|
31 |
final MetadataReference mdRef, |
|
32 |
final List<String> dsIds) throws IndexServiceException { |
|
33 |
|
|
34 |
final IndexQuery indexQuery = dao.getIndexQueryFactory().getIndexQuery(lang, query, dao, dsIds, mdRef); |
|
35 |
return new LookupResultSetListener(indexQuery, mdRef, dao.getIndexCollection(mdRef)); |
|
36 |
} |
|
37 |
} |
modules/dnet-modular-index-service/trunk/src/main/java/eu/dnetlib/functionality/index/resultset/factory/BrowsingResultSetListenerFactory.java | ||
---|---|---|
1 |
package eu.dnetlib.functionality.index.resultset.factory; |
|
2 |
|
|
3 |
import java.util.List; |
|
4 |
|
|
5 |
import org.springframework.beans.factory.annotation.Required; |
|
6 |
|
|
7 |
import eu.dnetlib.data.provision.index.rmi.BrowsingRow; |
|
8 |
import eu.dnetlib.data.provision.index.rmi.IndexServiceException; |
|
9 |
import eu.dnetlib.enabling.resultset.ResultSetListener; |
|
10 |
import eu.dnetlib.functionality.index.IndexServerDAO; |
|
11 |
import eu.dnetlib.functionality.index.query.IndexQuery; |
|
12 |
import eu.dnetlib.functionality.index.query.QueryLanguage; |
|
13 |
import eu.dnetlib.functionality.index.resultset.BrowsingResultSetListener; |
|
14 |
import eu.dnetlib.functionality.index.utils.MetadataReference; |
|
15 |
import eu.dnetlib.miscutils.jaxb.JaxbFactory; |
|
16 |
|
|
17 |
/** |
|
18 |
* A factory for creating BrowsingResultSetListener objects. |
|
19 |
* |
|
20 |
* @author claudio |
|
21 |
*/ |
|
22 |
public class BrowsingResultSetListenerFactory extends IndexResultSetListenerFactory { |
|
23 |
|
|
24 |
/** The jaxb factory. */ |
|
25 |
private JaxbFactory<BrowsingRow> jaxbFactory; |
|
26 |
|
|
27 |
/** The max browse results. */ |
|
28 |
private int maxBrowseResults; |
|
29 |
|
|
30 |
/** |
|
31 |
* {@inheritDoc} |
|
32 |
* |
|
33 |
* @see eu.dnetlib.functionality.index.resultset.factory.IndexResultSetListenerFactory#getListener(eu.dnetlib.functionality.index.query.QueryLanguage, |
|
34 |
* java.lang.String, eu.dnetlib.functionality.index.utils.MetadataReference, java.lang.String[]) |
|
35 |
*/ |
|
36 |
@Override |
|
37 |
public ResultSetListener getListener(final IndexServerDAO dao, |
|
38 |
final QueryLanguage lang, |
|
39 |
final String query, |
|
40 |
final MetadataReference mdRef, |
|
41 |
final List<String> dsIds) throws IndexServiceException { |
|
42 |
|
|
43 |
final IndexQuery indexQuery = dao.getIndexQueryFactory().getIndexQuery(lang, query, dao, dsIds, mdRef); |
|
44 |
return new BrowsingResultSetListener(indexQuery, mdRef, dao.getIndexCollection(mdRef), getJaxbFactory()); |
|
45 |
} |
|
46 |
|
|
47 |
/** |
|
48 |
* Gets the jaxb factory. |
|
49 |
* |
|
50 |
* @return the jaxb factory |
|
51 |
*/ |
|
52 |
public JaxbFactory<BrowsingRow> getJaxbFactory() { |
|
53 |
return jaxbFactory; |
|
54 |
} |
|
55 |
|
|
56 |
/** |
|
57 |
* Sets the jaxb factory. |
|
58 |
* |
|
59 |
* @param jaxbFactory |
|
60 |
* the new jaxb factory |
|
61 |
*/ |
|
62 |
@Required |
|
63 |
public void setJaxbFactory(final JaxbFactory<BrowsingRow> jaxbFactory) { |
|
64 |
this.jaxbFactory = jaxbFactory; |
|
65 |
} |
|
66 |
|
|
67 |
/** |
|
68 |
* Sets the max browse results. |
|
69 |
* |
|
70 |
* @param maxBrowseResults |
|
71 |
* the new max browse results |
|
72 |
*/ |
|
73 |
@Required |
|
74 |
public void setMaxBrowseResults(final int maxBrowseResults) { |
|
75 |
this.maxBrowseResults = maxBrowseResults; |
|
76 |
} |
|
77 |
|
|
78 |
/** |
|
79 |
* Gets the max browse results. |
|
80 |
* |
|
81 |
* @return the max browse results |
|
82 |
*/ |
|
83 |
public int getMaxBrowseResults() { |
|
84 |
return maxBrowseResults; |
|
85 |
} |
|
86 |
|
|
87 |
} |
modules/dnet-modular-index-service/trunk/src/main/java/eu/dnetlib/functionality/index/resultset/factory/IndexResultSetFactory.java | ||
---|---|---|
1 |
package eu.dnetlib.functionality.index.resultset.factory; |
|
2 |
|
|
3 |
import java.util.List; |
|
4 |
|
|
5 |
import javax.xml.ws.wsaddressing.W3CEndpointReference; |
|
6 |
|
|
7 |
import org.springframework.beans.factory.annotation.Required; |
|
8 |
|
|
9 |
import eu.dnetlib.data.provision.index.rmi.IndexServiceException; |
|
10 |
import eu.dnetlib.enabling.resultset.ResultSetFactory; |
|
11 |
import eu.dnetlib.functionality.index.IndexServerDAO; |
|
12 |
import eu.dnetlib.functionality.index.query.QueryLanguage; |
|
13 |
import eu.dnetlib.functionality.index.utils.MetadataReference; |
|
14 |
|
|
15 |
/** |
|
16 |
* |
|
17 |
* @author claudio |
|
18 |
* |
|
19 |
*/ |
|
20 |
public class IndexResultSetFactory { |
|
21 |
|
|
22 |
/** |
|
23 |
* resultSetListener factory for lookup. |
|
24 |
*/ |
|
25 |
private LookupResultSetListenerFactory lookupResultSetListenerFactory; |
|
26 |
|
|
27 |
/** |
|
28 |
* resultSetListener factory for browsing. |
|
29 |
*/ |
|
30 |
private BrowsingResultSetListenerFactory browsingResultSetListenerFactory; |
|
31 |
|
|
32 |
/** |
|
33 |
* resultSet factory |
|
34 |
*/ |
|
35 |
private ResultSetFactory resultsetFactory; |
|
36 |
|
|
37 |
/** |
|
38 |
* Gets the lookup result set. |
|
39 |
* |
|
40 |
* @param collection |
|
41 |
* the collection |
|
42 |
* @param lang |
|
43 |
* the lang |
|
44 |
* @param query |
|
45 |
* the query |
|
46 |
* @param mdRef |
|
47 |
* the md ref |
|
48 |
* @param dsId |
|
49 |
* the ds id |
|
50 |
* @return the lookup result set |
|
51 |
* @throws IndexServiceException |
|
52 |
* the index service exception |
|
53 |
*/ |
|
54 |
public W3CEndpointReference getLookupResultSet(final IndexServerDAO dao, |
|
55 |
final QueryLanguage lang, |
|
56 |
final String query, |
|
57 |
final MetadataReference mdRef, |
|
58 |
final List<String> dsIds) throws IndexServiceException { |
|
59 |
|
|
60 |
return resultsetFactory.createResultSet(lookupResultSetListenerFactory.getListener(dao, lang, query, mdRef, dsIds)); |
|
61 |
} |
|
62 |
|
|
63 |
public W3CEndpointReference getBrowsingResultSet(final IndexServerDAO dao, |
|
64 |
final QueryLanguage lang, |
|
65 |
final String query, |
|
66 |
final MetadataReference mdRef, |
|
67 |
final List<String> dsIds) throws IndexServiceException { |
|
68 |
|
|
69 |
return resultsetFactory.createResultSet(browsingResultSetListenerFactory.getListener(dao, lang, query, mdRef, dsIds)); |
|
70 |
} |
|
71 |
|
|
72 |
// /////////////////////////////////// setters |
|
73 |
|
|
74 |
@Required |
|
75 |
public void setLookupResultSetListenerFactory(final LookupResultSetListenerFactory lookupResultSetListenerFactory) { |
|
76 |
this.lookupResultSetListenerFactory = lookupResultSetListenerFactory; |
|
77 |
} |
|
78 |
|
|
79 |
@Required |
|
80 |
public void setBrowsingResultSetListenerFactory(final BrowsingResultSetListenerFactory browsingResultSetListenerFactory) { |
|
81 |
this.browsingResultSetListenerFactory = browsingResultSetListenerFactory; |
|
82 |
} |
|
83 |
|
|
84 |
@Required |
|
85 |
public void setResultsetFactory(final ResultSetFactory resultsetFactory) { |
|
86 |
this.resultsetFactory = resultsetFactory; |
|
87 |
} |
|
88 |
|
|
89 |
} |
modules/dnet-modular-index-service/trunk/src/main/java/eu/dnetlib/functionality/index/resultset/factory/IndexResultSetListenerFactory.java | ||
---|---|---|
1 |
package eu.dnetlib.functionality.index.resultset.factory; |
|
2 |
|
|
3 |
import java.util.List; |
|
4 |
|
|
5 |
import eu.dnetlib.data.provision.index.rmi.IndexServiceException; |
|
6 |
import eu.dnetlib.enabling.resultset.ResultSetListener; |
|
7 |
import eu.dnetlib.functionality.index.IndexServerDAO; |
|
8 |
import eu.dnetlib.functionality.index.query.QueryLanguage; |
|
9 |
import eu.dnetlib.functionality.index.utils.MetadataReference; |
|
10 |
|
|
11 |
/** |
|
12 |
* |
|
13 |
* |
|
14 |
* @author claudio |
|
15 |
* |
|
16 |
*/ |
|
17 |
public abstract class IndexResultSetListenerFactory { |
|
18 |
|
|
19 |
/** |
|
20 |
* Factory method. |
|
21 |
* |
|
22 |
* @param indexServer |
|
23 |
* the index server |
|
24 |
* @param lang |
|
25 |
* the lang |
|
26 |
* @param query |
|
27 |
* the query |
|
28 |
* @param mdRef |
|
29 |
* the md ref |
|
30 |
* @param dsId |
|
31 |
* the ds id |
|
32 |
* @return the listener |
|
33 |
* @throws IndexServiceException |
|
34 |
* the index service exception |
|
35 |
*/ |
|
36 |
public abstract ResultSetListener getListener(final IndexServerDAO dao, |
|
37 |
final QueryLanguage lang, |
|
38 |
final String query, |
|
39 |
final MetadataReference mdRef, |
|
40 |
final List<String> dsIds) throws IndexServiceException; |
|
41 |
|
|
42 |
} |
modules/dnet-modular-index-service/trunk/src/main/java/eu/dnetlib/functionality/index/resultset/IndexResultSetListener.java | ||
---|---|---|
1 |
package eu.dnetlib.functionality.index.resultset; |
|
2 |
|
|
3 |
import java.util.List; |
|
4 |
|
|
5 |
import org.apache.commons.logging.Log; |
|
6 |
import org.apache.commons.logging.LogFactory; |
|
7 |
|
|
8 |
import eu.dnetlib.data.provision.index.rmi.IndexServiceException; |
|
9 |
import eu.dnetlib.enabling.resultset.ResultSetListener; |
|
10 |
import eu.dnetlib.functionality.index.IndexCollection; |
|
11 |
import eu.dnetlib.functionality.index.query.IndexQuery; |
|
12 |
import eu.dnetlib.functionality.index.query.QueryResponseParser; |
|
13 |
import eu.dnetlib.functionality.index.utils.MetadataReference; |
|
14 |
|
|
15 |
/** |
|
16 |
* The listener interface for receiving indexResultSet events. The class that is interested in processing a indexResultSet event implements |
|
17 |
* this interface, and the object created with that class is registered with a component using the component's |
|
18 |
* <code>addIndexResultSetListener<code> method. When |
|
19 |
* the indexResultSet event occurs, that object's appropriate |
|
20 |
* method is invoked. |
|
21 |
* |
|
22 |
* @see IndexResultSetEvent |
|
23 |
*/ |
|
24 |
public abstract class IndexResultSetListener implements ResultSetListener { |
|
25 |
|
|
26 |
/** |
|
27 |
* logger. |
|
28 |
*/ |
|
29 |
private static final Log log = LogFactory.getLog(IndexResultSetListener.class); |
|
30 |
|
|
31 |
/** |
|
32 |
* ResultSet size. |
|
33 |
*/ |
|
34 |
protected Long size = null; |
|
35 |
|
|
36 |
/** |
|
37 |
* the query to be performed. |
|
38 |
*/ |
|
39 |
protected IndexQuery indexQuery; |
|
40 |
|
|
41 |
/** The md ref. */ |
|
42 |
protected MetadataReference mdRef; |
|
43 |
|
|
44 |
/** |
|
45 |
* SolrIndexServer instance. |
|
46 |
*/ |
|
47 |
protected IndexCollection indexServer; |
|
48 |
|
|
49 |
/** |
|
50 |
* Gets the contextual result. |
|
51 |
* |
|
52 |
* @param responseParser |
|
53 |
* the response parser |
|
54 |
* @param from |
|
55 |
* the from |
|
56 |
* @param to |
|
57 |
* the to |
|
58 |
* @return the contextual result |
|
59 |
*/ |
|
60 |
protected abstract List<String> getContextualResult(QueryResponseParser responseParser, int from, int to); |
|
61 |
|
|
62 |
/** |
|
63 |
* Gets the contextual query. |
|
64 |
* |
|
65 |
* @param indexQuery |
|
66 |
* the index query |
|
67 |
* @param from |
|
68 |
* the from |
|
69 |
* @param to |
|
70 |
* the to |
|
71 |
* @return the contextual query |
|
72 |
*/ |
|
73 |
protected abstract IndexQuery getContextualQuery(IndexQuery indexQuery, int from, int to); |
|
74 |
|
|
75 |
/** |
|
76 |
* Gets the contextual size. |
|
77 |
* |
|
78 |
* @return the contextual size |
|
79 |
*/ |
|
80 |
protected abstract int getContextualSize(); |
|
81 |
|
|
82 |
/** |
|
83 |
* Instantiates a new index result set listener. |
|
84 |
* |
|
85 |
* @param indexQuery |
|
86 |
* the index query |
|
87 |
* @param mdRef |
|
88 |
* the md ref |
|
89 |
* @param indexServer |
|
90 |
* the index server |
|
91 |
* @throws IndexServiceException |
|
92 |
* the index service exception |
|
93 |
*/ |
|
94 |
protected IndexResultSetListener(final IndexQuery indexQuery, final MetadataReference mdRef, final IndexCollection indexServer) |
|
95 |
throws IndexServiceException { |
|
96 |
|
|
97 |
this.indexQuery = indexQuery; |
|
98 |
this.mdRef = mdRef; |
|
99 |
this.indexServer = indexServer; |
|
100 |
} |
|
101 |
|
|
102 |
/** |
|
103 |
* {@inheritDoc} |
|
104 |
* |
|
105 |
* @see eu.dnetlib.enabling.resultset.TypedResultSetListener#getResult(int, int) |
|
106 |
*/ |
|
107 |
@Override |
|
108 |
public List<String> getResult(final int from, final int to) { |
|
109 |
return getContextualResult(performQuery(from, to), from, to); |
|
110 |
} |
|
111 |
|
|
112 |
/** |
|
113 |
* {@inheritDoc} |
|
114 |
* |
|
115 |
* @see eu.dnetlib.enabling.resultset.TypedResultSetListener#getSize() |
|
116 |
*/ |
|
117 |
@Override |
|
118 |
public int getSize() { |
|
119 |
return getContextualSize(); |
|
120 |
} |
|
121 |
|
|
122 |
/** |
|
123 |
* Perform query. |
|
124 |
* |
|
125 |
* @param from |
|
126 |
* the from |
|
127 |
* @param to |
|
128 |
* the to |
|
129 |
* @return the query response parser |
|
130 |
*/ |
|
131 |
protected QueryResponseParser performQuery(final int from, final int to) { |
|
132 |
try { |
|
133 |
return indexServer.lookup(getContextualQuery(indexQuery, from, to), mdRef); |
|
134 |
} catch (Exception e) { |
|
135 |
log.error("ops... cannot perform query", e); |
|
136 |
throw new RuntimeException(e); |
|
137 |
} |
|
138 |
} |
|
139 |
|
|
140 |
} |
modules/dnet-modular-index-service/trunk/src/main/java/eu/dnetlib/functionality/index/resultset/LookupResultSetListener.java | ||
---|---|---|
1 |
package eu.dnetlib.functionality.index.resultset; |
|
2 |
|
|
3 |
import java.util.List; |
|
4 |
|
|
5 |
import org.apache.commons.logging.Log; |
|
6 |
import org.apache.commons.logging.LogFactory; |
|
7 |
|
|
8 |
import eu.dnetlib.data.provision.index.rmi.IndexServiceException; |
|
9 |
import eu.dnetlib.functionality.index.IndexCollection; |
|
10 |
import eu.dnetlib.functionality.index.query.IndexQuery; |
|
11 |
import eu.dnetlib.functionality.index.query.QueryResponseParser; |
|
12 |
import eu.dnetlib.functionality.index.utils.MetadataReference; |
|
13 |
|
|
14 |
/** |
|
15 |
* The listener interface for receiving lookupResultSet events. The class that is interested in processing a lookupResultSet event |
|
16 |
* implements this interface, and the object created with that class is registered with a component using the component's |
|
17 |
* <code>addLookupResultSetListener<code> method. When |
|
18 |
* the lookupResultSet event occurs, that object's appropriate |
|
19 |
* method is invoked. |
|
20 |
* |
|
21 |
* @see LookupResultSetEvent |
|
22 |
*/ |
|
23 |
public class LookupResultSetListener extends IndexResultSetListener { |
|
24 |
|
|
25 |
/** |
|
26 |
* logger. |
|
27 |
*/ |
|
28 |
private static final Log log = LogFactory.getLog(LookupResultSetListener.class); |
|
29 |
|
|
30 |
/* |
|
31 |
* (non-Javadoc) |
|
32 |
* |
|
33 |
* @see |
|
34 |
* eu.dnetlib.functionality.index.resultset.IndexResultSetListener#getContextualQuery(eu.dnetlib.functionality.index.query.IndexQuery, |
|
35 |
* int, int) |
|
36 |
*/ |
|
37 |
@Override |
|
38 |
protected IndexQuery getContextualQuery(final IndexQuery indexQuery, final int from, final int to) { |
|
39 |
|
|
40 |
if (log.isDebugEnabled()) { |
|
41 |
log.debug("getContextualResult LOOKUP - from:" + from + " to: " + to); |
|
42 |
log.debug("indexQuery.setStart(" + (from - 1) + ").setRows(" + ((to - from) + 1) + ")"); |
|
43 |
} |
|
44 |
|
|
45 |
indexQuery.setQueryOffset(from - 1).setQueryLimit((to - from) + 1); |
|
46 |
|
|
47 |
return indexQuery; |
|
48 |
} |
|
49 |
|
|
50 |
/* |
|
51 |
* (non-Javadoc) |
|
52 |
* |
|
53 |
* @see eu.dnetlib.functionality.index.resultset.IndexResultSetListener#getContextualSize() |
|
54 |
*/ |
|
55 |
@Override |
|
56 |
protected int getContextualSize() { |
|
57 |
if (size == null) { |
|
58 |
try { |
|
59 |
if (log.isDebugEnabled()) { |
|
60 |
log.debug("setting LookupResultSetListener size with query: " + indexQuery.toString()); |
|
61 |
} |
|
62 |
|
|
63 |
indexQuery.setQueryLimit(0); |
|
64 |
size = indexServer.lookup(indexQuery, mdRef).getNumFound(); |
|
65 |
} catch (Throwable e) { |
|
66 |
log.error("ops getResult", e); |
|
67 |
throw new RuntimeException(e); |
|
68 |
} |
|
69 |
} |
|
70 |
return size.intValue(); |
|
71 |
} |
|
72 |
|
|
73 |
/** |
|
74 |
* Instantiates a new lookup result set listener. |
|
75 |
* |
|
76 |
* @param indexQuery |
|
77 |
* the index query |
|
78 |
* @param mdRef |
|
79 |
* the md ref |
|
80 |
* @param indexServer |
|
81 |
* the index server |
|
82 |
* @throws IndexServiceException |
|
83 |
* the index service exception |
|
84 |
*/ |
|
85 |
public LookupResultSetListener(final IndexQuery indexQuery, final MetadataReference mdRef, final IndexCollection indexServer) throws IndexServiceException { |
|
86 |
|
|
87 |
super(indexQuery, mdRef, indexServer); |
|
88 |
|
|
89 |
log.debug("LookupResultSetListener is up!"); |
|
90 |
} |
|
91 |
|
|
92 |
/* |
|
93 |
* (non-Javadoc) |
|
94 |
* |
|
95 |
* @see eu.dnetlib.functionality.index.resultset.IndexResultSetListener#getContextualResult(eu.dnetlib.functionality.index.query. |
|
96 |
* QueryResponseParser, int, int) |
|
97 |
*/ |
|
98 |
@Override |
|
99 |
protected List<String> getContextualResult(final QueryResponseParser responseParser, final int from, final int to) { |
|
100 |
return responseParser.getResults(); |
|
101 |
} |
|
102 |
|
|
103 |
} |
modules/dnet-modular-index-service/trunk/src/main/java/eu/dnetlib/functionality/index/resultset/BrowsingResultSetListener.java | ||
---|---|---|
1 |
package eu.dnetlib.functionality.index.resultset; |
|
2 |
|
|
3 |
import java.util.List; |
|
4 |
|
|
5 |
import javax.xml.bind.JAXBException; |
|
6 |
|
|
7 |
import org.apache.commons.logging.Log; |
|
8 |
import org.apache.commons.logging.LogFactory; |
|
9 |
|
|
10 |
import eu.dnetlib.data.provision.index.rmi.BrowsingRow; |
|
11 |
import eu.dnetlib.data.provision.index.rmi.IndexServiceException; |
|
12 |
import eu.dnetlib.functionality.index.IndexCollection; |
|
13 |
import eu.dnetlib.functionality.index.query.IndexQuery; |
|
14 |
import eu.dnetlib.functionality.index.query.QueryResponseParser; |
|
15 |
import eu.dnetlib.functionality.index.utils.MetadataReference; |
|
16 |
import eu.dnetlib.miscutils.collections.MappedCollection; |
|
17 |
import eu.dnetlib.miscutils.functional.UnaryFunction; |
|
18 |
import eu.dnetlib.miscutils.jaxb.JaxbFactory; |
|
19 |
|
|
20 |
/** |
|
21 |
* The listener interface for receiving browsingResultSet events. The class that is interested in processing a browsingResultSet event |
|
22 |
* implements this interface, and the object created with that class is registered with a component using the component's |
|
23 |
* <code>addBrowsingResultSetListener<code> method. When |
|
24 |
* the browsingResultSet event occurs, that object's appropriate |
|
25 |
* method is invoked. |
|
26 |
* |
|
27 |
* @see BrowsingResultSetEvent |
|
28 |
*/ |
|
29 |
public class BrowsingResultSetListener extends IndexResultSetListener { |
|
30 |
|
|
31 |
/** |
|
32 |
* logger. |
|
33 |
*/ |
|
34 |
private static final Log log = LogFactory.getLog(BrowsingResultSetListener.class); |
|
35 |
|
|
36 |
/** The jaxb factory. */ |
|
37 |
private JaxbFactory<BrowsingRow> jaxbFactory; |
|
38 |
|
|
39 |
/** |
|
40 |
* Instantiates a new browsing result set listener. |
|
41 |
* |
|
42 |
* @param indexQuery |
|
43 |
* the index query |
|
44 |
* @param mdRef |
|
45 |
* the md ref |
|
46 |
* @param indexServer |
|
47 |
* the index server |
|
48 |
* @param jaxbFactory |
|
49 |
* the jaxb factory |
|
50 |
* @throws IndexServiceException |
|
51 |
* the index service exception |
|
52 |
*/ |
|
53 |
public BrowsingResultSetListener(final IndexQuery indexQuery, final MetadataReference mdRef, final IndexCollection indexServer, |
|
54 |
final JaxbFactory<BrowsingRow> jaxbFactory) throws IndexServiceException { |
|
55 |
|
|
56 |
super(indexQuery, mdRef, indexServer); |
|
57 |
|
|
58 |
this.jaxbFactory = jaxbFactory; |
|
59 |
|
|
60 |
log.debug("BrowsingResultSetListener is up!"); |
|
61 |
} |
|
62 |
|
|
63 |
/** |
|
64 |
* {@inheritDoc} |
|
65 |
* |
|
66 |
* @see eu.dnetlib.functionality.index.resultset.IndexResultSetListener#getContextualQuery(eu.dnetlib.functionality.index.query.IndexQuery, |
|
67 |
* int, int) |
|
68 |
*/ |
|
69 |
@Override |
|
70 |
protected IndexQuery getContextualQuery(final IndexQuery indexQuery, final int from, final int to) { |
|
71 |
if (log.isDebugEnabled()) { |
|
72 |
log.debug("getContextualResult BROWSING - from:" + from + " to: " + to); |
|
73 |
log.debug("indexQuery.setFacetLimit(" + ((to - from) + 1) + ").set(\"facet.offset\", " + (from - 1) + ")"); |
|
74 |
} |
|
75 |
|
|
76 |
// indexQuery.setFacetSort("count"); |
|
77 |
indexQuery.setQueryFacetLimit((to - from) + 1).setQueryFacetOffset(from - 1); |
|
78 |
return indexQuery; |
|
79 |
} |
|
80 |
|
|
81 |
/** |
|
82 |
* {@inheritDoc} |
|
83 |
* |
|
84 |
* @see eu.dnetlib.functionality.index.resultset.IndexResultSetListener#getContextualSize() |
|
85 |
*/ |
|
86 |
@Override |
|
87 |
protected int getContextualSize() { |
|
88 |
if (size == null) { |
|
89 |
try { |
|
90 |
if (log.isDebugEnabled()) { |
|
91 |
log.debug("setting BrowsingResultSetListener size with query: " + indexQuery.toString()); |
|
92 |
} |
|
93 |
|
|
94 |
indexQuery.setQueryLimit(0); // .setFacetSort("index"); |
|
95 |
QueryResponseParser lookup = indexServer.lookup(indexQuery, mdRef); |
|
96 |
|
|
97 |
size = lookup.getNumberOfBrowsingResults(); |
|
98 |
} catch (Throwable e) { |
|
99 |
log.error("ops", e); |
|
100 |
throw new RuntimeException(e); |
|
101 |
} |
|
102 |
} |
|
103 |
// TODO if (size >= maxBrowseResults) { return maxBrowseResults; } |
|
104 |
return size.intValue(); |
|
105 |
} |
|
106 |
|
|
107 |
/** |
|
108 |
* {@inheritDoc} |
|
109 |
* |
|
110 |
* @see eu.dnetlib.functionality.index.resultset.IndexResultSetListener#getContextualResult(eu.dnetlib.functionality.index.query.QueryResponseParser, |
|
111 |
* int, int) |
|
112 |
*/ |
|
113 |
@Override |
|
114 |
protected List<String> getContextualResult(final QueryResponseParser responseParser, final int from, final int to) { |
|
115 |
return wrappedResults(responseParser.getBrowsingResults()); |
|
116 |
} |
|
117 |
|
|
118 |
/** |
|
119 |
* Wrapped results. |
|
120 |
* |
|
121 |
* @param browsingResults |
|
122 |
* the browsing results |
|
123 |
* @return the list |
|
124 |
*/ |
|
125 |
private List<String> wrappedResults(final List<BrowsingRow> browsingResults) { |
|
126 |
return MappedCollection.listMap(browsingResults, new UnaryFunction<String, BrowsingRow>() { |
|
127 |
|
|
128 |
@Override |
|
129 |
public String evaluate(final BrowsingRow value) { |
|
130 |
try { |
|
131 |
return jaxbFactory.serialize(value); |
|
132 |
} catch (final JAXBException e) { |
|
133 |
log.error("cannot serialize ", e); |
|
134 |
throw new RuntimeException(e); |
|
135 |
} |
|
136 |
} |
|
137 |
}); |
|
138 |
} |
|
139 |
|
|
140 |
} |
modules/dnet-modular-index-service/trunk/src/main/java/eu/dnetlib/functionality/index/model/DataFactoryCreator.java | ||
---|---|---|
1 |
package eu.dnetlib.functionality.index.model; |
|
2 |
|
|
3 |
import eu.dnetlib.functionality.index.model.impl.DefaultDataFactoryImpl; |
|
4 |
|
|
5 |
/** |
|
6 |
* Helper class to decouple Any and Record interfaces better from default implementation. |
|
7 |
*/ |
|
8 |
public final class DataFactoryCreator { |
|
9 |
|
|
10 |
/** |
|
11 |
* Private default constructor to avoid instance creation. |
|
12 |
*/ |
|
13 |
private DataFactoryCreator() { |
|
14 |
} |
|
15 |
|
|
16 |
/** |
|
17 |
* @return instance of the default DataFactory. |
|
18 |
*/ |
|
19 |
public static DataFactory newInstance() { |
|
20 |
return new DefaultDataFactoryImpl(); |
|
21 |
} |
|
22 |
} |
modules/dnet-modular-index-service/trunk/src/main/java/eu/dnetlib/functionality/index/model/impl/AnySeqImpl.java | ||
---|---|---|
1 |
package eu.dnetlib.functionality.index.model.impl; |
|
2 |
|
|
3 |
import java.util.ArrayList; |
|
4 |
import java.util.Collection; |
|
5 |
import java.util.Date; |
|
6 |
import java.util.Iterator; |
|
7 |
import java.util.List; |
|
8 |
import java.util.ListIterator; |
|
9 |
|
|
10 |
import eu.dnetlib.functionality.index.model.Any; |
|
11 |
import eu.dnetlib.functionality.index.model.AnyMap; |
|
12 |
import eu.dnetlib.functionality.index.model.AnySeq; |
|
13 |
import eu.dnetlib.functionality.index.model.InvalidValueTypeException; |
|
14 |
import eu.dnetlib.functionality.index.model.Value; |
|
15 |
|
|
16 |
/** |
|
17 |
* Sequence of Any objects. |
|
18 |
*/ |
|
19 |
public final class AnySeqImpl extends AbstractAny implements AnySeq { |
|
20 |
|
|
21 |
/** version. */ |
|
22 |
private static final long serialVersionUID = 1L; |
|
23 |
|
|
24 |
/** holds the Any objects of this AnySeq. */ |
|
25 |
private final List<Any> _anyList; |
|
26 |
|
|
27 |
/** |
|
28 |
* constructs a new instance of AnySeqImpl. |
|
29 |
*/ |
|
30 |
AnySeqImpl() { |
|
31 |
super(ValueType.SEQ); |
|
32 |
_anyList = new ArrayList<Any>(); |
|
33 |
} |
|
34 |
|
|
35 |
/** {@inheritDoc} */ |
|
36 |
@Override |
|
37 |
public boolean add(final Any value) { |
|
38 |
if (value == null) { |
|
39 |
throw new IllegalArgumentException("The value of any Any must not be null."); |
|
40 |
} |
|
41 |
return _anyList.add(value); |
|
42 |
} |
|
43 |
|
|
44 |
/** {@inheritDoc} */ |
|
45 |
@Override |
|
46 |
public boolean add(final String e) { |
|
47 |
return add(new ValueImpl(ValueType.STRING, e)); |
|
48 |
} |
|
49 |
|
|
50 |
/** {@inheritDoc} */ |
|
51 |
@Override |
|
52 |
public boolean add(final Number n) { |
|
53 |
if (n instanceof Double) { |
|
54 |
return add(new ValueImpl(ValueType.DOUBLE, n)); |
|
55 |
} else if (n instanceof Long) { |
|
56 |
return add(new ValueImpl(ValueType.LONG, n)); |
|
57 |
} else if (n instanceof Integer || n instanceof Short || n instanceof Byte) { |
|
58 |
return add(new ValueImpl(ValueType.LONG, Long.valueOf(n.longValue()))); |
|
59 |
} else { // default: DOUBLE |
|
60 |
return add(new ValueImpl(ValueType.DOUBLE, Double.valueOf(n.doubleValue()))); |
|
61 |
} |
|
62 |
} |
|
63 |
|
|
64 |
/** {@inheritDoc} */ |
|
65 |
@Override |
|
66 |
public void add(final int index, final Any element) { |
|
67 |
if (element == null) { |
|
68 |
throw new IllegalArgumentException("The value of any Any must not be null."); |
|
69 |
} |
|
70 |
_anyList.add(index, element); |
|
71 |
} |
|
72 |
|
|
73 |
/** {@inheritDoc} */ |
|
74 |
@Override |
|
75 |
public boolean addAll(final Collection<? extends Any> c) { |
|
76 |
return _anyList.addAll(c); |
|
77 |
} |
|
78 |
|
|
79 |
/** {@inheritDoc} */ |
|
80 |
@Override |
|
81 |
public boolean addAll(final int index, final Collection<? extends Any> c) { |
|
82 |
return _anyList.addAll(index, c); |
|
83 |
} |
|
84 |
|
|
85 |
/** {@inheritDoc} */ |
|
86 |
@Override |
|
87 |
public void clear() { |
|
88 |
_anyList.clear(); |
|
89 |
} |
|
90 |
|
|
91 |
/** {@inheritDoc} */ |
|
92 |
@Override |
|
93 |
public boolean contains(final Object o) { |
|
94 |
return _anyList.contains(o); |
|
95 |
} |
|
96 |
|
|
97 |
/** {@inheritDoc} */ |
|
98 |
@Override |
|
99 |
public boolean containsAll(final Collection<?> c) { |
|
100 |
return _anyList.containsAll(c); |
|
101 |
} |
|
102 |
|
|
103 |
/** {@inheritDoc} */ |
|
104 |
@Override |
|
105 |
public Any get(final int index) { |
|
106 |
return _anyList.get(index); |
|
107 |
} |
|
108 |
|
|
109 |
/** {@inheritDoc} */ |
|
110 |
@Override |
|
111 |
public Any set(final int index, final Any element) { |
|
112 |
if (element == null) { |
|
113 |
throw new IllegalArgumentException("The value of any Any must not be null."); |
|
114 |
} |
|
115 |
return _anyList.set(index, element); |
|
116 |
} |
|
117 |
|
|
118 |
/** {@inheritDoc} */ |
|
119 |
@Override |
|
120 |
public int indexOf(final Object o) { |
|
121 |
return _anyList.indexOf(o); |
|
122 |
} |
|
123 |
|
|
124 |
/** {@inheritDoc} */ |
|
125 |
@Override |
|
126 |
public boolean isEmpty() { |
|
127 |
return _anyList.isEmpty(); |
|
128 |
} |
|
129 |
|
|
130 |
/** {@inheritDoc} */ |
|
131 |
@Override |
|
132 |
public Iterator<Any> iterator() { |
|
133 |
return _anyList.iterator(); |
|
134 |
} |
|
135 |
|
|
136 |
/** {@inheritDoc} */ |
|
137 |
@Override |
|
138 |
public int lastIndexOf(final Object o) { |
|
139 |
return _anyList.lastIndexOf(o); |
|
140 |
} |
|
141 |
|
|
142 |
/** {@inheritDoc} */ |
|
143 |
@Override |
|
144 |
public ListIterator<Any> listIterator() { |
|
145 |
return _anyList.listIterator(); |
|
146 |
} |
|
147 |
|
|
148 |
/** {@inheritDoc} */ |
|
149 |
@Override |
|
150 |
public ListIterator<Any> listIterator(final int index) { |
|
151 |
return _anyList.listIterator(index); |
|
152 |
} |
|
153 |
|
|
154 |
/** {@inheritDoc} */ |
|
155 |
@Override |
|
156 |
public Any remove(final int index) { |
|
157 |
return _anyList.remove(index); |
|
158 |
} |
|
159 |
|
|
160 |
/** {@inheritDoc} */ |
|
161 |
@Override |
|
162 |
public boolean remove(final Object o) { |
|
163 |
return _anyList.remove(o); |
|
164 |
} |
|
165 |
|
|
166 |
/** {@inheritDoc} */ |
|
167 |
@Override |
|
168 |
public boolean removeAll(final Collection<?> c) { |
|
169 |
return _anyList.removeAll(c); |
|
170 |
} |
|
171 |
|
|
172 |
/** {@inheritDoc} */ |
|
173 |
@Override |
|
174 |
public boolean retainAll(final Collection<?> c) { |
|
175 |
return _anyList.retainAll(c); |
|
176 |
} |
|
177 |
|
|
178 |
/** {@inheritDoc} */ |
|
179 |
@Override |
|
180 |
public int size() { |
|
181 |
return _anyList.size(); |
|
182 |
} |
|
183 |
|
|
184 |
/** {@inheritDoc} */ |
|
185 |
@Override |
|
186 |
public List<Any> subList(final int fromIndex, final int toIndex) { |
|
187 |
return _anyList.subList(fromIndex, toIndex); |
|
188 |
} |
|
189 |
|
|
190 |
/** {@inheritDoc} */ |
|
191 |
@Override |
|
192 |
public Object[] toArray() { |
|
193 |
return _anyList.toArray(); |
|
194 |
} |
|
195 |
|
|
196 |
/** {@inheritDoc} */ |
|
197 |
@Override |
|
198 |
public <T> T[] toArray(final T[] a) { |
|
199 |
return _anyList.toArray(a); |
|
200 |
} |
|
201 |
|
|
202 |
/** {@inheritDoc} */ |
|
203 |
@Override |
|
204 |
public AnyMap getMap(final int index) { |
|
205 |
final Any anyValue = get(index); |
|
206 |
if (anyValue != null) { |
|
207 |
if (anyValue.isMap()) { |
|
208 |
return (AnyMap) anyValue; |
|
209 |
} else { |
|
210 |
throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to AnyMap."); |
|
211 |
} |
|
212 |
} |
|
213 |
return null; |
|
214 |
} |
|
215 |
|
|
216 |
/** {@inheritDoc} */ |
|
217 |
@Override |
|
218 |
public AnySeq getSeq(final int index) { |
|
219 |
final Any anyValue = get(index); |
|
220 |
if (anyValue != null) { |
|
221 |
if (anyValue.isSeq()) { |
|
222 |
return (AnySeq) anyValue; |
|
223 |
} else { |
|
224 |
throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to AnySeq."); |
|
225 |
} |
|
226 |
} |
|
227 |
return null; |
|
228 |
} |
|
229 |
|
|
230 |
/** {@inheritDoc} */ |
|
231 |
@Override |
|
232 |
public Value getValue(final int index) { |
|
233 |
final Any anyValue = get(index); |
|
234 |
if (anyValue != null) { |
|
235 |
if (anyValue instanceof Value) { |
|
236 |
return (Value) anyValue; |
|
237 |
} else { |
|
238 |
throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to Value."); |
|
239 |
} |
|
240 |
} |
|
241 |
return null; |
|
242 |
} |
|
243 |
|
|
244 |
/** {@inheritDoc} */ |
|
245 |
@Override |
|
246 |
public String getStringValue(final int index) { |
|
247 |
final Any anyValue = get(index); |
|
248 |
if (anyValue != null) { |
|
249 |
if (anyValue instanceof Value) { |
|
250 |
return ((Value) anyValue).asString(); |
|
251 |
} else { |
|
252 |
throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to String."); |
|
253 |
} |
|
254 |
} |
|
255 |
return null; |
|
256 |
} |
|
257 |
|
|
258 |
/** {@inheritDoc} */ |
|
259 |
@Override |
|
260 |
public Double getDoubleValue(final int index) { |
|
261 |
final Any anyValue = get(index); |
|
262 |
if (anyValue != null) { |
|
263 |
if (anyValue instanceof Value) { |
|
264 |
return ((Value) anyValue).asDouble(); |
|
265 |
} else { |
|
266 |
throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to double."); |
|
267 |
} |
|
268 |
} |
|
269 |
return null; |
|
270 |
} |
|
271 |
|
|
272 |
/** {@inheritDoc} */ |
|
273 |
@Override |
|
274 |
public Long getLongValue(final int index) { |
|
275 |
final Any anyValue = get(index); |
|
276 |
if (anyValue != null) { |
|
277 |
if (anyValue instanceof Value) { |
|
278 |
return ((Value) anyValue).asLong(); |
|
279 |
} else { |
|
280 |
throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to long."); |
|
281 |
} |
|
282 |
} |
|
283 |
return null; |
|
284 |
} |
|
285 |
|
|
286 |
/** {@inheritDoc} */ |
|
287 |
@Override |
|
288 |
public Boolean getBooleanValue(final int index) { |
|
289 |
final Any anyValue = get(index); |
|
290 |
if (anyValue != null) { |
|
291 |
if (anyValue instanceof Value) { |
|
292 |
return ((Value) anyValue).asBoolean(); |
|
293 |
} else { |
|
294 |
throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to boolean."); |
|
295 |
} |
|
296 |
} |
|
297 |
return null; |
|
298 |
} |
|
299 |
|
|
300 |
/** {@inheritDoc} */ |
|
301 |
@Override |
|
302 |
public Date getDateValue(final int index) { |
|
303 |
final Any anyValue = get(index); |
|
304 |
if (anyValue != null) { |
|
305 |
if (anyValue instanceof Value) { |
|
306 |
return ((Value) anyValue).asDate(); |
|
307 |
} else { |
|
308 |
throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to Date."); |
|
309 |
} |
|
310 |
} |
|
311 |
return null; |
|
312 |
} |
|
313 |
|
|
314 |
/** {@inheritDoc} */ |
|
315 |
@Override |
|
316 |
public Date getDateTimeValue(final int index) { |
|
317 |
final Any anyValue = get(index); |
|
318 |
if (anyValue != null) { |
|
319 |
if (anyValue instanceof Value) { |
|
320 |
return ((Value) anyValue).asDateTime(); |
|
321 |
} else { |
|
322 |
throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to DateTime."); |
|
323 |
} |
|
324 |
} |
|
325 |
return null; |
|
326 |
} |
|
327 |
|
|
328 |
/** {@inheritDoc} */ |
|
329 |
@Override |
|
330 |
public int hashCode() { |
|
331 |
final int prime = 31; |
|
332 |
int result = 1; |
|
333 |
result = prime * result + (_anyList == null ? 0 : _anyList.hashCode()); |
|
334 |
return result; |
|
335 |
} |
|
336 |
|
|
337 |
/** {@inheritDoc} */ |
|
338 |
@Override |
|
339 |
public boolean equals(final Object obj) { |
|
340 |
if (this == obj) { |
|
341 |
return true; |
|
342 |
} |
|
343 |
if (obj == null) { |
|
344 |
return false; |
|
345 |
} |
|
346 |
if (!(obj instanceof AnySeqImpl)) { |
|
347 |
return false; |
|
348 |
} |
|
349 |
final AnySeqImpl other = (AnySeqImpl) obj; |
|
350 |
if (_anyList == null) { |
|
351 |
if (other._anyList != null) { |
|
352 |
return false; |
|
353 |
} |
|
354 |
} else if (!_anyList.equals(other._anyList)) { |
|
355 |
return false; |
|
356 |
} |
|
357 |
return true; |
|
358 |
} |
|
359 |
|
|
360 |
/** |
|
361 |
* {@inheritDoc} |
|
362 |
*/ |
|
363 |
@Override |
|
364 |
public String toString() { |
|
365 |
return _anyList.toString(); |
|
366 |
} |
|
367 |
|
|
368 |
/** |
|
369 |
* {@inheritDoc} |
|
370 |
* |
|
371 |
* @see org.eclipse.smila.datamodel.Any#asSeq() |
|
372 |
*/ |
|
373 |
@Override |
|
374 |
public AnySeq asSeq() { |
|
375 |
return this; |
|
376 |
} |
|
377 |
|
|
378 |
/** |
|
379 |
* {@inheritDoc} |
|
380 |
* |
|
381 |
* @see org.eclipse.smila.datamodel.AnySeq#asStrings() |
|
382 |
*/ |
|
383 |
@Override |
|
384 |
public List<String> asStrings() { |
|
385 |
final List<String> values = new ArrayList<String>(_anyList.size()); |
|
386 |
for (final Any any : _anyList) { |
|
387 |
values.add(any.asValue().asString()); |
|
388 |
} |
|
389 |
return values; |
|
390 |
} |
|
391 |
|
|
392 |
/** |
|
393 |
* {@inheritDoc} |
|
394 |
* |
|
395 |
* @see org.eclipse.smila.datamodel.AnySeq#asLongs() |
|
396 |
*/ |
|
397 |
@Override |
|
398 |
public List<Long> asLongs() { |
|
399 |
final List<Long> values = new ArrayList<Long>(_anyList.size()); |
|
400 |
for (final Any any : _anyList) { |
|
401 |
values.add(any.asValue().asLong()); |
|
402 |
} |
|
403 |
return values; |
|
404 |
} |
|
405 |
|
|
406 |
} |
|
407 | 0 |
modules/dnet-modular-index-service/trunk/src/main/java/eu/dnetlib/functionality/index/model/impl/AnyMapImpl.java | ||
---|---|---|
1 |
package eu.dnetlib.functionality.index.model.impl; |
|
2 |
|
|
3 |
import java.util.Collection; |
|
4 |
import java.util.Date; |
|
5 |
import java.util.Iterator; |
|
6 |
import java.util.LinkedHashMap; |
|
7 |
import java.util.Map; |
|
8 |
import java.util.Set; |
|
9 |
|
|
10 |
import eu.dnetlib.functionality.index.model.Any; |
|
11 |
import eu.dnetlib.functionality.index.model.AnyMap; |
|
12 |
import eu.dnetlib.functionality.index.model.AnySeq; |
|
13 |
import eu.dnetlib.functionality.index.model.InvalidValueTypeException; |
|
14 |
import eu.dnetlib.functionality.index.model.Value; |
|
15 |
|
|
16 |
/** |
|
17 |
* Class implementing AnyMap. |
|
18 |
*/ |
|
19 |
public final class AnyMapImpl extends AbstractAny implements AnyMap { |
|
20 |
|
|
21 |
/** version. */ |
|
22 |
private static final long serialVersionUID = 1L; |
|
23 |
|
|
24 |
/** internal representation of the AnyMap. */ |
|
25 |
private final Map<String, Any> _anyMap; |
|
26 |
|
|
27 |
/** Constructs a new AnyMapImpl. */ |
|
28 |
AnyMapImpl() { |
|
29 |
super(ValueType.MAP); |
|
30 |
_anyMap = new LinkedHashMap<String, Any>(); |
|
31 |
} |
|
32 |
|
|
33 |
/** {@inheritDoc} */ |
|
34 |
@Override |
|
35 |
public void add(final String key, final Any value) { |
|
36 |
final Any any = _anyMap.get(key); |
|
37 |
AnySeq anySeq = null; |
|
38 |
if (any == null) { |
|
39 |
anySeq = getFactory().createAnySeq(); |
|
40 |
_anyMap.put(key, anySeq); |
|
41 |
} else if (any.isValue() || any.isMap()) { |
|
42 |
anySeq = getFactory().createAnySeq(); |
|
43 |
anySeq.add(any); |
|
44 |
_anyMap.put(key, anySeq); |
|
45 |
} else { // any.isSeq() |
|
46 |
anySeq = (AnySeq) any; |
|
47 |
} |
|
48 |
anySeq.add(value); |
|
49 |
} |
|
50 |
|
|
51 |
/** {@inheritDoc} */ |
|
52 |
@Override |
|
53 |
public void clear() { |
|
54 |
_anyMap.clear(); |
|
55 |
} |
|
56 |
|
|
57 |
/** {@inheritDoc} */ |
|
58 |
@Override |
|
59 |
public boolean isEmpty() { |
|
60 |
return _anyMap.isEmpty(); |
|
61 |
} |
|
62 |
|
|
63 |
/** {@inheritDoc} */ |
|
64 |
@Override |
|
65 |
public Set<String> keySet() { |
|
66 |
return _anyMap.keySet(); |
|
67 |
} |
|
68 |
|
|
69 |
/** {@inheritDoc} */ |
|
70 |
@Override |
|
71 |
public Any put(final String key, final String value) { |
|
72 |
try { |
|
73 |
return put(key, new ValueImpl(ValueType.STRING, value)); |
|
74 |
} catch (final Exception e) { |
|
75 |
throw new IllegalArgumentException("cannot add value for key: " + key, e); |
|
76 |
} |
|
77 |
} |
|
78 |
|
|
79 |
/** {@inheritDoc} */ |
|
80 |
@Override |
|
81 |
public Any put(final String key, final Number value) { |
|
82 |
if (value == null) { |
|
83 |
throw new IllegalArgumentException("The value of any Any must not be null."); |
|
84 |
} |
|
85 |
if (value instanceof Double) { |
|
86 |
return put(key, new ValueImpl(ValueType.DOUBLE, value)); |
|
87 |
} else if (value instanceof Long) { |
|
88 |
return put(key, new ValueImpl(ValueType.LONG, value)); |
|
89 |
} else if (value instanceof Integer) { |
|
90 |
return put(key, new ValueImpl(ValueType.LONG, Long.valueOf(value.longValue()))); |
|
91 |
} else if (value instanceof Short) { |
|
92 |
return put(key, new ValueImpl(ValueType.LONG, Long.valueOf(value.longValue()))); |
|
93 |
} else if (value instanceof Byte) { |
|
94 |
return put(key, new ValueImpl(ValueType.LONG, Long.valueOf(value.longValue()))); |
|
95 |
} else { // default: DOUBLE |
|
96 |
return put(key, new ValueImpl(ValueType.DOUBLE, Double.valueOf(value.doubleValue()))); |
|
97 |
} |
|
98 |
} |
|
99 |
|
|
100 |
/** {@inheritDoc} */ |
|
101 |
@Override |
|
102 |
public Any put(final String key, final Boolean value) { |
|
103 |
return put(key, new ValueImpl(ValueType.BOOLEAN, value)); |
|
104 |
} |
|
105 |
|
|
106 |
/** {@inheritDoc} */ |
|
107 |
@Override |
|
108 |
public Any put(final String key, final Any value) { |
|
109 |
if (value == null) { |
|
110 |
throw new IllegalArgumentException("The value of any Any must not be null."); |
|
111 |
} |
|
112 |
return _anyMap.put(key, value); |
|
113 |
} |
|
114 |
|
|
115 |
/** {@inheritDoc} */ |
|
116 |
@Override |
|
117 |
public int size() { |
|
118 |
return _anyMap.size(); |
|
119 |
} |
|
120 |
|
|
121 |
/** {@inheritDoc} */ |
|
122 |
@Override |
|
123 |
public Collection<Any> values() { |
|
124 |
return _anyMap.values(); |
|
125 |
} |
|
126 |
|
|
127 |
/** {@inheritDoc} */ |
|
128 |
@Override |
|
129 |
public Iterator<Any> iterator() { |
|
130 |
return _anyMap.values().iterator(); |
|
131 |
} |
|
132 |
|
|
133 |
/** {@inheritDoc} */ |
|
134 |
@Override |
|
135 |
public AnyMap getMap(final String key) { |
|
136 |
final Any anyValue = get(key); |
|
137 |
if (anyValue != null) { |
|
138 |
if (anyValue.isMap()) { |
|
139 |
return (AnyMap) anyValue; |
|
140 |
} else { |
|
141 |
throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to AnyMap."); |
|
142 |
} |
|
143 |
} |
|
144 |
return null; |
|
145 |
} |
|
146 |
|
|
147 |
/** {@inheritDoc} */ |
|
148 |
@Override |
|
149 |
public AnySeq getSeq(final String key) { |
|
150 |
final Any anyValue = get(key); |
|
151 |
if (anyValue != null) { |
|
152 |
if (anyValue.isSeq()) { |
|
153 |
return (AnySeq) anyValue; |
|
154 |
} else { |
|
155 |
throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to AnySeq."); |
|
156 |
} |
|
157 |
} |
|
158 |
return null; |
|
159 |
} |
|
160 |
|
|
161 |
/** {@inheritDoc} */ |
|
162 |
@Override |
|
163 |
public Value getValue(final String key) { |
|
164 |
final Any anyValue = get(key); |
|
165 |
if (anyValue != null) { |
|
166 |
if (anyValue instanceof Value) { |
|
167 |
return (Value) anyValue; |
|
168 |
} else { |
|
169 |
throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to Value."); |
|
170 |
} |
|
171 |
} |
|
172 |
return null; |
|
173 |
} |
|
174 |
|
|
175 |
/** {@inheritDoc} */ |
|
176 |
@Override |
|
177 |
public String getStringValue(final String key) { |
|
178 |
final Any anyValue = get(key); |
|
179 |
if (anyValue != null) { |
|
180 |
if (anyValue instanceof Value) { |
|
181 |
return ((Value) anyValue).asString(); |
|
182 |
} else { |
|
183 |
throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to String."); |
|
184 |
} |
|
185 |
} |
|
186 |
return null; |
|
187 |
} |
|
188 |
|
|
189 |
/** {@inheritDoc} */ |
|
190 |
@Override |
|
191 |
public Double getDoubleValue(final String key) { |
|
192 |
final Any anyValue = get(key); |
|
193 |
if (anyValue != null) { |
|
194 |
if (anyValue instanceof Value) { |
|
195 |
return ((Value) anyValue).asDouble(); |
|
196 |
} else { |
|
197 |
throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to double."); |
|
198 |
} |
|
199 |
} |
|
200 |
return null; |
|
201 |
} |
|
202 |
|
|
203 |
/** {@inheritDoc} */ |
|
204 |
@Override |
|
205 |
public Long getLongValue(final String key) { |
|
206 |
final Any anyValue = get(key); |
|
207 |
if (anyValue != null) { |
|
208 |
if (anyValue instanceof Value) { |
|
209 |
return ((Value) anyValue).asLong(); |
|
210 |
} else { |
|
211 |
throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to long."); |
|
212 |
} |
|
213 |
} |
|
214 |
return null; |
|
215 |
} |
|
216 |
|
|
217 |
/** {@inheritDoc} */ |
|
218 |
@Override |
|
219 |
public Boolean getBooleanValue(final String key) { |
|
220 |
final Any anyValue = get(key); |
|
221 |
if (anyValue != null) { |
|
222 |
if (anyValue instanceof Value) { |
|
223 |
return ((Value) anyValue).asBoolean(); |
|
224 |
} else { |
|
225 |
throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to boolean."); |
|
226 |
} |
|
227 |
} |
|
228 |
return null; |
|
229 |
} |
|
230 |
|
|
231 |
/** {@inheritDoc} */ |
|
232 |
@Override |
|
233 |
public Date getDateValue(final String key) { |
|
234 |
final Any anyValue = get(key); |
|
235 |
if (anyValue != null) { |
|
236 |
if (anyValue instanceof Value) { |
|
237 |
return ((Value) anyValue).asDate(); |
|
238 |
} else { |
|
239 |
throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to Date."); |
|
240 |
} |
|
241 |
} |
|
242 |
return null; |
|
243 |
} |
|
244 |
|
|
245 |
/** {@inheritDoc} */ |
|
246 |
@Override |
|
247 |
public Date getDateTimeValue(final String key) { |
|
248 |
final Any anyValue = get(key); |
|
249 |
if (anyValue != null) { |
|
250 |
if (anyValue instanceof Value) { |
|
251 |
return ((Value) anyValue).asDateTime(); |
|
252 |
} else { |
|
253 |
throw new InvalidValueTypeException("Cannot convert value of type '" + anyValue.getValueType() + "' to DateTime."); |
|
254 |
} |
|
255 |
} |
|
256 |
return null; |
|
257 |
} |
|
258 |
|
|
259 |
/** {@inheritDoc} */ |
|
260 |
@Override |
|
261 |
public int hashCode() { |
|
262 |
final int prime = 31; |
|
263 |
int result = 1; |
|
264 |
result = prime * result + (_anyMap == null ? 0 : _anyMap.hashCode()); |
|
265 |
return result; |
|
266 |
} |
|
267 |
|
|
268 |
/** {@inheritDoc} */ |
|
269 |
@Override |
|
270 |
public boolean equals(final Object obj) { |
|
271 |
if (this == obj) { |
|
272 |
return true; |
|
273 |
} |
|
274 |
if (obj == null) { |
|
275 |
return false; |
|
276 |
} |
|
277 |
if (getClass() != obj.getClass()) { |
|
278 |
return false; |
|
279 |
} |
|
280 |
final AnyMapImpl other = (AnyMapImpl) obj; |
|
281 |
if (_anyMap == null) { |
|
282 |
if (other._anyMap != null) { |
|
283 |
return false; |
|
284 |
} |
|
285 |
} else if (!_anyMap.equals(other._anyMap)) { |
|
286 |
return false; |
|
287 |
} |
|
288 |
return true; |
|
289 |
} |
|
290 |
|
|
291 |
/** |
|
292 |
* {@inheritDoc} |
|
293 |
*/ |
|
294 |
@Override |
|
295 |
public String toString() { |
|
296 |
return _anyMap.toString(); |
|
297 |
} |
|
298 |
|
|
299 |
/** {@inheritDoc} */ |
|
300 |
@Override |
|
301 |
public boolean containsKey(final Object key) { |
|
302 |
return _anyMap.containsKey(key); |
|
303 |
} |
|
304 |
|
|
305 |
/** {@inheritDoc} */ |
|
306 |
@Override |
|
307 |
public boolean containsValue(final Object value) { |
|
308 |
return _anyMap.containsValue(value); |
|
309 |
} |
|
310 |
|
|
311 |
/** {@inheritDoc} */ |
|
312 |
@Override |
|
313 |
public Any get(final Object key) { |
|
314 |
return _anyMap.get(key); |
|
315 |
} |
|
316 |
|
|
317 |
/** {@inheritDoc} */ |
|
318 |
@Override |
|
319 |
public void putAll(final Map<? extends String, ? extends Any> map) { |
|
320 |
_anyMap.putAll(map); |
|
321 |
} |
|
322 |
|
|
323 |
/** {@inheritDoc} */ |
|
324 |
@Override |
|
325 |
public Any remove(final Object key) { |
|
326 |
return _anyMap.remove(key); |
|
327 |
} |
|
328 |
|
|
329 |
/** {@inheritDoc} */ |
|
330 |
@Override |
|
331 |
public Set<java.util.Map.Entry<String, Any>> entrySet() { |
|
332 |
return _anyMap.entrySet(); |
|
333 |
} |
|
334 |
|
|
335 |
/** |
|
336 |
* {@inheritDoc} |
|
337 |
* |
|
338 |
* @see org.eclipse.smila.datamodel.impl.AbstractAny#asMap() |
|
339 |
*/ |
|
340 |
@Override |
|
341 |
public AnyMap asMap() { |
|
342 |
return this; |
|
343 |
} |
|
344 |
|
|
345 |
/** |
|
346 |
* {@inheritDoc} |
Also available in: Unified diff
changed implementation using module dnet-index-client