Java tutorial
package siddur.solidtrust.newprice2; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; 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.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.commons.lang3.ArrayUtils; 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.multipart.MultipartFile; import siddur.solidtrust.ConfigBean; import siddur.solidtrust.SolidtrustConstants; import siddur.solidtrust.azure.AzureCarService; import siddur.solidtrust.entity.AzureCar; import siddur.solidtrust.entity.Car2; import siddur.solidtrust.newprice2.Newprice2Persister; import siddur.solidtrust.newprice2.Newprice2Service; import siddur.solidtrust.site.FileStatus; @Controller @RequestMapping("/v2") @Transactional(readOnly = true) public class Newprice2Controller { private final static Logger log4j = Logger.getLogger(Newprice2Controller.class); private final static String[] FIELDS = { "BRAND", "MODEL", "ARRANGEMENT", "DOORS", "TYPE", "BUILD YEAR", "KW", "HP", "CYLINDER VOLUME", "FUEL TYPE", "NEW PRICE", "TOP SPEED", "ACCELERATION 0-100 km/h", "AVERAGE MILEAGE", "ROADTAX 3 MONTHS", "CO2 EMISSION OUTPUT", "ECONOMICAL LABEL", "TIRESIZE FRONT", "TIRESIZE REAR", "SERVICE INTERVALS", "BPM", "MASS EMPTY VEHICLE IN KG", "CONTENTS FUEL TANK", "RANGE IN KM", "NUMBER OF SEATS", "TRANSMISSION", "AVERAGE MILEAGE IN KM PER L", "BUILD YEAR FROM", "BUILD YEAR TO", "THUMBNAIL IMAGE URL" }; @PersistenceContext private EntityManager em; @Autowired private Newprice2Persister carService; @Autowired private Newprice2Service ss; @Autowired private AzureCarService azureService; @Autowired private ConfigBean configBean; @RequestMapping(value = "/search") public String findCars(@RequestParam(value = "id", required = false) String id, @RequestParam(value = "count", required = false, defaultValue = "10") Integer count, Model model) throws Exception { AzureCar entity = null; if (!StringUtils.isEmpty(id)) { entity = azureService.findByLicensePlate(id.trim()); } if (entity != null) { Car2 car = ss.entity2Car(entity); model.addAttribute("car", car); List<Object[]> cars = ss.search(count, car); model.addAttribute("cars", cars); } model.addAttribute("key", id); return "newprice2/search"; } @RequestMapping(value = "/boost.html") @Transactional(readOnly = true) public String toBoost(Model model) throws Exception { String s = configBean.getValue(SolidtrustConstants.NEWPRICE2_FIELD_BOOST); if (!StringUtils.isEmpty(s)) { model.addAttribute("boosts", s.split(";")); } return "newprice2/boost"; } @RequestMapping(value = "/boost") @Transactional(readOnly = false) public String setBoost(HttpServletRequest request) throws Exception { String s = ""; for (int i = 1; i < 15; i++) { String v = request.getParameter("f" + i); s += v + ";"; } configBean.setValue(SolidtrustConstants.NEWPRICE2_FIELD_BOOST, s.substring(0, s.length() - 1), true); return "redirect:/v2/search"; } @RequestMapping(value = "/upload.html") public String toUpload() { return "hidden/newprice2_upload"; } @RequestMapping(value = "/upload", method = RequestMethod.POST) public String handleFormUpload(@RequestParam("file") MultipartFile file, Model model, HttpSession session) throws IOException { //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(); } } FileStatus fs = new FileStatus(); fs.setFile(temp); log4j.info("Uploading complete"); //fields BufferedReader br = null; int[] orders; String[] fields; try { in = new FileInputStream(temp); br = new BufferedReader(new InputStreamReader(in)); //first line for fields String firstLine = br.readLine(); fields = StringUtils.split(firstLine, ";"); ; orders = new int[fields.length]; for (int i = 0; i < orders.length; i++) { orders[i] = ArrayUtils.indexOf(FIELDS, fields[i].trim()); } //count while (br.readLine() != null) { fs.next(); } } finally { if (br != null) { br.close(); } } fs.flip(); log4j.info("Total rows: " + fs.getTotalRow()); //persist carService.saveCars(fs, orders, carService); return "redirect:/v2/upload.html"; } }