@Transactional
  @Override
  public void generateStatementPdf(final Long billId) {

    try {
      BillMaster billMaster = this.billMasterRepository.findOne(billId);
      final String fileLocation = FileUtils.MIFOSX_BASE_DIR;
      /** Recursively create the directory if it does not exist * */
      if (!new File(fileLocation).isDirectory()) {
        new File(fileLocation).mkdirs();
      }
      final String statementDetailsLocation = fileLocation + File.separator + "StatementPdfFiles";
      if (!new File(statementDetailsLocation).isDirectory()) {
        new File(statementDetailsLocation).mkdirs();
      }
      final String printStatementLocation =
          statementDetailsLocation + File.separator + "Bill_" + billMaster.getId() + ".pdf";
      final String jpath = fileLocation + File.separator + "jasper";
      final String tenant = ThreadLocalContextUtil.getTenant().getTenantIdentifier();
      final String jfilepath = jpath + File.separator + "Statement_" + tenant + ".jasper";
      File destinationFile = new File(jfilepath);
      if (!destinationFile.exists()) {
        File sourceFile =
            new File(
                this.getClass().getClassLoader().getResource("Files/Statement.jasper").getFile());
        FileUtils.copyFileUsingApacheCommonsIO(sourceFile, destinationFile);
      }
      final Connection connection = this.dataSource.getConnection();
      Map<String, Object> parameters = new HashMap<String, Object>();
      final Integer id = Integer.valueOf(billMaster.getId().toString());
      parameters.put("param1", id);
      parameters.put("SUBREPORT_DIR", jpath + "" + File.separator);
      final JasperPrint jasperPrint =
          JasperFillManager.fillReport(jfilepath, parameters, connection);
      JasperExportManager.exportReportToPdfFile(jasperPrint, printStatementLocation);
      billMaster.setFileName(printStatementLocation);
      this.billMasterRepository.save(billMaster);
      connection.close();
      System.out.println("Filling report successfully...");

    } catch (final DataIntegrityViolationException ex) {

      LOGGER.error("Filling report failed..." + ex.getLocalizedMessage());
      System.out.println("Filling report failed...");
      ex.printStackTrace();

    } catch (final JRException | JRRuntimeException e) {

      LOGGER.error("Filling report failed..." + e.getLocalizedMessage());
      System.out.println("Filling report failed...");
      e.printStackTrace();

    } catch (final Exception e) {

      LOGGER.error("Filling report failed..." + e.getLocalizedMessage());
      System.out.println("Filling report failed...");
      e.printStackTrace();
    }
  }
  @Transactional
  @Override
  public String generateInovicePdf(final Long invoiceId) {

    final String fileLocation = FileUtils.MIFOSX_BASE_DIR;
    /** Recursively create the directory if it does not exist * */
    if (!new File(fileLocation).isDirectory()) {
      new File(fileLocation).mkdirs();
    }
    final String InvoiceDetailsLocation = fileLocation + File.separator + "InvoicePdfFiles";
    if (!new File(InvoiceDetailsLocation).isDirectory()) {
      new File(InvoiceDetailsLocation).mkdirs();
    }
    final String printInvoiceLocation =
        InvoiceDetailsLocation + File.separator + "Invoice_" + invoiceId + ".pdf";
    final Integer id = Integer.valueOf(invoiceId.toString());
    try {

      final String jpath = fileLocation + File.separator + "jasper";
      final String tenant = ThreadLocalContextUtil.getTenant().getTenantIdentifier();
      final String jasperfilepath = jpath + File.separator + "Invoicereport_" + tenant + ".jasper";
      File destinationFile = new File(jasperfilepath);
      if (!destinationFile.exists()) {
        File sourceFile =
            new File(
                this.getClass()
                    .getClassLoader()
                    .getResource("Files/Invoicereport.jasper")
                    .getFile());
        FileUtils.copyFileUsingApacheCommonsIO(sourceFile, destinationFile);
      }
      final Connection connection = this.dataSource.getConnection();
      Map<String, Object> parameters = new HashMap<String, Object>();
      parameters.put("param1", id);
      final JasperPrint jasperPrint =
          JasperFillManager.fillReport(jasperfilepath, parameters, connection);
      JasperExportManager.exportReportToPdfFile(jasperPrint, printInvoiceLocation);
      connection.close();
      System.out.println("Filling report successfully...");

    } catch (final DataIntegrityViolationException ex) {
      LOGGER.error("Filling report failed..." + ex.getLocalizedMessage());
      System.out.println("Filling report failed...");
      ex.printStackTrace();
    } catch (final JRException | JRRuntimeException e) {
      LOGGER.error("Filling report failed..." + e.getLocalizedMessage());
      System.out.println("Filling report failed...");
      e.printStackTrace();
    } catch (final Exception e) {
      LOGGER.error("Filling report failed..." + e.getLocalizedMessage());
      System.out.println("Filling report failed...");
      e.printStackTrace();
    }
    return printInvoiceLocation;
  }
  @POST
  @Path("/documents")
  @Consumes({MediaType.MULTIPART_FORM_DATA})
  @Produces({MediaType.APPLICATION_JSON})
  public Response createUploadFile(
      @HeaderParam("Content-Length") final Long fileSize,
      @FormDataParam("file") final InputStream inputStream,
      @FormDataParam("file") final FormDataContentDisposition fileDetails,
      @FormDataParam("file") final FormDataBodyPart bodyPart,
      @FormDataParam("status") final String name,
      @FormDataParam("description") final String description) {

    FileUtils.validateFileSizeWithinPermissibleRange(
        fileSize, name, ApiConstants.MAX_FILE_UPLOAD_SIZE_IN_MB);
    inputStreamObject = inputStream;
    DateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy");
    final Date date = DateUtils.getDateOfTenant();
    final DateTimeFormatter dtf = DateTimeFormat.forPattern("dd MMMM yyyy");
    final LocalDate localdate = dtf.parseLocalDate(dateFormat.format(date));
    final String fileUploadLocation = FileUtils.generateXlsFileDirectory();
    final String fileName = fileDetails.getFileName();
    if (!new File(fileUploadLocation).isDirectory()) {
      new File(fileUploadLocation).mkdirs();
    }
    final DataUploadCommand uploadStatusCommand =
        new DataUploadCommand(
            name,
            null,
            localdate,
            "",
            null,
            null,
            null,
            description,
            fileName,
            inputStream,
            fileUploadLocation);
    CommandProcessingResult id = this.dataUploadWritePlatformService.addItem(uploadStatusCommand);
    return Response.ok().entity(id.toString()).build();
    // return null;
  }
  @POST
  @Consumes({MediaType.MULTIPART_FORM_DATA})
  @Produces({MediaType.APPLICATION_JSON})
  public String createDocument(
      @PathParam("entityType") final String entityType,
      @PathParam("entityId") final Long entityId,
      @HeaderParam("Content-Length") final Long fileSize,
      @FormDataParam("file") final InputStream inputStream,
      @FormDataParam("file") final FormDataContentDisposition fileDetails,
      @FormDataParam("file") final FormDataBodyPart bodyPart,
      @FormDataParam("name") final String name,
      @FormDataParam("description") final String description) {

    FileUtils.validateFileSizeWithinPermissibleRange(
        fileSize, name, ApiConstants.MAX_FILE_UPLOAD_SIZE_IN_MB);

    /**
     * TODO: also need to have a backup and stop reading from stream after max size is reached to
     * protect against malicious clients
     */

    /** TODO: need to extract the actual file type and determine if they are permissable */
    final DocumentCommand documentCommand =
        new DocumentCommand(
            null,
            null,
            entityType,
            entityId,
            name,
            fileDetails.getFileName(),
            fileSize,
            bodyPart.getMediaType().toString(),
            description,
            null);

    final Long documentId =
        this.documentWritePlatformService.createDocument(documentCommand, inputStream);

    return this.toApiJsonSerializer.serialize(
        CommandProcessingResult.resourceResult(documentId, null));
  }
  @PUT
  @Path("{documentId}")
  @Consumes({MediaType.MULTIPART_FORM_DATA})
  @Produces({MediaType.APPLICATION_JSON})
  public String updateDocument(
      @PathParam("entityType") final String entityType,
      @PathParam("entityId") final Long entityId,
      @PathParam("documentId") final Long documentId,
      @HeaderParam("Content-Length") final Long fileSize,
      @FormDataParam("file") final InputStream inputStream,
      @FormDataParam("file") final FormDataContentDisposition fileDetails,
      @FormDataParam("file") final FormDataBodyPart bodyPart,
      @FormDataParam("name") final String name,
      @FormDataParam("description") final String description) {

    FileUtils.validateFileSizeWithinPermissibleRange(
        fileSize, name, ApiConstants.MAX_FILE_UPLOAD_SIZE_IN_MB);

    final Set<String> modifiedParams = new HashSet<String>();
    modifiedParams.add("name");
    modifiedParams.add("description");

    /**
     * * Populate Document command based on whether a file has also been passed in as a part of the
     * update *
     */
    DocumentCommand documentCommand = null;
    if (inputStream != null && fileDetails.getFileName() != null) {
      modifiedParams.add("fileName");
      modifiedParams.add("size");
      modifiedParams.add("type");
      modifiedParams.add("location");
      documentCommand =
          new DocumentCommand(
              modifiedParams,
              documentId,
              entityType,
              entityId,
              name,
              fileDetails.getFileName(),
              fileSize,
              bodyPart.getMediaType().toString(),
              description,
              null);
    } else {
      documentCommand =
          new DocumentCommand(
              modifiedParams,
              documentId,
              entityType,
              entityId,
              name,
              null,
              null,
              null,
              description,
              null);
    }
    /** * TODO: does not return list of changes, should be done for consistency with rest of API */
    final CommandProcessingResult identifier =
        this.documentWritePlatformService.updateDocument(documentCommand, inputStream);

    return this.toApiJsonSerializer.serialize(identifier);
  }
