Java tutorial
package siddur.solidtrust.classic; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.FileChannel; import java.nio.channels.ReadableByteChannel; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.servlet.http.HttpSession; import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Transactional; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import siddur.solidtrust.autoverleden.ClassicService; import siddur.solidtrust.azure.AzureCarService; import siddur.solidtrust.entity.AzureCar; import siddur.solidtrust.entity.ClassicCarItem; import siddur.solidtrust.entity.ClassicCarPrice; @Controller @RequestMapping("/classic") public class ClassicController { private static final Logger log4j = Logger.getLogger(ClassicController.class); @PersistenceContext private EntityManager em; @Autowired private ClassicPersister persister; @Autowired private AzureCarService azureService; @Autowired private PriceCalculator calculator; @Autowired private ClassicService autovSerivce; @RequestMapping(value = "/upload.html") public String toUpload() { return "hidden/classic_upload"; } @RequestMapping(value = "/upload", method = RequestMethod.POST) public String handleFormUpload(@RequestParam("file") MultipartFile file, Model model, HttpSession session) throws Exception { //upload log4j.info("Start uploading file: " + file.getName() + " with size: " + file.getSize()); File temp = File.createTempFile("data", ".csv"); log4j.info("Will save to " + temp.getAbsolutePath()); InputStream in = null; FileOutputStream fout = null; try { fout = new FileOutputStream(temp); FileChannel fcout = fout.getChannel(); in = file.getInputStream(); ReadableByteChannel cin = Channels.newChannel(in); ByteBuffer buf = ByteBuffer.allocate(1024 * 8); while (true) { buf.clear(); int r = cin.read(buf); if (r == -1) { break; } buf.flip(); fcout.write(buf); } } finally { if (in != null) { in.close(); } if (fout != null) { fout.close(); } } log4j.info("Uploading complete"); //fields BufferedReader br = null; int[] orders; try { in = new FileInputStream(temp); br = new BufferedReader(new InputStreamReader(in)); //first line for fields String firstLine = br.readLine(); orders = persister.validateTitle(firstLine); //persist persister.parseAndSave(br, orders, persister); } finally { if (br != null) { br.close(); } } return "redirect:upload.html"; } @SuppressWarnings("deprecation") @RequestMapping(value = "/search") public String calcPrice(@RequestParam(value = "type", required = false, defaultValue = "2") int searchType, @RequestParam(value = "key", required = false) String key, Model m) throws Exception { if (!StringUtils.isEmpty(key)) { key = key.trim(); if (key.length() > 4) { String brand, model; int year = 0; if (searchType == 2) { AzureCar entity = azureService.findByLicensePlate(key); year = (entity.getDateOfBuild().getYear() + 1900); brand = entity.getBrand(); model = entity.getType(); } else { try { year = Integer.parseInt(key.substring(0, 4)); } catch (Exception e) { e.printStackTrace(); } brand = model = key.substring(4); } log4j.info("year=" + year + ", brand=" + brand + ", model=" + model); List<ClassicCarItem> list = calculator.search(year, brand, model, false, true); if (searchType == 2 && list.size() < 5) { log4j.info("Found results are less than 5, enable to search with build year range"); list = calculator.search(year, brand, model, true, true); } if (!list.isEmpty()) { ClassicCarPrice price = calculator.calc(list); m.addAttribute("item", price); } m.addAttribute("list", list); m.addAttribute("key", key); } } return "classic/search"; } // @Scheduled(cron = "0 0 1 * * *") // public void scheduleUpdate(){ // persister.fetchFromScrapingDB(persister); // } @RequestMapping(value = "/remove") public @ResponseBody String remove(@RequestParam(value = "ids") String ids) { persister.remove(ids); return ids + " removed"; } // @RequestMapping(value = "/update") // public @ResponseBody String update(){ // int total = persister.fetchFromScrapingDB(persister); // if(total == 0){ // return "No items updated"; // } // return MessageFormat.format("Has updated {0} items", total); // } @Transactional(readOnly = true) @RequestMapping(value = "/searchByCatalog") public String calcPriceByCatalog(@RequestParam(value = "brand", required = false) String brand, @RequestParam(value = "model", required = false) String model, @RequestParam(value = "year", required = false, defaultValue = "0") int year, Model m, HttpSession session) throws Exception { List<ClassicCarItem> list = calculator.search(year, brand, model, false, true); if (!list.isEmpty()) { ClassicCarPrice price = calculator.calc(list); m.addAttribute("item", price); m.addAttribute("list", list); } session.setAttribute("brand", brand); session.setAttribute("model", model); session.setAttribute("year", year); return "classic/catalog"; } @Transactional(readOnly = true) @RequestMapping(value = "/getCatalogs") public String getCatalogs(@RequestParam(value = "brand", required = false) String brand, @RequestParam(value = "model", required = false) String model, HttpSession session) { String fBrand = null, fModel = null; //brand if (session.getAttribute("brands") == null) { List<String> brands = autovSerivce.getBrandNames(); if (!brands.isEmpty()) { fBrand = brands.get(0); } session.setAttribute("brands", brands); } else { @SuppressWarnings("unchecked") List<String> brands = (List<String>) session.getAttribute("brands"); if (!brands.isEmpty()) { fBrand = brands.get(0); } } //model if (StringUtils.isEmpty(brand)) { brand = fBrand; } List<String> models = autovSerivce.getModelNames(brand); if (!models.isEmpty()) { fModel = models.get(0); } session.setAttribute("models", models); //type if (StringUtils.isEmpty(model)) { model = fModel; } session.setAttribute("brand", brand); session.setAttribute("model", model); return "classic/catalog"; } }