Search in sources :

Example 1 with CreateOrUpdateForm

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

Aggregations

FormClass (org.activityinfo.model.form.FormClass)1 ResourceId (org.activityinfo.model.resource.ResourceId)1 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)1 FormEntity (org.activityinfo.store.hrd.entity.FormEntity)1 FormSchemaEntity (org.activityinfo.store.hrd.entity.FormSchemaEntity)1 CreateOrUpdateForm (org.activityinfo.store.hrd.op.CreateOrUpdateForm)1