Exemplo n.º 6
0
  @POST
  @Path("/documents")
  @Consumes({MediaType.MULTIPART_FORM_DATA})
  @Produces({MediaType.APPLICATION_JSON})
  public String createUploadFile(
      @HeaderParam("Content-Length") Long fileSize,
      @FormDataParam("file") InputStream inputStream,
      @FormDataParam("file") FormDataContentDisposition fileDetails,
      @FormDataParam("file") FormDataBodyPart bodyPart,
      @FormDataParam("name") String name,
      @FormDataParam("description") String description) {

    FileUtils.validateFileSizeWithinPermissibleRange(
        fileSize, name, ApiConstants.MAX_FILE_UPLOAD_SIZE_IN_MB);

    int i;

    /*DocumentCommand documentCommand = new DocumentCommand(null, null, null, null, name, fileDetails.getFileName(), fileSize,
    bodyPart.getMediaType().toString(), description, null);*/
    try {
      String fileUploadLocation = FileUtils.generateXlsFileDirectory();
      String fileName = fileDetails.getFileName();
      if (!new File(fileUploadLocation).isDirectory()) {
        new File(fileUploadLocation).mkdirs();
      }

      String fileLocation = FileUtils.saveToFileSystem(inputStream, fileUploadLocation, fileName);

      InputStream excelFileToRead = new FileInputStream(fileLocation);

      XSSFWorkbook wb = new XSSFWorkbook(excelFileToRead);

      XSSFSheet sheet = wb.getSheetAt(0);
      XSSFRow row;
      XSSFCell cell;
      String serialno = "0";
      int countno = Integer.parseInt(serialno);
      if (countno == 0) {
        countno = countno + 2;
      } else if (countno == 1) {
        countno = countno + 1;
      }
      System.out.println("Excel Row No is: " + countno);
      Iterator rows = sheet.rowIterator();
      Vector<XSSFCell> v = new Vector<XSSFCell>();
      if (countno > 0) {
        countno = countno - 1;
      }
      while (rows.hasNext()) {

        row = (XSSFRow) rows.next();
        i = row.getRowNum();
        if (i > 0) {
          if (i >= countno) {
            Iterator cells = row.cellIterator();
            while (cells.hasNext()) {

              cell = (XSSFCell) cells.next();

              if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
                // System.out.print(cell.getStringCellValue() +
                // " ");
                v.add(cell);
              } else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
                // System.out.print(cell.getNumericCellValue() +
                // " ");
                v.add(cell);
              } else {
                v.add(cell);
              }
            }
            // ItemDetailsCommand itemdetails=new
            // ItemDetailsCommand(Integer.parseInt(v.elementAt(0).toString()),v.elementAt(1).toString(),Integer.parseInt(v.elementAt(2).toString()),v.elementAt(3).toString(),v.elementAt(4).toString(),v.elementAt(5).toString(),Integer.parseInt(v.elementAt(6).toString()),Integer.parseInt(v.elementAt(7).toString()),Integer.parseInt(v.elementAt(8).toString()),v.elementAt(9).toString(),null);
            List<ItemDetailsCommand> ItemDetailsCommandList = new ArrayList<ItemDetailsCommand>();
            /*for(i=0;i<10;i++)
            {*/
            /* Iterator iterator = v.iterator();
             while(iterator.hasNext())
             {

            	System.out.println(v.elementAt(0).toString());
            	System.out.println( v.elementAt(1).toString());
            	System.out.println( v.elementAt(2).toString());
            	System.out.println( v.elementAt(3).toString());
            	System.out.println( v.elementAt(4).toString());
            	System.out.println( v.elementAt(5).toString());
            	System.out.println( v.elementAt(6).toString());
            	System.out.println( v.elementAt(7).toString());
            	System.out.println( v.elementAt(8).toString());
            	System.out.println( v.elementAt(9).toString());



            	// itemDetails = ItemDetails.create(command.getItemMasterId(), command.getSerialNumber(), command.getGrnId(),command.getProvisioningSerialNumber(), command.getQuality(),command.getStatus(), command.getOfficeId(), command.getClientId(), command.getWarranty(), command.getRemark());
            	//ItemDetailsCommand itemdetails=new ItemDetailsCommand(Long.parseLong(v.elementAt(0).toString()), v.elementAt(1).toString(), Long.parseLong(v.elementAt(2).toString()), v.elementAt(3).toString(), v.elementAt(4).toString(), v.elementAt(5).toString(), Long.parseLong(v.elementAt(6).toString()), Long.parseLong(v.elementAt(7).toString()), Long.parseLong(v.elementAt(8).toString()), v.elementAt(9).toString());
            	//ItemDetailsCommand itemdetails=new ItemDetailsCommand(Long.parseLong(v.elementAt(0).toString()), v.elementAt(1).toString(), Long.parseLong(v.elementAt(2).toString()), v.elementAt(3).toString(), v.elementAt(4).toString(), v.elementAt(5).toString(), Long.parseLong(v.elementAt(6).toString()), Long.parseLong(v.elementAt(7).toString()), Long.parseLong(v.elementAt(8).toString()), v.elementAt(9).toString());
            	//CommandProcessingResult id = this.itemDetailsWritePlatformService.addItem(itemdetails);
            	//ItemDetailsCommandList.add(itemdetails);

            	ItemDetailsCommand itemDetailsCommand=new ItemDetailsCommand();
            	new Double(v.elementAt(0).toString()).longValue();

            	itemDetailsCommand.setItemMasterId(new Double(v.elementAt(0).toString()).longValue());

            	itemDetailsCommand.setSerialNumber(v.elementAt(1).toString());
            	itemDetailsCommand.setGrnId(new Double(v.elementAt(2).toString()).longValue());
            	itemDetailsCommand.setProvisioningSerialNumber( v.elementAt(3).toString());
            	itemDetailsCommand.setQuality( v.elementAt(4).toString());
            	itemDetailsCommand.setRemark(v.elementAt(9).toString());
            	itemDetailsCommand.setStatus(v.elementAt(5).toString());
            	itemDetailsCommand.setOfficeId(new Double(v.elementAt(6).toString()).longValue());
            	itemDetailsCommand.setClientId(new Double(v.elementAt(7).toString()).longValue());
            	itemDetailsCommand.setWarranty(new Double(v.elementAt(8).toString()).longValue());
            	ItemDetailsCommandList.add(itemDetailsCommand);

            	CommandProcessingResult id = this.itemDetailsWritePlatformService.addItem(itemDetailsCommand);

            }
            */

          }
        }
      }
      Iterator iterator = v.iterator();
      while (iterator.hasNext()) {

        System.out.println(v.elementAt(0).toString());
        System.out.println(v.elementAt(1).toString());
        System.out.println(v.elementAt(2).toString());
        System.out.println(v.elementAt(3).toString());
        System.out.println(v.elementAt(4).toString());
        System.out.println(v.elementAt(5).toString());
        System.out.println(v.elementAt(6).toString());
        System.out.println(v.elementAt(7).toString());
        System.out.println(v.elementAt(8).toString());
        System.out.println(v.elementAt(9).toString());

        // itemDetails = ItemDetails.create(command.getItemMasterId(), command.getSerialNumber(),
        // command.getGrnId(),command.getProvisioningSerialNumber(),
        // command.getQuality(),command.getStatus(), command.getOfficeId(), command.getClientId(),
        // command.getWarranty(), command.getRemark());
        // ItemDetailsCommand itemdetails=new
        // ItemDetailsCommand(Long.parseLong(v.elementAt(0).toString()), v.elementAt(1).toString(),
        // Long.parseLong(v.elementAt(2).toString()), v.elementAt(3).toString(),
        // v.elementAt(4).toString(), v.elementAt(5).toString(),
        // Long.parseLong(v.elementAt(6).toString()), Long.parseLong(v.elementAt(7).toString()),
        // Long.parseLong(v.elementAt(8).toString()), v.elementAt(9).toString());
        // ItemDetailsCommand itemdetails=new
        // ItemDetailsCommand(Long.parseLong(v.elementAt(0).toString()), v.elementAt(1).toString(),
        // Long.parseLong(v.elementAt(2).toString()), v.elementAt(3).toString(),
        // v.elementAt(4).toString(), v.elementAt(5).toString(),
        // Long.parseLong(v.elementAt(6).toString()), Long.parseLong(v.elementAt(7).toString()),
        // Long.parseLong(v.elementAt(8).toString()), v.elementAt(9).toString());
        // CommandProcessingResult id = this.itemDetailsWritePlatformService.addItem(itemdetails);
        // ItemDetailsCommandList.add(itemdetails);

        ItemDetailsCommand itemDetailsCommand = new ItemDetailsCommand();
        new Double(v.elementAt(0).toString()).longValue();

        itemDetailsCommand.setItemMasterId(new Double(v.elementAt(0).toString()).longValue());

        itemDetailsCommand.setSerialNumber(v.elementAt(1).toString());
        itemDetailsCommand.setGrnId(new Double(v.elementAt(2).toString()).longValue());
        itemDetailsCommand.setProvisioningSerialNumber(v.elementAt(3).toString());
        itemDetailsCommand.setQuality(v.elementAt(4).toString());
        itemDetailsCommand.setRemark(v.elementAt(9).toString());
        itemDetailsCommand.setStatus(v.elementAt(5).toString());
        itemDetailsCommand.setOfficeId(new Double(v.elementAt(6).toString()).longValue());
        itemDetailsCommand.setClientId(new Double(v.elementAt(7).toString()).longValue());
        itemDetailsCommand.setWarranty(new Double(v.elementAt(8).toString()).longValue());
        // ItemDetailsCommandList.add(itemDetailsCommand);

        CommandProcessingResult id =
            this.itemDetailsWritePlatformService.addItem(itemDetailsCommand);
      }

    } catch (Exception e) {
      e.printStackTrace();
    }

    /**
     * TODO: also need to have a backup and stop reading from stream after max size is reached to
     * protect against malicious clients
     */

    /** TODO: need to extract the actual file type and determine if they are permissable */

    /// ItemDetailsCommand itemDetailsCommand=new ItemDetailsCommand()
    // DocumentCommand documentCommand = new DocumentCommand(null, null, entityType, entityId, name,
    // fileDetails.getFileName(), fileSize,
    ///     bodyPart.getMediaType().toString(), description, null);

    // Long documentId = this.documentWritePlatformService.createDocument(documentCommand,
    // inputStream);

    // return this.toApiJsonSerializer.serialize(CommandProcessingResult.resourceResult(1, null));
    return null;
  }