Skip to content

Commit

Permalink
changes for polymorphic lookup relationship
Browse files Browse the repository at this point in the history
Changes to support reference to the related object in a polymorphic lookup relationship using an idLookup field other than id.
  • Loading branch information
ashitsalesforce committed Jan 30, 2024
1 parent c7b84d7 commit 3f65240
Show file tree
Hide file tree
Showing 11 changed files with 349 additions and 165 deletions.
39 changes: 10 additions & 29 deletions src/main/java/com/salesforce/dataloader/client/PartnerClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public DescribeSObjectResult run(String entity) throws ConnectionException {
};

private DescribeGlobalResult describeGlobalResults;
private final Map<String, DescribeRefObject> referenceEntitiesDescribesMap = new HashMap<String, DescribeRefObject>();
private final ReferenceEntitiesDescribeMap referenceEntitiesDescribesMap = new ReferenceEntitiesDescribeMap();
private final Map<String, DescribeGlobalSObjectResult> describeGlobalResultsMap = new HashMap<String, DescribeGlobalSObjectResult>();
private final Map<String, DescribeSObjectResult> entityFieldDescribesMap = new HashMap<String, DescribeSObjectResult>();

Expand Down Expand Up @@ -562,7 +562,7 @@ public DescribeSObjectResult getFieldTypes() {
}
}

public Map<String, DescribeRefObject> getReferenceDescribes() {
public ReferenceEntitiesDescribeMap getReferenceDescribes() {
return referenceEntitiesDescribesMap;
}

Expand Down Expand Up @@ -938,42 +938,23 @@ private boolean checkConnectionException(ConnectionException ex, String operatio

private final Map<String, Field> fieldsByName = new HashMap<String, Field>();

public Field getField(String apiName) {
apiName = apiName.toLowerCase();
Field field = this.fieldsByName.get(apiName);
public Field getField(String sObjectFieldName) {
sObjectFieldName = sObjectFieldName.toLowerCase();
Field field = this.fieldsByName.get(sObjectFieldName);
if (field == null) {
field = lookupField(apiName);
this.fieldsByName.put(apiName, field);
field = lookupField(sObjectFieldName);
this.fieldsByName.put(sObjectFieldName, field);
}
return field;
}

private Field lookupField(String apiName) {
private Field lookupField(String sObjectFieldName) {
// look for field on target object
for (Field f : getFieldTypes().getFields()) {
if (apiName.equals(f.getName().toLowerCase()) || apiName.equals(f.getLabel().toLowerCase()))
if (sObjectFieldName.equals(f.getName().toLowerCase()) || sObjectFieldName.equals(f.getLabel().toLowerCase()))
return f;
}
// look for reference field on target object
if (apiName.contains(":")) {
Map<String, DescribeRefObject> refs = getReferenceDescribes();
for (Map.Entry<String, DescribeRefObject> ent : refs.entrySet()) {
String relName = ent.getKey().toLowerCase();
if (apiName.startsWith(relName)) {
for (Map.Entry<String, Field> refEntry : ent.getValue().getParentObjectFieldMap().entrySet()) {
String thisRefName = relName + ":" + refEntry.getKey().toLowerCase();
if (apiName.contains(".")) {
thisRefName = relName + ":"
+ ent.getValue().getParentObjectName()
+ "." + refEntry.getKey().toLowerCase();
}

if (apiName.equalsIgnoreCase(thisRefName)) return refEntry.getValue();
}
}
}
}
return null;
return this.referenceEntitiesDescribesMap.getParentField(sObjectFieldName);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright (c) 2015, salesforce.com, inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* Neither the name of salesforce.com, inc. nor the names of its contributors may be used to endorse or
* promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

package com.salesforce.dataloader.client;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.salesforce.dataloader.dyna.RelationshipField;
import com.sforce.soap.partner.Field;

/**
*
*/
public class ReferenceEntitiesDescribeMap {

private Map<String, DescribeRefObject> referenceEntitiesDescribeMap = new HashMap<String, DescribeRefObject>();
/**
*
*/
public ReferenceEntitiesDescribeMap() {

}

public void put(String relationshipFieldName, DescribeRefObject parent) {
RelationshipField objField = new RelationshipField(parent.getParentObjectName(), relationshipFieldName);
referenceEntitiesDescribeMap.put(objField.toFormattedRelationshipString(), parent);
}

// fieldName could be in the old format that assumes single parent:
// <relationship name attr of the field on child sobject>:<idlookup field name on parent sobject>
//
// fieldName could also be in the new format
// <name of parent object>:<rel name on child object>.<idlookup field name on parent>

public DescribeRefObject getParentSObject(String lookupFieldName) {
return getParentSObject(new RelationshipField(lookupFieldName, false));
}

public void clear() {
this.referenceEntitiesDescribeMap.clear();
}

public int size() {
return this.referenceEntitiesDescribeMap.size();
}

public Set<String> keySet() {
return this.referenceEntitiesDescribeMap.keySet();
}

// fieldName could be in the old format that assumes single parent:
// <relationship name attr of the field on child sobject>:<idlookup field name on parent sobject>
//
// fieldName could also be in the new format
// <name of parent object>:<rel name on child object>.<idlookup field name on parent>
public Field getParentField(String fieldName) {
RelationshipField fieldName4LR = new RelationshipField(fieldName, true);
if (fieldName4LR == null
|| fieldName4LR.getParentFieldName() == null
|| fieldName4LR.getRelationshipName() == null) {
return null;
} else {
DescribeRefObject parent = getParentSObject(fieldName4LR);
if (parent == null) {
return null;
}
for (Map.Entry<String, Field> refEntry : parent.getParentObjectFieldMap().entrySet()) {
if (fieldName4LR.getParentFieldName().equalsIgnoreCase(refEntry.getKey())) {
return refEntry.getValue();
}
}
return null;
}
}

private DescribeRefObject getParentSObject(RelationshipField fieldName4LR) {
if (fieldName4LR == null || fieldName4LR.getRelationshipName() == null) {
return null;
}
for (Map.Entry<String, DescribeRefObject> ent : referenceEntitiesDescribeMap.entrySet()) {
String relNameInEntry = ent.getKey().toLowerCase();
if (fieldName4LR.isRelationshipName(relNameInEntry)) {
return ent.getValue();
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.salesforce.dataloader.client.DescribeRefObject;
import com.salesforce.dataloader.client.HttpClientTransport;
import com.salesforce.dataloader.client.PartnerClient;
import com.salesforce.dataloader.client.ReferenceEntitiesDescribeMap;
import com.salesforce.dataloader.config.Config;
import com.salesforce.dataloader.config.Messages;
import com.salesforce.dataloader.dao.DataAccessObject;
Expand Down Expand Up @@ -205,7 +206,7 @@ public DescribeSObjectResult getFieldTypes() {
return getPartnerClient().getFieldTypes();
}

public Map<String, DescribeRefObject> getReferenceDescribes() {
public ReferenceEntitiesDescribeMap getReferenceDescribes() {
validateSession();
return getPartnerClient().getReferenceDescribes();
}
Expand Down
108 changes: 0 additions & 108 deletions src/main/java/com/salesforce/dataloader/dyna/ObjectField.java

This file was deleted.

Loading

0 comments on commit 3f65240

Please sign in to comment.