Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

storing List<String> in active objects

Ignat December 22, 2022

Hi all.

I need to associate and store in plugin settings List of customfields keys with lists of users

I've implemented entity class (there could be several exemplers of it to be saved)

@Getter
@Setter
public class FieldGroup extends Entity {
List<String> fieldKeys;
List<String> userKeys;
}

 but found out that AO do not work in such way.

one solution is to define separate FieldKey and UserKey entities and FieldToUser class to define many to many relationsheeps among them. But it won't be able to represent several FieldGroups as distinct entities.

Is it some other way to store List (or arrays) of Strings in AO entity field?

1 answer

1 accepted

0 votes
Answer accepted
Tuncay Senturk
Community Champion
December 28, 2022

Hi @Ignat 

Unfortunately with the AO entity we have very basic persistency capabilities. I'd have separate entities linked to FieldGroup with many-to-one relation (child table for each linking with fieldGroupId).

The second option is to have fieldKeys and userKeys as String fields delimited by a character and convert them into Lists before using them.

Ignat December 28, 2022

I've implemented the second option. just to keep things simple

Like Tuncay Senturk likes this
Tuncay Senturk
Community Champion
December 28, 2022

By the way, you can write a Wrapper Entity class using @Implementation annotation.

Something like this:

@Implementation(MyEntityImpl.class)
public interface MyEntity {
public String getFieldKeys() ...
}

 

public class MyEntityImpl {
public List<String> getFieldKeyList() {
String strList = getFieldKeys();
return StringUtils.split(....)
}
}
Like Ignat likes this
Ignat December 30, 2022

Thank for usefull hint! 

I've implemented transformation in method of my service class, whose purpose is to acquire settings groups List from AO

@Table("settings")
public interface FieldsGroupSettingsRaw extends Entity {
void setDescription(String description);
void setFieldsKeys(String fieldsKeys);
void setUsersKeys(String usersKeys);
String getDescription();
String getFieldsKeys();
String getUsersKeys();
}  

 

@Getter
@Setter
@AllArgsConstructor
public class FieldsGroupSettings {
private int ID;
private String description;
private List<String> fieldsKeys;
private List<String> usersKeys;
}

 

 public List<FieldsGroupSettings> all() {
log.info("retrying settings");
return Arrays.stream(activeObjects.find(FieldsGroupSettingsRaw.class))
.map(r -> new FieldsGroupSettings(r.getID(),
r.getDescription(),
Arrays.asList(r.getFieldsKeys()
.replace("[", "")
.replace("]", "")
.split(", ")),
Arrays.asList(r.getUsersKeys()
.replace("[", "")
.replace("]", "")
.split(", "))))
.collect(Collectors.toList());
}

 

But use implemening class as the adapter leads to more clear code.

Like Tuncay Senturk likes this
Tuncay Senturk
Community Champion
December 30, 2022

Happy to help!

Suggest an answer

Log in or Sign up to answer