/* * ============LICENSE_START========================================== * ONAP Portal SDK * =================================================================== * Copyright © 2017 AT&T Intellectual Property. All rights reserved. * =================================================================== * * Unless otherwise specified, all software contained herein is licensed * under the Apache License, Version 2.0 (the "License"); * you may not use this software except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Unless otherwise specified, all documentation contained herein is licensed * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); * you may not use this documentation except in compliance with the License. * You may obtain a copy of the License at * * https://creativecommons.org/licenses/by/4.0/ * * Unless required by applicable law or agreed to in writing, documentation * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * ============LICENSE_END============================================ * * */ package org.onap.portalsdk.analytics.util; import java.io.File; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Vector; import java.util.regex.Pattern; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.onap.portalsdk.analytics.error.RaptorException; import org.onap.portalsdk.analytics.model.base.IdNameValue; import org.onap.portalsdk.analytics.system.AppUtils; import org.onap.portalsdk.analytics.system.Globals; import org.onap.portalsdk.analytics.system.fusion.adapter.Item; public class Utils extends org.onap.portalsdk.analytics.RaptorObject { public Utils() { } public static String getCurrentDateTime() { return (new SimpleDateFormat(Globals.getJavaTimeFormat())).format(new Date()); } public static String truncateDecimals(String value, int maxDecimals) { return (maxDecimals < 0 || value == null || value.indexOf('.') < 0 || (value.indexOf('.') == value.length() - 1) || value.substring( value.indexOf('.')).length() - 1 <= maxDecimals) ? value : value.substring(0, value.indexOf('.') + maxDecimals + 1); } public static String truncateTotalDecimals(String value) { return truncateDecimals(value, Globals.getMaxDecimalsOnTotals()); } public static String replaceInString(String replaceInStr, String replaceStr, String replaceWithStr) { if (replaceStr.equals(replaceWithStr)) return replaceInStr; while (replaceInStr!=null && replaceInStr.indexOf(replaceStr) >= 0) { int startIdx = replaceInStr.indexOf(replaceStr); int endIdx = startIdx + replaceStr.length(); StringBuffer sb = new StringBuffer(); if (startIdx > 0) sb.append(replaceInStr.substring(0, startIdx)); sb.append(nvls(replaceWithStr)); if (endIdx < replaceInStr.length()) sb.append(replaceInStr.substring(endIdx)); replaceInStr = sb.toString(); } return replaceInStr; } public static String singleQuoteEncode(String value) { value = value!=null?value:""; value = Pattern.compile("[\']",Pattern.DOTALL).matcher(value).replaceAll("\\\\\\'"); return value; } public static String htmlEncode(String value) { return replaceInString(replaceInString(value, "<", "<"), ">", ">"); } public static String excelEncode(String value) { String replaceStr = replaceInString(replaceInString(value, "<", "<"), ">", ">"); String reg = "&(?!&#)"; Pattern p = Pattern.compile(reg); String replaceStrAmpersand = p.matcher(replaceStr).replaceAll("&"); return replaceStrAmpersand; } public static String oracleSafe(String s) { if (s == null) return null; StringBuffer sb = new StringBuffer(); for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (ch == '\''/* &&(i>=s.length()-1||s.charAt(i+1)!='\'') */) sb.append('\''); sb.append(ch); } return sb.toString(); } public static String javaSafe(String s) { if (s == null) return null; StringBuffer sb = new StringBuffer(); for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (ch == '"') sb.append('\\'); sb.append(ch); } return sb.toString(); } public static Vector getUsersNotInList(List excludeValues, HttpServletRequest request)throws RaptorException { HttpSession session = request.getSession(); String query = Globals.getCustomizedScheduleQueryForUsers(); session.setAttribute("login_id", AppUtils.getUserBackdoorLoginId(request)); String userId = AppUtils.getUserID(request); session.setAttribute("LOGGED_USERID", userId); String[] sessionParameters = Globals.getSessionParams().split(","); String param = ""; for (int i = 0; i < sessionParameters.length; i++) { param = (String)session.getAttribute(sessionParameters[0]); query = Utils.replaceInString(query, "[" + sessionParameters[i].toUpperCase()+"]", (String)session.getAttribute(sessionParameters[i]) ); } boolean isAdmin = AppUtils.isAdminUser(request); Vector allUsers = AppUtils.getAllUsers(query,param, isAdmin); Vector result = new Vector(allUsers.size()); for (Iterator iter = allUsers.iterator(); iter.hasNext();) { IdNameValue value = (IdNameValue) iter.next(); boolean exclude = false; for (Iterator iterE = excludeValues.iterator(); iterE.hasNext();) if (((IdNameValue) iterE.next()).getId().equals(value.getId())) { exclude = true; break; } if (!exclude) result.add(value); } return result; } public static Vector getRolesNotInList(List excludeValues, HttpServletRequest request) throws RaptorException { HttpSession session = request.getSession(); String query = Globals.getCustomizedScheduleQueryForRoles(); session.setAttribute("login_id", AppUtils.getUserBackdoorLoginId(request)); String userId = AppUtils.getUserID(request); session.setAttribute("LOGGED_USERID", userId); String[] sessionParameters = Globals.getSessionParams().split(","); String param = ""; for (int i = 0; i < sessionParameters.length; i++) { param = (String)session.getAttribute(sessionParameters[0]); query = Utils.replaceInString(query, "[" + sessionParameters[i].toUpperCase()+"]", (String)session.getAttribute(sessionParameters[i]) ); } boolean isAdmin = AppUtils.isAdminUser(request); Vector allRoles = AppUtils.getAllRoles(query, param, isAdmin); Vector result = new Vector(allRoles.size()); for (Iterator iter = allRoles.iterator(); iter.hasNext();) { IdNameValue value = (IdNameValue) iter.next(); boolean exclude = false; for (Iterator iterE = excludeValues.iterator(); iterE.hasNext();) if (((IdNameValue) iterE.next()).getId().equals(value.getId())) { exclude = true; break; } if (value.getId().equals(AppUtils.getSuperRoleID())) exclude = true; if (!exclude) result.add(value); } return result; } public static List getUsersNotInListLatest(List excludeValues, HttpServletRequest request)throws RaptorException { HttpSession session = request.getSession(); String query = Globals.getCustomizedScheduleQueryForUsers(); session.setAttribute("login_id", AppUtils.getUserBackdoorLoginId(request)); String userId = AppUtils.getUserID(request); session.setAttribute("LOGGED_USERID", userId); String[] sessionParameters = Globals.getSessionParams().split(","); String param = ""; for (int i = 0; i < sessionParameters.length; i++) { param = (String)session.getAttribute(sessionParameters[i]); query = Utils.replaceInString(query, "[" + sessionParameters[i].toUpperCase()+"]", (String)session.getAttribute(sessionParameters[i]) ); } boolean isAdmin = AppUtils.isAdminUser(request); Vector allUsers = AppUtils.getAllUsers(query,param, isAdmin); Vector result = new Vector(allUsers.size()); List resultLatest = new ArrayList<>(); for (Iterator iter = allUsers.iterator(); iter.hasNext();) { IdNameValue value = (IdNameValue) iter.next(); boolean exclude = false; for (Iterator iterE = excludeValues.iterator(); iterE.hasNext();) if (((IdNameValue) iterE.next()).getId().equals(value.getId())) { exclude = true; break; } if (!exclude) resultLatest.add(new Item(value.getId(), value.getName())); } return resultLatest; } public static List getRolesNotInListLatest(List excludeValues, HttpServletRequest request) throws RaptorException { HttpSession session = request.getSession(); String query = Globals.getCustomizedScheduleQueryForRoles(); session.setAttribute("login_id", AppUtils.getUserBackdoorLoginId(request)); String userId = AppUtils.getUserID(request); session.setAttribute("LOGGED_USERID", userId); String[] sessionParameters = Globals.getSessionParams().split(","); String param = ""; for (int i = 0; i < sessionParameters.length; i++) { param = (String)session.getAttribute(sessionParameters[i]); query = Utils.replaceInString(query, "[" + sessionParameters[i].toUpperCase()+"]", (String)session.getAttribute(sessionParameters[i]) ); } boolean isAdmin = AppUtils.isAdminUser(request); Vector allRoles = AppUtils.getAllRoles(query, param, isAdmin); Vector result = new Vector(allRoles.size()); List resultLatest = new ArrayList<>(); for (Iterator iter = allRoles.iterator(); iter.hasNext();) { IdNameValue value = (IdNameValue) iter.next(); boolean exclude = false; for (Iterator iterE = excludeValues.iterator(); iterE.hasNext();) if (((IdNameValue) iterE.next()).getId().equals(value.getId())) { exclude = true; break; } if (value.getId().equals(AppUtils.getSuperRoleID())) exclude = true; if (!exclude) resultLatest.add(new Item(value.getId(), value.getName())); } return resultLatest; } public static void _assert(boolean condition, String errMsg) { if (org.onap.portalsdk.analytics.system.Globals.getDebugLevel() > 0) if (!condition) throw new RuntimeException(errMsg); } public static boolean isNull(String a) { if ((a == null) || (a.length() == 0) || a.equalsIgnoreCase("null")) return true; else return false; } public static boolean isDownloadFileExists(String fileNamePrefix) { File f = new File (Globals.getShellScriptDir()+AppConstants.SHELL_DATA_DIR); String[] fileNames = f.list(); if(fileNames!=null) { for (int i = 0; i < fileNames.length; i++) { if(fileNames[i].startsWith(fileNamePrefix)) { return true; } } } return false; } public static String getLatestDownloadableFile(String fileNamePrefix) { File f = new File (Globals.getShellScriptDir()+AppConstants.SHELL_DATA_DIR); String[] fileNames = f.list(); ArrayList matchingFiles = new ArrayList(); for (int i = 0; i < fileNames.length; i++) { if(fileNames[i].startsWith(fileNamePrefix)) { matchingFiles.add(fileNames[i]); } } String tmpFileName = ""; int numberOfTimesLooped = 0; boolean isSorted = false; SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); Date date1 = null; Date date2 = null; Date currDate = new Date(); Object[] matchingfileNamesArr = matchingFiles.toArray(); String fileName1 = ""; String fileName2 =""; do { isSorted = true; for (int j = 1; j < matchingfileNamesArr.length - numberOfTimesLooped++; j++) { fileName1 = (String) matchingfileNamesArr[j]; fileName2 = (String) matchingfileNamesArr[j-1]; try{ date1 = sdf.parse(fileName1.substring(fileName1.lastIndexOf("_")+1,fileName1.lastIndexOf("."))); date2 = sdf.parse(fileName2.substring(fileName2.lastIndexOf("_")+1,fileName2.lastIndexOf("."))); } catch(ParseException ex) { return null; } if ( (currDate.getTime()-date1.getTime()) < (currDate.getTime()-date2.getTime())) { tmpFileName = fileName1; matchingfileNamesArr[j] = fileName2; matchingfileNamesArr[j-1] = tmpFileName; isSorted = false; } } } while (!isSorted); if(matchingfileNamesArr.length>0) return (String)matchingfileNamesArr[0]; else return null; } public static String removeLinkToForDownload(String value) { int i = value.length() - 1; int start, end = i + 1; StringBuffer result = new StringBuffer(); while (i >= 0) { if (value.charAt(i) == ',') { start = i + 1; while (start != end) result.append(value.charAt(start++)); result.append(','); end = i; } i--; } start = 0; while (start != end) result.append(value.charAt(start++)); if (result.toString().contains("clickToCheckBox|checked|filled") || result.toString().contains("clickToCheckBox|checked|disabled")) { // result = new StringBuffer(); // result.append("Y"); return "Y"; } else if (result.toString().contains("clickToCheckBox|Y|empty") || result.toString().contains("clickToCheckBox|N|disabled") || result.toString().contains("isComment")) { // result = new StringBuffer(""); return ""; } return result.toString(); } public static boolean isRemoveLinkToEligible(String value) { if(value.contains("linkTo") || value.contains("clickToCheckBox") || value.contains("isComment")) { return true; } return false; } }