-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1961 from scireum/feature/aha/import-scripting-3
Uses CustomEvents within ImportJobs
- Loading branch information
Showing
23 changed files
with
472 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Made with all the love in the world | ||
* by scireum in Remshalden, Germany | ||
* | ||
* Copyright by scireum GmbH | ||
* http://www.scireum.de - info@scireum.de | ||
*/ | ||
|
||
package sirius.biz.importer; | ||
|
||
import sirius.db.mixing.BaseEntity; | ||
import sirius.db.mixing.Entity; | ||
import sirius.kernel.commons.Context; | ||
import sirius.pasta.noodle.sandbox.NoodleSandbox; | ||
|
||
/** | ||
* Triggered within {@link ImportHandler#load(Context, BaseEntity)} in order to update an entity | ||
* using the given context. | ||
* <p> | ||
* Note that this is triggered after loading the data into the entity. Therefore the entity should | ||
* be modified by the context shouldn't. | ||
* | ||
* @param <E> the type of entity being updated | ||
*/ | ||
public class AfterLoadEvent<E extends Entity> extends ContextScriptableEvent<E> { | ||
private final E entity; | ||
|
||
/** | ||
* Creates a new event for the given entity | ||
* | ||
* @param entity the entity to update | ||
* @param context the context to read data from. Note that this can and should be modified by the handler, | ||
* as this is the whole purpose of this event anyway. | ||
* @param importerContext the import context which can be used to access other handlers / the importer itself | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public AfterLoadEvent(E entity, Context context, ImporterContext importerContext) { | ||
super((Class<E>) entity.getClass(), context, importerContext); | ||
this.entity = entity; | ||
} | ||
|
||
@NoodleSandbox(NoodleSandbox.Accessibility.GRANTED) | ||
public E getEntity() { | ||
return entity; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "AfterLoadEvent: " | ||
+ getType().getName() | ||
+ " into " | ||
+ entity | ||
+ "(ID: " | ||
+ entity.getIdAsString() | ||
+ ") with context: " | ||
+ getContext(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/sirius/biz/importer/BeforeCreateOrUpdateEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Made with all the love in the world | ||
* by scireum in Remshalden, Germany | ||
* | ||
* Copyright by scireum GmbH | ||
* http://www.scireum.de - info@scireum.de | ||
*/ | ||
|
||
package sirius.biz.importer; | ||
|
||
import sirius.biz.scripting.TypedScriptableEvent; | ||
import sirius.db.mixing.BaseEntity; | ||
import sirius.db.mixing.Entity; | ||
|
||
/** | ||
* Triggered within {@link ImportHandler#createOrUpdateNow(BaseEntity)} (or the batch equivalent) in order to update an | ||
* entity using the given context. | ||
* | ||
* @param <E> the type of entity being updated | ||
*/ | ||
public class BeforeCreateOrUpdateEvent<E extends Entity> extends TypedScriptableEvent<E> { | ||
private final E entity; | ||
private final ImporterContext importerContext; | ||
|
||
/** | ||
* Creates a new event for the given entity | ||
* | ||
* @param entity the entity to update | ||
* @param importerContext the import context which can be used to access other handlers / the importer itself | ||
*/ | ||
public BeforeCreateOrUpdateEvent(E entity, ImporterContext importerContext) { | ||
this.importerContext = importerContext; | ||
this.entity = entity; | ||
} | ||
|
||
public E getEntity() { | ||
return entity; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "BeforeCreateOrUpdateEvent: " | ||
+ getType().getName() | ||
+ " with entity " | ||
+ entity | ||
+ "(ID: " | ||
+ entity.getIdAsString() | ||
+ ")"; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public Class<E> getType() { | ||
return (Class<E>) entity.getClass(); | ||
} | ||
|
||
public ImporterContext getImporterContext() { | ||
return importerContext; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Made with all the love in the world | ||
* by scireum in Remshalden, Germany | ||
* | ||
* Copyright by scireum GmbH | ||
* http://www.scireum.de - info@scireum.de | ||
*/ | ||
|
||
package sirius.biz.importer; | ||
|
||
import sirius.db.mixing.Entity; | ||
import sirius.kernel.commons.Context; | ||
|
||
/** | ||
* Triggered within {@link sirius.biz.importer.ImportHandler#tryFind(Context)} before the entity is actually being | ||
* looked up. | ||
* | ||
* @param <E> the type of entity being looked up | ||
*/ | ||
public class BeforeFindEvent<E extends Entity> extends ContextScriptableEvent<E> { | ||
|
||
/** | ||
* Creates a new event for the given entity type, context and import context. | ||
* | ||
* @param entityType the type of entities being looked up | ||
* @param context the context to read data from. Note that this can and should be modified by the handler, | ||
* as this is the whole purpose of this event anyway. | ||
* @param importerContext the import context which can be used to access other handlers / the importer itself | ||
*/ | ||
public BeforeFindEvent(Class<E> entityType, Context context, ImporterContext importerContext) { | ||
super(entityType, context, importerContext); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "BeforeFindEvent: " + getType().getName() + " with context: " + getContext(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Made with all the love in the world | ||
* by scireum in Remshalden, Germany | ||
* | ||
* Copyright by scireum GmbH | ||
* http://www.scireum.de - info@scireum.de | ||
*/ | ||
|
||
package sirius.biz.importer; | ||
|
||
import sirius.db.mixing.BaseEntity; | ||
import sirius.db.mixing.Entity; | ||
import sirius.kernel.commons.Context; | ||
import sirius.pasta.noodle.sandbox.NoodleSandbox; | ||
|
||
/** | ||
* Triggered within {@link ImportHandler#load(Context, BaseEntity)} in order to update an entity | ||
* using the given context. | ||
* <p> | ||
* Note that this is triggered before actually loading the data into the entity. Therefore the context should | ||
* be modified by the entity shouldn't. | ||
* | ||
* @param <E> the type of entity being updated | ||
*/ | ||
public class BeforeLoadEvent<E extends Entity> extends ContextScriptableEvent<E> { | ||
private final E entity; | ||
|
||
/** | ||
* Creates a new event for the given entity | ||
* | ||
* @param entity the entity to update | ||
* @param context the context to read data from. Note that this can and should be modified by the handler, | ||
* as this is the whole purpose of this event anyway. | ||
* @param importerContext the import context which can be used to access other handlers / the importer itself | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public BeforeLoadEvent(E entity, Context context, ImporterContext importerContext) { | ||
super((Class<E>) entity.getClass(), context, importerContext); | ||
this.entity = entity; | ||
} | ||
|
||
@NoodleSandbox(NoodleSandbox.Accessibility.GRANTED) | ||
public E getEntity() { | ||
return entity; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "BeforeLoadEvent: " | ||
+ getType().getName() | ||
+ " into " | ||
+ entity | ||
+ "(ID: " | ||
+ entity.getIdAsString() | ||
+ ") with context: " | ||
+ getContext(); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/main/java/sirius/biz/importer/ContextScriptableEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Made with all the love in the world | ||
* by scireum in Remshalden, Germany | ||
* | ||
* Copyright by scireum GmbH | ||
* http://www.scireum.de - info@scireum.de | ||
*/ | ||
|
||
package sirius.biz.importer; | ||
|
||
import sirius.biz.scripting.TypedScriptableEvent; | ||
import sirius.kernel.commons.Context; | ||
import sirius.pasta.noodle.sandbox.NoodleSandbox; | ||
|
||
/** | ||
* Provides a base implementation for all events which are scriptable and have a context / import context | ||
* | ||
* @param <E> the generic type of entities being handled | ||
*/ | ||
public abstract class ContextScriptableEvent<E> extends TypedScriptableEvent<E> { | ||
|
||
private final Class<E> entityType; | ||
private final Context context; | ||
private final ImporterContext importerContext; | ||
|
||
/** | ||
* Creates a new event for the given entity type, context and import context. | ||
* | ||
* @param entityType the type of entity being handled | ||
* @param context the context to read data from. Note that this can and should be modified by the handler, | ||
* as this is the whole purpose of this event anyway. | ||
* @param importerContext the import context which can be used to access other handlers / the importer itself | ||
*/ | ||
@SuppressWarnings("AssignmentOrReturnOfFieldWithMutableType") | ||
protected ContextScriptableEvent(Class<E> entityType, Context context, ImporterContext importerContext) { | ||
this.entityType = entityType; | ||
this.context = context; | ||
this.importerContext = importerContext; | ||
} | ||
|
||
/** | ||
* Returns the context which contains the data of the current event. | ||
* <p> | ||
* Note that this is mutable by design, as the handler will most probably want to modify the context | ||
* before it is processed by the import handler | ||
* | ||
* @return the context containing the actual data being imported | ||
*/ | ||
@SuppressWarnings("AssignmentOrReturnOfFieldWithMutableType") | ||
@NoodleSandbox(NoodleSandbox.Accessibility.GRANTED) | ||
public Context getContext() { | ||
return context; | ||
} | ||
|
||
@NoodleSandbox(NoodleSandbox.Accessibility.GRANTED) | ||
public ImporterContext getImporterContext() { | ||
return importerContext; | ||
} | ||
|
||
@Override | ||
public Class<E> getType() { | ||
return entityType; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.