Search in sources :

Example 1 with FormSchemaEntity

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

the class CreateOrUpdateForm method create.

private void create() {
    FormEntity rootEntity = new FormEntity();
    rootEntity.setId(formClass.getId());
    rootEntity.setVersion(1);
    rootEntity.setSchemaVersion(1);
    FormSchemaEntity formClassEntity = new FormSchemaEntity(formClass);
    formClassEntity.setSchemaVersion(1);
    ofy().save().entities(rootEntity, formClassEntity);
}
Also used : FormEntity(org.activityinfo.store.hrd.entity.FormEntity) FormSchemaEntity(org.activityinfo.store.hrd.entity.FormSchemaEntity)

Example 2 with FormSchemaEntity

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

the class HrdStorageProvider method getForm.

@Override
public Optional<FormStorage> getForm(ResourceId formId) {
    FormSchemaEntity schemaEntity = Hrd.ofy().load().key(FormSchemaEntity.key(formId)).now();
    if (schemaEntity == null) {
        return Optional.absent();
    }
    HrdFormStorage accessor = new HrdFormStorage(schemaEntity.readFormClass());
    return Optional.<FormStorage>of(accessor);
}
Also used : FormStorage(org.activityinfo.store.spi.FormStorage) FormSchemaEntity(org.activityinfo.store.hrd.entity.FormSchemaEntity)

Example 3 with FormSchemaEntity

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

the class CreateOrUpdateForm method vrun.

@Override
public void vrun() {
    Key<FormSchemaEntity> schemaKey = FormSchemaEntity.key(formClass.getId());
    FormSchemaEntity schemaEntity = ofy().load().key(schemaKey).now();
    if (schemaEntity == null) {
        create();
    } else {
        update(schemaEntity);
    }
}
Also used : FormSchemaEntity(org.activityinfo.store.hrd.entity.FormSchemaEntity)

Example 4 with FormSchemaEntity

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

the class FixSubForm method maybeFixForm.

private boolean maybeFixForm(PrintWriter logger, FormClass parentForm, FormField formField) {
    SubFormReferenceType type = (SubFormReferenceType) formField.getType();
    ResourceId subFormId = type.getClassId();
    logger.println("Found subform " + formField.getLabel() + "(" + subFormId + ")");
    ResourceId parentFormId = parentForm.getId();
    FormSchemaEntity schemaEntity = Hrd.ofy().load().key(FormSchemaEntity.key(subFormId)).now();
    if (schemaEntity == null) {
        logger.println("Subform " + subFormId + " does not exist!!");
        return false;
    }
    FormClass schema = schemaEntity.readFormClass();
    if (schema.getParentFormId().get().equals(parentFormId)) {
        logger.println("Parent is OK");
        return false;
    }
    FormEntity root = Hrd.ofy().load().key(FormEntity.key(subFormId)).now();
    logger.println("At version " + root.getVersion());
    // Generate new subform id..
    ResourceId newSubFormId = ResourceId.generateId();
    schema.setId(newSubFormId);
    schema.setParentFormId(parentFormId);
    logger.println("Creating copy of subform => " + newSubFormId);
    if (!DRY_RUN) {
        new CreateOrUpdateForm(schema).run();
    }
    // Now find all records and update their schema
    copyRecords(logger, parentFormId, subFormId, newSubFormId);
    copySnapshots(logger, parentFormId, subFormId, newSubFormId);
    // Update the root entity to match version numbers
    FormEntity newRoot = new FormEntity();
    newRoot.setId(newSubFormId);
    newRoot.setVersion(root.getVersion());
    newRoot.setSchemaVersion(root.getSchemaVersion());
    if (!DRY_RUN) {
        Hrd.ofy().save().entity(newRoot);
    }
    // Finally update the MySQL parent
    formField.setType(new SubFormReferenceType(newSubFormId));
    return true;
}
Also used : SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) FormEntity(org.activityinfo.store.hrd.entity.FormEntity) CreateOrUpdateForm(org.activityinfo.store.hrd.op.CreateOrUpdateForm) FormSchemaEntity(org.activityinfo.store.hrd.entity.FormSchemaEntity)

