src/main/java/org/vivoweb/harvester/operations/DeleteProfile.java
/**
	 * @param cwid Unique identifier for a person
	 * This function will add the inactive author as an external author and remove the link from the publication to the deleted profile
	 */
@param cwid Unique identifier for a person
This function will add the inactive author as an external author and remove the link from the publication to the deleted profile
@param cwid Unique identifier for a person This function will add the inactive author as an external author and remove the link from the publication to the deleted profile
private void addAuthorAsExternalEntity(String cwid, List<PublicationBean> publications) {
		int firstCount = 0;
		String randomNumber;
		int inferenceCount = 0;
		SDBJenaConnect vivoJena = null;
		/*StringBuilder sbs = new StringBuilder();
		sbs.append("SELECT ?vcard ?arg ?person \n");
		sbs.append("WHERE {\n");
		sbs.append("GRAPH <http://vitro.mannlib.cornell.edu/a/graph/wcmcPublications> {\n");
		sbs.append("?vcard <http://www.w3.org/2006/vcard/ns#givenName> \"" + this.givenName + "\" . \n");
		if(!this.middleName.equals(" "))
			sbs.append("OPTIONAL { ?vcard <http://vivoweb.org/ontology/core#middleName> \"" + this.middleName.trim() + "\" .} \n");
		sbs.append("?vcard <http://www.w3.org/2006/vcard/ns#familyName> \"" + this.familyName + "\" . \n");
		sbs.append("?arg <http://www.w3.org/2006/vcard/ns#hasName> ?vcard . \n");
		sbs.append("?person <http://purl.obolibrary.org/obo/ARG_2000028> ?arg . \n");
		sbs.append("FILTER(REGEX(STR(?person),\"" + this.vivoNamespace + "person\",\"i\")) \n");
		sbs.append("}}");
		vivoJena = this.jcf.getConnectionfromPool("dataSet");
		ResultSet rs;
		try {
			rs = vivoJena.executeSelectQuery(sbs.toString(), true);
			if(rs.hasNext()) { 
				while(rs.hasNext())
				{
					QuerySolution qs =rs.nextSolution();
					randomNumber = Integer.parseInt(qs.get("person").toString().replace(this.vivoNamespace + "person", ""));
					//logger.info("Vcard - " + qs.get("vcard").toString() + " - Triple : " + "http://vivo.med.cornell.edu/individual/cwid-" + cwid.trim() + " " + qs.get("arg").toString() + " " + qs.get("person").toString());
				}
				logger.info("External Person Found: person" + randomNumber);
			}
			else
				logger.info("No external person record found for " + cwid);
			} catch(IOException e) {
				logger.info("IOException" , e);
			}
		this.jcf.returnConnectionToPool(vivoJena, "dataSet");
		//If the person does not have a record in VIVO
		if(randomNumber == 0) {
			randomNumber = getAuthorshipPk(cwid);
		}*/
		randomNumber = getExternalPersonIdentifier(this.givenName, this.familyName);		 
		Iterator<PublicationBean> i = publications.iterator();
		logger.info("Check for Additional WCMC Authored pubs.");
		while(i.hasNext())
		{
			PublicationBean pub = i.next();
			if(pub.isAdditionalWcmcAuthorFlag()) {
				/*if(firstCount==0 && randomNumber==0) {
					randomNumber = generateRandomNumber();
					firstCount = firstCount + 1;
				}*/
				StringBuilder sb = new StringBuilder();
				sb.append("PREFIX core: <http://vivoweb.org/ontology/core#> \n"); 
				sb.append("PREFIX obo: <http://purl.obolibrary.org/obo/> \n");
				sb.append("PREFIX vitro: <http://vitro.mannlib.cornell.edu/ns/vitro/0.7#> \n");
				sb.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n"); 
				sb.append("PREFIX wcmc: <http://weill.cornell.edu/vivo/ontology/wcmc#> \n");
				sb.append("WITH <http://vitro.mannlib.cornell.edu/a/graph/wcmcPublications> \n"); 
				sb.append("DELETE { \n");
				sb.append("<" + pub.getAuthorshipUrl().trim() + "> core:relates ?person . \n");
				sb.append("?person core:relatedBy <" + pub.getAuthorshipUrl().trim() + "> . \n");
				sb.append("} \n");
				sb.append("INSERT { \n");
				sb.append("<" + pub.getAuthorshipUrl().trim() + "> core:relates <" + JenaConnectionFactory.nameSpace + "person" + randomNumber +"> .\n");
				sb.append("<" + JenaConnectionFactory.nameSpace + "person" + randomNumber +"> rdf:type <http://purl.obolibrary.org/obo/BFO_0000001> . \n");
				sb.append("<" + JenaConnectionFactory.nameSpace + "person" + randomNumber +"> rdf:type <http://purl.obolibrary.org/obo/BFO_0000002> . \n");
				sb.append("<" + JenaConnectionFactory.nameSpace + "person" + randomNumber +"> rdf:type <http://purl.obolibrary.org/obo/IAO_0000030> . \n");
				sb.append("<" + JenaConnectionFactory.nameSpace + "person" + randomNumber +"> rdf:type <http://purl.obolibrary.org/obo/BFO_0000031> . \n");
				sb.append("<" + JenaConnectionFactory.nameSpace + "person" + randomNumber +"> rdf:type <http://purl.obolibrary.org/obo/ARG_2000379> . \n");
				sb.append("<" + JenaConnectionFactory.nameSpace + "person" + randomNumber +"> rdf:type <http://www.w3.org/2006/vcard/ns#Kind> . \n");
				sb.append("<" + JenaConnectionFactory.nameSpace + "person" + randomNumber +"> rdf:type <http://www.w3.org/2002/07/owl#Thing> . \n");
				sb.append("<" + JenaConnectionFactory.nameSpace + "person" + randomNumber +"> rdf:type <http://www.w3.org/2006/vcard/ns#Individual> . \n");
				sb.append("<" + JenaConnectionFactory.nameSpace + "person" + randomNumber +"> vitro:mostSpecificType <http://www.w3.org/2006/vcard/ns#Individual> . \n");
				sb.append("<" + JenaConnectionFactory.nameSpace + "person" + randomNumber +"> core:relatedBy <" + pub.getAuthorshipUrl().trim() + "> . \n");
				sb.append("<" + JenaConnectionFactory.nameSpace + "person" + randomNumber +"> <http://www.w3.org/2006/vcard/ns#hasName> <" + JenaConnectionFactory.nameSpace + "hasName-person" + randomNumber + "> . \n");
				sb.append("<" + JenaConnectionFactory.nameSpace + "hasName-person" + randomNumber + "> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2006/vcard/ns#Explanatory> . \n");
				sb.append("<" + JenaConnectionFactory.nameSpace + "hasName-person" + randomNumber + "> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2006/vcard/ns#Addressing> . \n");
				sb.append("<" + JenaConnectionFactory.nameSpace + "hasName-person" + randomNumber + "> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Thing> . \n");
				sb.append("<" + JenaConnectionFactory.nameSpace + "hasName-person" + randomNumber + "> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2006/vcard/ns#Communication> . \n");
				sb.append("<" + JenaConnectionFactory.nameSpace + "hasName-person" + randomNumber + "> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2006/vcard/ns#Identification> . \n");
				sb.append("<" + JenaConnectionFactory.nameSpace + "hasName-person" + randomNumber + "> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2006/vcard/ns#Name> . \n");
				sb.append("<" + JenaConnectionFactory.nameSpace + "hasName-person" + randomNumber + "> vitro:mostSpecificType <http://www.w3.org/2006/vcard/ns#Name> . \n");
				if(this.givenName != null)
					sb.append("<" + JenaConnectionFactory.nameSpace + "hasName-person" + randomNumber + "> <http://www.w3.org/2006/vcard/ns#givenName> \"" + this.givenName.replaceAll("'", "\'") + "\" . \n");
				if(this.familyName != null)
					sb.append("<" + JenaConnectionFactory.nameSpace + "hasName-person" + randomNumber + "> <http://www.w3.org/2006/vcard/ns#familyName> \"" + this.familyName.replaceAll("'", "\'") + "\" . \n");
				sb.append("}\n");
				sb.append("WHERE { \n");
				sb.append("OPTIONAL { <" + pub.getAuthorshipUrl().trim() + "> core:relates ?person .\n");
				sb.append("?person core:relatedBy <" + pub.getAuthorshipUrl().trim() + "> .\n");
				sb.append("FILTER(REGEX(STR(?person),\"cwid\",\"i\") || REGEX(STR(?person),\"person\",\"i\")) }\n");
				sb.append("}");
				logger.info(sb.toString());
				if(ingestType.equals(IngestType.VIVO_API.toString())) {
					logger.info(this.vivoClient.vivoUpdateApi(sb.toString()));
				} else if(ingestType.equals(IngestType.SDB_DIRECT.toString())){
					vivoJena = this.jcf.getConnectionfromPool("dataSet");
					try {
						vivoJena.executeUpdateQuery(sb.toString(), true);
					} catch(IOException e) {
						logger.error("IOException" , e);
					}
					this.jcf.returnConnectionToPool(vivoJena, "dataSet");
					if(inferenceCount == 0) {
						logger.info("Insert into inference graph");
						String sparqlQuery = "PREFIX wcmc: <http://weill.cornell.edu/vivo/ontology/wcmc#> \n" +
							"INSERT DATA { GRAPH <http://vitro.mannlib.cornell.edu/default/vitro-kb-inf> { \n" +
							"<" + this.vivoNamespace + "person" + randomNumber +"> <http://vitro.mannlib.cornell.edu/ns/vitro/0.7#mostSpecificType> wcmc:ExternalEntity . \n" +
							"<" + pub.getAuthorshipUrl().trim() + "> <http://purl.obolibrary.org/obo/ARG_2000028> <" + this.vivoNamespace + "arg2000028-" + randomNumber +"> .\n" +
							"}}";
						if(ingestType.equals(IngestType.VIVO_API.toString())) {
							logger.info(this.vivoClient.vivoUpdateApi(sparqlQuery));
						} else if(ingestType.equals(IngestType.SDB_DIRECT.toString())){
							vivoJena = this.jcf.getConnectionfromPool("dataSet");
							try {
								vivoJena.executeUpdateQuery(sparqlQuery, true);
							} catch(IOException e) {
								logger.error("IOException" , e);
							}
							this.jcf.returnConnectionToPool(vivoJena, "dataSet");
						}
					}
				}
				inferenceCount = inferenceCount + 1;
			}	
		}
	}
