Search in sources :

Example 1 with FormRecordSnapshotEntity

use of org.activityinfo.store.hrd.entity.FormRecordSnapshotEntity in project activityinfo by bedatadriven.

the class AuditLogWriter method writeForm.

public void writeForm(FormStorageProvider catalog, ResourceId formId) throws IOException {
    FormStorage formStorage = catalog.getForm(formId).get();
    FormClass formClass = formStorage.getFormClass();
    Key<FormEntity> parentKey = FormEntity.key(formId);
    Query<FormRecordSnapshotEntity> query = Hrd.ofy().load().type(FormRecordSnapshotEntity.class).ancestor(parentKey);
    for (FormRecordSnapshotEntity snapshot : query) {
        User user;
        try {
            user = userCache.get((int) snapshot.getUserId());
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
        csv.writeLine(formatTime(snapshot.getTime()), snapshot.getType().name(), user.getEmail(), user.getName(), databaseId(), db.getName(), formId.asString(), formClass.getLabel(), // Field ID
        "", // Field Name
        "", snapshot.getRecordId().asString(), partner());
    }
}
Also used : FormRecordSnapshotEntity(org.activityinfo.store.hrd.entity.FormRecordSnapshotEntity) User(org.activityinfo.server.database.hibernate.entity.User) FormStorage(org.activityinfo.store.spi.FormStorage) FormClass(org.activityinfo.model.form.FormClass) FormEntity(org.activityinfo.store.hrd.entity.FormEntity) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with FormRecordSnapshotEntity

use of org.activityinfo.store.hrd.entity.FormRecordSnapshotEntity in project activityinfo by bedatadriven.

the class FixSubForm method copySnapshots.

private void copySnapshots(PrintWriter logger, ResourceId parentFormId, ResourceId subFormId, ResourceId newSubFormId) {
    QueryResultIterable<FormRecordSnapshotEntity> subRecords = ofy().load().type(FormRecordSnapshotEntity.class).ancestor(FormEntity.key(subFormId)).iterable();
    for (FormRecordSnapshotEntity subRecord : subRecords) {
        ResourceId parentRecordId = ResourceId.valueOf(subRecord.getParentRecordId());
        String name = "sub record snapshot " + subRecord.getRecordId() + "@" + subRecord.getVersion();
        if (isSubRecordOf(parentFormId, parentRecordId)) {
            logger.println(name + " belongs to " + parentRecordId + " in " + parentFormId);
            copySnapshot(subRecord, newSubFormId);
        } else {
            logger.println(name + " does not belong");
        }
    }
}
Also used : FormRecordSnapshotEntity(org.activityinfo.store.hrd.entity.FormRecordSnapshotEntity) ResourceId(org.activityinfo.model.resource.ResourceId)

Example 3 with FormRecordSnapshotEntity

use of org.activityinfo.store.hrd.entity.FormRecordSnapshotEntity in project activityinfo by bedatadriven.

the class FixSubForm method copySnapshot.

private void copySnapshot(FormRecordSnapshotEntity old, ResourceId newSubFormId) {
    FormRecordSnapshotEntity newEntity = new FormRecordSnapshotEntity(old.getUserId(), old.getType(), reparent(old.getRecord(), newSubFormId));
    newEntity.setTime(old.getTime());
    newEntity.setParentRecordId(old.getParentRecordId());
    if (!DRY_RUN) {
        Hrd.ofy().save().entities(newEntity);
    }
}
Also used : FormRecordSnapshotEntity(org.activityinfo.store.hrd.entity.FormRecordSnapshotEntity)

Example 4 with FormRecordSnapshotEntity

use of org.activityinfo.store.hrd.entity.FormRecordSnapshotEntity in project activityinfo by bedatadriven.

the class SyncSetBuilder method buildUpdateArrays.

private List<FormRecord> buildUpdateArrays() {
    FormRecord[] records = new FormRecord[snapshots.size()];
    int i = 0;
    for (FormRecordSnapshotEntity snapshotEntity : snapshots.values()) {
        records[i++] = snapshotEntity.getRecord().toFormRecord(formClass);
    }
    return Arrays.asList(records);
}
Also used : FormRecordSnapshotEntity(org.activityinfo.store.hrd.entity.FormRecordSnapshotEntity) FormRecord(org.activityinfo.model.form.FormRecord)

Example 5 with FormRecordSnapshotEntity

use of org.activityinfo.store.hrd.entity.FormRecordSnapshotEntity in project activityinfo by bedatadriven.

the class CreateOrUpdateRecord method vrun.

@Override
public void vrun() {
    FormEntity rootEntity = ofy().load().key(FormEntity.key(formId)).safe();
    FormClass formClass = ofy().load().key(FormSchemaEntity.key(formId)).safe().readFormClass();
    long currentVersion = rootEntity.getVersion();
    long newVersion = currentVersion + 1;
    rootEntity.setVersion(newVersion);
    FormRecordEntity existingEntity = ofy().load().key(FormRecordEntity.key(formClass, update.getRecordId())).now();
    FormRecordEntity updated;
    RecordChangeType changeType;
    if (existingEntity != null) {
        updated = existingEntity;
        changeType = update.isDeleted() ? RecordChangeType.DELETED : RecordChangeType.UPDATED;
    } else {
        updated = new FormRecordEntity(formId, update.getRecordId());
        changeType = RecordChangeType.CREATED;
        if (update.isDeleted()) {
            throw new InvalidUpdateException("Creation of entity with deleted flag is not allowed.");
        }
        if (formClass.getParentFormId().isPresent()) {
            ResourceId parentId = update.getParentId();
            if (parentId == null) {
                throw new InvalidUpdateException("@parent is required for subform submissions");
            }
            updated.setParentRecordId(parentId);
        }
    }
    updated.setVersion(newVersion);
    updated.setSchemaVersion(rootEntity.getSchemaVersion());
    updated.setFieldValues(formClass, update.getChangedFieldValues());
    // Store a copy as a snapshot
    FormRecordSnapshotEntity snapshotEntity = new FormRecordSnapshotEntity(update.getUserId(), changeType, updated);
    if (update.isDeleted()) {
        ofy().save().entities(rootEntity, snapshotEntity);
        ofy().delete().entities(updated);
    } else {
        ofy().save().entities(rootEntity, updated, snapshotEntity);
    }
}
Also used : FormRecordSnapshotEntity(org.activityinfo.store.hrd.entity.FormRecordSnapshotEntity) RecordChangeType(org.activityinfo.store.spi.RecordChangeType) ResourceId(org.activityinfo.model.resource.ResourceId) FormEntity(org.activityinfo.store.hrd.entity.FormEntity) FormClass(org.activityinfo.model.form.FormClass) FormRecordEntity(org.activityinfo.store.hrd.entity.FormRecordEntity) InvalidUpdateException(org.activityinfo.store.query.server.InvalidUpdateException)

Aggregations

FormRecordSnapshotEntity (org.activityinfo.store.hrd.entity.FormRecordSnapshotEntity)8 FormEntity (org.activityinfo.store.hrd.entity.FormEntity)4 ResourceId (org.activityinfo.model.resource.ResourceId)3 FormRecordEntity (org.activityinfo.store.hrd.entity.FormRecordEntity)3 ArrayList (java.util.ArrayList)2 FormClass (org.activityinfo.model.form.FormClass)2 SQLException (java.sql.SQLException)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 FormInstance (org.activityinfo.model.form.FormInstance)1 FormRecord (org.activityinfo.model.form.FormRecord)1 User (org.activityinfo.server.database.hibernate.entity.User)1 FormSchemaEntity (org.activityinfo.store.hrd.entity.FormSchemaEntity)1 ProjectTable (org.activityinfo.store.mysql.collections.ProjectTable)1 RecordCursor (org.activityinfo.store.mysql.cursor.RecordCursor)1 TableMapping (org.activityinfo.store.mysql.mapping.TableMapping)1 DatabaseCacheImpl (org.activityinfo.store.mysql.metadata.DatabaseCacheImpl)1 InvalidUpdateException (org.activityinfo.store.query.server.InvalidUpdateException)1