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 soc.scar.service.excel; import com.google.common.base.Strings; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.Serializable; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; 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.stereotype.Service; import soc.scar.controller.Test; import soc.scar.domain.excel.Feature; import soc.scar.domain.excel.FeatureType; import soc.scar.domain.excel.FeatureValue; import soc.scar.domain.excel.Module; import soc.scar.repository.excel.FeatureRepository; import soc.scar.repository.excel.FeatureTypeRepository; import soc.scar.repository.excel.FeatureValueRepository; import soc.scar.repository.excel.ModuleRepository; @Service("projectExcelService") public class ProjectExcelService implements Serializable { @Autowired private FeatureRepository featureRepository; @Autowired private FeatureTypeRepository featureTypeRepository; @Autowired private FeatureValueRepository featureValueRepository; @Autowired private ModuleRepository moduleRepository; private final String REF = "Ref."; private final String EMPTY1 = "-"; private final String EMPTY2 = " "; private final String EMPTY3 = ""; private HSSFWorkbook workbook; private Iterator<Row> rowIterator; private FileInputStream file; private List<FeatureType> featuresTypeList = new ArrayList<>(); private List<Feature> featuresList = new ArrayList<>(); private List<Module> modulesList = new ArrayList<>(); boolean checkNextFeatureType; public List<Feature> findAllFeature() { return featureRepository.findAll(); } public List<FeatureType> findAllFeatureType() { return featureTypeRepository.findAll(); } public List<FeatureValue> findAllFeatureValue() { return featureValueRepository.findAll(); } public Feature insertFeature(Feature feature) { return featureRepository.insert(feature); } public FeatureValue insertFeatureValue(FeatureValue featureValue) { return featureValueRepository.insert(featureValue); } public FeatureType insertFeatureType(FeatureType featureType) { return featureTypeRepository.insert(featureType); } public Module insertModule(Module module) { return moduleRepository.insert(module); } public Feature saveFeature(Feature feature) { return featureRepository.save(feature); } public FeatureType saveFeatureType(FeatureType featureType) { return featureTypeRepository.save(featureType); } public FeatureValue saveFeatureValue(FeatureValue featureValue) { return featureValueRepository.save(featureValue); } public Module saveModule(Module module) { return moduleRepository.insert(module); } public void deleteFeature(Feature feature) { featureRepository.delete(feature); } public void deleteFeatureType(FeatureType featureType) { featureTypeRepository.delete(featureType); } public void deleteFeatureValue(FeatureValue featureValue) { featureValueRepository.delete(featureValue); } public Module deleteModule(Module module) { return moduleRepository.insert(module); } public void load() throws IOException { featuresTypeList = new ArrayList<>(); featuresList = new ArrayList<>(); modulesList = new ArrayList<>(); checkNextFeatureType = false; try { file = new FileInputStream(new File( "/Users/migueljimenezromero/NetBeansProjects/scarProject/scar/src/main/webapp/data/prueba.xls")); //Get the workbook instance for XLS file workbook = new HSSFWorkbook(file); //Get first sheet from the workbook HSSFSheet sheet = workbook.getSheet("Configuracin de alcance"); //Iterate through each rows from first sheet rowIterator = sheet.iterator(); getFeatureType(); file.close(); } catch (FileNotFoundException ex) { Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex); } } public void getFeatureType() { Row row; Row rowP = null; Cell cell; boolean checkRefCell = false; while (rowIterator.hasNext()) { if (rowP != null) { row = rowP; rowP = null; } else { row = rowIterator.next(); } cell = row.getCell(0); if (null != cell && (REF).equalsIgnoreCase(cell.getStringCellValue())) { checkRefCell = true; modulesList = getModule(row); row = rowIterator.next(); } if (checkRefCell) { if (null == cell || (row.getCell(0).getStringCellValue().equalsIgnoreCase(EMPTY1)) || (row.getCell(0).getStringCellValue().isEmpty())) { FeatureType featureType = new FeatureType(); if (null != cell) { List<Feature> featuresByTypeList = new ArrayList<>(); cell = row.getCell(1); featureType.setFeatureType(cell.getStringCellValue()); rowP = getFeature(rowIterator, featureType, featuresByTypeList); featureType.setFeatures(featuresByTypeList); featuresTypeList.add(featureType); } else { List<Feature> featuresByTypeList = new ArrayList<>(); featureType.setFeatureType("Others"); rowP = getFeature(rowIterator, featureType, featuresByTypeList); featureType.setFeatures(featuresByTypeList); featuresTypeList.add(featureType); } } } } System.out.println(""); saveAll(featuresList, featuresTypeList, modulesList); } public Row getFeature(Iterator<Row> rowIterator, FeatureType featureType, List<Feature> featuresByTypeList) { Row row = null; Cell cell = null; while (rowIterator.hasNext()) { if (!checkNextFeatureType) { row = rowIterator.next(); cell = row.getCell(0); } if (cell != null && !Strings.isNullOrEmpty(row.getCell(0).getStringCellValue())) { List<FeatureValue> featuresValueByFeatureList = new ArrayList<>(); Feature feature = new Feature(); cell = row.getCell(1); feature.setFeatureDescription(cell.getStringCellValue()); // feature.setFeatureType(featureType); featuresValueByFeatureList = getFeatureValue(row, feature); feature.setFeatureValue(featuresValueByFeatureList); featuresList.add(feature); featuresByTypeList.add(feature); } else { if (cell == null || Strings.isNullOrEmpty(row.getCell(0).getStringCellValue()) && Strings.isNullOrEmpty(row.getCell(1).getStringCellValue())) { if (checkNextFeatureType) { List<FeatureValue> featuresValueByFeatureList = new ArrayList<>(); Feature feature = new Feature(); row = rowIterator.next(); cell = row.getCell(1); feature.setFeatureDescription(cell.getStringCellValue()); // feature.setFeatureType(featureType); featuresValueByFeatureList = getFeatureValue(row, feature); feature.setFeatureValue(featuresValueByFeatureList); featuresList.add(feature); featuresByTypeList.add(feature); } checkNextFeatureType = true; return row; } else { return row; } } } return row; } public List<FeatureValue> getFeatureValue(Row row, Feature feature) { List<FeatureValue> featuresValueByFeatureList = new ArrayList<>(); for (int i = 2; i < row.getLastCellNum(); i++) { Cell cell = row.getCell(i); if (cell != null && !row.getCell(i).getStringCellValue().equalsIgnoreCase(EMPTY3)) { FeatureValue featureValue = new FeatureValue(); featureValue.setFeatureValue(row.getCell(i).getStringCellValue()); featuresValueByFeatureList.add(featureValue); modulesList.get(i - 2).addFeaturesValue(featureValue); } if (cell == null) { FeatureValue featureValue = new FeatureValue(); featureValue.setFeatureValue(""); featuresValueByFeatureList.add(featureValue); modulesList.get(i - 2).addFeaturesValue(featureValue); } } return featuresValueByFeatureList; } public List<Module> getModule(Row rowRef) { List<Module> modulesList = new ArrayList<>(); Iterator<Cell> cellIteratorRef = rowRef.cellIterator(); Cell cell = cellIteratorRef.next(); cell = cellIteratorRef.next(); while (cellIteratorRef.hasNext()) { Module module = new Module(); cell = cellIteratorRef.next(); module.setReference(cell.getStringCellValue()); if (!checkModuleExist(module) && !Strings.isNullOrEmpty(module.getReference())) { insertModule(module); } if (checkModuleExist(module)) modulesList.add(module); } return modulesList; } public boolean checkFeatureTypeExist(FeatureType newFeatureType) { FeatureType featureType = featureTypeRepository.findByFeatureType(newFeatureType.getFeatureType()); return featureType != null; } public boolean checkFeatureExist(Feature newFeature) { Feature feature = featureRepository.findByFeatureDescription(newFeature.getFeatureDescription()); return feature != null; } public boolean checkModuleExist(Module newModule) { Module module = moduleRepository.findByReference(newModule.getReference()); return module != null; } public Feature getFeatureByDescription(Feature featureToCompare) { List<Feature> tempFeatures = new ArrayList<>(); tempFeatures = featureRepository.findAll(); Feature feature = new Feature(); for (Feature tempFeature : tempFeatures) { if (tempFeature.getFeatureDescription().equalsIgnoreCase(featureToCompare.getFeatureDescription())) feature = tempFeature; } return feature; } public void setNewFeatureValueToModule(Module module, FeatureValue featureValue) { Module newModule = moduleRepository.findByReference(module.getReference()); newModule.addFeaturesValue(featureValue); moduleRepository.save(newModule); } public void setNewFeatureTypeValueToModule(FeatureType featureType) { FeatureType newFeatureType = featureTypeRepository.findByFeatureType(featureType.getFeatureType()); newFeatureType.setFeatureType(featureType.getFeatureType()); featureTypeRepository.save(newFeatureType); } public void saveAll(List<Feature> featuresList, List<FeatureType> featuresTypeList, List<Module> modulesList) { for (Module module : modulesList) { moduleRepository.save(module); } for (Feature feature : featuresList) { featureRepository.save(feature); } for (FeatureType featureType : featuresTypeList) { featureTypeRepository.save(featureType); } System.out.println(""); } }