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.BaseParamDao; import data.entity.BaseParam; import data.entity.PercentParam; import data.services.parent.PrimService; import data.support.ConvertSupport; import java.io.File; import java.io.FileInputStream; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.TreeSet; import logic.ParamType; import logic.StaticType; import org.apache.log4j.Logger; 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; 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; /** * * @author */ @Service @Transactional @Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS) public class BaseParamService extends PrimService { @Autowired private BaseParamDao baseParamDao; @Autowired private PercentParamService percentParamService; public List<BaseParam> getParams() { List<BaseParam> bpList = baseParamDao.getAllAsc("uid"); Collections.sort(bpList, bpComparator); return bpList; } public Set<String> getStaticParams() { Set<String> bpSet = new HashSet<>(); for (BaseParam bp : getParams()) { if (StaticType.STATIC.equals(bp.getStaticType())) { bpSet.add(bp.getUid()); } } return bpSet; } public void createParam(BaseParam bp) throws Exception { if (baseParamDao.find(bp).isEmpty()) { bp.setAudial(ConvertSupport.getZeroInNullAndTrim(bp.getAudial())); bp.setVisual(ConvertSupport.getZeroInNullAndTrim(bp.getVisual())); bp.setKinestet(ConvertSupport.getZeroInNullAndTrim(bp.getKinestet())); if (bp.getPercentParams() == null) { bp.setPercentParams(new ArrayList()); } if (validate(bp)) { if (!bp.getUid().equals("")) { baseParamDao.save(bp); if (!bp.getPercentParams().isEmpty()) { for (PercentParam pp : bp.getPercentParams()) { percentParamService.createParam(pp); } } } else { addError("UID ?"); } } } else { addError(" ? uid ??"); } } public void updateParam(Object paramId, Object paramName, Object paramValue) throws Exception { if (paramId != null) { Long newPramId = Long.valueOf(StringAdapter.getString(paramId)); BaseParam bp = baseParamDao.find(newPramId); if (bp != null) { switch (StringAdapter.getString(paramName)) { case "uid": bp.setUid(StringAdapter.getString(paramValue)); break; case "name": bp.setName(StringAdapter.getString(paramValue)); break; case "paramType": bp.setParamType(ParamType.getParamTypeFromString(StringAdapter.getString(paramValue))); break; case "staticType": bp.setStaticType(StaticType.getStaticTypeFromString(StringAdapter.getString(paramValue))); break; case "radical": bp.setRadical(StringAdapter.getString(paramValue)); break; /*case "perception": bp.setPerception(StringAdapter.getString(paramValue)); break;*/ case "audial": bp.setAudial(StringAdapter.getString(paramValue)); break; case "visual": bp.setVisual(StringAdapter.getString(paramValue)); break; case "kinestet": bp.setKinestet(StringAdapter.getString(paramValue)); break; } if (validate(bp)) { baseParamDao.update(bp); } } else { addError(" ."); } } } public void delete(Long baseParamId) { BaseParam bp = baseParamDao.find(baseParamId); if (bp != null) { List<PercentParam> percparlist = bp.getPercentParams(); if (!percparlist.isEmpty()) { for (PercentParam pp : percparlist) { percentParamService.delete(pp.getId()); } } baseParamDao.delete(bp); } } public void addHeap(String heap) { String[] baseParamsplit = heap.trim().split(";;"); String exception = ""; int cnt = 0; for (String param : baseParamsplit) { cnt++; if (!param.equals("")) { String[] paramslist = param.split("::"); try { BaseParam exampl = getBaseParam(paramslist); /*BaseParam supbp = new BaseParam(); supbp.setUid(paramslist[0].trim());*/ if (validate(exampl)) { if (!exampl.getUid().equals("")) { List<BaseParam> oldParamList = baseParamDao.findByUid(paramslist[0]); if (!oldParamList.isEmpty()) { for (BaseParam obp : oldParamList) { percentParamService.delete(obp); baseParamDao.delete(obp); } baseParamDao.save(exampl); } else { baseParamDao.save(exampl); } if (!paramslist[9].equals("")) { ArrayList<PercentParam> percParamList = getPercentParams(exampl, paramslist[9].trim()); if (!percParamList.isEmpty()) { for (PercentParam pp : percParamList) { percentParamService.createParam(pp); } } } } else { addError(" " + cnt + ": UID ?"); } } } catch (Exception e) { addError(" " + cnt + ":" + StringAdapter.getStackTraceException(e)); } } } } public BaseParam getBaseParam(String[] param) throws Exception { BaseParam bp = new BaseParam(); try { bp.setUid(param[0].trim()); bp.setName(param[1].toString().trim()); bp.setParamType(ParamType.getParamTypeFromString(param[2].toString().trim())); bp.setStaticType(StaticType.getStaticTypeFromString(param[3].toString().trim())); bp.setRadical(param[4].toString().trim()); //bp.setPerception(param[5].toString().trim()); bp.setAudial(param[6].toString().trim()); bp.setVisual(param[7].toString().trim()); bp.setKinestet(param[8].toString().trim()); //bp.setPercentParams(getPercentParams(param[9])); } catch (Exception e) { addError("??? : " + param[0] + ":" + StringAdapter.getStackTraceException(e)); } return bp; } private ArrayList<PercentParam> getPercentParams(BaseParam bp, String ppstr) { ArrayList<PercentParam> result = new ArrayList(); if (!ppstr.equals("")) { String[] params = ppstr.split("@@"); if (params.length > 0) { String except = ""; int paramnum = 0; for (String param : params) { paramnum++; if (!param.equals("")) { PercentParam pp = new PercentParam(); String[] vals = param.split("!!"); if (!param.trim().equals("!!")) { if (param.contains("!!") && vals.length == 2) { if (!vals[0].equals("") && !vals[1].equals("")) { pp.setName(vals[1].trim()); pp.setPercentParamVal(Long.valueOf(vals[0].trim())); pp.setBaseParam(bp); result.add(pp); } } else { except += " ? ? : uid=" + bp.getUid().toString() + "; " + paramnum + ":" + param + ". "; } } } } if (!except.equals("")) { addError(except); } } } return result; } public static Double uidFilter(Object uid) throws Exception { Double fuid; String suid = StringAdapter.getString(uid); try { try { fuid = Double.valueOf(suid); } catch (NumberFormatException exc1) { suid = suid.replaceFirst("[,]", "."); suid = suid.replaceAll("[^0-9-.]*", ""); fuid = Double.valueOf(suid); } } catch (NumberFormatException exc) { throw new Exception(" ?."); } return fuid; } public boolean checkUid(String uid) { if (baseParamDao.findByUid(StringAdapter.getString(uid).trim()).isEmpty()) { return false; } return true; } public boolean deleteAllParams() { if (!percentParamService.deleteAll()) { addError("? ? "); } List<BaseParam> fullList = baseParamDao.deleteAll(); if (fullList.isEmpty()) { return true; } addError("? ? "); return false; } Comparator<BaseParam> bpComparator = new Comparator() { @Override public int compare(Object o1, Object o2) { BaseParam b1 = (BaseParam) o1; BaseParam b2 = (BaseParam) o2; Long d1 = Long.valueOf(b1.getUid()); Long d2 = Long.valueOf(b2.getUid()); return d1.compareTo(d2); } }; public BaseParam getBaseParam(List<BaseParam> bpList, String uid) { if (uid != null) { for (BaseParam bp : bpList) { if (bp.getUid().equals(uid)) { return bp; } } } return null; } public BaseParam getBaseParam(String uid) { if (uid != null) { for (BaseParam bp : getParams()) { if (bp.getUid().trim().equals(uid.trim())) { return bp; } } } return null; } public HSSFWorkbook getXls() { Logger log = Logger.getLogger(this.getClass()); try { HSSFWorkbook workbook = new HSSFWorkbook(); List<BaseParam> bpList = getParams(); HSSFSheet sheet = workbook.createSheet("FirstSheet"); HSSFRow rowhead = sheet.createRow((short) 0); rowhead.createCell(0).setCellValue("UID"); rowhead.createCell(1).setCellValue("????"); rowhead.createCell(2).setCellValue(""); rowhead.createCell(3).setCellValue("??"); rowhead.createCell(4).setCellValue("??"); rowhead.createCell(5).setCellValue("??"); rowhead.createCell(6).setCellValue("??"); rowhead.createCell(7).setCellValue("?"); rowhead.createCell(8).setCellValue("?"); int n = 1; if (!bpList.isEmpty()) { for (BaseParam bp : bpList) { HSSFRow rowbody = sheet.createRow((short) n); rowbody.createCell(0).setCellValue(StringAdapter.getString(bp.getUid().trim())); rowbody.createCell(1).setCellValue(StringAdapter.getString(bp.getName())); rowbody.createCell(2).setCellValue(StringAdapter.getString(bp.getParamType().getName())); rowbody.createCell(3).setCellValue(StringAdapter.getString(bp.getStaticType().getName())); rowbody.createCell(4).setCellValue(StringAdapter.getString(getPercentParamsAsString(bp))); rowbody.createCell(5).setCellValue(StringAdapter.getString(bp.getRadical())); rowbody.createCell(6).setCellValue(StringAdapter.getString(bp.getAudial())); rowbody.createCell(7).setCellValue(StringAdapter.getString(bp.getVisual())); rowbody.createCell(8).setCellValue(StringAdapter.getString(bp.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(); it.next(); while (it.hasNext()) { Row row = it.next(); Cell uidc = row.getCell(0); String uid = ""; if (uidc != null) { uid = StringAdapter.HSSFSellValue(uidc).trim(); if (uid.contains(".")) { int point = uid.indexOf("."); uid = uid.substring(0, point); } } Cell namec = row.getCell(1); String name = ""; if (namec != null) { name = StringAdapter.HSSFSellValue(namec).trim(); } Cell typec = row.getCell(2); String type = ""; if (typec != null) { type = StringAdapter.HSSFSellValue(typec).trim(); //ParamType ptype = ParamType.getParamTypeFromString(type); } Cell statc = row.getCell(3); String stat = ""; if (statc != null) { stat = StringAdapter.HSSFSellValue(statc).trim(); //StaticType stype = StaticType.getStaticTypeFromString(stat); } Cell ppc = row.getCell(4); String ppStr = ""; if (ppc != null) { ppStr = StringAdapter.HSSFSellValue(ppc).trim(); } Cell radc = row.getCell(5); 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(6); 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(7); 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(8); 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"; } } BaseParam bp = getBaseParam(uid); if (bp != null) { bp.setName(name); bp.setParamType(ParamType.getParamTypeFromString(type)); bp.setStaticType(StaticType.getStaticTypeFromString(stat)); //bp.setPercentParams(getPercentParamsFromString(ppStr,bp)); bp.setRadical(rad); bp.setAudial(a); bp.setVisual(v); bp.setKinestet(k); update(bp, getPercentParamsFromString(ppStr, bp)); } else { bp = new BaseParam(); bp.setUid(uid); bp.setName(name); bp.setParamType(ParamType.getParamTypeFromString(type)); bp.setStaticType(StaticType.getStaticTypeFromString(stat)); bp.setPercentParams(getPercentParamsFromString(ppStr, bp)); bp.setRadical(rad); bp.setAudial(a); bp.setVisual(v); bp.setKinestet(k); createParam(bp); } } workbook.close(); } catch (Exception e) { addError(StringAdapter.getStackTraceException(e)); } fi.close(); } catch (Exception e) { addError(" xml"); addError(e.getMessage()); } } private String getPercentParamsAsString(BaseParam bp) { String res = ""; List<PercentParam> pplist = bp.getPercentParams(); if (!pplist.isEmpty()) { for (PercentParam pp : pplist) { res += pp.getPercentParamVal() + " = " + pp.getName() + "; "; } } return res; } private List<PercentParam> getPercentParamsFromString(String ppString, BaseParam bp) { List<PercentParam> pplist = new ArrayList(); if (!ppString.equals("")) { if (ppString.contains(";")) { String[] strParams = ppString.split(";"); for (String strParam : strParams) { if (!strParam.trim().equals("") && strParam.contains("=")) { String[] paramPart = strParam.trim().split("="); String value = paramPart[0].trim(); String name = paramPart[1].trim(); PercentParam pp = new PercentParam(); pp.setName(name); pp.setPercentParamVal(Long.valueOf(value)); pp.setBaseParam(bp); pplist.add(pp); } } } } return pplist; } public void update(BaseParam bp, List<PercentParam> ppList) throws Exception { //BaseParam oldBp = getBaseParam(bp.getUid()); if (validate(bp)) { if (!bp.getPercentParams().isEmpty()) { for (PercentParam pp : bp.getPercentParams()) { percentParamService.delete(pp.getId()); } } baseParamDao.update(bp); for (PercentParam pp : ppList) { percentParamService.createParam(pp); } } } public static String uidColorFiltr(String uid) { String fuid = uid; if (!uid.equals("")) { if (uid.length() == 7) { fuid = " <font color=\"#38571a\">" + uid.substring(0, 2) + "</font>"; fuid += " <font color=\"#4d22b3\">" + uid.substring(2, 4) + "</font>"; fuid += " <font color=\"#0061ff\">" + uid.substring(4) + "</font> "; } else if (uid.length() > 7) { fuid = " <font color=\"#38571a\">" + uid.substring(0, 2) + "</font>"; fuid += " <font color=\"#4d22b3\">" + uid.substring(2, 4) + "</font>"; fuid += " <font color=\"#0061ff\">" + uid.substring(4, 7) + "</font>"; fuid += " <font color=\"#d58400\">" + uid.substring(7) + "</font> "; } } return fuid; } public String uidSpaceFiltr(String uid) { String fuid = uid; if (!uid.equals("")) { if (uid.length() == 7) { fuid = " " + uid.substring(0, 2); fuid += " " + uid.substring(2, 4); fuid += " " + uid.substring(4); } else if (uid.length() > 7) { fuid = " " + uid.substring(0, 2); fuid += " " + uid.substring(2, 4); fuid += " " + uid.substring(4, 7); fuid += " " + uid.substring(7); } } return fuid; } }