private void addAuthorAsExternalEntity ( String cwid , List < PublicationBean > publications ) { int firstCount = 0 ; String randomNumber ; int inferenceCount = 0 ; SDBJenaConnect vivoJena = null ; /*StringBuilder sbs = new StringBuilder();
		sbs.append("SELECT ?vcard ?arg ?person \n");
		sbs.append("WHERE {\n");
		sbs.append("GRAPH <http://vitro.mannlib.cornell.edu/a/graph/wcmcPublications> {\n");
		sbs.append("?vcard <http://www.w3.org/2006/vcard/ns#givenName> \"" + this.givenName + "\" . \n");
		if(!this.middleName.equals(" "))
			sbs.append("OPTIONAL { ?vcard <http://vivoweb.org/ontology/core#middleName> \"" + this.middleName.trim() + "\" .} \n");
		sbs.append("?vcard <http://www.w3.org/2006/vcard/ns#familyName> \"" + this.familyName + "\" . \n");
		sbs.append("?arg <http://www.w3.org/2006/vcard/ns#hasName> ?vcard . \n");
		sbs.append("?person <http://purl.obolibrary.org/obo/ARG_2000028> ?arg . \n");
		sbs.append("FILTER(REGEX(STR(?person),\"" + this.vivoNamespace + "person\",\"i\")) \n");
		sbs.append("}}");
		vivoJena = this.jcf.getConnectionfromPool("dataSet");
		
		ResultSet rs;
		try {
			rs = vivoJena.executeSelectQuery(sbs.toString(), true);
			if(rs.hasNext()) { 
				while(rs.hasNext())
				{
					QuerySolution qs =rs.nextSolution();
					
					randomNumber = Integer.parseInt(qs.get("person").toString().replace(this.vivoNamespace + "person", ""));
					
					//logger.info("Vcard - " + qs.get("vcard").toString() + " - Triple : " + "http://vivo.med.cornell.edu/individual/cwid-" + cwid.trim() + " " + qs.get("arg").toString() + " " + qs.get("person").toString());
				}
				logger.info("External Person Found: person" + randomNumber);
			}
			else
				logger.info("No external person record found for " + cwid);
			} catch(IOException e) {
				// TODO Auto-generated catch block
				logger.info("IOException" , e);
			}
		
		this.jcf.returnConnectionToPool(vivoJena, "dataSet");
		
		//If the person does not have a record in VIVO
		if(randomNumber == 0) {
			randomNumber = getAuthorshipPk(cwid);
		}*/ randomNumber = getExternalPersonIdentifier ( this . givenName , this . familyName ) ; Iterator < PublicationBean > i = publications . iterator ( ) ; logger . info ( "Check for Additional WCMC Authored pubs." ) ; while ( i . hasNext ( ) ) { PublicationBean pub = i . next ( ) ; if ( pub . isAdditionalWcmcAuthorFlag ( ) ) { /*if(firstCount==0 && randomNumber==0) {
					randomNumber = generateRandomNumber();
					firstCount = firstCount + 1;
				}*/ StringBuilder sb = new StringBuilder ( ) ; sb . append ( "PREFIX core: <http://vivoweb.org/ontology/core#> \n" ) ; sb . append ( "PREFIX obo: <http://purl.obolibrary.org/obo/> \n" ) ; sb . append ( "PREFIX vitro: <http://vitro.mannlib.cornell.edu/ns/vitro/0.7#> \n" ) ; sb . append ( "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n" ) ; sb . append ( "PREFIX wcmc: <http://weill.cornell.edu/vivo/ontology/wcmc#> \n" ) ; sb . append ( "WITH <http://vitro.mannlib.cornell.edu/a/graph/wcmcPublications> \n" ) ; sb . append ( "DELETE { \n" ) ; sb . append ( "<" + pub . getAuthorshipUrl ( ) . trim ( ) + "> core:relates ?person . \n" ) ; sb . append ( "?person core:relatedBy <" + pub . getAuthorshipUrl ( ) . trim ( ) + "> . \n" ) ; sb . append ( "} \n" ) ; sb . append ( "INSERT { \n" ) ; sb . append ( "<" + pub . getAuthorshipUrl ( ) . trim ( ) + "> core:relates <" + JenaConnectionFactory . nameSpace + "person" + randomNumber + "> .\n" ) ; sb . append ( "<" + JenaConnectionFactory . nameSpace + "person" + randomNumber + "> rdf:type <http://purl.obolibrary.org/obo/BFO_0000001> . \n" ) ; sb . append ( "<" + JenaConnectionFactory . nameSpace + "person" + randomNumber + "> rdf:type <http://purl.obolibrary.org/obo/BFO_0000002> . \n" ) ; sb . append ( "<" + JenaConnectionFactory . nameSpace + "person" + randomNumber + "> rdf:type <http://purl.obolibrary.org/obo/IAO_0000030> . \n" ) ; sb . append ( "<" + JenaConnectionFactory . nameSpace + "person" + randomNumber + "> rdf:type <http://purl.obolibrary.org/obo/BFO_0000031> . \n" ) ; sb . append ( "<" + JenaConnectionFactory . nameSpace + "person" + randomNumber + "> rdf:type <http://purl.obolibrary.org/obo/ARG_2000379> . \n" ) ; sb . append ( "<" + JenaConnectionFactory . nameSpace + "person" + randomNumber + "> rdf:type <http://www.w3.org/2006/vcard/ns#Kind> . \n" ) ; sb . append ( "<" + JenaConnectionFactory . nameSpace + "person" + randomNumber + "> rdf:type <http://www.w3.org/2002/07/owl#Thing> . \n" ) ; sb . append ( "<" + JenaConnectionFactory . nameSpace + "person" + randomNumber + "> rdf:type <http://www.w3.org/2006/vcard/ns#Individual> . \n" ) ; sb . append ( "<" + JenaConnectionFactory . nameSpace + "person" + randomNumber + "> vitro:mostSpecificType <http://www.w3.org/2006/vcard/ns#Individual> . \n" ) ; sb . append ( "<" + JenaConnectionFactory . nameSpace + "person" + randomNumber + "> core:relatedBy <" + pub . getAuthorshipUrl ( ) . trim ( ) + "> . \n" ) ; sb . append ( "<" + JenaConnectionFactory . nameSpace + "person" + randomNumber + "> <http://www.w3.org/2006/vcard/ns#hasName> <" + JenaConnectionFactory . nameSpace + "hasName-person" + randomNumber + "> . \n" ) ; sb . append ( "<" + JenaConnectionFactory . nameSpace + "hasName-person" + randomNumber + "> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2006/vcard/ns#Explanatory> . \n" ) ; sb . append ( "<" + JenaConnectionFactory . nameSpace + "hasName-person" + randomNumber + "> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2006/vcard/ns#Addressing> . \n" ) ; sb . append ( "<" + JenaConnectionFactory . nameSpace + "hasName-person" + randomNumber + "> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Thing> . \n" ) ; sb . append ( "<" + JenaConnectionFactory . nameSpace + "hasName-person" + randomNumber + "> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2006/vcard/ns#Communication> . \n" ) ; sb . append ( "<" + JenaConnectionFactory . nameSpace + "hasName-person" + randomNumber + "> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2006/vcard/ns#Identification> . \n" ) ; sb . append ( "<" + JenaConnectionFactory . nameSpace + "hasName-person" + randomNumber + "> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2006/vcard/ns#Name> . \n" ) ; sb . append ( "<" + JenaConnectionFactory . nameSpace + "hasName-person" + randomNumber + "> vitro:mostSpecificType <http://www.w3.org/2006/vcard/ns#Name> . \n" ) ; if ( this . givenName != null ) sb . append ( "<" + JenaConnectionFactory . nameSpace + "hasName-person" + randomNumber + "> <http://www.w3.org/2006/vcard/ns#givenName> \"" + this . givenName . replaceAll ( "'" , "\'" ) + "\" . \n" ) ; if ( this . familyName != null ) sb . append ( "<" + JenaConnectionFactory . nameSpace + "hasName-person" + randomNumber + "> <http://www.w3.org/2006/vcard/ns#familyName> \"" + this . familyName . replaceAll ( "'" , "\'" ) + "\" . \n" ) ; sb . append ( "}\n" ) ; sb . append ( "WHERE { \n" ) ; sb . append ( "OPTIONAL { <" + pub . getAuthorshipUrl ( ) . trim ( ) + "> core:relates ?person .\n" ) ; sb . append ( "?person core:relatedBy <" + pub . getAuthorshipUrl ( ) . trim ( ) + "> .\n" ) ; sb . append ( "FILTER(REGEX(STR(?person),\"cwid\",\"i\") || REGEX(STR(?person),\"person\",\"i\")) }\n" ) ; sb . append ( "}" ) ; logger . info ( sb . toString ( ) ) ; if ( ingestType . equals ( IngestType . VIVO_API . toString ( ) ) ) { logger . info ( this . vivoClient . vivoUpdateApi ( sb . toString ( ) ) ) ; } else if ( ingestType . equals ( IngestType . SDB_DIRECT . toString ( ) ) ) { vivoJena = this . jcf . getConnectionfromPool ( "dataSet" ) ; try { vivoJena . executeUpdateQuery ( sb . toString ( ) , true ) ; } catch ( IOException e ) { logger . error ( "IOException" , e ) ; } this . jcf . returnConnectionToPool ( vivoJena , "dataSet" ) ; if ( inferenceCount == 0 ) { logger . info ( "Insert into inference graph" ) ; String sparqlQuery = "PREFIX wcmc: <http://weill.cornell.edu/vivo/ontology/wcmc#> \n" + "INSERT DATA { GRAPH <http://vitro.mannlib.cornell.edu/default/vitro-kb-inf> { \n" + "<" + this . vivoNamespace + "person" + randomNumber + "> <http://vitro.mannlib.cornell.edu/ns/vitro/0.7#mostSpecificType> wcmc:ExternalEntity . \n" + "<" + pub . getAuthorshipUrl ( ) . trim ( ) + "> <http://purl.obolibrary.org/obo/ARG_2000028> <" + this . vivoNamespace + "arg2000028-" + randomNumber + "> .\n" + "}}" ; if ( ingestType . equals ( IngestType . VIVO_API . toString ( ) ) ) { logger . info ( this . vivoClient . vivoUpdateApi ( sparqlQuery ) ) ; } else if ( ingestType . equals ( IngestType . SDB_DIRECT . toString ( ) ) ) { vivoJena = this . jcf . getConnectionfromPool ( "dataSet" ) ; try { vivoJena . executeUpdateQuery ( sparqlQuery , true ) ; } catch ( IOException e ) { logger . error ( "IOException" , e ) ; } this . jcf . returnConnectionToPool ( vivoJena , "dataSet" ) ; } } } inferenceCount = inferenceCount + 1 ; } } }
@param cwid Unique identifier for a person
 This function will add the inactive author as an external author and remove the link from the publication to the deleted profile
@param cwid Unique identifier for a person This function will add the inactive author as an external author and remove the link from the publication to the deleted profile
//Check for person in VIVO // TODO Auto-generated catch block //Insert into inference graph // TODO Auto-generated catch block