import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.ss.usermodel.CellType;
import com.collibra.dgc.workflow.api.exception.WorkflowException;
import com.collibra.dgc.core.api.dto.MatchMode;
import com.collibra.dgc.core.api.dto.instance.community.FindCommunitiesRequest;
import com.collibra.dgc.core.api.dto.instance.domain.FindDomainsRequest;
import com.collibra.dgc.core.api.dto.meta.assettype.FindAssetTypesRequest;
import com.collibra.dgc.core.api.dto.instance.asset.FindAssetsRequest;
import com.collibra.dgc.core.api.dto.instance.attribute.FindAttributesRequest;
import com.collibra.dgc.core.api.dto.instance.asset.AddAssetRequest;
import com.collibra.dgc.core.api.dto.instance.asset.SetAssetAttributesRequest;
import com.collibra.dgc.core.api.dto.instance.asset.ChangeAssetRequest;
import com.collibra.dgc.core.api.component.application.ApplicationApi;
import com.collibra.dgc.core.api.dto.meta.relationtype.FindRelationTypesRequest;
import com.collibra.dgc.core.api.model.instance.Relation;
import com.collibra.dgc.core.api.dto.instance.relation.AddRelationRequest;
import com.collibra.dgc.core.api.model.instance.Asset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import org.apache.commons.collections.MultiMap;
import org.apache.commons.collections.map.MultiValueMap;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

loggerApi.info('---Bulk Import Veld Data Point start---');
loggerApi.info("**** importFile: ${importFile}")

if(!fileApi.getFileInfo(string2Uuid(importFile.toString())).getName().endsWith('.xlsx')){
	def dgcError = new WorkflowException("File Type Not Recognized, please use .xlsx")
	dgcError.setTitleMessage("File Type Not Recognized");
	dgcError.setUserMessage('File Type Not Recognized, please use .xlsx');
	throw dgcError;  
}

String importFileName = fileApi.getFileInfo(string2Uuid(importFile.toString())).getName();
FindCommunitiesRequest comReq = new FindCommunitiesRequest();
comReq.setName(communityName);
comReq.setNameMatchMode(MatchMode.valueOf("EXACT"));
def coms = communityApi.findCommunities(comReq).getResults();



def uidVdpCom;
def uidVdpDom;
if(coms.size() == 1) {
	uidVdpCom = coms[0].getId();
	FindDomainsRequest domReq = new FindDomainsRequest();
	domReq.setCommunityId(uidVdpCom);
	domReq.setName(domainName);
	domReq.setNameMatchMode(MatchMode.valueOf("EXACT"));
	def doms = domainApi.findDomains(domReq).getResults();
	if(doms.size()==1) {
		uidVdpDom = doms[0].getId();
	}
	if(doms.size()==0) {
		loggerApi.info('---Domain not found---');
	}
}
loggerApi.info('---Bulk Import Veld Data Point start1---');

def uidVdpTypeId = assetTypeApi.findAssetTypes(FindAssetTypesRequest.builder().
	name(assetType).nameMatchMode(MatchMode.EXACT).build()).
	getResults()[0].getId();
loggerApi.info('---Bulk Import Veld Data Point start2---');

uidVdpDef = attributeTypeApi.getAttributeTypeByName('Definition').getId();
def uidBusinessSubArea = attributeTypeApi.getAttributeTypeByName('Business Subject Area').getId();
def uidVeldId = attributeTypeApi.getAttributeTypeByName('Veld Id').getId();
def uidVpd = attributeTypeApi.getAttributeTypeByName('Veld Primary Domain').getId();
def uidVsecD = attributeTypeApi.getAttributeTypeByName('Veld Secondary Domain').getId();
def uidVsD = attributeTypeApi.getAttributeTypeByName('Veld Sub Domain').getId();
def uidnao = attributeTypeApi.getAttributeTypeByName('Next Action Owner').getId();
loggerApi.info('---Bulk Import Veld Data Point start3---');
def uidVqp = attributeTypeApi.getAttributeTypeByName('Veld Query Path').getId();
def uidCl = attributeTypeApi.getAttributeTypeByName('Computing Logic').getId();
def uidds = attributeTypeApi.getAttributeTypeByName('Delivery Status').getId();
def uidVms = attributeTypeApi.getAttributeTypeByName('Veld Model Status').getId();
loggerApi.info('---Bulk Import Veld Data Point start4---');
def VDP2DARelTypeUuid = relationTypeApi.findRelationTypes(FindRelationTypesRequest.builder()
	.coRole('implemented by')
	.role('implements')
	.sourceTypeName('Veld Data Point')
	.targetTypeName('Data Attribute')
	.build()).getResults()[0].getId();
loggerApi.info('---Bulk Import Veld Data Point star5t---');
def uidDETypeId = assetTypeApi.findAssetTypes(FindAssetTypesRequest.builder().
	name('Data Entity').nameMatchMode(MatchMode.EXACT).build()).
	getResults()[0].getId();
loggerApi.info('---Bulk Import Veld Data Point start6---');
def uidDATypeId = assetTypeApi.findAssetTypes(FindAssetTypesRequest.builder().
	name('Data Attribute').nameMatchMode(MatchMode.EXACT).build()).
	getResults()[0].getId();
loggerApi.info('---Bulk Import Veld Data Point start7---');
def VDP2BDPRelTypeUuid = relationTypeApi.findRelationTypes(FindRelationTypesRequest.builder()
	.coRole('connected to')
	.role('connected to')
	.sourceTypeName('Business Term')
	.targetTypeName('Veld Data Point')
	.build()).getResults()[0].getId();
