Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/net/enLearn/controller/DemoController.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public String showReportPage() {
return "report-page";
}

@GetMapping("/discount-rate-report")
public String showDiscountRateReportPage() { return "discount-report"; }
/*@GetMapping("/discount-rate-report")
public String showDiscountRateReportPage() { return "discount-report"; }*/


@GetMapping("/portal")
Expand Down
69 changes: 33 additions & 36 deletions src/main/java/net/enLearn/controller/DiscountController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.enLearn.controller;

import net.enLearn.entity.Discount;
import net.enLearn.reportView.DiscountReportView;
import net.enLearn.service.DiscountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
Expand All @@ -13,6 +14,8 @@
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedOutputStream;
import java.io.File;
Expand Down Expand Up @@ -73,21 +76,21 @@ public String processDiscountForm(@RequestParam("admin_id") int admin_id,
}*/



//This worked
private static final String UPLOAD_DIRECTORY ="/resources/img";
private static final String UPLOAD_DIRECTORY ="/resources/Discount_img";


@RequestMapping(path = "/addDiscount", method = RequestMethod.POST)
public String processDiscountForm(@RequestParam("admin_id") int admin_id,
@RequestParam("discount") int discount,
public String processDiscountForm(@RequestParam("discount") int discount,
@RequestParam("teacher_name") String teacher_name,
@RequestParam("course") String course,
@RequestParam("image") MultipartFile image,
@RequestParam("description") String description,
@RequestParam("grade") int grade,
@RequestParam("title") String title, HttpSession session) throws Exception{



ServletContext context = session.getServletContext();
String path = context.getRealPath(UPLOAD_DIRECTORY);
String filename = image.getOriginalFilename();
Expand All @@ -101,19 +104,17 @@ public String processDiscountForm(@RequestParam("admin_id") int admin_id,
stream.close();



int admin_id = 1;
Discount discountObj;
discountObj = new Discount(admin_id,discount,teacher_name,course,image.getBytes(),description,grade,title);

disService.saveDiscount(discountObj);


return "Add-Discount";
}




//==================================================================================================================
//Method for deleting added discounts (DELETE)
@RequestMapping("/deleteDiscount")
Expand Down Expand Up @@ -181,35 +182,6 @@ public String update(@RequestParam("id") int id,



/*@RequestMapping(path = "/update", method = RequestMethod.POST)
public String update(@RequestParam("id") int id,
@RequestParam("admin_id") int admin_id,
@RequestParam("discount") int discount,
@RequestParam("teacher_name") String teacher_name,
@RequestParam("course") String course,
@RequestParam("image") MultipartFile image,
@RequestParam("description") String description,
@RequestParam("grade") int grade,
@RequestParam("title") String title,
Model model1){


String photo = null;
try {
photo = "data:image/jpg;base64,"+
Base64.getEncoder().encodeToString(image.getBytes());
} catch (
IOException e) {
e.printStackTrace();
}


Discount discount1 = new Discount(id,admin_id,discount,teacher_name,course,photo,description,grade,title);
disService.updateDiscount(discount1);

return "redirect:/discounts/showDiscounts";
}*/


//==================================================================================================================
/*@RequestMapping(path = "/getDiscountCode")
Expand All @@ -219,4 +191,29 @@ public String getDiscountCode(@RequestParam("DiscountCodeForModel") int discount
return "AddedDiscounts";
}*/





//==================================================================================================================
//Generate PDF
@RequestMapping(path = "/discountPDFReport", method = RequestMethod.GET)
public ModelAndView DiscountListReport(HttpServletRequest req, HttpServletResponse res){

String typeReport = req.getParameter("type");

//Create data
List<Discount> list = disService.getAllDiscountByAdminId();

if(typeReport != null && typeReport.equals("pdf")){
return new ModelAndView(new DiscountReportView(),"discountList",list);
}

//default
return new ModelAndView("discount-report","discountList",list);
}


//==================================================================================================================

}
2 changes: 2 additions & 0 deletions src/main/java/net/enLearn/dao/DiscountDAOImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,6 @@ public void updateDiscount(Discount discount){
}




}
58 changes: 58 additions & 0 deletions src/main/java/net/enLearn/reportView/DiscountReportView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package net.enLearn.reportView;

import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import net.enLearn.entity.Discount;
import org.springframework.web.servlet.view.document.AbstractPdfView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

public class DiscountReportView extends AbstractPdfView {

@Override
protected void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter writer, HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setHeader("Content-Disposition","attachment; filename=\"Available Discounts.pdf\"");

@SuppressWarnings("Unchecked")
List<Discount> list = (List<Discount>) model.get("discountList");

//Create Image object
Image image = Image.getInstance("D:\\ITP-Project_03\\target\\EnLearn-1.0-SNAPSHOT\\resources\\img\\logo.png");
image .setAlignment(Image.MIDDLE);
image.scaleAbsolute(200f, 200f); // Scale the image to an absolute width and an absolute height
image.scaleToFit(200f, 400f); // Scales the image so that it fits a certain width and height
//Add content to the document using Image object.
document.add(image);


//Adding text to pdf file
Paragraph p = new Paragraph("Available Discounts - Report");
p.setAlignment(Element.ALIGN_CENTER);
document.add(p);

//Table heading
Table table = new Table(6);
table.addCell("Discount ID");
table.addCell("Admin ID");
table.addCell("Discount");
table.addCell("Teacher ");
table.addCell("Course");
table.addCell("Grade");

//Adding table data
for(Discount dis : list){
table.addCell(String.valueOf(dis.getId()));
table.addCell(String.valueOf(dis.getAdmin_id()));
table.addCell(String.valueOf(dis.getDiscount()));
table.addCell(String.valueOf(dis.getTeacher_name()));
table.addCell(String.valueOf(dis.getCourse()));
table.addCell(String.valueOf(dis.getGrade()));
}

//Add table to document
document.add(table);
}
}
Loading