33import com .fasterxml .jackson .annotation .JsonIgnoreProperties ;
44import com .fasterxml .jackson .annotation .JsonInclude ;
55import com .fasterxml .jackson .annotation .JsonProperty ;
6+ import com .google .common .annotations .VisibleForTesting ;
7+ import com .google .common .base .Strings ;
8+ import com .google .common .collect .Lists ;
69import com .google .common .collect .Maps ;
10+ import org .slf4j .Logger ;
11+ import org .slf4j .LoggerFactory ;
712
813import java .util .HashMap ;
914import java .util .List ;
15+ import java .util .Map ;
1016
1117@ SuppressWarnings ("UnusedDeclaration" )
1218@ JsonIgnoreProperties (ignoreUnknown = true )
1319@ JsonInclude (JsonInclude .Include .NON_EMPTY )
1420public class Tag extends TypedData {
1521
22+ private static final Logger logger = LoggerFactory .getLogger ("intercom-java" );
1623 private static final HashMap <String , String > SENTINEL = Maps .newHashMap ();
1724
25+
1826 public static Tag tag (Tag tag , UserCollection users ) throws InvalidException , AuthorizationException {
19- TagTypedCollection tagTypedCollection = new TagTypedCollection ();
20- tagTypedCollection .setName (tag .getName ());
21- tagTypedCollection .setId (tag .getId ());
22- tagTypedCollection .setUsers (users .getPageItems ());
27+ TagTypedCollection tagTypedCollection = createTagTypedCollection (tag , users );
2328 return DataResource .create (tagTypedCollection , "tags" , Tag .class );
2429 }
2530
2631 public static Tag tag (Tag tag , CompanyCollection companies ) throws InvalidException , AuthorizationException {
27- TagTypedCollection tagTypedCollection = new TagTypedCollection ();
28- tagTypedCollection .setName (tag .getName ());
29- tagTypedCollection .setId (tag .getId ());
30- tagTypedCollection .setCompanies (companies .getPageItems ());
32+ TagTypedCollection tagTypedCollection = createTagTypedCollection (tag , companies );
3133 return DataResource .create (tagTypedCollection , "tags" , Tag .class );
3234 }
3335
@@ -54,30 +56,89 @@ public static TagCollection list() throws InvalidException, AuthorizationExcepti
5456 return DataResource .list (SENTINEL , "tags" , TagCollection .class );
5557 }
5658
59+ @ VisibleForTesting
60+ static TagTypedCollection createTagTypedCollection (Tag tag , UserCollection users ) {
61+ TagTypedCollection tagTypedCollection = new TagTypedCollection ();
62+ tagTypedCollection .setName (tag .getName ());
63+ tagTypedCollection .setId (tag .getId ());
64+ final List <Map <String ,String >> usersLite = Lists .newArrayList ();
65+ final List <User > pageItems = users .getPageItems ();
66+ for (User user : pageItems ) {
67+ Map <String , String > userMap = Maps .newHashMap ();
68+ final String id = user .getId ();
69+ final String email = user .getEmail ();
70+ final String userId = user .getUserId ();
71+ if (!Strings .isNullOrEmpty (id )) {
72+ userMap .put ("id" , id );
73+ usersLite .add (userMap );
74+ } else if (!Strings .isNullOrEmpty (email )) {
75+ userMap .put ("email" , email );
76+ usersLite .add (userMap );
77+ } else if (!Strings .isNullOrEmpty (userId )) {
78+ userMap .put ("user_id" , userId );
79+ usersLite .add (userMap );
80+ } else {
81+ logger .warn ("no identifiers found for user tag target, skipping [" + tag + "] [" + user .toString () + "]" );
82+ }
83+ }
84+ tagTypedCollection .setUsers (usersLite );
85+ return tagTypedCollection ;
86+ }
87+
88+ @ VisibleForTesting
89+ static TagTypedCollection createTagTypedCollection (Tag tag , CompanyCollection companies ) {
90+ TagTypedCollection tagTypedCollection = new TagTypedCollection ();
91+ tagTypedCollection .setName (tag .getName ());
92+ tagTypedCollection .setId (tag .getId ());
93+
94+ final List <Map <String , String >> companiesLite = Lists .newArrayList ();
95+ final List <Company > pageItems = companies .getPageItems ();
96+ for (Company company : pageItems ) {
97+ Map <String , String > companyMap = Maps .newHashMap ();
98+ final String companyID = company .getCompanyID ();
99+ final String id1 = company .getId ();
100+ final String name = company .getName ();
101+ if (!Strings .isNullOrEmpty (companyID )) {
102+ companyMap .put ("company_id" , companyID );
103+ companiesLite .add (companyMap );
104+ } else if (!Strings .isNullOrEmpty (id1 )) {
105+ companyMap .put ("id" , id1 );
106+ companiesLite .add (companyMap );
107+ } else if (!Strings .isNullOrEmpty (name )) {
108+ companyMap .put ("name" , name );
109+ companiesLite .add (companyMap );
110+ } else {
111+ logger .warn ("no identifiers found for company tag target, skipping [" + tag + "] [" + company .toString () + "]" );
112+ }
113+ tagTypedCollection .setCompanies (companiesLite );
114+ }
115+ return tagTypedCollection ;
116+ }
117+
57118 @ SuppressWarnings ("UnusedDeclaration" )
58119 @ JsonIgnoreProperties (ignoreUnknown = true )
59120 @ JsonInclude (JsonInclude .Include .NON_DEFAULT )
60121 static class TagTypedCollection extends Tag {
61122
62123 @ JsonProperty ("users" )
63- private List <User > users ;
124+ private List <Map < String , String > > users ;
64125
65126 @ JsonProperty ("companies" )
66- private List <Company > companies ;
127+ private List <Map < String , String > > companies ;
67128
68- public List <User > getUsers () {
129+ public List <Map < String , String > > getUsers () {
69130 return users ;
70131 }
71132
72- public void setUsers (List <User > users ) {
73- this .users = users ;
133+ public void setUsers (List <Map < String , String >> usersLite ) {
134+ this .users = usersLite ;
74135 }
75136
76- public List <Company > getCompanies () {
137+ public List <Map < String , String > > getCompanies () {
77138 return companies ;
78139 }
79140
80- public void setCompanies (List <Company > companies ) {
141+ public void setCompanies (List <Map < String , String > > companies ) {
81142 this .companies = companies ;
82143 }
83144 }
0 commit comments