loggerApi.info('---Bulk Import Veld Data Point start8---');
def allVdpAssets = assetApi.findAssets(FindAssetsRequest.builder().communityId(uidVdpCom).
	domainId(uidVdpDom).build()).getResults();

def uidSchemaTypeId = assetTypeApi.findAssetTypes(FindAssetTypesRequest.builder().
	name('Schema').nameMatchMode(MatchMode.EXACT).build()).
	getResults()[0].getId();

def uidTableTypeId = assetTypeApi.findAssetTypes(FindAssetTypesRequest.builder().
	name('Table').nameMatchMode(MatchMode.EXACT).build()).
	getResults()[0].getId();

def uidColumnTypeId = assetTypeApi.findAssetTypes(FindAssetTypesRequest.builder().
	name('Column').nameMatchMode(MatchMode.EXACT).build()).
	getResults()[0].getId();
	

// def VDPNotCreatedList = []
Set<String> VDPNotCreatedList = new TreeSet<String>();
// Set<String> VDPDupList = new TreeSet<String>();
List<String> VDPDupList = new ArrayList<String>(); 
List<String> VDPDupList_name = new ArrayList<String>(); 
def VDPNotcreatedIDLIST = []
def newVDPs = []
int skippedVDPCounter = 0
def DANotexistList = []

MultiMap attributeNonExistMultiValueMap = new MultiValueMap();
//MultiMap<String, Set<String>> attributeNonExistMultiValueMap = new MultiValueMap();


workbook = new XSSFWorkbook(fileApi.getFileAsStream(string2Uuid(importFile))) ;
def sheet = workbook.getSheetAt(0) ;
def sheet_name = workbook.getSheetName(0) ;
loggerApi.info('---sheet_name---' + sheet_name);

//def sheet = workbook.getSheet('ProductandParty_BDP')

def excelCellTypeandValue(cellType, cellObj) {
	String cellValue = "";
	String cellRawValue = "";
	int dotIndex;
	
	if(cellType.equals(CellType.STRING)){
		cellValue = cellObj.getStringCellValue()
	} else if (cellType.equals(CellType.NUMERIC)){
		cellRawValue = cellObj.getNumericCellValue()

		dotIndex = cellRawValue.indexOf(".");

		if(dotIndex >= 0){

			if(Integer.parseInt(cellRawValue.substring(cellRawValue.indexOf(".") + 1)) == 0){
				cellValue = cellRawValue.substring(0, cellRawValue.indexOf("."))

			} else {
				cellValue = cellRawValue;

			}
		} else {
			cellValue = cellRawValue;
		}
	} else if (cellType.equals(CellType.BOOLEAN)){
		cellValue = cellObj.getBooleanCellValue()
	}
	return cellValue.trim();
}

def inValidValueExist(map, mapKey, attrValue) {
	boolean attrValueExist = false;
	Collection<String> values = map.get(mapKey);

	if(values != null){
		for (String value : values) {
			if(value.equalsIgnoreCase(attrValue)){
				attrValueExist = true;
				break;
			}
		}
	}
	return attrValueExist;
}

