use of com.localidata.bean.SkosBean in project local-data-aragopedia by aragonopendata.
the class GenerateConfig method extractSkosConcept.
private void extractSkosConcept(List<String> csvLines, ArrayList<DataBean> skosData, ConfigBean configBean) {
log.debug("Init extractSkosConcept");
String headerLine = Utils.weakClean(csvLines.get(0));
String[] cells = headerLine.split("\t");
int[] posColumn = new int[skosData.size()];
for (int h = 0; h < skosData.size(); h++) {
String name = skosData.get(h).getName();
for (int i = 0; i < cells.length; i++) {
if (cells[i].equalsIgnoreCase(name)) {
boolean incluido = false;
for (int j = 0; j < posColumn.length; j++) {
if (posColumn[j] == i)
incluido = true;
}
if (!incluido) {
posColumn[h] = i;
break;
}
}
}
}
for (int h = 1; h < csvLines.size(); h++) {
String line = Utils.weakClean(csvLines.get(h));
if (Utils.v(line)) {
cells = line.split("\t");
for (int i = 0; i < skosData.size(); i++) {
try {
if (cells.length > posColumn[i]) {
String cell = cells[posColumn[i]];
SkosBean skosBean = new SkosBean();
String skosUrified = Utils.urlify(cell);
skosBean.setId(skosUrified);
skosBean.setLabel(Utils.weakClean(cell));
skosBean.setURI(Prop.host + "/kos/" + Prop.datasetName + "/" + Utils.urlify(skosData.get(i).getName()) + "/" + skosUrified);
DataBean dataBean = null;
if (skosExtrated.get(skosData.get(i).getName()) != null) {
dataBean = skosExtrated.get(skosData.get(i).getName());
} else {
dataBean = skosData.get(i);
}
if (dataBean.getMapSkos().get(skosBean.getId()) == null) {
dataBean.getMapSkos().put(skosBean.getId(), skosBean);
skosExtrated.put(dataBean.getName(), dataBean);
if (configBean != null)
configBean.getMapData().put(dataBean.getName(), dataBean);
}
}
} catch (ArrayIndexOutOfBoundsException e) {
log.error("ERROR al extraer los skos debido a incoherencia de columnas", e);
DataBean dataBean = null;
if (skosExtrated.get(skosData.get(i).getName()) != null) {
dataBean = skosExtrated.get(skosData.get(i).getName());
} else {
dataBean = skosData.get(i);
}
skosExtrated.put(dataBean.getName(), dataBean);
}
}
}
}
log.debug("End extractSkosConcept");
}
use of com.localidata.bean.SkosBean in project local-data-aragopedia by aragonopendata.
the class GenerateRDF method readXlsxFile.
private void readXlsxFile(File file, ConfigBean configBean) {
log.debug("Init readXlsxFile");
InputStream inp = null;
Workbook wb = null;
try {
inp = new FileInputStream(file);
wb = WorkbookFactory.create(inp);
} catch (FileNotFoundException e) {
log.error(e.getMessage());
} catch (InvalidFormatException e) {
log.error(e.getMessage());
} catch (IOException e) {
log.error(e.getMessage());
}
Sheet sheet = wb.getSheetAt(0);
Row rowName = sheet.getRow(0);
Row rowNameNormalized = sheet.getRow(1);
Row rowNormalization = sheet.getRow(2);
Row rowDimMesure = sheet.getRow(3);
Row rowType = sheet.getRow(4);
Row rowSkosfile = sheet.getRow(5);
Row rowConstant = sheet.getRow(6);
Row rowConstantValue = sheet.getRow(7);
Row rowRelationKos = sheet.getRow(8);
Row rowKosNameNormalized = sheet.getRow(9);
Row rowKosName = sheet.getRow(10);
boolean cont = true;
int columnReaded = 0;
while (cont) {
Cell cellName = rowName.getCell(columnReaded);
Cell cellNameNormalized = rowNameNormalized.getCell(columnReaded);
Cell cellNormalization = rowNormalization.getCell(columnReaded);
Cell cellDimMesure = rowDimMesure.getCell(columnReaded);
Cell cellType = rowType.getCell(columnReaded);
Cell cellSkosfile = null;
if (rowSkosfile != null)
cellSkosfile = rowSkosfile.getCell(columnReaded);
Cell cellConstant = null;
if (rowConstant != null)
cellConstant = rowConstant.getCell(columnReaded);
Cell cellConstantValue = null;
if (rowConstantValue != null)
cellConstantValue = rowConstantValue.getCell(columnReaded);
Cell cellRelationKos = null;
if (rowRelationKos != null)
cellRelationKos = rowRelationKos.getCell(columnReaded);
Cell cellKosName = null;
if (rowKosName != null)
cellKosName = rowKosName.getCell(columnReaded);
Cell cellKosNameNormalized = null;
if (rowKosNameNormalized != null)
cellKosNameNormalized = rowKosNameNormalized.getCell(columnReaded);
DataBean dataBean = new DataBean();
if (cellName == null) {
if (rowName.getCell((columnReaded + 1)) == null)
cont = false;
else
columnReaded++;
} else {
dataBean.setName(cellName.getStringCellValue());
if (cellNameNormalized != null) {
dataBean.setNameNormalized(cellNameNormalized.getStringCellValue());
} else {
log.error("Error in config " + file.getName() + " in cell name normalized");
}
if (cellNormalization != null) {
dataBean.setNormalizacion(cellNormalization.getStringCellValue());
} else {
log.error("Error in config " + file.getName() + " in cell normalization");
}
if (cellDimMesure != null) {
dataBean.setDimensionMesure(cellDimMesure.getStringCellValue());
} else {
log.error("Error in config " + file.getName() + " in cell dim mesure");
}
dataBean.setIdConfig(configBean.getId());
String type = "";
if (cellType != null) {
type = cellType.getStringCellValue();
} else {
type = "xsd:string";
}
dataBean.setType(type);
if (cellSkosfile != null && !cellSkosfile.getStringCellValue().equals("")) {
HashMap<String, SkosBean> mapSkos = readMappingFile(cellSkosfile.getStringCellValue());
dataBean.setMapSkos(mapSkos);
configBean.getMapData().put(dataBean.getNameNormalized(), dataBean);
dataWithSkos.add(dataBean);
} else {
configBean.getMapData().put(dataBean.getNameNormalized(), dataBean);
}
if (Prop.addDataConstant && cellConstant != null && cellConstant.getStringCellValue().equals(Constants.constante)) {
if (cellConstantValue != null) {
dataBean.setConstant(cellConstantValue.getStringCellValue() + "");
configBean.getListDataConstant().add(dataBean);
}
}
if (cellRelationKos != null) {
if (Utils.v(cellRelationKos.getStringCellValue())) {
dataBean.setRelationKos(cellRelationKos.getStringCellValue());
dataWithSkosHierarchical.add(dataBean);
}
}
if (Utils.v(cellKosNameNormalized) && Utils.v(cellKosNameNormalized.getStringCellValue())) {
dataBean.setKosNameNormalized(cellKosNameNormalized.getStringCellValue());
} else {
dataBean.setKosNameNormalized(dataBean.getNameNormalized());
}
if (Utils.v(cellKosName) && Utils.v(cellKosName.getStringCellValue())) {
dataBean.setKosName(cellKosName.getStringCellValue());
} else {
dataBean.setKosName(dataBean.getName());
}
columnReaded++;
}
}
log.debug("End readXlsxFile");
}
use of com.localidata.bean.SkosBean in project local-data-aragopedia by aragonopendata.
the class GenerateRDF method readMappingFileCSV.
private HashMap<String, SkosBean> readMappingFileCSV(String skosPath) {
log.debug("Init readMappingFileCSV");
HashMap<String, SkosBean> mapSkos = new HashMap<String, SkosBean>();
if (skosPath.endsWith("xlsx"))
skosPath = skosPath.replace("xlsx", "csv");
File skosMappingg = new File(configDirectoryString + File.separator + skosPath);
List<String> csvLines;
try {
csvLines = FileUtils.readLines(skosMappingg, "UTF-8");
for (String line : csvLines) {
String[] cells = line.split("\",\"");
String cellId = removeStartEndCaracter(cells[0]);
String cellUri = removeStartEndCaracter(cells[1]);
SkosBean skosBean = new SkosBean();
SkosBean skosBeanExtra = new SkosBean();
skosBean.setLabel(cellId);
cellId = Utils.urlify(cellId);
skosBean.setId(cellId);
String id = cellUri.substring(cellUri.lastIndexOf("/") + 1, cellUri.length());
if (!cellId.equals(id)) {
skosBeanExtra.setId(id);
skosBeanExtra.setLabel(id);
skosBeanExtra.setURI(cellUri);
mapSkos.put(id, skosBeanExtra);
}
skosBean.setURI(cellUri);
mapSkos.put(cellId, skosBean);
}
} catch (IOException e) {
log.error("Error read csv ", e);
}
log.debug("End readMappingFileCSV");
return mapSkos;
}
use of com.localidata.bean.SkosBean in project local-data-aragopedia by aragonopendata.
the class GenerateRDF method writeSkosTTL.
public void writeSkosTTL() {
log.debug("Init createSkos");
log.info("Init to create skos");
File kosFile = new File(outputDirectoryString + File.separator + "DatosTTL" + File.separator + "codelists" + File.separator + "kos.ttl");
StringBuffer resultIni = new StringBuffer();
StringBuffer resultFin = new StringBuffer();
ArrayList<String> kosCreated = new ArrayList<String>();
resultIni.append(TransformToRDF.addPrefix());
for (Iterator<DataBean> itDataBean = dataWithSkos.iterator(); itDataBean.hasNext(); ) {
DataBean dataBean = itDataBean.next();
if (dataBean != null && dataBean.isWriteSkos() && !kosCreated.contains(dataBean.getNameNormalized()) && dataBean.getMapSkos().size() > 0) {
String suject = Prop.host + "/" + Prop.kosName + "/" + Prop.datasetName + "/" + dataBean.getKosNameNormalized();
resultIni.append("<" + suject + "> " + "a skos:ConceptScheme;\n");
resultIni.append("\tskos:notation \"" + dataBean.getKosNameNormalized() + "\";\n");
resultIni.append("\trdfs:label \"" + dataBean.getKosName() + "\";\n");
for (Iterator<String> iterator3 = dataBean.getMapSkos().keySet().iterator(); iterator3.hasNext(); ) {
String keySkos = iterator3.next();
SkosBean skosBean = dataBean.getMapSkos().get(keySkos);
if (skosBean != null) {
String sujectKos = suject + "/" + Utils.urlify(skosBean.getId());
if (skosBean.getParent() == null) {
resultIni.append("\tskos:hasTopConcept <" + sujectKos + ">");
if (iterator3.hasNext()) {
resultIni.append(";\n");
} else {
resultIni.append(".\n");
}
}
resultFin.append("<" + sujectKos + "> a skos:Concept;\n");
resultFin.append("\tskos:inScheme <" + suject + ">;\n");
String label = skosBean.getId();
if (skosBean.getLabel() != null && !skosBean.getLabel().equals(""))
label = skosBean.getLabel();
resultFin.append("\tskos:notation \"" + skosBean.getId() + "\";\n");
resultFin.append("\tskos:prefLabel \"" + Utils.prefLabelClean(label) + "\"");
if (skosBean.getParent() != null) {
resultFin.append(";\n");
resultFin.append("\tskos:broader <" + suject + "/" + skosBean.getParent().getId() + ">");
}
if (skosBean.getSons().size() > 0) {
resultFin.append(";\n");
for (Iterator<SkosBean> itSons = skosBean.getSons().iterator(); itSons.hasNext(); ) {
SkosBean son = itSons.next();
resultFin.append("\tskos:narrower <" + suject + "/" + son.getId() + ">");
if (itSons.hasNext()) {
resultFin.append(";\n");
} else {
resultFin.append(".\n");
}
}
} else {
resultFin.append(".\n");
}
resultFin.append("\n");
}
}
resultIni.append("\n");
resultIni.append(resultFin);
kosCreated.add(dataBean.getNameNormalized());
}
Utils.stringToFileAppend(resultIni.toString(), kosFile);
resultIni.setLength(0);
resultFin.setLength(0);
}
log.info("end to create skos");
log.debug("End createSkos");
}
Aggregations