Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package data.services; import data.dao.ColorDao; import data.entity.Color; import data.services.parent.PrimService; import java.io.File; import java.io.FileInputStream; import java.util.Iterator; import java.util.List; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.ScopedProxyMode; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import support.StringAdapter; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; /** * * @author bezdatiuzer */ @Service @Transactional @Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS) public class ColorService extends PrimService { @Autowired private ColorDao colorDao; public List<Color> getColors() { return colorDao.getAllAsc("colorId"); } public void createColor(Color c) throws Exception { Color cOne = new Color(); cOne.setOldGroupId(c.getOldGroupId()); cOne.setOldId(c.getOldId()); if (colorDao.find(cOne).isEmpty()) { if (validate(c)) { colorDao.save(c); } } } public void updateColor(Long colorId, String uid, String paramValue, Long percentValue, String radical, Integer a, Integer v, Integer k) { Color cl = colorDao.find(colorId); if (cl != null) { cl.setUid(uid); cl.setParamValue(StringAdapter.toDouble(paramValue)); cl.setPercentValue(percentValue); cl.setRadical(radical); cl.setAudial(a); cl.setVisual(v); cl.setKinestet(k); if (validate(cl)) { colorDao.update(cl); } } else { addError(" ? " + colorId); } } public void updateAllInTitle(String title, String uid, String paramValue, Long percentValue, String radical, Integer a, Integer v, Integer k) { Color clg = new Color(); clg.setTitle(title); for (Color clgCl : colorDao.find(clg)) { clgCl.setUid(uid); clgCl.setParamValue(StringAdapter.toDouble(paramValue)); clgCl.setPercentValue(percentValue); clgCl.setRadical(radical); clgCl.setAudial(a); clgCl.setVisual(v); clgCl.setKinestet(k); if (validate(clgCl)) { colorDao.update(clgCl); } } } public Color findByOldId(Long oldId) { List<Color> list = colorDao.findByQId(oldId); if (!list.isEmpty()) { //if(list.size()==1){ return list.get(0); /*}else{ addError(" quto: "+oldId); }*/ } else { addError(" , : " + oldId); } return null; } public HSSFWorkbook getXls() { Logger log = Logger.getLogger(this.getClass()); try { HSSFWorkbook workbook = new HSSFWorkbook(); List<Color> oblist = colorDao.getAllAsc(); HSSFSheet sheet = workbook.createSheet("FirstSheet"); HSSFRow rowhead = sheet.createRow((short) 0); rowhead.createCell(0).setCellValue("ID"); rowhead.createCell(1).setCellValue("TITLE"); rowhead.createCell(2).setCellValue("????"); rowhead.createCell(3).setCellValue("UID"); rowhead.createCell(4).setCellValue(""); rowhead.createCell(5).setCellValue(""); rowhead.createCell(6).setCellValue(""); rowhead.createCell(7).setCellValue("?"); rowhead.createCell(8).setCellValue(""); rowhead.createCell(9).setCellValue("?"); int n = 1; if (!oblist.isEmpty()) { for (Color ob : oblist) { HSSFRow rowbody = sheet.createRow((short) n); rowbody.createCell(0).setCellValue(StringAdapter.getString(ob.getColorId())); rowbody.createCell(1).setCellValue(StringAdapter.getString(ob.getName())); rowbody.createCell(2).setCellValue(StringAdapter.getString(ob.getTitle())); rowbody.createCell(3).setCellValue(StringAdapter.getString(ob.getUid())); rowbody.createCell(4) .setCellValue(StringAdapter.getString(ob.getParamValue()).replace(".", ",")); rowbody.createCell(5).setCellValue(StringAdapter.getString(ob.getPercentValue())); rowbody.createCell(6).setCellValue(StringAdapter.getString(ob.getRadical())); rowbody.createCell(7).setCellValue(StringAdapter.getString(ob.getAudial())); rowbody.createCell(8).setCellValue(StringAdapter.getString(ob.getVisual())); rowbody.createCell(9).setCellValue(StringAdapter.getString(ob.getKinestet())); n++; } ; } return workbook; } catch (Exception e) { log.warn("HSSFWorkbook.getXls()", e); } return null; } public void updateFromXml(File fl) { try { FileInputStream fi = new FileInputStream(fl); try { HSSFWorkbook workbook = new HSSFWorkbook(fi); HSSFSheet sheet = workbook.getSheetAt(0); Iterator<Row> it = sheet.iterator(); while (it.hasNext()) { Row row = it.next(); Long id = StringAdapter.toLong(StringAdapter.HSSFSellValue(row.getCell(0))); /*String uid=StringAdapter.HSSFSellValue(row.getCell(3)); String val=StringAdapter.HSSFSellValue(row.getCell(4)); String pers=StringAdapter.HSSFSellValue(row.getCell(5)); String rad=StringAdapter.HSSFSellValue(row.getCell(6));*/ Cell uidc = row.getCell(3); String uid = ""; if (uidc != null) { uid = StringAdapter.HSSFSellValue(uidc); if (uid.contains(".")) { int point = uid.indexOf("."); uid = uid.substring(0, point); } } Cell valc = row.getCell(4); Double val = (double) 0; if (valc != null) { String valstr = StringAdapter.HSSFSellValue(valc).replace(",", ".").trim(); val = StringAdapter.toDouble(valstr); } Cell percc = row.getCell(5); Long perc = (long) 0; if (percc != null) { String percstr = StringAdapter.HSSFSellValue(percc); if (percstr.contains(".")) { int point = percstr.indexOf("."); percstr = percstr.substring(0, point); } if (!percstr.equals("")) { perc = StringAdapter.toLong(percstr); } } Cell radc = row.getCell(6); String rad = ""; if (radc != null) { rad = StringAdapter.HSSFSellValue(radc).trim(); if (rad.contains(".")) { int point = rad.indexOf("."); rad = rad.substring(0, point); } if (rad.trim().equals("0")) { rad = ""; } else { rad = rad.replace(" ", ""); } } Cell ac = row.getCell(7); String a = "0"; if (ac != null) { a = StringAdapter.HSSFSellValue(ac); if (a.contains(".")) { int point = a.indexOf("."); a = a.substring(0, point); } if (a.equals("")) { a = "0"; } } Cell vc = row.getCell(8); String v = "0"; if (vc != null) { v = StringAdapter.HSSFSellValue(vc); if (v.contains(".")) { int point = v.indexOf("."); v = v.substring(0, point); } if (v.equals("")) { v = "0"; } } Cell kc = row.getCell(9); String k = "0"; if (kc != null) { k = StringAdapter.HSSFSellValue(ac); if (k.contains(".")) { int point = k.indexOf("."); k = k.substring(0, point); } if (k.equals("")) { k = "0"; } } Color cl = colorDao.find(id); if (cl != null) { cl.setUid(uid); cl.setParamValue(val); cl.setPercentValue(perc); cl.setRadical(rad); cl.setAudial(Integer.valueOf(a)); cl.setVisual(Integer.valueOf(v)); cl.setKinestet(Integer.valueOf(k)); if (validate(cl)) { colorDao.update(cl); } } } workbook.close(); } catch (Exception e) { addError(StringAdapter.getStackTraceException(e)); } fi.close(); } catch (Exception e) { addError(" xml"); addError(e.getMessage()); } } }