-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
BatchMetadataImportServlet.java
276 lines (226 loc) · 8.36 KB
/
BatchMetadataImportServlet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.app.webui.servlet;
import java.io.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.jstl.fmt.LocaleSupport;
import org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.dspace.app.webui.util.JSPManager;
import org.dspace.app.webui.util.FileUploadRequest;
import org.dspace.app.itemimport.BTEBatchImportService;
import org.dspace.app.itemimport.ItemImport;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Collection;
import org.dspace.core.*;
import org.dspace.utils.DSpace;
import org.elasticsearch.common.collect.Lists;
/**
* Servlet to batch import metadata via the BTE
*
* @author Stuart Lewis
*/
public class BatchMetadataImportServlet extends DSpaceServlet
{
/** log4j category */
private static Logger log = Logger.getLogger(BatchMetadataImportServlet.class);
/**
* Respond to a post request for metadata bulk importing via csv
*
* @param context a DSpace Context object
* @param request the HTTP request
* @param response the HTTP response
*
* @throws ServletException
* @throws IOException
* @throws SQLException
* @throws AuthorizeException
*/
protected void doDSPost(Context context, HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException,
SQLException, AuthorizeException
{
int type = -1;
String typeS = request.getParameter("type");
if (typeS != null){
try {
type = Integer.parseInt(typeS);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//Get all collections
List<Collection> collections = null;
String colIdS = request.getParameter("colId");
if (colIdS!=null){
collections = new ArrayList<Collection>();
collections.add(Collection.find(context, Integer.parseInt(colIdS)));
}
else {
collections = Arrays.asList(Collection.findAll(context));
}
request.setAttribute("collections", collections);
if (type != 0){
// First, see if we have a multipart request (uploading a metadata file)
String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") != -1))
{
String message = null;
// Process the file uploaded
try {
// Wrap multipart request to get the submission info
FileUploadRequest wrapper = new FileUploadRequest(request);
File f = wrapper.getFile("file");
int colId = Integer.parseInt(wrapper.getParameter("collection"));
Collection collection = Collection.find(context, colId);
String inputType = wrapper.getParameter("inputType");
try {
ItemImport.processUploadableImport(f, new Collection[]{collection}, inputType, context);
request.setAttribute("has-error", "false");
} catch (Exception e) {
request.setAttribute("has-error", "true");
message = e.getMessage();
e.printStackTrace();
}
} catch (FileSizeLimitExceededException e) {
request.setAttribute("has-error", "true");
message = e.getMessage();
e.printStackTrace();
} catch (Exception e) {
request.setAttribute("has-error", "true");
message = e.getMessage();
e.printStackTrace();
}
//Get all the possible data loaders from the Spring configuration
BTEBatchImportService dls = new DSpace().getSingletonService(BTEBatchImportService.class);
List<String> inputTypes =dls.getFileDataLoaders();
request.setAttribute("input-types", inputTypes);
request.setAttribute("message", message);
// Show the upload screen
JSPManager.showJSP(request, response, "/dspace-admin/batchmetadataimport.jsp");
}
else
{
request.setAttribute("has-error", "true");
// Show the upload screen
JSPManager.showJSP(request, response, "/dspace-admin/batchmetadataimport.jsp");
}
}
else {
request.setAttribute("type", type);
String message = null;
String uploadId = request.getParameter("uploadid");
if (uploadId != null){
request.setAttribute("uploadid", uploadId);
}
String zipurl = request.getParameter("zipurl");
if (StringUtils.isEmpty(zipurl)) {
request.setAttribute("has-error", "true");
}
else {
Collection owningCollection = null;
if (request.getParameter("collection") != null) {
int colId = Integer.parseInt(request.getParameter("collection"));
if (colId > 0)
owningCollection = Collection.find(context, colId);
}
String[] reqCollections = request.getParameterValues("collections");
if (owningCollection==null && reqCollections != null && reqCollections.length > 0){
request.setAttribute("has-error", "true");
Locale locale = request.getLocale();
ResourceBundle msgs = ResourceBundle.getBundle("Messages", locale);
String ms = msgs.getString("jsp.layout.navbar-admin.batchimport.owningcollection");
if (ms == null){
ms = "???jsp.layout.navbar-admin.batchimport.owningcollection???";
}
request.setAttribute("message", ms);
JSPManager.showJSP(request, response, "/dspace-admin/batchimport.jsp");
return;
}
try {
//Decide if it is a new upload or a resume one!
if (uploadId != null){ //resume upload
ItemImport.processResumableImport(zipurl, owningCollection, reqCollections, uploadId, context);
}
else { //New upload
ItemImport.processUploadableImport(zipurl, owningCollection, reqCollections, context);
}
request.setAttribute("has-error", "false");
} catch (Exception e) {
request.setAttribute("has-error", "true");
message = e.getMessage();
e.printStackTrace();
}
}
request.setAttribute("message", message);
JSPManager.showJSP(request, response, "/dspace-admin/batchimport.jsp");
}
}
/**
* GET request is only ever used to show the upload form
*
* @param context
* a DSpace Context object
* @param request
* the HTTP request
* @param response
* the HTTP response
*
* @throws ServletException
* @throws IOException
* @throws SQLException
* @throws AuthorizeException
*/
protected void doDSGet(Context context, HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException,
SQLException, AuthorizeException
{
String typeS = request.getParameter("type");
int type = -1;
try {
type = Integer.parseInt(typeS);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.setAttribute("type", type);
//Get all collections
List<Collection> collections = null;
String colIdS = request.getParameter("colId");
if (colIdS!=null){
collections = new ArrayList<Collection>();
collections.add(Collection.find(context, Integer.parseInt(colIdS)));
}
else {
collections = Arrays.asList(Collection.findAll(context));
}
request.setAttribute("collections", collections);
if (type==0){
// Show the upload screen
JSPManager.showJSP(request, response, "/dspace-admin/batchimport.jsp");
}
else {
//Get all the possible data loaders from the Spring configuration
BTEBatchImportService dls = new DSpace().getSingletonService(BTEBatchImportService.class);
List<String> inputTypes = dls.getFileDataLoaders();
request.setAttribute("input-types", inputTypes);
// Show the upload screen
JSPManager.showJSP(request, response, "/dspace-admin/batchmetadataimport.jsp");
}
}
}