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;
}
Aggregations