if(sheet.getPhysicalNumberOfRows() > 0) {
    int Veld_entries = sheet.getPhysicalNumberOfRows() - 1 ;
	execution.setVariable("Veld_entries",Veld_entries); 
	def rowIter = sheet.iterator()
//	int VeldID ; 
	def VdpName = ""
	def VdpDefinition = ""
	String VeldPrimaryDomain = ""
	String VeldSecondaryDomain = ""
	String VeldSubDomain = ""
	Date businessRequestDate;
	def NextActionOwner = ""
	def VeldQueryPath = ""
	def ComputingLogic = ""
	String DeliveryStatus = ""
	String VeldModelStatus = ""
	def DataAttribute  = ""
	def BusinessDataPoints = ""
	def columnDomainIds = [];
	def columnDomainId = "";
	def allColumnAssets = [];
	def columnUUID = "";
	int dotCount = 0;
	String schema = "";
	String table = "";
	String column = "";
	String decimal = "." ;
	String columnFullName = "";
	int firstIndex=0;
	int lastIndex=0;
	// int Vdp2bdprelcounter =0;
	// int bdpcounter= 0;
	
	def existingVDPAssets = []
//	def existingVDPIdList = []
//	def existingVDPdpList = []
List<String> existingVDPIdList = new ArrayList<String>(); 
List<String> existingVDPdpList = new ArrayList<String>();

	def existVdpAssetId = []
	int existVdpSeq = 0
	def existVdpRequestId
	//ArrayList<String> existVeldIdList = new ArrayList<String>();
	Set<String> existVeldIdList = new TreeSet<String>();
	//ArrayList<Integer> seqList = new ArrayList<Integer>();
	Set<Integer> seqList = new TreeSet<Integer>();
	
	Set<String> VDPDANotexistList = new TreeSet<String>();
	execution.setVariable("VDPDANotexistList",VDPDANotexistList);
	def fullName
	String sequenceNumber = ""
	def VdpAssetIdToCheck = []
	def fullNameToSearch
	def assetPatternToSrearch
	String finalVeldId = ""
//	int finalVeldId
	def uidCurrentVdpAsset
	int number = 7;
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	String strBusinessRequestDate = ""
	boolean processing = true;
	
	def allVeldAssets = assetApi.findAssets(FindAssetsRequest.builder().communityId(uidVdpCom).
	domainId(uidVdpDom).build()).getResults();
	// start  list for the Veld Id's
	if (allVeldAssets.size()>0) {
	//loggerApi.info('---Sequence loop starts---');
	for(itrVeldAsset in allVeldAssets) {
		existAssetId = itrVeldAsset.getId();
		existAssetdp = itrVeldAsset.getDisplayName();
		loggerApi.info('---existAssetdp---' + existAssetdp);
		existVeldID = attributeApi.findAttributes(FindAttributesRequest.builder().assetId(existAssetId).
			typeIds([uidVeldId]).build()).getResults()[0].getValueAsString();
			//loggerApi.info('---inside sequence for loop2---');
		existingVDPIdList.add(existVeldID);
		existingVDPdpList.add(existAssetdp) ;
		loggerApi.info('---existingVDPIdList---' + existingVDPIdList);
	}
	
	
}


// end list of rthe veld ID's
	def row = rowIter.next() // skip the header
	

	while (rowIter.hasNext())
	{
		
		row = rowIter.next()
       if(row.getCell(0) != null && sheet_name == "VDPImportTemplate_withVELDID")
		{		
		Pattern letter = Pattern.compile("[a-zA-z]");
        Pattern digit = Pattern.compile("[0-9]");
       Pattern special = Pattern.compile("[!@#%&*()_+=|<>?{}\\[\\]~-]");
		 Matcher hasLetter = letter.matcher(row.getCell(0).toString());
		 Matcher hasDigit = digit.matcher(row.getCell(0).toString());
        Matcher hasSpecial = special.matcher(row.getCell(0).toString());
		 boolean constainsalphabet   = hasLetter.find();
        boolean containsspeccharter = hasSpecial.find();
		boolean containsdigit = hasDigit.find();
		execution.setVariable("constainsalphabet",constainsalphabet);
		execution.setVariable("containsspeccharter",containsspeccharter);
		execution.setVariable("containsdigit",containsdigit);
		loggerApi.info('---constainsalphabet---' + constainsalphabet);
		loggerApi.info('---containsspeccharter---' + containsspeccharter);
		loggerApi.info('---containsdigit---' + containsdigit);
		VeldID = excelCellTypeandValue(row.getCell(0).getCellType(), row.getCell(0));
		
	    loggerApi.info('---VeldID---' + VeldID);
		}
		
		

		if(row.getCell(1) != null)
		{
		     
			VdpName = excelCellTypeandValue(row.getCell(1).getCellType(), row.getCell(1));
			//loggerApi.info('---VdpName---' + VdpName);
		}
		if(row.getCell(2) != null)
		{
			VdpDefinition  = excelCellTypeandValue(row.getCell(2).getCellType(), row.getCell(2));
			//loggerApi.info('---VdpDefinition---' + VdpDefinition);
		}
		if(row.getCell(3) != null)
		{
			VeldPrimaryDomain  = excelCellTypeandValue(row.getCell(3).getCellType(), row.getCell(3));
			//loggerApi.info('---VeldPrimaryDomain---' + VeldPrimaryDomain);
		}
		if(row.getCell(4) != null)
		{
			VeldSecondaryDomain = excelCellTypeandValue(row.getCell(4).getCellType(), row.getCell(4));
			//loggerApi.info('---VeldSecondaryDomain---' + VeldSecondaryDomain);
		}
		if(row.getCell(5) != null)
		{
			VeldSubDomain  = excelCellTypeandValue(row.getCell(5).getCellType(), row.getCell(5));
			//loggerApi.info('---VeldSubDomain---' + VeldSubDomain);
		}
		if(row.getCell(6) != null)
		{
			NextActionOwner  = excelCellTypeandValue(row.getCell(6).getCellType(), row.getCell(6));
			//loggerApi.info('---NextActionOwner ---' + NextActionOwner);
		}
		if(row.getCell(7) != null)
		{
			VeldQueryPath  = excelCellTypeandValue(row.getCell(7).getCellType(), row.getCell(7));
			//loggerApi.info('---VeldQueryPath ---' + VeldQueryPath );
		}
		if(row.getCell(8) != null)
		{
			ComputingLogic  = excelCellTypeandValue(row.getCell(8).getCellType(), row.getCell(8));

			//loggerApi.info('---ComputingLogic ---' + ComputingLogic );
		}
		if(row.getCell(9) != null)
		{
			DeliveryStatus  = excelCellTypeandValue(row.getCell(9).getCellType(), row.getCell(9));
			//loggerApi.info('---DeliveryStatus---' + DeliveryStatus );
		}
		if(row.getCell(10) != null)
		{
			VeldModelStatus  = excelCellTypeandValue(row.getCell(10).getCellType(), row.getCell(10));
			//loggerApi.info('---VeldModelStatus ---' + VeldModelStatus);
		}
		if(row.getCell(11) != null)
		{
			DataAttribute   = excelCellTypeandValue(row.getCell(11).getCellType(), row.getCell(11));
			//loggerApi.info('---DataAttribute  ---' + DataAttribute  );
		}
		if(row.getCell(12) != null)
		{
			BusinessDataPoints  = excelCellTypeandValue(row.getCell(12).getCellType(), row.getCell(12));
		    loggerApi.info('---BusinessDataPoints  ---' + BusinessDataPoints  );
		}
		
		
		
		if ( ((row.getCell(0).getCellType() == CellType.BLANK )  && (sheet_name == "VDPImportTemplate_withVELDID"))  ||
			row.getCell(1).getCellType() == CellType.BLANK  ||
			row.getCell(2).getCellType() == CellType.BLANK  ||
			row.getCell(3).getCellType() == CellType.BLANK  ||
			row.getCell(12).getCellType() == CellType.BLANK ||
		//	(existingVDPIdList.contains(row.getCell(0)) )   ||
		//	(existingVDPdpList.contains(row.getCell(1)) )
			
			((existingVDPIdList.contains(excelCellTypeandValue(row.getCell(0).getCellType(), row.getCell(0)))) 
			&& (sheet_name == "VDPImportTemplate_withVELDID") )   ||
		//	(existingVDPdpList.contains(excelCellTypeandValue(row.getCell(1).getCellType(), row.getCell(1))))   ||
			(constainsalphabet == true) ||
			(containsspeccharter == true) ||
            (BusinessDataPoints == "")	||
			(VeldID == "" && sheet_name == "VDPImportTemplate_withVELDID")	||
			(VdpName == "") ||
			(VeldPrimaryDomain == "") ||
			(VdpDefinition == "")		
			)  {
			 processing = false;
			 execution.setVariable("processing",processing);
			 
			loggerApi.info('---row.getCell(0).getCellType()---' + row.getCell(0).getCellType());
			loggerApi.info('---row.getCell(1).getCellType()---' + row.getCell(1).getCellType());
			loggerApi.info('---row.getCell(2).getCellType()---' + row.getCell(2).getCellType());
			loggerApi.info('---row.getCell(3).getCellType()---' + row.getCell(3).getCellType());
			loggerApi.info('---row.getCell(12).getCellType()---' + row.getCell(12).getCellType());
			
            if (
			(row.getCell(0).getCellType() == CellType.BLANK && sheet_name == "VDPImportTemplate_withVELDID")  ||
			row.getCell(1).getCellType() == CellType.BLANK  ||
			row.getCell(2).getCellType() == CellType.BLANK  ||
			row.getCell(3).getCellType() == CellType.BLANK  ||
			row.getCell(12).getCellType() == CellType.BLANK  ||
			(BusinessDataPoints == "")	||
			(VeldID == "" && sheet_name == "VDPImportTemplate_withVELDID")	||
			(VdpName == "") ||
			(VeldPrimaryDomain == "") ||
			(VdpDefinition == "")
			) 
			{
			VDPNotCreatedList.add(VdpName) ;
			
			}
            if (
			(existingVDPIdList.contains(excelCellTypeandValue(row.getCell(0).getCellType(), row.getCell(0)))
			&& sheet_name == "VDPImportTemplate_withVELDID" )
			) {
            VDPDupList.add(VeldID);
			VDPDupList_name.add(VdpName) ;
			}
			loggerApi.info('---The processing for the Veld ID is ---' + row.getCell(0));
            loggerApi.info('---The processing for the Veld ID is ---' + processing);

		} else {
			processing = true;
		}
		
		if(processing){

            loggerApi.info('---The processing for the Veld ID is ---' + row.getCell(0));
            loggerApi.info('---The processing for the Veld ID is ---' + processing);
      
      int Vdp2bdprelcounter =0;
    	int bdpcounter= 0;
	  
		fullNameToSearch = VeldPrimaryDomain + ' > ' + VdpName;
		loggerApi.info('---fullNameToSearch---' + fullNameToSearch);
		
		VdpAssetIdToCheck = assetApi.findAssets(FindAssetsRequest.builder().communityId(uidVdpCom).
			domainId(uidVdpDom).name(fullNameToSearch).nameMatchMode(MatchMode.END).build()).getResults();
		
		loggerApi.info('---VdpAssetIdToCheck size---' + VdpAssetIdToCheck.size());
		loggerApi.info('---existingVDPIdList size---' + existingVDPIdList);
		loggerApi.info('---existingVDPdpList size---' + existingVDPdpList);
		loggerApi.info('---VdpAssetIdToCheck size---' + row.getCell(0));
		
		if (VdpAssetIdToCheck.size().equals(0)) {
			loggerApi.info('---VDP display name to be created---' + VdpName);
			String veldidnew = (excelCellTypeandValue(row.getCell(0).getCellType(), row.getCell(0))).trim() ;
			String veldnamenew = (excelCellTypeandValue(row.getCell(1).getCellType(), row.getCell(1))).trim() ;
			loggerApi.info('---veldidnew---' + veldidnew);
			loggerApi.info('---veldidnew---' + veldidnew.getClass().getSimpleName());
			loggerApi.info('---row.getCell(0)---' + row.getCell(0).getClass().getSimpleName());
			loggerApi.info('---existingVDPIdList[0]---' + existingVDPIdList[0].getClass().getSimpleName());
			
			loggerApi.info('---existingVDPIdList[0]---' + existingVDPIdList[0]);
			if (existingVDPIdList.contains(excelCellTypeandValue(row.getCell(0).getCellType(), row.getCell(0))) && 
			sheet_name == "VDPImportTemplate_withVELDID") { 
			loggerApi.info('---row.getCell(0)-row.getCell(1) existing Veld Id or name--' + row.getCell(0));
			VDPDupList_name.add(VdpName);
			VDPDupList.add(VeldID);
			// loggerApi.info('----skippedVDPCounter---' + skippedVDPCounter) ;
                			} else {
			assetPatternToSearch = '%> ' + VeldPrimaryDomain + ' >%';
			//loggerApi.info('---assetPatternToSearch---' + assetPatternToSearch);
			existingVDPAssets = assetApi.findAssets(FindAssetsRequest.builder().communityId(uidVdpCom).
				domainId(uidVdpDom).build()).getResults();
			loggerApi.info('---existingVDPAssets size---' + existingVDPAssets.size());
			
			
			if((existingVDPAssets.size().equals(0))  && 
			(sheet_name == "VDPImportTemplate_withVELDID" )){
				sequenceNumber = VeldID;
			} 
			if ((existingVDPAssets.size() > 0 ) && (sheet_name == "VDPImportTemplate_withVELDID" )) {
				
				sequenceNumber = VeldID;
			
			}			
			
			finalVeldID = VeldID;
			
	// start 
	if((existingVDPAssets.size().equals(0)) &&
	(sheet_name == "VDPImportTemplate_withoutVELDID")){
				VeldID = 10001;
				finalVeldID = VeldID;	} else ((existingVDPAssets.size() > 0) 
			&& (sheet_name == "VDPImportTemplate_withoutVELDID")) {
loggerApi.info('---skippedVDPCounter----' + skippedVDPCounter ) 
				for(itrVdpAsset in existingVDPAssets) {
				existVdpAssetId_new = itrVdpAsset.getId();
				existVdpRequestId_new = attributeApi.findAttributes(FindAttributesRequest.builder().assetId(existVdpAssetId_new).
						typeIds([uidVeldId]).build()).getResults()[0].getValue();
				existVeldidlistformax.add(existVdpRequestId_new)
				execution.setVariable("existVeldidlistformax",existVeldidlistformax);
				  
				}  
				if (existVeldidlistformax.size() > 0) {
				
				veldID_max_temp = existVeldidlistformax.max() ;
				if (veldID_max_temp > 10000) {
				veldID = veldID_max_temp + 1 ; 
				execution.setVariable("veldID",veldID);
				} else {
				veldID = 10001 ;
				execution.setVariable("veldID",veldID);
				}
			}
				
			finalVeldID = VeldID;
			}
	
	
	// end 
			
			fullName = finalVeldID + ' > ' + VeldPrimaryDomain + ' > ' + VdpName;
//			fullName = Integer.parseInt(finalVeldID) + ' > ' + VeldPrimaryDomain + ' > ' + VdpName;
			
			Asset newAsset = assetApi.addAsset(AddAssetRequest.builder()
				.name(fullName).displayName(VdpName).
				typeId(uidVdpTypeId).domainId(uidVdpDom).build())
			
			uidCurrentVdpAsset = newAsset.getId();	
			newVDPs.add(VdpName);
			loggerApi.info('---newVDPs---' + newVDPs.size());
			loggerApi.info('---VDP full name to be Created---' + fullName);
			
			assetApi.setAssetAttributes(SetAssetAttributesRequest.builder().assetId(uidCurrentVdpAsset)
				.typeId(uidVdpDef).values([VdpDefinition.replaceAll(" ", "&nbsp;").replaceAll("\n", "<br>")]).build());
			
		
			
		//	assetApi.setAssetAttributes(SetAssetAttributesRequest.builder().assetId(uidCurrentVdpAsset)
		//		.typeId(uidVeldId).values([sequenceNumber.toString()]).build());

            assetApi.setAssetAttributes(SetAssetAttributesRequest.builder().assetId(uidCurrentVdpAsset)
				.typeId(uidVeldId).values([finalVeldID.toString()]).build());
			
			List veldprimarydomainAllowedValList = attributeTypeApi.getAttributeType(uidVpd).getAllowedValues();
			if(VeldPrimaryDomain.trim() != ""){
				if(veldprimarydomainAllowedValList.contains(VeldPrimaryDomain)){
					assetApi.setAssetAttributes(SetAssetAttributesRequest.builder().assetId(uidCurrentVdpAsset)
						.typeId(uidVpd).values([VeldPrimaryDomain]).build());
				} else {
					if(!inValidValueExist(attributeNonExistMultiValueMap, 'Veld Primary Domain', VeldPrimaryDomain.trim())){
						attributeNonExistMultiValueMap.put('Veld Primary Domain', VeldPrimaryDomain.trim());
						 VDPNotcreatedIDLIST.add(uidCurrentVdpAsset);
				         VDPNotCreatedList.add(VdpName);
						 loggerApi.info('VDPNotcreatedIDLIST' + VDPNotcreatedIDLIST.size())
					}
				}			
			}
			
			//loggerApi.info('---VeldSecondaryDomain---' + VeldSecondaryDomain);			
			List VeldSecondaryDomainAllowedValList = attributeTypeApi.getAttributeType(uidVsecD).getAllowedValues();
			if(VeldSecondaryDomain.trim() != ""){
				if(VeldSecondaryDomainAllowedValList.contains(VeldSecondaryDomain)){
					assetApi.setAssetAttributes(SetAssetAttributesRequest.builder().assetId(uidCurrentVdpAsset)
						.typeId(uidVsecD).values([VeldSecondaryDomain]).build());
				} else {
					if(!inValidValueExist(attributeNonExistMultiValueMap, 'Veld Secondary Domain', VeldSecondaryDomain.trim())){
						attributeNonExistMultiValueMap.put('Veld Secondary Domain', VeldSecondaryDomain.trim());
					}
				}				
			}
			
			// loggerApi.info('---VeldSubDomain---' + VeldSubDomain);
			List VeldSubDomainAllowedValList = attributeTypeApi.getAttributeType(uidVsD).getAllowedValues();
			if(VeldSubDomain.trim() != ""){
				if(VeldSubDomainAllowedValList.contains(VeldSubDomain)){
					assetApi.setAssetAttributes(SetAssetAttributesRequest.builder().assetId(uidCurrentVdpAsset)
						.typeId(uidVsD).values([VeldSubDomain]).build());
				} else {
					if(!inValidValueExist(attributeNonExistMultiValueMap, 'Veld Sub Domain', VeldSubDomain.trim())){
						attributeNonExistMultiValueMap.put('Veld Sub Domain', VeldSubDomain.trim());
					}
				}			
			}
			
			// loggerApi.info('---DeliveryStatus---' + DeliveryStatus);
			List DeliveryStatusAllowedValList = attributeTypeApi.getAttributeType(uidds).getAllowedValues();
			if(DeliveryStatus.trim() != ""){
				if(DeliveryStatusAllowedValList.contains(DeliveryStatus)){
					assetApi.setAssetAttributes(SetAssetAttributesRequest.builder().assetId(uidCurrentVdpAsset)
						.typeId(uidds).values([DeliveryStatus]).build());
				} else {
					if(!inValidValueExist(attributeNonExistMultiValueMap, 'Delivery Status', DeliveryStatus.trim())){
						attributeNonExistMultiValueMap.put('Delivery Status', DeliveryStatus.trim());
					}
				}			
			}
			
			//loggerApi.info('---VeldModelStatus---' + VeldModelStatus);
			List VeldModelStatusAllowedValList = attributeTypeApi.getAttributeType(uidVms).getAllowedValues();
			if(VeldModelStatus.trim() != ""){
				if(VeldModelStatusAllowedValList.contains(VeldModelStatus)){
					assetApi.setAssetAttributes(SetAssetAttributesRequest.builder().assetId(uidCurrentVdpAsset)
						.typeId(uidVms).values([VeldModelStatus]).build());
				} else {
					if(!inValidValueExist(attributeNonExistMultiValueMap, 'Veld Model Status', VeldModelStatus.trim())){
						attributeNonExistMultiValueMap.put('Veld Model Status', VeldModelStatus.trim());
					}
				}			
			}
			
		
			
			//loggerApi.info('---NextActionOwner---' + NextActionOwner);			
			if(NextActionOwner.trim() != ""){
				assetApi.setAssetAttributes(SetAssetAttributesRequest.builder().assetId(uidCurrentVdpAsset)
				.typeId(uidnao).values([NextActionOwner]).build());
			}
			
			//loggerApi.info('---VeldQueryPath---' + VeldQueryPath);			
			if(VeldQueryPath.trim() != ""){
				assetApi.setAssetAttributes(SetAssetAttributesRequest.builder().assetId(uidCurrentVdpAsset)
				.typeId(uidVqp).values([VeldQueryPath.replaceAll(" ", "&nbsp;").replaceAll("\n", "<br>")]).build());
			}	
			
			//loggerApi.info('---ComputingLogic ---' + ComputingLogic );			
			if(ComputingLogic.trim() != ""){
				assetApi.setAssetAttributes(SetAssetAttributesRequest.builder().assetId(uidCurrentVdpAsset)
				.typeId(uidCl).values([ComputingLogic.replaceAll(" ", "&nbsp;").replaceAll("\n", "<br>")]).build());
			}
			
			
			
			loggerApi.info('---all attributes done---');
			
		    if(BusinessDataPoints!= ""){
				loggerApi.info('---BusinessDataPoints is ---'   + BusinessDataPoints);
				for(BusinessDataPoint in BusinessDataPoints.split(",")){
					loggerApi.info(BusinessDataPoint.trim());
					
					bdpcounter++ ;
					
					loggerApi.info('---bdpcounter---'   + bdpcounter);
					findAssetsRequest_bdp = FindAssetsRequest.builder()
																.typeIds([string2Uuid(UUID_business_data_point)])
																.name((BusinessDataPoint.toString()).trim())
																.nameMatchMode(MatchMode.valueOf("EXACT"))
																.excludeMeta(true)
																.typeInheritance(true)
																.limit(100000)
																.offset(0)
																.build()          
					
					def assetList_bdp_temp_V = assetApi.findAssets(findAssetsRequest_bdp).getResults();
					
				if (!assetList_bdp_temp_V.isEmpty()) {
				
				
				def assetList_bdp_temp_V_id = assetApi.findAssets(findAssetsRequest_bdp).getResults()[0].getId() ;
                    def assetList_bdp_Total_V = assetApi.findAssets(findAssetsRequest_bdp).getTotal();
					
					
				 if (assetList_bdp_Total_V > 0 ) { 
				 
				 Vdp2bdprelcounter++ ;
				 
				 loggerApi.info('---Vdp2bdprelcounter---'   + Vdp2bdprelcounter);
				 execution.setVariable("Vdp2bdprelcounter",Vdp2bdprelcounter);
				 relationApi.addRelation(AddRelationRequest.builder()
									.targetId(uidCurrentVdpAsset)
									.sourceId(assetList_bdp_temp_V_id)
									.typeId(VDP2BDPRelTypeUuid)
									.build())
				 
				 }  }
					//loggerApi.info('---dotCount---' + dotCount);
					
				 else {
						loggerApi.info("---Business Data Point doesnt exist/not in correct format---" + BusinessDataPoint);
					}
				   
				
				
				}		

                             if ( bdpcounter > 0 && Vdp2bdprelcounter == 0 ) {
				                        VDPNotcreatedIDLIST.add(uidCurrentVdpAsset);
				                        VDPNotCreatedList.add(VdpName);
										loggerApi.info('VDPNotcreatedIDLIST' + VDPNotcreatedIDLIST.size())
				
				                                                             }
							if ( bdpcounter == 0 ) {
				                        VDPNotcreatedIDLIST.add(uidCurrentVdpAsset);
				                        VDPNotCreatedList.add(VdpName);
										loggerApi.info('VDPNotcreatedIDLIST' + VDPNotcreatedIDLIST.size())
				
				                                                             }
				
		    }
			
			// start of adding data attributes relation
			
			loggerApi.info('---DataAttribute---'   + DataAttribute);
			loggerApi.info('---VeldID---'   + VeldID);
			if(DataAttribute!= ""){
				
				for(da in DataAttribute.split(",")){			
				
					loggerApi.info(da.trim());
					loggerApi.info('----Da----' + (((da.replace(".", " > "))).trim()));
					
				//	bdpcounter++ ;
				//	loggerApi.info('---bdpcounter---'   + bdpcounter);
					findAssetsRequest_da = FindAssetsRequest.builder()
																.typeIds([uidDATypeId])
																.name(((da.replace(".", " > "))).trim())
																.nameMatchMode(MatchMode.valueOf("EXACT"))
																.excludeMeta(true)
																.typeInheritance(true)
																.limit(100000)
																.offset(0)
																.build()          
					
				def assetList_bdp_temp_da = assetApi.findAssets(findAssetsRequest_da).getResults();
					
				if (!assetList_bdp_temp_da.isEmpty()) {
				
				
				def assetList_da_temp_V_id = assetApi.findAssets(findAssetsRequest_da).getResults()[0].getId() ;
                    def assetList_da_Total_V = assetApi.findAssets(findAssetsRequest_da).getTotal();
					loggerApi.info('---assetList_da_temp_V_id---'   + assetList_da_temp_V_id);
					loggerApi.info('---assetList_da_temp_V_NAME---'   + (((da.toString()).replaceAll(".", " > ")).trim()));
					
				 if (assetList_da_Total_V > 0 ) { 
				 
			//	 Vdp2bdprelcounter++ ; 
				 
			//	 loggerApi.info('---Vdp2bdprelcounter---'   + Vdp2bdprelcounter);
			//	 execution.setVariable("Vdp2bdprelcounter",Vdp2bdprelcounter);
				 relationApi.addRelation(AddRelationRequest.builder()
									.sourceId(uidCurrentVdpAsset)
									.targetId(assetList_da_temp_V_id)
									.typeId(VDP2DARelTypeUuid)
									.build())
				 
				 }  }
					//loggerApi.info('---dotCount---' + dotCount);
					
				 else {
						loggerApi.info("---Data Attribute doesnt exist/not in correct format---" + da);
						DANotexistList.add(da) ;
						VDPDANotexistList.add(excelCellTypeandValue(row.getCell(1).getCellType(), row.getCell(1)));
						
						loggerApi.info("---VDPDANotexistList---" + VDPDANotexistList);
					}
				   
				
				
				}	
				DataAttribute = "" ;				
		    }
			
			
			
			
			
			
			
			// end of adding data attributes relation
		} 
		
		}
		else {
			VDPDupList.add(VeldID);
			VDPDupList_name.add(VdpName);
			loggerApi.info('---VDP already exists---' + VdpName);
			loggerApi.info('---VDPDupList  ---' + VDPDupList  );
		}	
   
	existingVDPdpList.add(excelCellTypeandValue(row.getCell(1).getCellType(), row.getCell(1)));
//    existingVDPIdList.add(row.getCell(0).getStringCellValue()) ;
//    existingVDPIdList.add(row.getCell(0).toString()) ;
//      existingVDPIdList.add(row.getCell(0).toString()) ;
      existingVDPIdList.add(excelCellTypeandValue(row.getCell(0).getCellType(), row.getCell(0))) ;
	
	
	
	} 
	
	

      else {
			loggerApi.info('---Duplicate found for VPD > VDPNAME to import Veld Data Point---' + 
			excelCellTypeandValue(row.getCell(0).getCellType(), row.getCell(0)));
		 
		//	VDPNotCreatedList.add(excelCellTypeandValue(row.getCell(1).getCellType(), row.getCell(1)));		
        //    loggerApi.info('---VDPNotCreatedList----' + VDPNotCreatedList ) ;
        //    execution.setVariable('VDPNotCreatedList',VDPNotCreatedList);			
	    //  skippedVDPCounter++;
	    //  loggerApi.info('---skippedVDPCounter----' + skippedVDPCounter ) ;
	   }
	
	
	}
}


