You need to sign in to do that
Don't have an account?
Laxmi Pandey 20
Conversion into Apex
Hi ,
I have got a requirement to convert the below Java code in Apex. This is to flatten multivalue for row level security. Any suggestion/help will be appreciated. Thanks.
public class MultiValueFileUtils {
private static final String FILE_DIR= "src/main/resources/";
private static final String DELIM=",";
private static final String MV_DELIM="|";
public static void main(String[] aArgs){
try{
// createHashSet();
// printOutFlatten();
// printFlattenFile();
printNormalizedFile();
}catch(Exception e){
e.printStackTrace();
}
}
private static void createHashSet(){
HashSet<String> userIds = new HashSet<String>();
userIds.add("ABC");
userIds.add("DEF");
userIds.add("GHI");
Map<String, HashSet<String>> territoryMap = new HashMap<String, HashSet<String>>();
territoryMap.put("TER1", userIds);
HashSet<String> moreuserIds = new HashSet<String>();
moreuserIds.add("ABC");
moreuserIds.add("DDD");
moreuserIds.add("HHH");
territoryMap.get("TER1").addAll(moreuserIds);
HashSet<String> completeList =territoryMap.get("TER1");
for (String s : completeList) {
System.out.println(s);
}
}
private static Map<String,HashSet<String>> joinMVtoHiearchy(String aHiearchyFile, String aMultiValueFile ){
Map<String,HashSet<String>> hiearchyMap=flattenHiearchy(aHiearchyFile);
Map<String,HashSet<String>> keyMVMap=flattenField(aMultiValueFile);
Map<String,HashSet<String>> hiearchyMVMap =new HashMap <String,HashSet<String>>();
for (Map.Entry<String, HashSet<String>> entry : hiearchyMap.entrySet()) {
HashSet<String> hiearchySet =entry.getValue();
HashSet<String> completeMVSet = new HashSet<String>();
for (String s : hiearchySet) {
HashSet<String> partMVSet =keyMVMap.get(s);
if(partMVSet!=null){
completeMVSet.addAll(partMVSet);
}
}
hiearchyMVMap.put(entry.getKey(), completeMVSet);
}
return hiearchyMVMap;
}
private static void printOutFlatten(){
Map<String,HashSet<String>> keyMVMap=flattenField("TerritoryUserNarrow.csv");
int i=1;
for (Map.Entry<String, HashSet<String>> entry : keyMVMap.entrySet()) {
System.out.print("Key :"+entry.getKey() +" MV : ");
HashSet<String> hashSet =entry.getValue();
for (String s : hashSet) {
System.out.print(s);
}
System.out.println();
System.out.println(i++);
}
}
private static void printFlattenFile(){
// Map<String,HashSet<String>> keyMVMap=flattenField("TerritoryUserNarrow.csv");
Map<String,HashSet<String>> keyMVMap=joinMVtoHiearchy("TerritoryNarrow.csv","TerritoryUserNarrow.csv");
BufferedWriter bw=null;
String line=null;
File fileOut=null;
int noOfLines=0;
try {
fileOut=FileUtils.getFile(FILE_DIR+"TerritoryUsersComplete.csv");
bw=new BufferedWriter(new FileWriter(fileOut));
bw.write("TerritoryId,UserIds");
for (Map.Entry<String, HashSet<String>> entry : keyMVMap.entrySet()) {
HashSet<String> hashSet =entry.getValue();
StringBuffer sbuf=new StringBuffer();
sbuf.append(entry.getKey()).append(DELIM);
for (String s : hashSet) {
sbuf.append(s).append("|");
}
log("last index "+sbuf.lastIndexOf(""));
line=sbuf.toString();
line =line.substring(0,line.lastIndexOf("|"));
bw.newLine();
bw.write(line);
}
}catch (Exception e){
log("Total of lines :"+(noOfLines));
e.printStackTrace();
}finally{
if(bw!=null)try{bw.close();}catch(IOException ioe){}
}
}
private static void printNormalizedFile(){
Map<String,HashSet<String>> keyMVMap=joinMVtoHiearchy("TerritoryNarrow.csv","TerritoryUserNarrow.csv");
BufferedWriter bw=null;
String line=null;
File fileOut=null;
int noOfLines=0;
try {
fileOut=FileUtils.getFile(FILE_DIR+"TerritoryUserNormalized.csv");
bw=new BufferedWriter(new FileWriter(fileOut));
bw.write("TerritoryId,UserId");
for (Map.Entry<String, HashSet<String>> entry : keyMVMap.entrySet()) {
HashSet<String> hashSet =entry.getValue();
for (String s : hashSet) {
StringBuffer sbuf=new StringBuffer();
sbuf.append(entry.getKey()).append(DELIM).append(s);
line=sbuf.toString();
bw.newLine();
bw.write(line);
}
}
}catch (Exception e){
log("Total of lines :"+(noOfLines));
e.printStackTrace();
}finally{
if(bw!=null)try{bw.close();}catch(IOException ioe){}
}
}
private static Map<String,HashSet<String>> flattenHiearchy(String aFileName){
BufferedReader br=null;
BufferedWriter bw=null;
String line=null;
File fileIn=null;
long noOfLines=0;
Map <String,String> childParentMap =new HashMap<String,String>();
Map<String,HashSet<String>> keyMVMap=new HashMap <String,HashSet<String>>();
HashSet<String> hiearchySet=null;
try {
long startTime =System.currentTimeMillis();
fileIn =FileUtils.getFile(FILE_DIR+aFileName);
br=new BufferedReader(new FileReader(fileIn));
boolean isHeader=true;
String id=null;
String parentId=null;
String childMostId=null;
while ((line = br.readLine()) != null) {
String[] fields=line.split(DELIM,-1);
if(isHeader){
isHeader =false;
}else {
id=fields[0];
if (StringUtils.isNotBlank(fields[1])){
parentId=fields[1];
}else{
parentId=null;
}
childParentMap.put(id,parentId);
}
}
for (Map.Entry<String, String> entry :childParentMap.entrySet()) {
hiearchySet= new HashSet<String>();
addParentId(childParentMap,entry.getKey(),hiearchySet );
keyMVMap.put(entry.getKey(),hiearchySet);
}
log("Duration ms : "+(System.currentTimeMillis()-startTime));
}catch (Exception e){
log("Total of lines :"+(noOfLines));
e.printStackTrace();
}finally{
if(br!=null)try{br.close();}catch(IOException ioe){}
if(bw!=null)try{bw.close();}catch(IOException ioe){}
}
return keyMVMap;
}
private static String addParentId(Map<String,String> childParentMap, String childId, HashSet<String> hiearchySet){
hiearchySet.add(childId);
String parentId =childParentMap.get(childId);
if(parentId!=null){
parentId=addParentId(childParentMap,parentId, hiearchySet);
}
return parentId;
}
private static Map<String,HashSet<String>> flattenField(String fileName){
BufferedReader br=null;
BufferedWriter bw=null;
String line=null;
File fileIn=null;
long noOfLines=0;
Map<String,HashSet<String>> keyMVMap=new HashMap <String,HashSet<String>>();
HashSet<String> multiValue=null;
try {
long startTime =System.currentTimeMillis();
fileIn =FileUtils.getFile(FILE_DIR+fileName);
br=new BufferedReader(new FileReader(fileIn));
boolean isHeader=true;
String prevRowId=null;
String currRowId=null;
while ((line = br.readLine()) != null) {
String[] fields=line.split(DELIM,-1);
if(isHeader){
isHeader =false;
}else {
if (StringUtils.isNotBlank(fields[1])){
currRowId=fields[0];
if(currRowId!=null&&!currRowId.equals(prevRowId)){
if(multiValue!=null){
keyMVMap.put(prevRowId, multiValue);
}
multiValue= new HashSet<String>();
}
multiValue.add(fields[1]);
}
prevRowId=currRowId;
}
}
if(multiValue!=null){
keyMVMap.put(currRowId, multiValue);
}
log("Duration ms : "+(System.currentTimeMillis()-startTime));
}catch (Exception e){
log("Total of lines :"+(noOfLines));
e.printStackTrace();
}finally{
if(br!=null)try{br.close();}catch(IOException ioe){}
if(bw!=null)try{bw.close();}catch(IOException ioe){}
}
return keyMVMap;
}
private static void log(Object aOut){
System.out.println(aOut);
}
}
I have got a requirement to convert the below Java code in Apex. This is to flatten multivalue for row level security. Any suggestion/help will be appreciated. Thanks.
public class MultiValueFileUtils {
private static final String FILE_DIR= "src/main/resources/";
private static final String DELIM=",";
private static final String MV_DELIM="|";
public static void main(String[] aArgs){
try{
// createHashSet();
// printOutFlatten();
// printFlattenFile();
printNormalizedFile();
}catch(Exception e){
e.printStackTrace();
}
}
private static void createHashSet(){
HashSet<String> userIds = new HashSet<String>();
userIds.add("ABC");
userIds.add("DEF");
userIds.add("GHI");
Map<String, HashSet<String>> territoryMap = new HashMap<String, HashSet<String>>();
territoryMap.put("TER1", userIds);
HashSet<String> moreuserIds = new HashSet<String>();
moreuserIds.add("ABC");
moreuserIds.add("DDD");
moreuserIds.add("HHH");
territoryMap.get("TER1").addAll(moreuserIds);
HashSet<String> completeList =territoryMap.get("TER1");
for (String s : completeList) {
System.out.println(s);
}
}
private static Map<String,HashSet<String>> joinMVtoHiearchy(String aHiearchyFile, String aMultiValueFile ){
Map<String,HashSet<String>> hiearchyMap=flattenHiearchy(aHiearchyFile);
Map<String,HashSet<String>> keyMVMap=flattenField(aMultiValueFile);
Map<String,HashSet<String>> hiearchyMVMap =new HashMap <String,HashSet<String>>();
for (Map.Entry<String, HashSet<String>> entry : hiearchyMap.entrySet()) {
HashSet<String> hiearchySet =entry.getValue();
HashSet<String> completeMVSet = new HashSet<String>();
for (String s : hiearchySet) {
HashSet<String> partMVSet =keyMVMap.get(s);
if(partMVSet!=null){
completeMVSet.addAll(partMVSet);
}
}
hiearchyMVMap.put(entry.getKey(), completeMVSet);
}
return hiearchyMVMap;
}
private static void printOutFlatten(){
Map<String,HashSet<String>> keyMVMap=flattenField("TerritoryUserNarrow.csv");
int i=1;
for (Map.Entry<String, HashSet<String>> entry : keyMVMap.entrySet()) {
System.out.print("Key :"+entry.getKey() +" MV : ");
HashSet<String> hashSet =entry.getValue();
for (String s : hashSet) {
System.out.print(s);
}
System.out.println();
System.out.println(i++);
}
}
private static void printFlattenFile(){
// Map<String,HashSet<String>> keyMVMap=flattenField("TerritoryUserNarrow.csv");
Map<String,HashSet<String>> keyMVMap=joinMVtoHiearchy("TerritoryNarrow.csv","TerritoryUserNarrow.csv");
BufferedWriter bw=null;
String line=null;
File fileOut=null;
int noOfLines=0;
try {
fileOut=FileUtils.getFile(FILE_DIR+"TerritoryUsersComplete.csv");
bw=new BufferedWriter(new FileWriter(fileOut));
bw.write("TerritoryId,UserIds");
for (Map.Entry<String, HashSet<String>> entry : keyMVMap.entrySet()) {
HashSet<String> hashSet =entry.getValue();
StringBuffer sbuf=new StringBuffer();
sbuf.append(entry.getKey()).append(DELIM);
for (String s : hashSet) {
sbuf.append(s).append("|");
}
log("last index "+sbuf.lastIndexOf(""));
line=sbuf.toString();
line =line.substring(0,line.lastIndexOf("|"));
bw.newLine();
bw.write(line);
}
}catch (Exception e){
log("Total of lines :"+(noOfLines));
e.printStackTrace();
}finally{
if(bw!=null)try{bw.close();}catch(IOException ioe){}
}
}
private static void printNormalizedFile(){
Map<String,HashSet<String>> keyMVMap=joinMVtoHiearchy("TerritoryNarrow.csv","TerritoryUserNarrow.csv");
BufferedWriter bw=null;
String line=null;
File fileOut=null;
int noOfLines=0;
try {
fileOut=FileUtils.getFile(FILE_DIR+"TerritoryUserNormalized.csv");
bw=new BufferedWriter(new FileWriter(fileOut));
bw.write("TerritoryId,UserId");
for (Map.Entry<String, HashSet<String>> entry : keyMVMap.entrySet()) {
HashSet<String> hashSet =entry.getValue();
for (String s : hashSet) {
StringBuffer sbuf=new StringBuffer();
sbuf.append(entry.getKey()).append(DELIM).append(s);
line=sbuf.toString();
bw.newLine();
bw.write(line);
}
}
}catch (Exception e){
log("Total of lines :"+(noOfLines));
e.printStackTrace();
}finally{
if(bw!=null)try{bw.close();}catch(IOException ioe){}
}
}
private static Map<String,HashSet<String>> flattenHiearchy(String aFileName){
BufferedReader br=null;
BufferedWriter bw=null;
String line=null;
File fileIn=null;
long noOfLines=0;
Map <String,String> childParentMap =new HashMap<String,String>();
Map<String,HashSet<String>> keyMVMap=new HashMap <String,HashSet<String>>();
HashSet<String> hiearchySet=null;
try {
long startTime =System.currentTimeMillis();
fileIn =FileUtils.getFile(FILE_DIR+aFileName);
br=new BufferedReader(new FileReader(fileIn));
boolean isHeader=true;
String id=null;
String parentId=null;
String childMostId=null;
while ((line = br.readLine()) != null) {
String[] fields=line.split(DELIM,-1);
if(isHeader){
isHeader =false;
}else {
id=fields[0];
if (StringUtils.isNotBlank(fields[1])){
parentId=fields[1];
}else{
parentId=null;
}
childParentMap.put(id,parentId);
}
}
for (Map.Entry<String, String> entry :childParentMap.entrySet()) {
hiearchySet= new HashSet<String>();
addParentId(childParentMap,entry.getKey(),hiearchySet );
keyMVMap.put(entry.getKey(),hiearchySet);
}
log("Duration ms : "+(System.currentTimeMillis()-startTime));
}catch (Exception e){
log("Total of lines :"+(noOfLines));
e.printStackTrace();
}finally{
if(br!=null)try{br.close();}catch(IOException ioe){}
if(bw!=null)try{bw.close();}catch(IOException ioe){}
}
return keyMVMap;
}
private static String addParentId(Map<String,String> childParentMap, String childId, HashSet<String> hiearchySet){
hiearchySet.add(childId);
String parentId =childParentMap.get(childId);
if(parentId!=null){
parentId=addParentId(childParentMap,parentId, hiearchySet);
}
return parentId;
}
private static Map<String,HashSet<String>> flattenField(String fileName){
BufferedReader br=null;
BufferedWriter bw=null;
String line=null;
File fileIn=null;
long noOfLines=0;
Map<String,HashSet<String>> keyMVMap=new HashMap <String,HashSet<String>>();
HashSet<String> multiValue=null;
try {
long startTime =System.currentTimeMillis();
fileIn =FileUtils.getFile(FILE_DIR+fileName);
br=new BufferedReader(new FileReader(fileIn));
boolean isHeader=true;
String prevRowId=null;
String currRowId=null;
while ((line = br.readLine()) != null) {
String[] fields=line.split(DELIM,-1);
if(isHeader){
isHeader =false;
}else {
if (StringUtils.isNotBlank(fields[1])){
currRowId=fields[0];
if(currRowId!=null&&!currRowId.equals(prevRowId)){
if(multiValue!=null){
keyMVMap.put(prevRowId, multiValue);
}
multiValue= new HashSet<String>();
}
multiValue.add(fields[1]);
}
prevRowId=currRowId;
}
}
if(multiValue!=null){
keyMVMap.put(currRowId, multiValue);
}
log("Duration ms : "+(System.currentTimeMillis()-startTime));
}catch (Exception e){
log("Total of lines :"+(noOfLines));
e.printStackTrace();
}finally{
if(br!=null)try{br.close();}catch(IOException ioe){}
if(bw!=null)try{bw.close();}catch(IOException ioe){}
}
return keyMVMap;
}
private static void log(Object aOut){
System.out.println(aOut);
}
}
See example here