public static void savePage(Long listingId, String[] p3q1, String return_to_summary) {

    Listing listing = Listing.getByListingId(listingId);

    notFoundIfNull(listing);

    if (!listing.supplierId.equals(getSupplierId())) {
      Logger.error(
          "Supplier id of listing did not match the logged in supplier. Expected: "
              + listing.supplierId
              + ", Found: "
              + getSupplierId());
      notFound();
    }

    if (listing.serviceSubmitted) {
      Logger.info("Trying to edit a submitted service; redirect to summary page.");
      redirect(listing.summaryPageUrl());
    }

    // Validate all fields on this page requiring validation
    validation.required(p3q1);
    validation
        .isTrue(ValidationUtils.stringArrayValuesAreNotTooLong(p3q1, 30))
        .key("p3q1")
        .message("Invalid values");

    if (validation.hasErrors()) {
      flash.put("body", params.get("body"));
      for (Map.Entry<String, List<Error>> entry : validation.errorsMap().entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue().get(0).message();

        flash.put(key, value);
      }
      Logger.info(
          String.format(
              "Validation errors: %s; reloading page.", validation.errorsMap().toString()));
      if (return_to_summary.contains("yes")) {
        redirect(String.format("/page/%d/%d?return_to_summary=yes", PAGE_ID, listing.id));
      } else {
        redirect(String.format("/page/%d/%d", PAGE_ID, listing.id));
      }
    }

    // Save the form data as a Page into the correct page index
    Map<String, String> pageResponses = new HashMap<String, String>();
    Gson gson = new Gson();
    pageResponses.put("p3q1", gson.toJson(p3q1));
    saveResponseToPage(PAGE_ID, listing, pageResponses);
    if (return_to_summary.contains("yes")) {
      redirect(listing.summaryPageUrl(PAGE_ID));
    } else {
      redirect(listing.nextPageUrl(PAGE_ID, listing.id));
    }
  }
  public static void savePage(
      Long listingId,
      String p34q1,
      String p34q2,
      String p34q1assurance,
      String p34q2assurance,
      String return_to_summary) {

    Listing listing = Listing.getByListingId(listingId);

    notFoundIfNull(listing);

    if (!listing.supplierId.equals(getSupplierId())) {
      Logger.error(
          "Supplier id of listing did not match the logged in supplier. Expected: "
              + listing.supplierId
              + ", Found: "
              + getSupplierId());
      notFound();
    }

    if (listing.serviceSubmitted) {
      Logger.info("Trying to edit a submitted service; redirect to summary page.");
      redirect(listing.summaryPageUrl());
    }

    // Validate all fields on this page requiring validation
    validation.required(p34q1).key("p34q1");
    validation.maxSize(p34q1, 10);
    validation.required(p34q2).key("p34q2");
    validation.maxSize(p34q2, 10);
    validation.required(p34q1assurance).key("p34q1");
    validation.maxSize(p34q1assurance, 60);
    validation.required(p34q2assurance).key("p34q2");
    validation.maxSize(p34q2assurance, 60);

    if (validation.hasErrors()) {
      flash.put("body", params.get("body"));
      for (Map.Entry<String, List<Error>> entry : validation.errorsMap().entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue().get(0).message();

        flash.put(key, value);
      }
      Logger.info(
          String.format(
              "Validation errors: %s; reloading page.", validation.errorsMap().toString()));
      if (return_to_summary.contains("yes")) {
        redirect(String.format("/page/%d/%d?return_to_summary=yes", PAGE_ID, listing.id));
      } else {
        redirect(String.format("/page/%d/%d", PAGE_ID, listing.id));
      }
    }

    Map<String, String> pageResponses = new HashMap<String, String>();
    pageResponses.put("p34q1", p34q1);
    pageResponses.put("p34q2", p34q2);
    pageResponses.put("p34q1assurance", p34q1assurance);
    pageResponses.put("p34q2assurance", p34q2assurance);
    saveResponseToPage(PAGE_ID, listing, pageResponses);
    if (return_to_summary.contains("yes")) {
      redirect(listing.summaryPageUrl(PAGE_ID));
    } else {
      redirect(listing.nextPageUrl(PAGE_ID, listing.id));
    }
  }
Example #3
0
  public static void export(Long project_id) {
    // Prepare data
    Project p = getActiveProject();
    if (p == null) {
      notFound("Project", project_id);
    }
    List<Listing> listings = p.listings;
    List<String> sheetNames = new ArrayList();
    List maps = new ArrayList();
    for (Listing l : listings) {
      Map map = new HashMap();
      map.put("project", VProject.createFromProject(p));

      sheetNames.add("(" + l.id + ") " + l.listingName);

      // l.items = Item.findByListing(l, "", l.sort, 1000);
      map.put("listing", l.listingName);
      map.put("items", Item.findByListing(l, "", l.sort, 1000));
      List<ItemField> selected_fields = l.getItemFields();
      List<ItemField> all_fields = Item.getItemFields();
      List<ItemField> other_fields = new ArrayList<ItemField>();

      for (ItemField i : all_fields) {
        if (!l.hasField(i.fieldName)) {
          other_fields.add(i);
        }
      }
      ;

      map.put("selected_fields", selected_fields);
      map.put("other_fields", other_fields);

      maps.add(map);
    }

    // Prepare template and data for excel export
    String filepath =
        Play.applicationPath
            + File.separator
            + "app"
            + File.separator
            + "views"
            + File.separator
            + "projects"
            + File.separator
            + "export.xls";
    VirtualFile templateFile = VirtualFile.open(filepath);
    InputStream inputStream = templateFile.inputstream();

    MiscUtil.ConsoleLog("use sync excel rendering");
    long start = System.currentTimeMillis();
    try {
      Workbook workbook =
          new XLSTransformer()
              .transformMultipleSheetsList(inputStream, maps, sheetNames, "map", new HashMap(), 0);
      workbook.write(response.out);
      inputStream.close();
      MiscUtil.ConsoleLog("Excel sync render takes " + (System.currentTimeMillis() - start));
    } catch (Exception e) {
      MiscUtil.ConsoleLog("ERROR: " + e.getMessage());
      error();
    }

    // Set correct response header
    try {
      response.setHeader(
          "Content-Disposition",
          "attachment; filename=" + (new URLCodec("utf-8").encode(p.name)) + ".xls");
    } catch (EncoderException e) {
      response.setHeader("Content-Disposition", "attachment; filename=exported.xls");
    }
    response.setContentTypeIfNotSet("application/vnd.ms-excel");
  }