if (VDPNotcreatedIDLIST == null ){
            return;
       } else 
{ 
         assetApi.removeAssets(VDPNotcreatedIDLIST)  ;
   }  ;
   
   userApi.getCurrentUser().get().getFirstName().toString().replace("%20", " ");

StringBuilder metrics = new StringBuilder();
Set<String> keys = attributeNonExistMultiValueMap.keySet();

def baseUrl = applicationApi.getInfo().getBaseUrl();
def domainUrl = baseUrl + 'vocabulary' + '/' + uidVdpDom;

def image_voya = baseUrl + "resources/images/voya_footer.png";
def image_collibra = baseUrl + "resources/images/logo.png";

def env = '';
if (baseUrl.contains('dev')){
	env = 'DEV' ;
} else if (baseUrl.contains('test2')) {
	env = 'TEST' ;
} else {
	env = 'PROD';
}

TimeZone.setDefault(TimeZone.getTimeZone("EST"));
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yy hh.mm a");
String formattedDate = dateFormat.format(new Date()).toString() + " ET";

def currentUserFN = userApi.getCurrentUser().get().getFirstName().toString().replace("%20", " ");
def currentUserLN = userApi.getCurrentUser().get().getLastName().toString().replace("%20", " ");


if(newVDPs.size > 0 || VDPDupList_name.size > 0 || VDPNotCreatedList.size() > 0 ){
	loggerApi.info('newVDPs.size'+newVDPs.size) ;
	loggerApi.info('VDPNotcreatedIDLIST.size'+  VDPNotcreatedIDLIST.size) ;
	def newVDPSsize = newVDPs.size - VDPNotcreatedIDLIST.size ;
	
	metrics.append("<div>Hi All,</div>");
	metrics.append("<div>&nbsp;</div>");
	metrics.append("The bulk import of Veld Data Points have been completed successfully for the file <b>" + importFileName + "</b>.");
	metrics.append("<div>&nbsp;</div>");
	
	metrics.append("<b>Import Summary:</b> <i>(Operation Performed by</i> - <span style=\"background-color:LemonChiffon;\">" + currentUserFN + " " + currentUserLN + "</span>)");
	metrics.append("<br>Total number of entries read from the import file (including any blank entries) - " + "<b>" + Veld_entries + "</b><br>");
	if ( newVDPs.size > 0  ) {
	metrics.append("<br>Total number of Veld Data Points created as part of the bulk import - " + "<b>" + newVDPSsize + "</b><br>");
	}
	if (VDPDupList_name.size()  > 0 ) {
     loggerApi.info('VDPDupList_name.size() '+VDPDupList_name.size() ) ;
	metrics.append("<br>Total number of Veld Data Points skipped as part of the bulk import because either they are duplicate in the file or already existing in Collibra - " + "<b>" + VDPDupList_name.size()  + "</b><br>");
	for (String listItem : VDPDupList_name) {
			metrics.append("<li>" + listItem + "<br></li>");
		}
	
	}
	
	
	if(VDPNotCreatedList.size() > 0){
		metrics.append("<br>Below are the Veld Data Points (<i>count = " + VDPNotCreatedList.size() + "</i>) that got rejected due to one or more mandatory fields (Veld Id,VDP Name, Veld Primary Domain, Definition,Business Data Point) are not populated/have invalid values."
			+ " Please fix the corresponding VDP records and reload them: <br><i>");

		for (String listItem : VDPNotCreatedList) {
			metrics.append("<li>" + listItem + "<br></li>");
		}
	}

if(VDPDANotexistList.size() > 0){
		metrics.append("<br>Total number of Veld Data Points which are having invalid Data Attribute Name in the import file (<i>count = " + VDPDANotexistList.size() + "</i>) that doesn't exist in Collibra."
			+ " The corresponding VDP records are loaded .<br><i>");

//		for (String listItem : DANotexistList) {
//			metrics.append("<li>" + listItem + "<br></li>");
//		}
	}

	if(keys.size() > 0){
		metrics.append("</i><br>Below are Picklist attributes along with their values which doesn't exist in Collibra. Please use the refinement workflow to update the problematic records: <br>");
		// iterate through the key set and display key and values
		metrics.append("<table>");
		for (String key : keys) {
			//bdpAttrMetricsDetails.append(key + " --> " + attributeNonExistMultiValueMap.get(key).toString().replace("[", "").replace("]", "") + "<br>");
			metrics.append("<tr><td><b>"+ key + "</b></td><td style=\"text-align:left;color:red;\"><i>" + attributeNonExistMultiValueMap.get(key).toString().replace("[", "").replace("]", "") + "</i></td></tr>");
		}
		metrics.append("</table>");
	}

	metrics.append("<br><a href=\"${domainUrl}\" target=\"_blank\">Click Here</a> to navigate to VDP domain to review the newly created Veld Data Points");
}

