private AssociationPojo getAssociation(AssociationSpecPojo esp, String field, Long count, DocumentPojo f)
{
String index = (count != null) ? count.toString() : null;
try
{
AssociationPojo e = new AssociationPojo();
// If the AssociationSpecPojo has a creation criteria script check the association for validity
if (esp.getCreationCriteriaScript() != null && JavaScriptUtils.containsScript(esp.getCreationCriteriaScript()))
{
boolean addAssoc = executeEntityAssociationValidation(esp.getCreationCriteriaScript(), field, index);
if (!addAssoc) {
return null;
}
}
boolean bDontResolveToIndices = false; // (can always override to summary)
if (null != esp.getAssoc_type() && (esp.getAssoc_type().equalsIgnoreCase("summary"))) {
bDontResolveToIndices = true;
}
// Assoc.entity1
if ((esp.getEntity1() != null) || (esp.getEntity1_index() != null))
{
// Association.entity1_index
if (esp.getEntity1_index() != null)
{
if (JavaScriptUtils.containsScript(esp.getEntity1_index()))
{
String s = (String)getValueFromScript(esp.getEntity1_index(), field, index);
if (null != s) e.setEntity1_index(s.toLowerCase());
}
else
{
if ((_iterator != null) && (esp.getEntity1_index().startsWith("$metadata.") || esp.getEntity1_index().startsWith("${metadata."))) {
if (_context.isStandalone()) { // (minor message, while debugging only)
_context.getHarvestStatus().logMessage("Warning: in entity1_index, using global $metadata when iterating", true);
}
}
String s = getFormattedTextFromField(esp.getEntity1_index(), field);
if (null != s) e.setEntity1_index(s.toLowerCase());
}
if (null != e.getEntity1_index()) { // Convert to entity1
int nTypeIndex = e.getEntity1_index().lastIndexOf('/');
if (nTypeIndex > 0) {
e.setEntity1(e.getEntity1_index().substring(0, nTypeIndex));
if (!_entityMap.contains(e.getEntity1_index())) { // Needs to correlate with an entity
StringBuffer error = new StringBuffer("Failed to correlate entity1_index with: ").append(esp.getEntity1_index());
if (_context.isStandalone()) {
error.append(" using ").append(e.getEntity1_index());
}
_context.getHarvestStatus().logMessage(error.toString(), true);
e.setEntity1_index(null);
}//TESTED (INF1360_test_source.json:test8)
}
else { // index must be malformed
StringBuffer error = new StringBuffer("Malformed entity1_index with: ").append(esp.getEntity1_index());
if (_context.isStandalone()) {
error.append(" using ").append(e.getEntity1_index());
}
_context.getHarvestStatus().logMessage(error.toString(), true);
e.setEntity1_index(null);
}
}
}//TESTED (see INF1360_test_source.json:test2)
// entity1
if (null != esp.getEntity1()) {
if (JavaScriptUtils.containsScript(esp.getEntity1()))
{
e.setEntity1((String)getValueFromScript(esp.getEntity1(), field, index));
}
else
{
if ((_iterator != null) && (esp.getEntity1().startsWith("$metadata.") || esp.getEntity1().startsWith("${metadata."))) {
if (_context.isStandalone()) { // (minor message, while debugging only)
_context.getHarvestStatus().logMessage("Warning: in entity1, using global $metadata when iterating", true);
}
}
e.setEntity1(getFormattedTextFromField(esp.getEntity1(), field));
}
if (!bDontResolveToIndices && (null == e.getEntity1_index()))
{
// Try using the entity.disambiguated name, this isn't perfect because 2 entities with different
// types can have different dnames, but we'll try and then abandon if we get multiple hits
int nHits = 0;
String matchingIndex = null;
for (EntityPojo entity : f.getEntities())
{
if (entity.getDisambiguatedName().equalsIgnoreCase(e.getEntity1()))
{
nHits++;
if (1 == nHits) {
matchingIndex = entity.getIndex();
e.setEntity1_index(entity.getIndex());
}
else if (!matchingIndex.equals(entity.getIndex())) { // Ambiguous reference so bail out
StringBuffer error = new StringBuffer("Failed entity1_index disambiguation with: ").append(esp.getEntity1());
if (_context.isStandalone()) {
error.append(" using ").append(e.getEntity1());
}
_context.getHarvestStatus().logMessage(error.toString(), true);
e.setEntity1_index(null);
break;
}
}
} // (end loop across all indices)
}//TESTED (success and fail cases, see INF1360_test_source.json:test3)
} // (end no entity1_index extracted, entity1 specified)
// Quality checks:
if ((esp.getEntity1() != null) && (null == e.getEntity1()) && (null == esp.getCreationCriteriaScript())) {
// Specified this (entity1), so going to insist on it
if (_context.isStandalone()) { // (minor message, while debugging only)
_context.getHarvestStatus().logMessage(new StringBuffer("Failed to get required entity1 from: ").append(esp.getEntity1()).toString(), true);
}
return null;
}
if ((esp.getEntity1_index() != null) && (null == e.getEntity1_index()) && (null == esp.getCreationCriteriaScript())) {
// Specified this (entity1_index), so going to insist on it
if (_context.isStandalone()) { // (minor message, while debugging only)
_context.getHarvestStatus().logMessage(new StringBuffer("Failed to get required entity1_index from: ").append(esp.getEntity1_index()).toString(), true);
}
return null;
}
//TESTED INF1360_test_source:test7 (no criteria), test8 (criteria)
} // (end entity1)
// Assoc.entity2
if ((esp.getEntity2() != null) || (esp.getEntity2_index() != null))
{
// Association.entity2_index
if (esp.getEntity2_index() != null)
{
if (JavaScriptUtils.containsScript(esp.getEntity2_index()))
{
String s = (String)getValueFromScript(esp.getEntity2_index(), field, index);
if (null != s) e.setEntity2_index(s.toLowerCase());
}
else
{
if ((_iterator != null) && (esp.getEntity2_index().startsWith("$metadata.") || esp.getEntity2_index().startsWith("${metadata."))) {
if (_context.isStandalone()) { // (minor message, while debugging only)
_context.getHarvestStatus().logMessage("Warning: in entity2_index, using global $metadata when iterating", true);
}
}
String s = getFormattedTextFromField(esp.getEntity2_index(), field);
if (null != s) e.setEntity2_index(s.toLowerCase());
}
if (null != e.getEntity2_index()) { // Convert to entity2
int nTypeIndex = e.getEntity2_index().lastIndexOf('/');
if (nTypeIndex > 0) {
e.setEntity2(e.getEntity2_index().substring(0, nTypeIndex));
if (!_entityMap.contains(e.getEntity2_index())) { // Needs to correlate with an entity
StringBuffer error = new StringBuffer("Failed to correlate entity2_index with: ").append(esp.getEntity2_index());
if (_context.isStandalone()) {
error.append(" using ").append(e.getEntity2_index());
}
_context.getHarvestStatus().logMessage(error.toString(), true);
e.setEntity2_index(null);
}//TESTED (INF1360_test_source.json:test8)
}
else { // index must be malformed
StringBuffer error = new StringBuffer("Malformed entity2_index with: ").append(esp.getEntity2_index());
if (_context.isStandalone()) {
error.append(" using ").append(e.getEntity2_index());
}
_context.getHarvestStatus().logMessage(error.toString(), true);
e.setEntity2_index(null);
}
}
}//TESTED (see INF1360_test_source.json:test2)
// entity2
if (null != esp.getEntity2()) {
if (JavaScriptUtils.containsScript(esp.getEntity2()))
{
e.setEntity2((String)getValueFromScript(esp.getEntity2(), field, index));
}
else
{
if ((_iterator != null) && (esp.getEntity2().startsWith("$metadata.") || esp.getEntity2().startsWith("${metadata."))) {
if (_context.isStandalone()) { // (minor message, while debugging only)
_context.getHarvestStatus().logMessage("Warning: in entity2, using global $metadata when iterating", true);
}
}
e.setEntity2(getFormattedTextFromField(esp.getEntity2(), field));
}
if (!bDontResolveToIndices && (null == e.getEntity2_index()))
{
// Try using the entity.disambiguated name, this isn't perfect because 2 entities with different
// types can have different dnames, but we'll try and then abandon if we get multiple hits
int nHits = 0;
String matchingIndex = null;
for (EntityPojo entity : f.getEntities())
{
if (entity.getDisambiguatedName().equalsIgnoreCase(e.getEntity2()))
{
nHits++;
if (1 == nHits) {
matchingIndex = entity.getIndex();
e.setEntity2_index(entity.getIndex());
}
else if (!matchingIndex.equals(entity.getIndex())) { // Ambiguous reference so bail out
StringBuffer error = new StringBuffer("Failed entity2_index disambiguation with: ").append(esp.getEntity2());
if (_context.isStandalone()) {
error.append(" using ").append(e.getEntity2());
}
_context.getHarvestStatus().logMessage(error.toString(), true);
e.setEntity2_index(null);
break;
}
}
} // (end loop across all indices)
}//TESTED (success and fail cases, see INF1360_test_source.json:test3)
} // (end no entity2_index extracted, entity2 specified)
// Quality checks:
if ((esp.getEntity2() != null) && (null == e.getEntity2()) && (null == esp.getCreationCriteriaScript())) {
// Specified this (entity2), so going to insist on it
if (_context.isStandalone()) { // (minor message, while debugging only)
_context.getHarvestStatus().logMessage(new StringBuffer("Failed to get required entity2 from: ").append(esp.getEntity2()).toString(), true);
}
return null;
}
if ((esp.getEntity2_index() != null) && (null == e.getEntity2_index()) && (null == esp.getCreationCriteriaScript())) {
// Specified this (entity2_index), so going to insist on it
if (_context.isStandalone()) { // (minor message, while debugging only)
_context.getHarvestStatus().logMessage(new StringBuffer("Failed to get required entity2_index from: ").append(esp.getEntity2_index()).toString(), true);
}
return null;
}
//TESTED INF1360_test_source:test7 (no criteria), test8 (criteria)
} // (end entity2)
// Association.verb
if (esp.getVerb() != null)
{
if (JavaScriptUtils.containsScript(esp.getVerb()))
{
e.setVerb((String)getValueFromScript(esp.getVerb(), field, index));
}
else
{
e.setVerb(getFormattedTextFromField(esp.getVerb(), field));
}
if ((null == e.getVerb()) && (null == esp.getCreationCriteriaScript())) {
// Specified this, so going to insist on it
if (_context.isStandalone()) { // (minor message, while debugging only)
_context.getHarvestStatus().logMessage(new StringBuffer("Failed to get required verb from: ").append(esp.getVerb()).toString(), true);
}
return null;
}
}
// Association.verb_category
if (esp.getVerb_category() != null)
{
if (JavaScriptUtils.containsScript(esp.getVerb_category()))
{
String s = (String)getValueFromScript(esp.getVerb_category(), field, index);
if (null != s) e.setVerb_category(s.toLowerCase());
}
else
{
String s = getFormattedTextFromField(esp.getVerb_category(), field);
if (null != s) e.setVerb_category(s.toLowerCase());
}
}
if (null == e.getVerb_category()) { // Needed: verb category (get from verb if not specified)
_context.getHarvestStatus().logMessage(new StringBuffer("Failed to get required verb_category from: ").append(esp.getVerb_category()).toString(), true);
return null;
}
if (null == e.getVerb()) { // set from verb cat
e.setVerb(e.getVerb_category());
}
// Entity.start_time
if (esp.getTime_start() != null)
{
String startTimeString = null;
if (JavaScriptUtils.containsScript(esp.getTime_start()))
{
startTimeString = (String)getValueFromScript(esp.getTime_start(), field, index);
}
else
{
startTimeString = getFormattedTextFromField(esp.getTime_start(), field);
}
if (null != startTimeString) {
e.setTime_start(DateUtility.getIsoDateString(startTimeString));
}
// Allow this to be intrinsically optional
}
// Entity.end_time
if (esp.getTime_end() != null)
{
String endTimeString = null;
if (JavaScriptUtils.containsScript(esp.getTime_end()))
{
endTimeString = (String)getValueFromScript(esp.getTime_end(), field, index);
}
else
{
endTimeString = getFormattedTextFromField(esp.getTime_end(), field);
}
if (null != endTimeString) {
e.setTime_end(DateUtility.getIsoDateString(endTimeString));
}
// Allow this to be intrinsically optional
}
// Entity.geo_index
if (esp.getGeo_index() != null)
{
String geo_entity = null;
if (JavaScriptUtils.containsScript(esp.getGeo_index()))
{
geo_entity = (String)getValueFromScript(esp.getGeo_index(), field, index);
}
else
{
if ((_iterator != null) && (esp.getGeo_index().startsWith("$metadata.") || esp.getGeo_index().startsWith("${metadata."))) {
if (_context.isStandalone()) { // (minor message, while debugging only)
_context.getHarvestStatus().logMessage("Warning: in geo_index, using global $metadata when iterating", true);
}
}
geo_entity = getFormattedTextFromField(esp.getGeo_index(), field);
}
if (null != geo_entity) {
geo_entity = geo_entity.toLowerCase();
if (geo_entity.lastIndexOf('/') < 0) {
StringBuffer error = new StringBuffer("Malformed entity2_index with: ").append(esp.getGeo_index());
if (_context.isStandalone()) {
error.append(" using ").append(geo_entity);
}
_context.getHarvestStatus().logMessage(error.toString(), true);
geo_entity = null;
}
if (!_entityMap.contains(geo_entity)) {
StringBuffer error = new StringBuffer("Failed to disambiguate geo_index with: ").append(esp.getGeo_index());
if (_context.isStandalone()) {
error.append(" using ").append(geo_entity);
}
_context.getHarvestStatus().logMessage(error.toString(), true);
geo_entity = null;
}
//TESTED (INF1360_test_source:test4b)
}
//TESTED (INF1360_test_source:test4, test5, test6)
if (null != geo_entity) e.setGeo_index(geo_entity);
GeoPojo s1 = _geoMap.get(geo_entity);
e.setGeotag(s1);
//TESTED (INF1360_test_source:test4)
// Allow this to be intrinsically optional
}
// Get geo information based on geo tag
if (e.getGeotag() == null)
{
// Extract association geoTag if it exists in the association
if (esp.getGeotag() != null)
{
e.setGeotag(getEntityGeo(esp.getGeotag(), null, field));
}
// Otherwise search geoMap on index (entity1_index, entity2_index) for a geoTag
else
{
if (e.getEntity1_index() != null || e.getEntity2_index() != null)
{
GeoPojo s1 = _geoMap.get(e.getEntity1_index());
if (s1 != null)
{
e.setGeotag(s1);
e.setGeo_index(e.getEntity1_index());
}
else {
GeoPojo s2 = _geoMap.get(e.getEntity2_index());
if (s2 != null)
{
e.setGeotag(s2);
e.setGeo_index(e.getEntity2_index());
}
}
}
}
// Allow this to be intrinsically optional
}
// If all the indexes are null don't add the association
if (e.getEntity1_index() == null && e.getEntity2_index() == null && e.getGeo_index() == null) {
if (bDontResolveToIndices && _context.isStandalone()) { // (minor message, while debugging only)
_context.getHarvestStatus().logMessage("Warning: for summaries, at least one entity must be manually specified as an index", true);
}
return null;
}
// Calculate association type
if (bDontResolveToIndices) {
e.setAssociation_type("Summary");
}
else {
e.setAssociation_type(AssociationUtils.getAssocType(e));
if (null != esp.getAssoc_type()) {
if (!e.getAssociation_type().equals("Summary")) {
// Allowed to switch event<->fact
if (esp.getAssoc_type().equalsIgnoreCase("fact")) {
e.setAssociation_type("Fact");
}
else if (esp.getAssoc_type().equalsIgnoreCase("event")) {
e.setAssociation_type("Event");
}
}
}
}