use of org.activityinfo.model.type.subform.SubFormReferenceType 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;
}
use of org.activityinfo.model.type.subform.SubFormReferenceType in project activityinfo by bedatadriven.
the class FormDesignerPanel method fillPanel.
private void fillPanel(final FormClass formClass, final FormDesigner formDesigner) {
formClass.traverse(formClass, new TraverseFunction() {
@Override
public void apply(FormElement element, FormElementContainer container) {
if (isSubFormKey(formClass, element)) {
// NOOP
} else if (element instanceof FormField) {
FormField formField = (FormField) element;
WidgetContainer widgetContainer = containerMap.get(formField.getId());
if (widgetContainer != null) {
// widget container may be null if domain is not supported, should be removed later
Widget widget = widgetContainer.asWidget();
formDesigner.getDragController().makeDraggable(widget, widgetContainer.getDragHandle());
FlowPanel parentDropPanel = (FlowPanel) formDesigner.getDropControllerRegistry().getDropController(widgetContainer.getParentId()).getDropTarget();
parentDropPanel.add(widget);
}
if (formField.getType() instanceof SubFormReferenceType) {
ResourceId subFormId = ((SubFormReferenceType) formField.getType()).getClassId();
FormClass subForm = (FormClass) formDesigner.getModel().getElementContainer(subFormId);
if (subForm == null) {
throw new IllegalStateException("Subform " + subFormId + " does not exist.");
}
fillPanel(subForm, formDesigner);
}
} else if (element instanceof FormSection) {
FormSection section = (FormSection) element;
WidgetContainer widgetContainer = containerMap.get(section.getId());
Widget widget = widgetContainer.asWidget();
formDesigner.getDragController().makeDraggable(widget, widgetContainer.getDragHandle());
dropPanel.add(widget);
} else if (element instanceof FormLabel) {
FormLabel label = (FormLabel) element;
WidgetContainer widgetContainer = containerMap.get(label.getId());
Widget widget = widgetContainer.asWidget();
formDesigner.getDragController().makeDraggable(widget, widgetContainer.getDragHandle());
dropPanel.add(widget);
} else {
throw new UnsupportedOperationException("Unknown form element.");
}
}
});
}
use of org.activityinfo.model.type.subform.SubFormReferenceType in project activityinfo by bedatadriven.
the class FormDesignerPanel method buildWidgetContainers.
private void buildWidgetContainers(final FormDesigner formDesigner, final FormElementContainer container, final FormClass owner, final int depth, final List<Promise<Void>> promises) {
for (FormElement element : container.getElements()) {
if (element instanceof FormSection) {
FormSection formSection = (FormSection) element;
containerMap.put(formSection.getId(), FieldsHolderWidgetContainer.section(formDesigner, formSection, container.getId()));
buildWidgetContainers(formDesigner, formSection, owner, depth + 1, promises);
} else if (element instanceof FormLabel) {
FormLabel label = (FormLabel) element;
containerMap.put(label.getId(), new LabelWidgetContainer(formDesigner, label, container.getId()));
} else if (element instanceof FormField && !isSubFormKey(owner, (FormField) element)) {
final FormField formField = (FormField) element;
if (formField.getType() instanceof SubFormReferenceType) {
// subform
SubFormReferenceType subform = (SubFormReferenceType) formField.getType();
Promise<Void> promise = formDesigner.getResourceLocator().getFormClass(subform.getClassId()).then(new Function<FormClass, Void>() {
@Override
public Void apply(FormClass subform) {
formDesigner.getModel().registerSubform(formField.getId(), subform);
containerMap.put(formField.getId(), FieldsHolderWidgetContainer.subform(formDesigner, formField, subform, container.getId()));
buildWidgetContainers(formDesigner, subform, subform, depth + 1, promises);
return null;
}
});
promises.add(promise);
} else {
if (formField.getId().asString().startsWith("_")) {
// skip if form field is built-in
continue;
}
Promise<Void> promise = formDesigner.getFormFieldWidgetFactory().createWidget(owner, formField, new FieldUpdater() {
@Override
public void onInvalid(String errorMessage) {
}
@Override
public void update(Object value) {
formDesigner.getSavedGuard().setSaved(false);
}
}).then(new Function<FormFieldWidget, Void>() {
@Nullable
@Override
public Void apply(@Nullable FormFieldWidget input) {
containerMap.put(formField.getId(), new FieldWidgetContainer(formDesigner, input, formField, container.getId()));
if (container instanceof FormClass && ((FormClass) container).isSubForm()) {
// Possibly returned after initial fill - refill for current subform
fillPanel((FormClass) container, formDesigner);
}
return null;
}
});
promises.add(promise);
}
}
}
}
use of org.activityinfo.model.type.subform.SubFormReferenceType in project activityinfo by bedatadriven.
the class SubFormTemplate method create.
@Override
public FormField create() {
FormField field = new FormField(ResourceId.generateFieldId(SubFormReferenceType.TYPE_CLASS));
field.setLabel(label);
field.setType(new SubFormReferenceType());
return field;
}
use of org.activityinfo.model.type.subform.SubFormReferenceType in project activityinfo by bedatadriven.
the class NodeMatch method joinsTo.
private static List<JoinNode> joinsTo(List<List<FormTree.Node>> partitions, Optional<StatFunction> aggregation) {
/*
* Given a parent: "Site.Location.Territoire.District"
* This is represented as a tree of nodes:
* District -> Territoire -> Location -> Site
*
* We want to turn into a list of joins:
* (site field -> form site),
* (location field -> form school),
* (field territoire -> form Territoire)
* (field district -> form District)
*/
LinkedList<JoinNode> joins = new LinkedList<>();
for (int i = 0; i < partitions.size() - 1; i++) {
// Reference field that functions as a foreign key
List<FormTree.Node> left = partitions.get(i);
FormField leftField = left.get(0).getField();
ResourceId leftFormId = left.get(0).getDefiningFormClass().getId();
FormulaNode leftFieldExpr = toExpr(left);
// "RIGHT" side
// Joining fom left to right using resource ids (primary key)
List<FormTree.Node> right = partitions.get(i + 1);
ResourceId rightFormId = right.get(0).getDefiningFormClass().getId();
if (leftField.getType() instanceof ReferenceType) {
// Join based on the (left) foreign key ==> (right) primary key
joins.add(new JoinNode(JoinType.REFERENCE, leftFormId, leftFieldExpr, rightFormId, Optional.<StatFunction>absent()));
} else if (leftField.getType() instanceof SubFormReferenceType) {
joins.add(new JoinNode(JoinType.SUBFORM, leftFormId, new SymbolNode(ColumnModel.ID_SYMBOL), rightFormId, aggregation));
} else {
throw new IllegalStateException("Invalid field for joining: " + leftField.getType());
}
}
return joins;
}
Aggregations