From b94bd70f1595fad9546c3506393613f68504f495 Mon Sep 17 00:00:00 2001 From: Ofir Sonsino Date: Sun, 11 Feb 2018 19:21:35 +0200 Subject: org.onap migration Change-Id: I77143c1fee8cb918a04881dfa4c33bedb811e9c6 Issue-ID: VID-86 Signed-off-by: Ofir Sonsino --- .../org/onap/portalapp/conf/ExternalAppConfig.java | 2 +- .../portalapp/conf/HibernateMappingLocations.java | 2 +- .../main/webapp/WEB-INF/jsp/searchexistingsi.jsp | 2 +- .../src/main/webapp/WEB-INF/jsp/serviceModels.jsp | 2 +- .../src/main/webapp/WEB-INF/jsp/testMso.jsp | 2 +- .../src/main/webapp/WEB-INF/jsp/testViewEdit.jsp | 2 +- vid-app-common/pom.xml | 8 + .../java/org/onap/vid/services/CsvServiceImpl.java | 500 ++++++++++----------- 8 files changed, 264 insertions(+), 256 deletions(-) diff --git a/epsdk-app-onap/src/main/java/org/onap/portalapp/conf/ExternalAppConfig.java b/epsdk-app-onap/src/main/java/org/onap/portalapp/conf/ExternalAppConfig.java index eb110931e..1583bc92d 100644 --- a/epsdk-app-onap/src/main/java/org/onap/portalapp/conf/ExternalAppConfig.java +++ b/epsdk-app-onap/src/main/java/org/onap/portalapp/conf/ExternalAppConfig.java @@ -64,7 +64,7 @@ import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate; */ @Configuration @EnableWebMvc -@ComponentScan(basePackages = "org.openecomp") +@ComponentScan(basePackages = {"org.openecomp", "org.onap"}) @PropertySource(value = { "${container.classpath:}/WEB-INF/conf/app/test.properties" }, ignoreResourceNotFound = true) @Profile("src") @EnableAsync diff --git a/epsdk-app-onap/src/main/java/org/onap/portalapp/conf/HibernateMappingLocations.java b/epsdk-app-onap/src/main/java/org/onap/portalapp/conf/HibernateMappingLocations.java index e0e09d6a9..3116346ee 100644 --- a/epsdk-app-onap/src/main/java/org/onap/portalapp/conf/HibernateMappingLocations.java +++ b/epsdk-app-onap/src/main/java/org/onap/portalapp/conf/HibernateMappingLocations.java @@ -51,7 +51,7 @@ public class HibernateMappingLocations implements HibernateMappingLocatable { */ @Override public String[] getPackagesToScan() { - return new String[] { "org.openecomp" }; + return new String[] { "org.openecomp", "org.onap" }; } } diff --git a/epsdk-app-onap/src/main/webapp/WEB-INF/jsp/searchexistingsi.jsp b/epsdk-app-onap/src/main/webapp/WEB-INF/jsp/searchexistingsi.jsp index ca840e62f..76fe13e4b 100644 --- a/epsdk-app-onap/src/main/webapp/WEB-INF/jsp/searchexistingsi.jsp +++ b/epsdk-app-onap/src/main/webapp/WEB-INF/jsp/searchexistingsi.jsp @@ -43,7 +43,7 @@ -<%@ page import="org.openecomp.vid.mso.*"%> +<%@ page import="org.onap.vid.mso.*"%> <%@ page import="org.openecomp.portalsdk.core.util.SystemProperties"%> <% String properties = "{msoMaxPolls:" + SystemProperties.getProperty(MsoProperties.MSO_MAX_POLLS) diff --git a/epsdk-app-onap/src/main/webapp/WEB-INF/jsp/serviceModels.jsp b/epsdk-app-onap/src/main/webapp/WEB-INF/jsp/serviceModels.jsp index 88131c593..7a983a109 100755 --- a/epsdk-app-onap/src/main/webapp/WEB-INF/jsp/serviceModels.jsp +++ b/epsdk-app-onap/src/main/webapp/WEB-INF/jsp/serviceModels.jsp @@ -87,7 +87,7 @@ -<%@ page import="org.openecomp.vid.mso.*"%> +<%@ page import="org.onap.vid.mso.*"%> <%@ page import="org.openecomp.portalsdk.core.util.SystemProperties"%> <% String properties = "{msoMaxPolls:" + SystemProperties.getProperty(MsoProperties.MSO_MAX_POLLS) diff --git a/epsdk-app-onap/src/main/webapp/WEB-INF/jsp/testMso.jsp b/epsdk-app-onap/src/main/webapp/WEB-INF/jsp/testMso.jsp index 8678dcee1..87f863f0c 100755 --- a/epsdk-app-onap/src/main/webapp/WEB-INF/jsp/testMso.jsp +++ b/epsdk-app-onap/src/main/webapp/WEB-INF/jsp/testMso.jsp @@ -31,7 +31,7 @@ Consider / investigate replacing this approach with an alternative REST / Angular mechanism. --> -<%@ page import="org.openecomp.vid.mso.*"%> +<%@ page import="org.onap.vid.mso.*"%> <%@ page import="org.openecomp.portalsdk.core.util.SystemProperties"%> <% String properties = "{msoMaxPolls:" + SystemProperties.getProperty(MsoProperties.MSO_MAX_POLLS) diff --git a/epsdk-app-onap/src/main/webapp/WEB-INF/jsp/testViewEdit.jsp b/epsdk-app-onap/src/main/webapp/WEB-INF/jsp/testViewEdit.jsp index 2c8022263..f8d8d9c3f 100755 --- a/epsdk-app-onap/src/main/webapp/WEB-INF/jsp/testViewEdit.jsp +++ b/epsdk-app-onap/src/main/webapp/WEB-INF/jsp/testViewEdit.jsp @@ -40,7 +40,7 @@ Consider / investigate replacing this approach with an alternative REST / Angular mechanism. --> -<%@ page import="org.openecomp.vid.mso.*"%> +<%@ page import="org.onap.vid.mso.*"%> <%@ page import="org.openecomp.portalsdk.core.util.SystemProperties"%> <% String properties = "{msoMaxPolls:" + SystemProperties.getProperty(MsoProperties.MSO_MAX_POLLS) diff --git a/vid-app-common/pom.xml b/vid-app-common/pom.xml index dae47987f..dd00576bc 100755 --- a/vid-app-common/pom.xml +++ b/vid-app-common/pom.xml @@ -244,6 +244,14 @@ + + + com.opencsv + opencsv + 4.1 + + + diff --git a/vid-app-common/src/main/java/org/onap/vid/services/CsvServiceImpl.java b/vid-app-common/src/main/java/org/onap/vid/services/CsvServiceImpl.java index 8152dae04..689da34b2 100644 --- a/vid-app-common/src/main/java/org/onap/vid/services/CsvServiceImpl.java +++ b/vid-app-common/src/main/java/org/onap/vid/services/CsvServiceImpl.java @@ -1,250 +1,250 @@ -//package org.onap.vid.services; -// -////import com.opencsv.CSVReader; -//import org.json.JSONArray; -//import org.json.JSONObject; -//import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate; -//import org.springframework.stereotype.Service; -//import org.springframework.web.multipart.MultipartFile; -// -//import javax.ws.rs.BadRequestException; -//import java.io.FileNotFoundException; -//import java.io.FileReader; -//import java.io.IOException; -//import java.io.InputStreamReader; -//import java.util.ArrayList; -//import java.util.Arrays; -//import java.util.List; -// -//import static org.onap.vid.utils.Logging.getMethodName; -// -//@Service -//public class CsvServiceImpl implements CsvService{ -// -// -// /** The logger. */ -// static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CsvServiceImpl.class); -// -// private static final String arrayRegex = "\\[(.*?)\\]"; -// -// -// /** -// * In UTF-8 the first line starts with "\uFEFF" so need to remove it -// * @param line is first line contains BOM -// * @return line after change -// */ -// private String [] removeBOMFromCsv(String [] line){ -// if (line.length>0) -// line[0] = line[0].replaceFirst("\uFEFF","").replaceFirst("",""); -// return line; -// } -// -// /** -// * read a csv file and puts its content in list of string arrays (without the empty lines) -// * @param filePath - the path of file to read -// * @return the content of file -// * @throws IOException -// */ -// /*@Override -// public List readCsv(String filePath) throws IOException { -// CSVReader reader = new CSVReader(new FileReader(filePath)); -// return readCsv(reader); -// } -// -// @Override -// public List readCsv(MultipartFile file) throws IOException { -// CSVReader reader = new CSVReader(new InputStreamReader(file.getInputStream())); -// return readCsv(reader); -// } -// -// private List addLineWithoutSpaces(List myEntries, String [] line){ -// line = Arrays.stream(line).filter(x -> !"".equals(x)).toArray(String[]::new); -// if(line.length > 0) -// myEntries.add(line); -// return myEntries; -// } -// -// -// private List readCsv(CSVReader reader) throws IOException { -// try { -// List myEntries = new ArrayList<>() ; -// String [] line; -// Boolean firstLine = true; -// while ((line = reader.readNext())!= null) { -// if (firstLine) { -// line = removeBOMFromCsv(line); -// firstLine = false; -// } -// myEntries = addLineWithoutSpaces(myEntries, line); -// } -// return myEntries; -// } -// catch (Exception e){ -// logger.error("error during reading CSV file. exception:" + e.getMessage()); -// throw e; -// } -// -// }*/ -// -// /** -// * main function that call to the recursive function with initial parameters -// * @param myEntries - the matrix with file content -// * @return the json -// * @throws IOException -// * @throws InstantiationException -// * @throws IllegalAccessException -// */ -// @Override -// public JSONObject convertCsvToJson (List myEntries) throws InstantiationException, IllegalAccessException { -// try { -// return buildJSON(myEntries, 0, 0, myEntries.size(), JSONObject.class); -// } -// catch (Exception e){ -// logger.error("error during parsing CSV file. exception:" + e.getMessage()); -// throw e; -// } -// -// } -// -// /** -// * it goes over the matrix column while the values are the same and returns the index of changed value -// * @param myEntries the matrix -// * @param i row index refer to the whole matrix -// * @param j column index -// * @param numLines the length of the current inner matrix -// * @param startLine row index of inner matrix -// * @return the index of changed line -// */ -// private int findIndexOfChangedLine(List myEntries, final int i, final int j, final int numLines, final int startLine) { -// int k; -// for(k = 0; k + i - startLine < numLines && myEntries.get(i)[j].equals(myEntries.get(k + i)[j]) ; k++); -// return k; -// } -// -// /** -// * check in array if its first element or if the key already exist in the previous item -// * @param jsonArray - the array to search in -// * @param key - the key to check -// * @return if exists or first element return true, otherwise- false -// */ -// private Boolean keyExistsOrFirstElement( JSONArray jsonArray,String key){ -// Boolean exists = false; -// Boolean first = false; -// JSONObject lastItem = lastItemInArray(jsonArray); -// if (lastItem == null) { -// first = true; -// } -// else { -// if (lastItem.has(key)) { -// exists = true; -// } -// } -// return exists||first; -// } -// -// /** -// * return last json in json array -// * @param jsonArray -// * @return last item or null if the array is empty -// */ -// private JSONObject lastItemInArray(JSONArray jsonArray){ -// JSONObject lastItem = null; -// if(jsonArray.length()>0) { -// lastItem = (JSONObject) jsonArray.get(jsonArray.length() - 1); -// } -// return lastItem; -// } -// -// /** -// * append current json to the main json -// * @param json - the main json to append to it -// * @param key - key to append -// * @param values - value(s) to append -// * @param can be JSONObject or JSONArray -// * @param string or jsonObject or jsonArray -// * @return json after put -// * @throws IllegalAccessException -// * @throws InstantiationException -// */ -// private T putJson(T json, String key, E values) throws IllegalAccessException, InstantiationException { -// if (json instanceof JSONArray){ -// JSONArray currentJson= ((JSONArray)json); -// if (values == null) //array of strings (for last item) -// { -// currentJson.put(key); -// } -// else { -// if (keyExistsOrFirstElement(currentJson, key)) { -// currentJson.put(new JSONObject().put(key, values)); -// } else { -// JSONObject lastItem = lastItemInArray(currentJson); -// lastItem.put(key, values); -// } -// } -// } -// if (json instanceof JSONObject){ -// if (values == null) -// throw new BadRequestException("Invalid csv file"); -// ((JSONObject)json).put(key,values); -// } -// return json; -// } -// -// -// /** -// * recursive function to build JSON. Each time it counts the same values in left and send the smaller matrix -// * (until the changed value) to the next time. -// * -// * @param myEntries - the whole matrix -// * @param i- row index of the whole matrix -// * @param j - column index -// * @param numLines - number of lines of inner matrix (num of same values in the left column) -// * @param clazz JSONArray or JSONObject -// * @param JSONArray or JSONObject -// * @return the json object -// * @throws IllegalAccessException -// * @throws InstantiationException -// */ -// private T buildJSON(List myEntries, int i, final int j, final int numLines, Class clazz) throws IllegalAccessException, InstantiationException { -// logger.debug(EELFLoggerDelegate.debugLogger, "start {}({}, {}, {})", getMethodName(), i, j, numLines); -// T json = clazz.newInstance(); -// int startLine = i; -// while(i < numLines + startLine){ -// String[] currentRow = myEntries.get(i); -// int length = currentRow.length; -// int currentDuplicateRows = findIndexOfChangedLine(myEntries,i,j,numLines, startLine); -// String key = currentRow[j]; -// if (j == length-1) { -// json = putJson(json,currentRow[j],null); -// -// } -// else -// { -// if (key.matches(arrayRegex)){ -// JSONArray arrayObjects = buildJSON(myEntries, i, j + 1, currentDuplicateRows, JSONArray.class); -// json = putJson(json,key.replaceAll("\\[","").replaceAll("]",""),arrayObjects); -// } -// else { -// if (j < length - 2) { -// json = putJson(json, currentRow[j], buildJSON(myEntries, i, j + 1, currentDuplicateRows, JSONObject.class)); -// } -// else -// { -// if (j == length - 2)//last object -// { -// if(currentDuplicateRows > 1) { -// throw new BadRequestException("Invalid csv file"); -// } -// json = putJson(json, currentRow[j], currentRow[j + 1]); -// } -// } -// } -// } -// i += currentDuplicateRows; -// } -// logger.debug(EELFLoggerDelegate.debugLogger, "end {} json = {}", getMethodName(), json); -// return json; -// } -// -//} -// +package org.onap.vid.services; + +import com.opencsv.CSVReader; +import org.json.JSONArray; +import org.json.JSONObject; +import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; + +import javax.ws.rs.BadRequestException; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.onap.vid.utils.Logging.getMethodName; + +@Service +public class CsvServiceImpl implements CsvService{ + + + /** The logger. */ + static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CsvServiceImpl.class); + + private static final String arrayRegex = "\\[(.*?)\\]"; + + + /** + * In UTF-8 the first line starts with "\uFEFF" so need to remove it + * @param line is first line contains BOM + * @return line after change + */ + private String [] removeBOMFromCsv(String [] line){ + if (line.length>0) + line[0] = line[0].replaceFirst("\uFEFF","").replaceFirst("",""); + return line; + } + + /** + * read a csv file and puts its content in list of string arrays (without the empty lines) + * @param filePath - the path of file to read + * @return the content of file + * @throws IOException + */ + @Override + public List readCsv(String filePath) throws IOException { + CSVReader reader = new CSVReader(new FileReader(filePath)); + return readCsv(reader); + } + + @Override + public List readCsv(MultipartFile file) throws IOException { + CSVReader reader = new CSVReader(new InputStreamReader(file.getInputStream())); + return readCsv(reader); + } + + private List addLineWithoutSpaces(List myEntries, String [] line){ + line = Arrays.stream(line).filter(x -> !"".equals(x)).toArray(String[]::new); + if(line.length > 0) + myEntries.add(line); + return myEntries; + } + + + private List readCsv(CSVReader reader) throws IOException { + try { + List myEntries = new ArrayList<>() ; + String [] line; + Boolean firstLine = true; + while ((line = reader.readNext())!= null) { + if (firstLine) { + line = removeBOMFromCsv(line); + firstLine = false; + } + myEntries = addLineWithoutSpaces(myEntries, line); + } + return myEntries; + } + catch (Exception e){ + logger.error("error during reading CSV file. exception:" + e.getMessage()); + throw e; + } + + } + + /** + * main function that call to the recursive function with initial parameters + * @param myEntries - the matrix with file content + * @return the json + * @throws IOException + * @throws InstantiationException + * @throws IllegalAccessException + */ + @Override + public JSONObject convertCsvToJson (List myEntries) throws InstantiationException, IllegalAccessException { + try { + return buildJSON(myEntries, 0, 0, myEntries.size(), JSONObject.class); + } + catch (Exception e){ + logger.error("error during parsing CSV file. exception:" + e.getMessage()); + throw e; + } + + } + + /** + * it goes over the matrix column while the values are the same and returns the index of changed value + * @param myEntries the matrix + * @param i row index refer to the whole matrix + * @param j column index + * @param numLines the length of the current inner matrix + * @param startLine row index of inner matrix + * @return the index of changed line + */ + private int findIndexOfChangedLine(List myEntries, final int i, final int j, final int numLines, final int startLine) { + int k; + for(k = 0; k + i - startLine < numLines && myEntries.get(i)[j].equals(myEntries.get(k + i)[j]) ; k++); + return k; + } + + /** + * check in array if its first element or if the key already exist in the previous item + * @param jsonArray - the array to search in + * @param key - the key to check + * @return if exists or first element return true, otherwise- false + */ + private Boolean keyExistsOrFirstElement( JSONArray jsonArray,String key){ + Boolean exists = false; + Boolean first = false; + JSONObject lastItem = lastItemInArray(jsonArray); + if (lastItem == null) { + first = true; + } + else { + if (lastItem.has(key)) { + exists = true; + } + } + return exists||first; + } + + /** + * return last json in json array + * @param jsonArray + * @return last item or null if the array is empty + */ + private JSONObject lastItemInArray(JSONArray jsonArray){ + JSONObject lastItem = null; + if(jsonArray.length()>0) { + lastItem = (JSONObject) jsonArray.get(jsonArray.length() - 1); + } + return lastItem; + } + + /** + * append current json to the main json + * @param json - the main json to append to it + * @param key - key to append + * @param values - value(s) to append + * @param can be JSONObject or JSONArray + * @param string or jsonObject or jsonArray + * @return json after put + * @throws IllegalAccessException + * @throws InstantiationException + */ + private T putJson(T json, String key, E values) throws IllegalAccessException, InstantiationException { + if (json instanceof JSONArray){ + JSONArray currentJson= ((JSONArray)json); + if (values == null) //array of strings (for last item) + { + currentJson.put(key); + } + else { + if (keyExistsOrFirstElement(currentJson, key)) { + currentJson.put(new JSONObject().put(key, values)); + } else { + JSONObject lastItem = lastItemInArray(currentJson); + lastItem.put(key, values); + } + } + } + if (json instanceof JSONObject){ + if (values == null) + throw new BadRequestException("Invalid csv file"); + ((JSONObject)json).put(key,values); + } + return json; + } + + + /** + * recursive function to build JSON. Each time it counts the same values in left and send the smaller matrix + * (until the changed value) to the next time. + * + * @param myEntries - the whole matrix + * @param i- row index of the whole matrix + * @param j - column index + * @param numLines - number of lines of inner matrix (num of same values in the left column) + * @param clazz JSONArray or JSONObject + * @param JSONArray or JSONObject + * @return the json object + * @throws IllegalAccessException + * @throws InstantiationException + */ + private T buildJSON(List myEntries, int i, final int j, final int numLines, Class clazz) throws IllegalAccessException, InstantiationException { + logger.debug(EELFLoggerDelegate.debugLogger, "start {}({}, {}, {})", getMethodName(), i, j, numLines); + T json = clazz.newInstance(); + int startLine = i; + while(i < numLines + startLine){ + String[] currentRow = myEntries.get(i); + int length = currentRow.length; + int currentDuplicateRows = findIndexOfChangedLine(myEntries,i,j,numLines, startLine); + String key = currentRow[j]; + if (j == length-1) { + json = putJson(json,currentRow[j],null); + + } + else + { + if (key.matches(arrayRegex)){ + JSONArray arrayObjects = buildJSON(myEntries, i, j + 1, currentDuplicateRows, JSONArray.class); + json = putJson(json,key.replaceAll("\\[","").replaceAll("]",""),arrayObjects); + } + else { + if (j < length - 2) { + json = putJson(json, currentRow[j], buildJSON(myEntries, i, j + 1, currentDuplicateRows, JSONObject.class)); + } + else + { + if (j == length - 2)//last object + { + if(currentDuplicateRows > 1) { + throw new BadRequestException("Invalid csv file"); + } + json = putJson(json, currentRow[j], currentRow[j + 1]); + } + } + } + } + i += currentDuplicateRows; + } + logger.debug(EELFLoggerDelegate.debugLogger, "end {} json = {}", getMethodName(), json); + return json; + } + +} + -- cgit 1.2.3-korg