Project

General

Profile

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

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

    
7
import eu.dnetlib.xml.database.XMLDatabase;
8
import org.apache.commons.logging.Log;
9
import org.apache.commons.logging.LogFactory;
10
import org.xmldb.api.base.XMLDBException;
11

    
12
/**
13
 * ISStore service implementation.
14
 * 
15
 * @author Sandro & Michele
16
 * 
17
 */
18

    
19
public class ISStoreImpl  implements ISStore { // NOPMD by marko on 11/24/08
20

    
21
	/**
22
	 * logger.
23
	 */
24
	private static final Log log = LogFactory.getLog(ISStoreImpl.class); // NOPMD by marko on 11/24/08 4:46 PM
25

    
26
	/**
27
	 * xml database used by the ISStore.
28
	 */
29
	private XMLDatabase xmlDatabase;
30

    
31

    
32
	@Override
33
	public boolean createFileColl(final String fileColl) throws ISStoreException {
34
		try {
35
			xmlDatabase.createCollection(fileColl);
36
		} catch (XMLDBException e) {
37
			throw new ISStoreException(e);
38
		}
39
		return true;
40
	}
41

    
42

    
43

    
44
	@Override
45
	public boolean deleteFileColl(final String fileColl) throws ISStoreException {
46
		try {
47
			xmlDatabase.removeCollection(fileColl);
48
		} catch (XMLDBException e) {
49
			throw new ISStoreException(e);
50
		}
51
		return true;
52
	}
53

    
54

    
55
	@Override
56
	public boolean deleteXML(final String fileName, final String fileColl) throws ISStoreException {
57
		try {
58
			return xmlDatabase.remove(fileName, fileColl);
59
		} catch (XMLDBException e) {
60
			throw new ISStoreException(e);
61
		}
62
	}
63

    
64

    
65
	@Override
66
	public boolean executeXUpdate(final String query) throws ISStoreException {
67
		try {
68
			xmlDatabase.xupdate(query);
69
		} catch (XMLDBException e){
70
			throw new ISStoreException("Error while executing xupdate "+query, e);
71
		}
72
		return true;
73
	}
74

    
75

    
76
	@Override
77
	public List<String> getFileColls() throws ISStoreException {
78
		try {
79
			return xmlDatabase.listChildCollections(xmlDatabase.getRootCollection());
80
		} catch (XMLDBException e) {
81
			throw new ISStoreException(e);
82
		}
83
	}
84

    
85

    
86
	@Override
87
	public List<String> getFileNames(final String fileColl) throws ISStoreException {
88
		try {
89
			return xmlDatabase.list(fileColl);
90
		} catch (XMLDBException e) {
91
			throw new ISStoreException(e);
92
		}
93
	}
94

    
95
	@Override
96
	public String getXML(final String fileName, final String fileColl) throws ISStoreException {
97
		try {
98
			return xmlDatabase.read(fileName, fileColl);
99
		} catch (XMLDBException e) {
100
			throw new ISStoreException(e);
101
		}
102
	}
103

    
104
	@Override
105
	public String getXMLbyQuery(final String query) throws ISStoreException {
106
		log.debug(query);
107

    
108
		try {
109
			final Iterator<String> xquery = xmlDatabase.xquery(query);
110
			if (xquery!=null && xquery.hasNext()){
111
				return xquery.next();
112
			}
113
			return null;
114
		} catch (XMLDBException e) {
115
			throw new ISStoreException(e);
116
		}
117
	}
118

    
119

    
120
	@Override
121
	public boolean insertXML(final String fileName, final String fileColl, final String file) throws ISStoreException {
122
		try {
123
			xmlDatabase.create(fileName, fileColl, file);
124
		} catch (XMLDBException e) {
125
			throw new ISStoreException(e);
126
		}
127
		return true;
128
	}
129

    
130

    
131
	@Override
132
	public List<String> quickSearchXML(final String query) throws ISStoreException {
133
		log.debug(query);
134
		try {
135
			final Iterator<String> res = xmlDatabase.xquery(query);
136
			if (res == null) return new ArrayList<>();
137
			final ArrayList<String> ans = new ArrayList<>();
138
			res.forEachRemaining(ans::add);
139
			return ans;
140
		} catch (XMLDBException e) {
141
			log.fatal("searching", e);
142
			throw new ISStoreException(e);
143
		}
144
	}
145

    
146
	@Override
147
	public boolean updateXML(final String fileName, final String fileColl, final String file) throws ISStoreException {
148
		try {
149
			xmlDatabase.update(fileName, fileColl, file);
150
		} catch (XMLDBException e) {
151
			throw new ISStoreException(e);
152
		}
153
		return true;
154
	}
155

    
156
	@Override
157
	public String backup() throws ISStoreException {
158
		try {
159
			return xmlDatabase.backup();
160
		} catch (Exception e) {
161
			throw new ISStoreException(e);
162
		}
163
	}
164

    
165
	public XMLDatabase getXmlDatabase() {
166
		return xmlDatabase;
167
	}
168

    
169
	public void setXmlDatabase(final XMLDatabase xmlDatabase) {
170
		this.xmlDatabase = xmlDatabase;
171
	}
172

    
173
}
(3-3/3)