Search in sources :

Example 1 with FormRecordEntity

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

the class QuerySubRecords method run.

@Override
public List<FormRecord> run() {
    QueryResultIterable<FormRecordEntity> query = ofy().load().type(FormRecordEntity.class).ancestor(FormEntity.key(formClass)).filter("parentRecordId", this.parentRecordId.asString()).iterable();
    List<FormRecord> records = Lists.newArrayList();
    for (FormRecordEntity entity : query) {
        records.add(entity.toFormRecord(formClass));
    }
    return records;
}
Also used : FormRecordEntity(org.activityinfo.store.hrd.entity.FormRecordEntity) FormRecord(org.activityinfo.model.form.FormRecord)

Example 2 with FormRecordEntity

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

the class FixSubForm method copyRecords.

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

Example 3 with FormRecordEntity

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

the class FixSubForm method isSubRecordOf.

private boolean isSubRecordOf(ResourceId parentFormId, ResourceId parentRecordId) {
    Key<FormRecordEntity> key = FormRecordEntity.key(parentFormId, parentRecordId);
    FormRecordEntity entity = Hrd.ofy().load().key(key).now();
    return entity != null;
}
Also used : FormRecordEntity(org.activityinfo.store.hrd.entity.FormRecordEntity)

Example 4 with FormRecordEntity

use of org.activityinfo.store.hrd.entity.FormRecordEntity 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)

Example 5 with FormRecordEntity

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

the class QueryVersions method run.

@Override
public List<RecordVersion> run() {
    Query<FormRecordSnapshotEntity> query;
    if (recordId != null) {
        Key<FormRecordEntity> recordKey = FormRecordEntity.key(formClass, recordId);
        query = ofy().load().type(FormRecordSnapshotEntity.class).ancestor(recordKey);
    } else {
        Key<FormEntity> rootKey = FormEntity.key(formClass);
        query = ofy().load().type(FormRecordSnapshotEntity.class).ancestor(rootKey).filter("parentRecordId", parentRecordId.asString());
    }
    List<RecordVersion> versions = new ArrayList<>();
    for (FormRecordSnapshotEntity snapshot : query.iterable()) {
        RecordVersion version = new RecordVersion();
        version.setRecordId(snapshot.getRecordId());
        version.setVersion(snapshot.getVersion());
        version.setUserId(snapshot.getUserId());
        version.setTime(snapshot.getTime().getTime());
        version.setType(snapshot.getType());
        if (formClass.isSubForm()) {
            version.setSubformKind(formClass.getSubFormKind());
            version.setSubformKey(subformKey(snapshot));
        }
        version.getValues().putAll(snapshot.getRecord().toFieldValueMap(formClass));
        versions.add(version);
    }
    return versions;
}
Also used : FormRecordSnapshotEntity(org.activityinfo.store.hrd.entity.FormRecordSnapshotEntity) RecordVersion(org.activityinfo.store.spi.RecordVersion) FormEntity(org.activityinfo.store.hrd.entity.FormEntity) FormRecordEntity(org.activityinfo.store.hrd.entity.FormRecordEntity) ArrayList(java.util.ArrayList)

Aggregations

FormRecordEntity (org.activityinfo.store.hrd.entity.FormRecordEntity)8 ResourceId (org.activityinfo.model.resource.ResourceId)4 FormEntity (org.activityinfo.store.hrd.entity.FormEntity)3 FormRecordSnapshotEntity (org.activityinfo.store.hrd.entity.FormRecordSnapshotEntity)3 ArrayList (java.util.ArrayList)2 SQLException (java.sql.SQLException)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 List (java.util.List)1 FormClass (org.activityinfo.model.form.FormClass)1 FormInstance (org.activityinfo.model.form.FormInstance)1 FormRecord (org.activityinfo.model.form.FormRecord)1 FieldValue (org.activityinfo.model.type.FieldValue)1 RecordRef (org.activityinfo.model.type.RecordRef)1 ReferenceValue (org.activityinfo.model.type.ReferenceValue)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