data.services.FeatureService.java Source code

Java tutorial

Introduction

Here is the source code for data.services.FeatureService.java

Source

/*
 * 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.FeatureDao;
import data.entity.Car;
import data.entity.CarCompletionOption;
import data.entity.Feature;
import data.services.parent.PrimService;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
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 bezdatiuzer
 */
@Service
@Transactional
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class FeatureService extends PrimService {

    Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private FeatureDao featureDao;
    @Autowired
    private BaseParamService baseParamService;

    public List<Feature> getFeatures() {
        return featureDao.getAllAsc("featureId");
    }

    public void createFeature(Feature feat) throws Exception {
        Feature fOne = new Feature();
        fOne.setOldId(feat.getOldId());
        if (featureDao.find(fOne).isEmpty()) {
            if (validate(feat)) {
                featureDao.save(feat);
            }
        }
    }

    public void updateFeature(Long featureId, String uid, String paramValue, Long percentValue, String radical,
            Integer a, Integer v, Integer k) {
        if (baseParamService.checkUid(uid) || StringAdapter.isNull(uid)) {
            Feature ft = featureDao.find(featureId);
            if (ft != null) {
                ft.setUid(uid);
                ft.setParamValue(StringAdapter.toDouble(paramValue));
                ft.setPercentValue(percentValue);
                ft.setRadical(radical);
                ft.setAudial(a);
                ft.setVisual(v);
                ft.setKinestet(k);
                if (validate(ft)) {
                    featureDao.update(ft);
                }
            } else {
                addError("? ?  : " + featureId);
            }
        } else {
            addError("     ? " + uid);

        }
    }

    public List<CarCompletionOption> getCcos() {
        return featureDao.getCcoDict();
    }

    public List<Long> getCmgs() {
        return featureDao.getCmgDict();
    }

    public List<Feature> getGroupOfFeatures(Long cmgId, Long ccoId) {
        /**
         * Feature ft = new Feature(); if(ccoId!=null){
         * if(ccoId.equals(Long.valueOf("0"))){ ft.setCcoId(new Long(null));
         * }else{ ft.setCcoId(ccoId); } } if(cmgId!=null){
         * if(cmgId.equals(Long.valueOf("0"))){ ft.setCmgId(new Long(null));
         * }else{ ft.setCmgId(ccoId); } }*
         */
        if (ccoId == null && cmgId == null) {
            addError("?  ");
        } else {
            return featureDao.getGroupOfFeatures(cmgId, ccoId);
        }
        return new ArrayList();

    }

    public void updateAllInGroup(Long cmgId, Long ccoId, String uid, String paramValue, Long percentValue,
            String radical, Integer a, Integer v, Integer k) {
        if (baseParamService.checkUid(uid)) {
            if (ccoId == null && cmgId == null) {
                addError("?  ");
            } else {
                List<Feature> flist = featureDao.getGroupOfFeatures(cmgId, ccoId);
                if (!flist.isEmpty()) {
                    for (Feature ufeat : flist) {
                        if (uid != null && !uid.equals("")) {
                            ufeat.setUid(uid);
                        }
                        if (paramValue != null && !paramValue.equals("")) {
                            ufeat.setParamValue(Double.valueOf(paramValue));
                        }
                        if (percentValue != null) {
                            ufeat.setPercentValue(percentValue);
                        }
                        if (radical != null) {
                            ufeat.setRadical(radical);
                        }
                        if (a != null) {
                            ufeat.setAudial(a);
                        }
                        if (v != null) {
                            ufeat.setAudial(v);
                        }
                        if (k != null) {
                            ufeat.setAudial(k);
                        }
                        featureDao.update(ufeat);
                    }
                }
            }
        } else {
            addError("     ? " + uid);

        }
    }

    public List<Feature> getFeatures(Car car) {
        return featureDao.getByCar(car);
    }

    public Feature findByOldId(Long oldId) {
        Feature supccg = new Feature();
        supccg.setOldId(oldId);
        List<Feature> list = featureDao.find(supccg);
        if (!list.isEmpty()) {
            if (list.size() == 1) {
                return list.get(0);
            } else {
                addError("       quto");
            }
        } else {
            addError("?  ");
        }
        return null;
    }

    public HSSFWorkbook getXls() {
        try {
            HSSFWorkbook workbook = new HSSFWorkbook();
            List<Feature> ftlist = featureDao.getAllAsc();
            HSSFSheet sheet = workbook.createSheet("FirsSheet");
            HSSFRow rowhead = sheet.createRow((short) 0);
            rowhead.createCell(0).setCellValue("ID");
            rowhead.createCell(1).setCellValue("TITLE");
            rowhead.createCell(2).setCellValue("DESCRIPTION");
            rowhead.createCell(3).setCellValue("CMGID");
            rowhead.createCell(4).setCellValue("CCOID");
            rowhead.createCell(5).setCellValue("UID");
            rowhead.createCell(6).setCellValue("");
            rowhead.createCell(7).setCellValue("");
            rowhead.createCell(8).setCellValue("");
            rowhead.createCell(9).setCellValue("?");
            rowhead.createCell(10).setCellValue("");
            rowhead.createCell(11).setCellValue("?");
            int n = 1;
            if (!ftlist.isEmpty()) {
                for (Feature ft : ftlist) {
                    HSSFRow rowbody = sheet.createRow((short) n);
                    rowbody.createCell(0).setCellValue(StringAdapter.getString(ft.getFeatureId()));
                    rowbody.createCell(1).setCellValue(StringAdapter.getString(ft.getTitle()));
                    rowbody.createCell(2).setCellValue(StringAdapter.getString(ft.getDescription()));
                    rowbody.createCell(3).setCellValue(StringAdapter.getString(ft.getCmgId()));
                    rowbody.createCell(4).setCellValue(StringAdapter.getString(ft.getCcoId()));
                    rowbody.createCell(5).setCellValue(StringAdapter.getString(ft.getUid()));
                    rowbody.createCell(6)
                            .setCellValue(StringAdapter.getString(ft.getParamValue()).replace(".", ","));
                    rowbody.createCell(7).setCellValue(StringAdapter.getString(ft.getPercentValue()));
                    rowbody.createCell(8).setCellValue(StringAdapter.getString(ft.getRadical()));
                    rowbody.createCell(9).setCellValue(StringAdapter.getString(ft.getAudial()));
                    rowbody.createCell(10).setCellValue(StringAdapter.getString(ft.getVisual()));
                    rowbody.createCell(11).setCellValue(StringAdapter.getString(ft.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);
            int rownumber = 1;
            HSSFWorkbook workbook = new HSSFWorkbook(fi);
            try {
                HSSFSheet sheet = workbook.getSheetAt(0);
                Iterator<Row> it = sheet.iterator();
                it.next();
                while (it.hasNext()) {
                    rownumber++;
                    Row row = it.next();
                    Cell idc = row.getCell(0);
                    Long id = null;
                    if (idc != null) {
                        id = StringAdapter.toLong(StringAdapter.HSSFSellValue(row.getCell(0)));
                    }
                    if (id != null) {

                        Cell uidc = row.getCell(5);
                        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(6);
                        Double val = (double) 0;
                        if (valc != null) {
                            String valstr = StringAdapter.HSSFSellValue(valc);
                            val = StringAdapter.toDouble(valstr);
                        }

                        Cell percc = row.getCell(7);
                        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(8);
                        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(9);
                        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(10);
                        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(11);
                        String k = "0";
                        if (kc != null) {
                            k = StringAdapter.HSSFSellValue(kc);
                            if (k.contains(".")) {
                                int point = k.indexOf(".");
                                k = k.substring(0, point);
                            }
                            if (k.equals("")) {
                                k = "0";
                            }
                        }
                        Feature cl = featureDao.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, " " + rownumber + ";   ")) {
                                featureDao.update(cl);
                            }
                        }
                    }
                }
            } catch (NumberFormatException e) {
                addError(" " + rownumber + ";   "
                        + StringAdapter.getStackTraceException(e));
            }
            workbook.close();
            fi.close();
        } catch (IOException e) {
            addError("  xml");
            addError(e.getMessage());
        }
    }
}