/**
   * 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
   */
  @Override
  protected void doDSGet(Context context, HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException, SQLException, AuthorizeException {
    // Get all collections
    List<Collection> collections = null;
    String colIdS = request.getParameter("colId");
    if (colIdS != null) {
      collections = new ArrayList<>();
      collections.add(collectionService.findByIdOrLegacyId(context, colIdS));

    } else {
      collections = collectionService.findAll(context);
    }

    request.setAttribute("collections", collections);

    // 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/batchimport.jsp");
  }
  /**
   * 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
   */
  @Override
  protected void doDSPost(Context context, HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException, SQLException, AuthorizeException {

    // 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);

        String inputType = wrapper.getParameter("inputType");
        List<String> reqCollectionsTmp =
            getRepeatedParameter(wrapper, "collections", "collections");
        String[] reqCollections = new String[reqCollectionsTmp.size()];
        reqCollectionsTmp.toArray(reqCollections);

        // Get all collections
        List<Collection> collections = null;
        String colIdS = wrapper.getParameter("colId");
        if (colIdS != null) {
          collections = new ArrayList<>();
          collections.add(collectionService.findByIdOrLegacyId(context, colIdS));

        } else {
          collections = collectionService.findAll(context);
        }
        request.setAttribute("collections", collections);

        Collection owningCollection = null;
        if (wrapper.getParameter("collection") != null) {
          owningCollection =
              collectionService.findByIdOrLegacyId(context, wrapper.getParameter("collection"));
        }

        // 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);

        if (reqCollectionsTmp != null) request.setAttribute("otherCollections", reqCollectionsTmp);
        if (owningCollection != null)
          request.setAttribute("owningCollection", owningCollection.getID());
        request.setAttribute("inputType", inputType);

        File f = null;
        String zipurl = null;

        if (inputType.equals("saf")) {
          zipurl = wrapper.getParameter("zipurl");
          if (StringUtils.isEmpty(zipurl)) {
            request.setAttribute("has-error", "true");
            Locale locale = request.getLocale();
            ResourceBundle msgs = ResourceBundle.getBundle("Messages", locale);
            try {
              message = msgs.getString("jsp.layout.navbar-admin.batchimport.fileurlempty");
            } catch (Exception e) {
              message = "???jsp.layout.navbar-admin.batchimport.fileurlempty???";
            }

            request.setAttribute("message", message);

            JSPManager.showJSP(request, response, "/dspace-admin/batchimport.jsp");

            return;
          }
        } else {
          f = wrapper.getFile("file");
          if (f == null) {
            request.setAttribute("has-error", "true");
            Locale locale = request.getLocale();
            ResourceBundle msgs = ResourceBundle.getBundle("Messages", locale);
            try {
              message = msgs.getString("jsp.layout.navbar-admin.batchimport.fileempty");
            } catch (Exception e) {
              message = "???jsp.layout.navbar-admin.batchimport.fileempty???";
            }

            request.setAttribute("message", message);

            JSPManager.showJSP(request, response, "/dspace-admin/batchimport.jsp");

            return;
          } else if (owningCollection == null) {
            request.setAttribute("has-error", "true");
            Locale locale = request.getLocale();
            ResourceBundle msgs = ResourceBundle.getBundle("Messages", locale);
            try {
              message = msgs.getString("jsp.layout.navbar-admin.batchimport.owningcollectionempty");
            } catch (Exception e) {
              message = "???jsp.layout.navbar-admin.batchimport.owningcollectionempty???";
            }

            request.setAttribute("message", message);

            JSPManager.showJSP(request, response, "/dspace-admin/batchimport.jsp");

            return;
          }
        }

        String uploadId = wrapper.getParameter("uploadId");
        if (uploadId != null) {
          request.setAttribute("uploadId", uploadId);
        }

        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 {
          String finalInputType = "saf";
          String filePath = zipurl;
          if (f != null) {
            finalInputType = inputType;
            filePath = f.getAbsolutePath();
          }

          itemImportService.processUIImport(
              filePath, owningCollection, reqCollections, uploadId, finalInputType, context, true);

          request.setAttribute("has-error", "false");
          request.setAttribute("uploadId", null);

        } 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();
      }

      request.setAttribute("message", message);

      // Show the upload screen
      JSPManager.showJSP(request, response, "/dspace-admin/batchimport.jsp");

    } else {
      request.setAttribute("has-error", "true");

      // Show the upload screen
      JSPManager.showJSP(request, response, "/dspace-admin/batchimport.jsp");
    }
  }