Screen Controller Inheritance

Hi Support Team :slight_smile:

I have the following entity relationship in my structure:

ParentJob
(Shared Attributes)

Child: Job, Child: DraftJob

They are all basically the same (the only difference is D-Type).

I am trying to create one editor screen that can be used for all types of Job (as long as they extend ParentJob)

Is this possible?

I have extended ParentJobEdit and called it JobEdit - the xml extends fine but the screen controller still thinks it’s a ParentJob instead of the child class. (GetEditedEntity returns ParentJob instead of Job)

I can submit a sample project if that helps :slight_smile:

Thank you

Regards

Matt

Hi!
Please check our guide that demonstrates how to use entity inheritance in CUBA applications.

Regards,
Natalia

Hi Natalia,

I have already looked at the documentation and it doesn’t document properly the use case I mention above.

When you extend an edit screen - getEditedEntity returns the parent object rather than the new entity type you are extending the screen to be used with.

My question is whether that return type can be specified somewhere to return the new entity to be edited.

Thank you

Regards

Matt

Yes that would help.

1 Like

Here is my sample project :slight_smile:

SDL-Testing-Project copy.zip (545.0 KB)

So if you try to use getEditedEntity in the JobEdit screen - it will return a ParentJob object rather than a Job object. (I would like the Job object)

Thank you for your help

Regards

Matt

Hi Matt,

Thanks for the demo project.
In my opinion, you did everything right, and getEditedEntity() in JobEdit actually returns an instance of Job (descendant).

Try to add the following code to JobEdit:

@Subscribe
public void onAfterShow(AfterShowEvent event) {
    ParentJob editedEntity = getEditedEntity();
    if (editedEntity instanceof Job) {
        System.out.println("editedEntity is actually Job");
    } else {
        System.out.println("editedEntity is " + editedEntity.getClass());
    }
}

So you can safely cast to Job like this:

Job job = (Job) getEditedEntity();

Regards,
Konstantin

1 Like

Hi Konstantin thank you for that :slight_smile:

I have tried casting - but how do you use the screen builders appropriately so that it opens a Job?

When I try to build one normally you get the following error:

image

image

Kind regards

Matt

Perhaps the most correct solution will be to make the parent screen generic:

public class ParentJobEdit<T extends ParentJob> extends StandardEditor<T> {
}

and parameterize the child screen with concrete entity:

public class JobEdit extends ParentJobEdit<Job> {
}
1 Like

This was exactly what I was looking for :slight_smile: Thank you very much Konstantin.

Top notch.

Regards

Matt