execution.setVariable('env',env);
execution.setVariable('domainUrl',domainUrl);
execution.setVariable("image_voya",image_voya);
execution.setVariable("image_collibra",image_collibra);
execution.setVariable("formattedDate",formattedDate);
execution.setVariable("importFileName",importFileName);
execution.setVariable("metrics",metrics);

loggerApi.info('---Bulk Import Veld Data Point end---');
 

Groovy online compiler

Write, Run & Share Groovy code online using OneCompiler's Groovy online compiler for free. It's one of the robust, feature-rich online compilers for Groovy language, running the latest Groovy version 2.6. Getting started with the OneCompiler's Groovy editor is easy and fast. The editor shows sample boilerplate code when you choose language as Groovy and start coding.

Read inputs from stdin

OneCompiler's Groovy online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Groovy program which takes name as input and prints hello message with your name.

def name = System.in.newReader().readLine()
println "Hello " + name

About Groovy

Groovy is an object-oriented programming language based on java. Apache Groovy is a dynamic and agile language which is similar to Python, Ruby, Smalltalk etc.

Key Features

  • It's not a replacement for java but it's an enhancer to Java with extra features like DSL support, dynamic typing, closures etc.
  • Accepts Java code as it extends JDK
  • Greater flexibility
  • Concise and much simpler compared to Java
  • Can be used as both programming language and scripting language.

