Skip to content

Commit

Permalink
Merge pull request #283 from scireum/aha/ImportFixx
Browse files Browse the repository at this point in the history
Permits to verify an entity before it is imported.
  • Loading branch information
andyHa authored Mar 1, 2019
2 parents fb52065 + b9bf8d7 commit da60d82
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 12 deletions.
26 changes: 26 additions & 0 deletions src/main/java/sirius/biz/jobs/batch/ImportJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
import sirius.biz.importer.Importer;
import sirius.biz.process.ProcessContext;
import sirius.biz.process.logs.ProcessLog;
import sirius.db.mixing.BaseEntity;
import sirius.db.mixing.types.BaseEntityRef;
import sirius.kernel.health.Exceptions;

import java.io.IOException;
import java.util.Objects;

/**
* Provides a base class for batch jobs which utilize an {@link Importer} to import data.
Expand All @@ -34,6 +38,28 @@ protected ImportJob(ProcessContext process) {
this.importer = new Importer(process.getTitle());
}

/**
* Enusures or establishes a parent child relation.
* <p>
* For new entities (owner), the given reference is initialized with the given entity. For existing entities
* it is verified, that the given reference points to the given entity.
*
* @param owner the entity which contains the reference
* @param ref the reference which is either filled or verified that it points to <tt>entity</tt>
* @param entity the entity the reference must point to
* @param <E> the generic type the the entity being referenced
* @throws sirius.kernel.health.HandledException if the entities do no match
*/
protected <I, E extends BaseEntity<I>> void setOrVerify(BaseEntity<?> owner, BaseEntityRef<I, E> ref, E entity) {
if (!Objects.equals(ref.getId(), entity.getId())) {
if (owner.isNew()) {
ref.setValue(entity);
} else {
throw Exceptions.createHandled().withNLSKey("ImportJob.invalidReference").handle();
}
}
}

@Override
public void close() throws IOException {
try {
Expand Down
30 changes: 24 additions & 6 deletions src/main/java/sirius/biz/jobs/batch/file/LineBasedImportJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
* Utilizing {@link sirius.biz.importer.ImportHandler import handlers} this can be used as is in most cases. However
* a subclass overwriting {@link #handleRow(int, Context)} might be required to perform some mappings.
*/
public class LineBasedImportJob extends FileImportJob implements RowProcessor {
public class LineBasedImportJob<E extends BaseEntity<?>> extends FileImportJob implements RowProcessor {

protected final ImportDictionary dictionary;
protected final EntityDescriptor descriptor;
protected LineBasedAliases aliases;
protected Class<? extends BaseEntity<?>> type;
protected Class<E> type;

@Part
private static Mixing mixing;
Expand All @@ -48,9 +48,7 @@ public class LineBasedImportJob extends FileImportJob implements RowProcessor {
* @param type the type of entities being imported
* @param process the process context itself
*/
public LineBasedImportJob(VirtualObjectParameter fileParameter,
Class<? extends BaseEntity<?>> type,
ProcessContext process) {
public LineBasedImportJob(VirtualObjectParameter fileParameter, Class<E> type, ProcessContext process) {
super(fileParameter, process);
this.dictionary = importer.getDictionary(type);
this.type = type;
Expand Down Expand Up @@ -90,6 +88,26 @@ public void handleRow(int index, Values row) {
* @param ctx the row represented as context
*/
protected void handleRow(int index, Context ctx) {
importer.createOrUpdateInBatch(importer.findAndLoad(type, ctx));
importer.createOrUpdateInBatch(fillAndVerify(findAndLoad(ctx)));
}

/**
* Completes the given entity and verifies the integrity of the data.
*
* @param entity the entity which has be loaded previously
* @return the filled and verified entity
*/
protected E fillAndVerify(E entity) {
return entity;
}

/**
* Tries to resolve the context into an entity.
*
* @param ctx the context containing all relevant data
* @return the entity which was either found in he database or create using the given data
*/
protected E findAndLoad(Context ctx) {
return importer.findAndLoad(type, ctx);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,35 @@
import sirius.biz.jobs.batch.file.LineBasedImportJob;
import sirius.biz.jobs.batch.file.LineBasedImportJobFactory;
import sirius.biz.process.ProcessContext;
import sirius.biz.tenants.TenantUserManager;
import sirius.biz.tenants.UserAccountController;
import sirius.kernel.di.std.Part;
import sirius.kernel.di.std.Register;
import sirius.web.security.Permission;

import javax.annotation.Nonnull;

/**
* Provides an import job for {@link SQLUserAccount user accounts} stored in a JDBC database.
*/
@Register(classes = JobFactory.class, framework = SQLTenants.FRAMEWORK_TENANTS_JDBC)
@Permission(UserAccountController.PERMISSION_MANAGE_USER_ACCOUNTS)
public class SQLUserAccountImportJobFactory extends LineBasedImportJobFactory {

@Part
private SQLTenants tenants;

@Override
protected LineBasedImportJob createJob(ProcessContext process) {
return new LineBasedImportJob(fileParameter, SQLUserAccount.class, process);
protected LineBasedImportJob<?> createJob(ProcessContext process) {
SQLTenant currentTenant = tenants.getRequiredTenant();

return new LineBasedImportJob<SQLUserAccount>(fileParameter, SQLUserAccount.class, process) {
@Override
protected SQLUserAccount fillAndVerify(SQLUserAccount entity) {
setOrVerify(entity, entity.getTenant(), currentTenant);
return super.fillAndVerify(entity);
}
};
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,34 @@
import sirius.biz.jobs.batch.file.LineBasedImportJob;
import sirius.biz.jobs.batch.file.LineBasedImportJobFactory;
import sirius.biz.process.ProcessContext;
import sirius.biz.tenants.mongo.MongoTenants;
import sirius.biz.tenants.mongo.MongoUserAccount;
import sirius.biz.tenants.UserAccountController;
import sirius.kernel.di.std.Part;
import sirius.kernel.di.std.Register;
import sirius.web.security.Permission;

import javax.annotation.Nonnull;

/**
* Provides an import job for {@link MongoUserAccount user accounts} stored in MongoDB.
*/
@Register(classes = JobFactory.class, framework = MongoTenants.FRAMEWORK_TENANTS_MONGO)
@Permission(UserAccountController.PERMISSION_MANAGE_USER_ACCOUNTS)
public class MongoUserAccountImportJobFactory extends LineBasedImportJobFactory {

@Part
private MongoTenants tenants;

@Override
protected LineBasedImportJob createJob(ProcessContext process) {
return new LineBasedImportJob(fileParameter, MongoUserAccount.class, process);
protected LineBasedImportJob<?> createJob(ProcessContext process) {
MongoTenant currentTenant = tenants.getRequiredTenant();

return new LineBasedImportJob<MongoUserAccount>(fileParameter, MongoUserAccount.class, process) {
@Override
protected MongoUserAccount fillAndVerify(MongoUserAccount entity) {
setOrVerify(entity, entity.getTenant(), currentTenant);
return super.fillAndVerify(entity);
}
};
}

@Nonnull
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/biz_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,4 @@ VirtualObject.unreference = Referenzierung löschen
VirtualObject.versions = Versionen
LineBasedAliases.column=Spalte ${column}
PermissionData.permissionString=Berechtigungen
ImportJob.invalidReference = Eine Beziehung zwischen den gegebenen Datenobjekten is inkonsistent.

0 comments on commit da60d82

Please sign in to comment.