-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve support for record multiple value custom fields (#52)
* improve support for record multiple value custom fields * update changelog * fix changelog * fix test
- Loading branch information
Showing
11 changed files
with
413 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/main/java/com/darksci/pardot/api/parser/prospect/ProspectCustomFieldDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* Copyright 2017, 2018, 2019, 2020 Stephen Powis https://github.com/Crim/pardot-java-client | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | ||
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the | ||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit | ||
* persons to whom the Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the | ||
* Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE | ||
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package com.darksci.pardot.api.parser.prospect; | ||
|
||
import com.darksci.pardot.api.response.customfield.ProspectCustomFieldValue; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Custom deserializer to be able to support Record Multiple custom fields. | ||
*/ | ||
public class ProspectCustomFieldDeserializer extends StdDeserializer<ProspectCustomFieldValue> { | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public ProspectCustomFieldDeserializer() { | ||
this(null); | ||
} | ||
|
||
/** | ||
* Constructor. | ||
* @param vc the type of the class this handles. | ||
*/ | ||
public ProspectCustomFieldDeserializer(final Class<?> vc) { | ||
super(vc); | ||
} | ||
|
||
@Override | ||
public ProspectCustomFieldValue deserialize(final JsonParser parser, final DeserializationContext context) throws IOException { | ||
// Get the current custom field name. | ||
final String fieldName = parser.getCurrentName(); | ||
|
||
// Keep track of all the values associated with the field. | ||
final List<String> fieldValues = new ArrayList<>(); | ||
|
||
// If we have multiple values | ||
if (parser.getCurrentToken() == JsonToken.START_OBJECT) { | ||
// Loop until we hit end object | ||
while (parser.nextToken() != JsonToken.END_OBJECT) { | ||
// Pull out each value | ||
if (parser.getCurrentToken() == JsonToken.VALUE_STRING) { | ||
fieldValues.add(parser.getValueAsString()); | ||
} | ||
} | ||
} else if (parser.getCurrentToken() == JsonToken.VALUE_STRING) { | ||
// If we have a single value, we just record the value as is. | ||
fieldValues.add(parser.getValueAsString()); | ||
} | ||
|
||
// Return our deserialized instance. | ||
return new ProspectCustomFieldValue(fieldName, fieldValues); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
src/main/java/com/darksci/pardot/api/response/customfield/ProspectCustomFieldValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* Copyright 2017, 2018, 2019, 2020 Stephen Powis https://github.com/Crim/pardot-java-client | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | ||
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the | ||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit | ||
* persons to whom the Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the | ||
* Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE | ||
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package com.darksci.pardot.api.response.customfield; | ||
|
||
import com.darksci.pardot.api.parser.prospect.ProspectCustomFieldDeserializer; | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
/** | ||
* Represents a Prospect CustomField value. | ||
* Since custom fields can be stored as a single value, or as a list of values (for record multiple fields), | ||
* this attempts to normalize the way these two types are accessed. | ||
*/ | ||
@JsonDeserialize(using = ProspectCustomFieldDeserializer.class) | ||
public class ProspectCustomFieldValue { | ||
private final String fieldName; | ||
private final List<String> values = new ArrayList<>(); | ||
|
||
/** | ||
* Constructor for custom field which only has a single value. | ||
* @param fieldName name of the field. | ||
* @param value value of the field. | ||
*/ | ||
public ProspectCustomFieldValue(final String fieldName, final String value) { | ||
this.fieldName = fieldName; | ||
if (value != null) { | ||
this.values.add(value); | ||
} | ||
} | ||
|
||
/** | ||
* Constructor for custom field which has multiple values. | ||
* @param fieldName name of the field. | ||
* @param values values for the field. | ||
*/ | ||
public ProspectCustomFieldValue(final String fieldName, final Collection<String> values) { | ||
this.fieldName = fieldName; | ||
if (values != null) { | ||
this.values.addAll(values); | ||
} | ||
} | ||
|
||
public String getFieldName() { | ||
return fieldName; | ||
} | ||
|
||
/** | ||
* The value of the custom field. | ||
* @return value of the custom field. | ||
* *NOTE* If this is a record multiple field with multiple values, this will return the first value only. | ||
*/ | ||
public String getValue() { | ||
if (values.isEmpty()) { | ||
return null; | ||
} | ||
return values.get(0); | ||
} | ||
|
||
public List<String> getValues() { | ||
return values; | ||
} | ||
|
||
public boolean hasMultipleValues() { | ||
return getValues().size() > 1; | ||
} | ||
|
||
public void addValue(final String value) { | ||
values.add(value); | ||
} | ||
|
||
public void addValues(final Collection<String> values) { | ||
values.addAll(values); | ||
} | ||
|
||
public void setValue(final String value) { | ||
values.clear(); | ||
addValue(value); | ||
} | ||
|
||
public void setValues(final Collection<String> values) { | ||
values.clear(); | ||
addValues(values); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ProspectCustomFieldValue{" | ||
+ "fieldName='" + fieldName + '\'' | ||
+ ", values=" + values | ||
+ '}'; | ||
} | ||
} |
Oops, something went wrong.