public void doJSONRequest(Context context, HttpServletRequest req,
HttpServletResponse resp) throws AuthorizeException, IOException
{
Gson json = new Gson();
String suuid = req.getParameter("s_uuid");
SubmissionLookupDTO subDTO = service.getSubmissionLookupDTO(req, suuid);
// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(req);
if ("identifiers".equalsIgnoreCase(req.getParameter("type")))
{
Map<String, Set<String>> identifiers = new HashMap<String, Set<String>>();
Enumeration e = req.getParameterNames();
while (e.hasMoreElements())
{
String parameterName = (String) e.nextElement();
String parameterValue = req.getParameter(parameterName);
if (parameterName.startsWith("identifier_")
&& StringUtils.isNotBlank(parameterValue))
{
Set<String> set = new HashSet<String>();
set.add(parameterValue);
identifiers.put(
parameterName.substring("identifier_".length()),
set);
}
}
List<ItemSubmissionLookupDTO> result = new ArrayList<ItemSubmissionLookupDTO>();
TransformationEngine transformationEngine = service
.getPhase1TransformationEngine();
if (transformationEngine != null)
{
MultipleSubmissionLookupDataLoader dataLoader = (MultipleSubmissionLookupDataLoader) transformationEngine
.getDataLoader();
dataLoader.setIdentifiers(identifiers);
try
{
SubmissionLookupOutputGenerator outputGenerator = (SubmissionLookupOutputGenerator) transformationEngine
.getOutputGenerator();
outputGenerator.setDtoList(new ArrayList<ItemSubmissionLookupDTO>());
log.debug("BTE transformation is about to start!");
transformationEngine.transform(new TransformationSpec());
log.debug("BTE transformation finished!");
result = outputGenerator.getDtoList();
}
catch (BadTransformationSpec e1)
{
log.error(e1.getMessage(), e1);
}
catch (MalformedSourceException e1)
{
log.error(e1.getMessage(), e1);
}
}
subDTO.setItems(result);
service.storeDTOs(req, suuid, subDTO);
List<Map<String, Object>> dto = getLightResultList(result);
JsonElement tree = json.toJsonTree(dto);
JsonObject jo = new JsonObject();
jo.add("result", tree);
resp.getWriter().write(jo.toString());
}
else if ("search".equalsIgnoreCase(req.getParameter("type")))
{
String title = req.getParameter("title");
String author = req.getParameter("authors");
int year = UIUtil.getIntParameter(req, "year");
Map<String, Set<String>> searchTerms = new HashMap<String, Set<String>>();
Set<String> tmp1 = new HashSet<String>();
tmp1.add(title);
Set<String> tmp2 = new HashSet<String>();
tmp2.add(author);
Set<String> tmp3 = new HashSet<String>();
tmp3.add(String.valueOf(year));
searchTerms.put("title", tmp1);
searchTerms.put("authors", tmp2);
searchTerms.put("year", tmp3);
List<ItemSubmissionLookupDTO> result = new ArrayList<ItemSubmissionLookupDTO>();
TransformationEngine transformationEngine = service
.getPhase1TransformationEngine();
if (transformationEngine != null)
{
MultipleSubmissionLookupDataLoader dataLoader = (MultipleSubmissionLookupDataLoader) transformationEngine
.getDataLoader();
dataLoader.setSearchTerms(searchTerms);
try
{
SubmissionLookupOutputGenerator outputGenerator = (SubmissionLookupOutputGenerator) transformationEngine
.getOutputGenerator();
outputGenerator.setDtoList(new ArrayList<ItemSubmissionLookupDTO>());
log.debug("BTE transformation is about to start!");
transformationEngine.transform(new TransformationSpec());
log.debug("BTE transformation finished!");
result = outputGenerator.getDtoList();
}
catch (BadTransformationSpec e1)
{
log.error(e1.getMessage(), e1);
}
catch (MalformedSourceException e1)
{
log.error(e1.getMessage(), e1);
}
}
subDTO.setItems(result);
service.storeDTOs(req, suuid, subDTO);
List<Map<String, Object>> dto = getLightResultList(result);
JsonElement tree = json.toJsonTree(dto);
JsonObject jo = new JsonObject();
jo.add("result", tree);
resp.getWriter().write(jo.toString());
}
else if ("details".equalsIgnoreCase(req.getParameter("type")))
{
String i_uuid = req.getParameter("i_uuid");
Map<String, Object> dto = getDetails(subDTO.getLookupItem(i_uuid),
context);
JsonElement tree = json.toJsonTree(dto);
JsonObject jo = new JsonObject();
jo.add("result", tree);
resp.getWriter().write(jo.toString());
}
else if (isMultipart)
{
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Parse the request
Map<String, String> valueMap = new HashMap<String, String>();
InputStream io = null;
// Parse the request
List<FileItem> iter;
String filename = null;
try
{
iter = upload.parseRequest(req);
for (FileItem item : iter)
{
String name = item.getFieldName();
InputStream stream = item.getInputStream();
if (item.isFormField())
{
String value = Streams.asString(stream);
valueMap.put(name, value);
}
else
{
io = stream;
}
}
}
catch (FileUploadException e)
{
throw new IOException(e);
}
suuid = valueMap.get("s_uuid");
subDTO = service.getSubmissionLookupDTO(req, suuid);
List<ItemSubmissionLookupDTO> result = new ArrayList<ItemSubmissionLookupDTO>();
TransformationEngine transformationEngine = service
.getPhase1TransformationEngine();
if (transformationEngine != null)
{
MultipleSubmissionLookupDataLoader dataLoader = (MultipleSubmissionLookupDataLoader) transformationEngine
.getDataLoader();
String tempDir = (ConfigurationManager
.getProperty("upload.temp.dir") != null) ? ConfigurationManager
.getProperty("upload.temp.dir") : System
.getProperty("java.io.tmpdir");
File uploadDir = new File(tempDir);
if (!uploadDir.exists()) {
if (!uploadDir.mkdir()) {
uploadDir = null;
}
}
File file = File.createTempFile("submissionlookup-loader",
".temp",
uploadDir);
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(file));
Utils.bufferedCopy(io, out);
dataLoader.setFile(file.getAbsolutePath(),
valueMap.get("provider_loader"));
try
{
SubmissionLookupOutputGenerator outputGenerator = (SubmissionLookupOutputGenerator) transformationEngine
.getOutputGenerator();
outputGenerator.setDtoList(new ArrayList<ItemSubmissionLookupDTO>());
log.debug("BTE transformation is about to start!");
transformationEngine.transform(new TransformationSpec());
log.debug("BTE transformation finished!");
result = outputGenerator.getDtoList();
}
catch (BadTransformationSpec e1)
{
log.error(e1.getMessage(), e1);
}
catch (MalformedSourceException e1)
{
log.error(e1.getMessage(), e1);
}
finally
{
file.delete();
}
}
subDTO.setItems(result);
service.storeDTOs(req, suuid, subDTO);
List<Map<String, Object>> dto = getLightResultList(result);
if (valueMap.containsKey("skip_loader"))
{
if (valueMap.get("skip_loader").equals("true"))