Syntax help

Data Types

Data typeDescriptionRange
StringTo represent text literalsNA
charTo represent single character literalNA
intTo represent whole numbers-2,147,483,648 to 2,147,483,647
shortTo represent short numbers-32,768 to 32,767
longTo represent long numbers-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
doubleTo represent 64 bit floating point numbers4.94065645841246544e-324d to 1.79769313486231570e+308d
floatTo represent 32 bit floating point numbers1.40129846432481707e-45 to 3.40282346638528860e+38
byteTo represent byte value-128 to 127
booleanTo represent boolean values either true or falseTrue or False

Variables

You can define variables in two ways

Syntax:

data-type variable-name;

[or]

def variable-name;

Loops

0.upto(n) {println "$it"}

or

n.times{println "$it"}

where n is the number of loops and 0 specifies the starting index

Decision-Making

1. If / Nested-If / If-Else:

When ever you want to perform a set of operations based on a condition or set of conditions, then If / Nested-If / If-Else is used.

if(conditional-expression) {
  // code
} else {
  // code
}

2. Switch:

Switch is an alternative to If-Else-If ladder and to select one among many blocks of code.

switch(conditional-expression) {    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
...    
    
default:     
 //code to be executed when all the above cases are not matched;    
} 

List

List allows you to store ordered collection of data values.

Example:

def mylist = [1,2,3,4,5];
List MethodsDescription
size()To find size of elements
sort()To sort the elements
add()To append new value at the end
contains()Returns true if this List contains requested value.
get()Returns the element of the list at the definite position
pop()To remove the last item from the List
isEmpty()Returns true if List contains no elements
minus()This allows you to exclude few specified elements from the elements of the original
plus()This allows you to add few specified elements to the elements of the original
remove()To remove the element present at the specific position
reverse()To reverse the elements of the original List and creates new list