dehaatbi
Returning Observer

Release Referenced Data Entities

Jump to solution

Hello,

I have a question regarding release/publication workflows. I wanted to create a Workflow for a page, which automatically releases all referenced data entities.

I quickly found out that I can get the entities with page.getOutgoingReferences(). But the Entities Object does not have a .release() method. For that I need a corresponding Dataset.

I solved this with the following function:

private Dataset findDatasetForEntity(Entity entity){
    StoreAgent storeAgent = context.requireSpecialist(StoreAgent.TYPE);
    ContentStoreRoot contentStoreRoot = storeAgent.getStore(Store.Type.CONTENTSTORE);
    for (Content2 content2: contentStoreRoot.getChildren(Content2.class, true)){
        if (content2.getEntityType().equals(entity.getEntityType())){
            return content2.getDataset(entity);
        }
    }
    return null;
}

 

This works, but seems overly complicated. Is there a better way to do this?

 

Best regards,

Tobi

0 Kudos
1 Solution

Accepted Solutions
mbergmann
Crownpeak employee

Hi Tobi,

theoretically, you could use the schema session to release an entity:

Session session = entity.getSession();
session.release(entitiy);
session.commit();

But - depending on your project - there might be some drawbacks...

When releasing an entity "directly", the <RULES> will not be checked because they are part of the table template which is the "glue" between an entity and a dataset.

You might also want to further check outgoing references of the entity/dataset - of course that's also possible for an entity via Schema.getOutgoingReferences(...) - but in case of a Dataset you'd also have an IDProvider (more precisely: StoreElement) where you can also just use .getOutouingReferences (like for other elements). So you could handle this more "generally" without having to distinguish between cases (only relevant if you are doing more reference checks also for other elements).

Of course, iterating through all datasources for every entity is not that optimal. One idea would be to load the datasources upfront (and create some kind of mapping).  

Also, as you just use the first found Datasource, that might (depending on your project) not the best one (e.g. without rules). What's the "best" approach always depends on your project.

A quite common alternative approach is not to use a Datasource (Content2) to create the Dataset but a TableTemplate (both are DatasetProviders and have the getEntity method). The idea would be to iterate through the Schema's tabletemplates and filter for those having the matching EntityType. Among those, you could use

  • the first one that has a preview page defined (because in many projects, that is usually the template with all input components and thus also rules), or
  • the one with the highest number of input components
  • ... etc.

Hope this helps 😉 

Michael

View solution in original post

2 Replies
mbergmann
Crownpeak employee

Hi Tobi,

theoretically, you could use the schema session to release an entity:

Session session = entity.getSession();
session.release(entitiy);
session.commit();

But - depending on your project - there might be some drawbacks...

When releasing an entity "directly", the <RULES> will not be checked because they are part of the table template which is the "glue" between an entity and a dataset.

You might also want to further check outgoing references of the entity/dataset - of course that's also possible for an entity via Schema.getOutgoingReferences(...) - but in case of a Dataset you'd also have an IDProvider (more precisely: StoreElement) where you can also just use .getOutouingReferences (like for other elements). So you could handle this more "generally" without having to distinguish between cases (only relevant if you are doing more reference checks also for other elements).

Of course, iterating through all datasources for every entity is not that optimal. One idea would be to load the datasources upfront (and create some kind of mapping).  

Also, as you just use the first found Datasource, that might (depending on your project) not the best one (e.g. without rules). What's the "best" approach always depends on your project.

A quite common alternative approach is not to use a Datasource (Content2) to create the Dataset but a TableTemplate (both are DatasetProviders and have the getEntity method). The idea would be to iterate through the Schema's tabletemplates and filter for those having the matching EntityType. Among those, you could use

  • the first one that has a preview page defined (because in many projects, that is usually the template with all input components and thus also rules), or
  • the one with the highest number of input components
  • ... etc.

Hope this helps 😉 

Michael

Hi,

thank you for the detailed answer. I have now a few options to consider 🙂

Tobi

0 Kudos