Nested Objects - Inspiredsoft/exporter GitHub Wiki
The most important feature of the framework is the way nested objects are managed. Suppose we have another java class referencing the User class defined in the Basic Example.
@ExpoElement
public class Book {
private User author;
private String title;
private String iban;
public Book(User author, String title, String iban) {
super();
this.author = author;
this.title = title;
this.iban = iban;
}
/* getter and setter */
}
Trying to export the following list with a TextEporter:
List<Book> list = new ArrayList<Book>();
list.add( new Book( new User( "John", "Doe", "jodoe", "john.doe.1" ), "Title One", "12345" ) );
list.add( new Book( new User( "Jane", "Doe", "jadoe", "jane.doe.2" ), "Title Two", "54321" ) );
The result is:
"12345";"Title One";"jodoe";"John";"Doe"
"54321";"Title Two";"jadoe";"Jane";"Doe"
Where the first column is the iban, then the book title and finally the complete user properties. If we are not interested in exporting the entire set of properties for the User class we can annotate the getter of the author property in that way:
@ExpoProperty(value="name")
public User getAuthor() {...}
to have just the name or:
@ExpoProperties( property = {@ExpoProperty(value="name"), @ExpoProperty(value="surname")} )
public User getAuthor() {...}
to have name and surname only.
Note that the annotation in the getter method overwrite what defined in the User class. Therefore if, for example, the User class is declared unexportable, when nested in another object the annotation is ignored, and the user data is exported as part of the including object.
#Inheritance To explain how to manage inheritance suppose we have a class Person:
public class Person {
private String name;
private String surname;
/* getter and setter */
}
and the class User that extends the class Person adding the properties login and password:
public class User extends Person {
private String login;
private String password;
/* getter and setter */
}
Annotating the class User with @ExpoElement we have three options for the property value:
-
@ExpoElement( value = ExportType.CLASS ) which is the default value and telling the framework that all the properties from class and superclass can be exported. The output is exactly the same shown in the Basic Example;
-
@ExpoElement( value = ExportType.SUPERCLASS ) where only the properties defined in the superclass Person are taken into account for the export process. The output in that way is:
"John";"Doe"
"Jane";"Doe"
-
@ExpoElement( value = ExportType.IGNORE ) where the entire set of properties from class and superclass are completely ignored. The output is empty.