data.services.ColorGroupService.java Source code

Java tutorial

Introduction

Here is the source code for data.services.ColorGroupService.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.ColorGroupDao;
import data.entity.ColorGroup;
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.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.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 ColorGroupService extends PrimService {

    @Autowired
    private ColorGroupDao colorGroupDao;
    @Autowired
    private BaseParamService baseParamService;

    public List<ColorGroup> getColorGroups() {
        return colorGroupDao.getAllAsc("colorGroupId");
    }

    public void createColorGroup(ColorGroup cg) throws Exception {
        ColorGroup cgOne = new ColorGroup();
        cgOne.setOldGroupId(cg.getOldGroupId());
        cgOne.setOldId(cg.getOldId());
        if (colorGroupDao.find(cgOne).isEmpty()) {
            if (validate(cg)) {
                colorGroupDao.save(cg);
            }
        }
    }

    public void updateColor(Long colorGroupId, String uid, String paramValue, Long percentValue, String radical) {
        if (baseParamService.checkUid(uid)) {
            ColorGroup clg = colorGroupDao.find(colorGroupId);
            if (clg != null) {
                clg.setUid(uid);
                clg.setParamValue(StringAdapter.toDouble(paramValue));
                clg.setPercentValue(percentValue);
                clg.setRadical(radical);
                if (validate(clg)) {
                    colorGroupDao.update(clg);
                }
            } else {
                addError(" ?     " + colorGroupId);
            }
        } else {
            addError("     ? " + uid);

        }
    }

    public ColorGroup findByOldId(Long oldId) {
        List<ColorGroup> list = colorGroupDao.findByQId(oldId);
        if (!list.isEmpty()) {
            //if(list.size()==1){
            return list.get(0);
            /*}else{
            addError("       quto: "+oldId);
            }*/
        } else {
            addError("?   , : " + oldId);
        }
        return null;
    }

    public void updateAllInGroup(Long oldGroupId, String uid, String paramValue, Long percentValue,
            String radical) {
        if (baseParamService.checkUid(uid)) {
            ColorGroup clg = new ColorGroup();
            clg.setOldGroupId(oldGroupId);

            for (ColorGroup groupClg : colorGroupDao.find(clg)) {
                groupClg.setUid(uid);
                groupClg.setParamValue(StringAdapter.toDouble(paramValue));
                groupClg.setPercentValue(percentValue);
                groupClg.setRadical(radical);
                if (validate(groupClg)) {
                    colorGroupDao.update(groupClg);
                }
            }
        } else {
            addError("     ? " + uid);

        }
    }

    public List<ColorGroup> getGroups() {
        return colorGroupDao.findDict();
    }

    public HSSFWorkbook getXls() {
        Logger log = Logger.getLogger(this.getClass());
        try {
            HSSFWorkbook workbook = new HSSFWorkbook();
            List<ColorGroup> oblist = colorGroupDao.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("????");
            rowhead.createCell(3).setCellValue("UID");
            rowhead.createCell(4).setCellValue("");
            rowhead.createCell(5).setCellValue("");
            rowhead.createCell(6).setCellValue("");
            int n = 1;
            if (!oblist.isEmpty()) {
                for (ColorGroup ob : oblist) {
                    HSSFRow rowbody = sheet.createRow((short) n);
                    rowbody.createCell(0).setCellValue(StringAdapter.getString(ob.getColorGroupId()));
                    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()));
                    rowbody.createCell(5).setCellValue(StringAdapter.getString(ob.getPercentValue()));
                    rowbody.createCell(6).setCellValue(StringAdapter.getString(ob.getRadical()));
                    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));*/
                    String uid = row.getCell(3).getStringCellValue();
                    String val = row.getCell(4).getStringCellValue();
                    String pers = row.getCell(5).getStringCellValue();
                    String rad = row.getCell(6).getStringCellValue();

                    ColorGroup cl = colorGroupDao.find(id);
                    if (cl != null) {
                        cl.setUid(uid);
                        cl.setParamValue(StringAdapter.toDouble(val));
                        cl.setParamValue(StringAdapter.toDouble(pers));
                        cl.setRadical(rad);
                        if (validate(cl)) {
                            colorGroupDao.update(cl);
                        }
                    }
                }
                workbook.close();
            } catch (Exception e) {
                addError(StringAdapter.getStackTraceException(e));
            }
            fi.close();
        } catch (Exception e) {
            addError("  xml");
            addError(e.getMessage());
        }
    }
}