Example 5 with FormSchemaEntity

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

the class ProjectMigrator method map.

@Override
public void map(final Integer databaseId) {
    try (MySqlQueryExecutor executor = new MySqlQueryExecutor()) {
        ProjectTable table = new ProjectTable(new DatabaseCacheImpl(executor));
        final TableMapping mapping = table.getMapping(executor, CuidAdapter.projectFormClass(databaseId));
        final ResourceId formId = mapping.getFormClass().getId();
        Hrd.run(new Work<Void>() {

            @Override
            public Void run() {
                RecordCursor recordBuilder = new RecordCursor(mapping, executor);
                Iterator<FormInstance> it = recordBuilder.execute();
                // Only create if there is one or more projects....
                if (!it.hasNext()) {
                    return null;
                }
                final List<Object> toSave = new ArrayList<>();
                FormEntity formEntity = new FormEntity();
                formEntity.setId(formId);
                formEntity.setSchemaVersion(1L);
                formEntity.setVersion(mapping.getVersion());
                toSave.add(formEntity);
                FormSchemaEntity schemaEntity = new FormSchemaEntity(mapping.getFormClass());
                schemaEntity.setSchemaVersion(1L);
                schemaEntity.setSchema(mapping.getFormClass());
                toSave.add(schemaEntity);
                Date updateTime = new Date();
                long userId = -1L;
                while (it.hasNext()) {
                    FormInstance record = it.next();
                    FormRecordEntity recordEntity = new FormRecordEntity(formId, record.getId());
                    recordEntity.setFieldValues(mapping.getFormClass(), record.getFieldValueMap());
                    recordEntity.setSchemaVersion(1L);
                    recordEntity.setVersion(1L);
                    FormRecordSnapshotEntity snapshot = new FormRecordSnapshotEntity(userId, RecordChangeType.CREATED, recordEntity);
                    snapshot.setTime(updateTime);
                    snapshot.setMigrated(true);
                    toSave.add(record);
                    toSave.add(snapshot);
                }
                Hrd.ofy().save().entities(toSave).now();
                return null;
            }
        });
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : FormRecordSnapshotEntity(org.activityinfo.store.hrd.entity.FormRecordSnapshotEntity) RecordCursor(org.activityinfo.store.mysql.cursor.RecordCursor) SQLException(java.sql.SQLException) FormRecordEntity(org.activityinfo.store.hrd.entity.FormRecordEntity) TableMapping(org.activityinfo.store.mysql.mapping.TableMapping) Date(java.util.Date) DatabaseCacheImpl(org.activityinfo.store.mysql.metadata.DatabaseCacheImpl) ResourceId(org.activityinfo.model.resource.ResourceId) FormEntity(org.activityinfo.store.hrd.entity.FormEntity) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) FormSchemaEntity(org.activityinfo.store.hrd.entity.FormSchemaEntity) FormInstance(org.activityinfo.model.form.FormInstance) ProjectTable(org.activityinfo.store.mysql.collections.ProjectTable)

Aggregations

FormSchemaEntity (org.activityinfo.store.hrd.entity.FormSchemaEntity)6 ResourceId (org.activityinfo.model.resource.ResourceId)3 FormEntity (org.activityinfo.store.hrd.entity.FormEntity)3 FormClass (org.activityinfo.model.form.FormClass)2 Key (com.googlecode.objectify.Key)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 List (java.util.List)1 FormInstance (org.activityinfo.model.form.FormInstance)1 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)1 FormRecordEntity (org.activityinfo.store.hrd.entity.FormRecordEntity)1 FormRecordSnapshotEntity (org.activityinfo.store.hrd.entity.FormRecordSnapshotEntity)1 CreateOrUpdateForm (org.activityinfo.store.hrd.op.CreateOrUpdateForm)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 FormStorage (org.activityinfo.store.spi.FormStorage)1