Search in sources :

Example 66 with FormClass

use of org.activityinfo.model.form.FormClass in project activityinfo by bedatadriven.

the class DatabaseResource method createFormFromXForm.

@POST
@Path("/forms")
@Consumes("application/xml")
public Response createFormFromXForm(@Context UriInfo uri, XForm xForm) {
    UserDatabaseDTOWithForms schema = getSchema();
    LocationTypeDTO locationType = schema.getCountry().getNullLocationType();
    ActivityFormDTO activityDTO = new ActivityFormDTO();
    activityDTO.setName(xForm.getHead().getTitle());
    activityDTO.set("databaseId", databaseId);
    activityDTO.set("locationTypeId", locationType.getId());
    CreateResult createResult = dispatcher.execute(new CreateEntity(activityDTO));
    int activityId = createResult.getNewId();
    XFormReader builder = new XFormReader(xForm);
    FormClass formClass = builder.build();
    formClass.setId(CuidAdapter.activityFormClass(activityId));
    formClass.setDatabaseId(CuidAdapter.databaseId(databaseId));
    MySqlStorageProvider formCatalog = (MySqlStorageProvider) catalog.get();
    formCatalog.createOrUpdateFormSchema(formClass);
    return Response.created(uri.getAbsolutePathBuilder().path(RootResource.class).path("forms").path(formClass.getId().asString()).build()).build();
}
Also used : CreateEntity(org.activityinfo.legacy.shared.command.CreateEntity) CreateResult(org.activityinfo.legacy.shared.command.result.CreateResult) FormClass(org.activityinfo.model.form.FormClass) XFormReader(org.activityinfo.io.xform.XFormReader) MySqlStorageProvider(org.activityinfo.store.mysql.MySqlStorageProvider)

Example 67 with FormClass

use of org.activityinfo.model.form.FormClass in project activityinfo by bedatadriven.

the class XFormSubmissionResource method submit.

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_XML)
public Response submit(byte[] bytes) {
    XFormInstance instance = new XFormInstanceImpl(bytes);
    AuthenticatedUser user = authenticationTokenService.authenticate(instance.getAuthenticationToken());
    FormClass formClass = locator.getFormClass(instance.getFormClassId());
    ResourceId formId = newLegacyFormInstanceId(formClass.getId());
    FormInstance formInstance = new FormInstance(formId, formClass.getId());
    String instanceId = instance.getId();
    LOGGER.log(Level.INFO, "Saving XForm " + instance.getId() + " as " + formId);
    for (FormField formField : formClass.getFields()) {
        if (formField.getType().isUpdatable()) {
            Optional<Element> element = instance.getFieldContent(formField.getId());
            if (element.isPresent()) {
                formInstance.set(formField.getId(), tryParse(formInstance, formField, element.get()));
            } else if (isLocation(formClass, formField)) {
                FieldType fieldType = formField.getType();
                Optional<Element> gpsField = instance.getFieldContent(field(formClass.getId(), GPS_FIELD));
                Optional<Element> nameField = instance.getFieldContent(field(formClass.getId(), LOCATION_NAME_FIELD));
                if (fieldType instanceof ReferenceType && gpsField.isPresent() && nameField.isPresent()) {
                    ResourceId locationFieldId = field(formClass.getId(), LOCATION_FIELD);
                    int newLocationId = new KeyGenerator().generateInt();
                    ReferenceType locationRefType = (ReferenceType) fieldType;
                    if (locationRefType.getRange().isEmpty()) {
                        throw new IllegalStateException("Location field has empty range");
                    }
                    ResourceId locationFormId = locationRefType.getRange().iterator().next();
                    int locationTypeId = getLegacyIdFromCuid(locationFormId);
                    FieldValue fieldValue = new ReferenceValue(new RecordRef(locationFormId, locationInstanceId(newLocationId)));
                    String name = OdkHelper.extractText(nameField.get());
                    if (Strings.isNullOrEmpty(name)) {
                        throw new WebApplicationException(Response.status(BAD_REQUEST).entity("Name value for location field is blank. ").build());
                    }
                    Optional<GeoPoint> geoPoint = parseLocation(gpsField);
                    formInstance.set(locationFieldId, fieldValue);
                    createLocation(newLocationId, locationTypeId, name, geoPoint);
                }
            }
        }
    }
    ensurePartnerIsSet(formClass, formInstance);
    if (!instanceIdService.exists(instanceId)) {
        for (FieldValue fieldValue : formInstance.getFieldValueMap().values()) {
            if (fieldValue instanceof AttachmentValue) {
                persist(user, instance, (AttachmentValue) fieldValue);
            }
        }
        locator.persist(formInstance);
        instanceIdService.submit(instanceId);
    }
    // Backup the original XForm in case something went wrong with processing
    submissionArchiver.backup(formClass.getId(), formId, ByteSource.wrap(bytes));
    return Response.status(CREATED).build();
}
Also used : AttachmentValue(org.activityinfo.model.type.attachment.AttachmentValue) Optional(com.google.common.base.Optional) Element(org.w3c.dom.Element) AuthenticatedUser(org.activityinfo.legacy.shared.AuthenticatedUser) ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) XFormInstanceImpl(org.activityinfo.server.endpoint.odk.xform.XFormInstanceImpl) XFormInstance(org.activityinfo.server.endpoint.odk.xform.XFormInstance) FormInstance(org.activityinfo.model.form.FormInstance) FormField(org.activityinfo.model.form.FormField) KeyGenerator(org.activityinfo.model.legacy.KeyGenerator) XFormInstance(org.activityinfo.server.endpoint.odk.xform.XFormInstance)

Example 68 with FormClass

use of org.activityinfo.model.form.FormClass in project activityinfo by bedatadriven.

the class SchemaCsvWriterV3 method writeForms.

@VisibleForTesting
void writeForms(UserDatabaseDTO db, List<ResourceId> formIds) throws IOException {
    writeHeaders();
    Map<ResourceId, FormClass> formClasses = catalog.getFormClasses(formIds);
    for (ResourceId formId : formIds) {
        FormClass formClass = formClasses.get(formId);
        if (formClass == null) {
            throw new IllegalStateException("No FormClass for " + formId);
        }
        writeForm(db, formClass);
    }
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 69 with FormClass

use of org.activityinfo.model.form.FormClass in project activityinfo by bedatadriven.

the class SchemaCsvWriterV3 method writeSubForm.

private void writeSubForm(FieldContext context, FormField field) throws IOException {
    SubFormReferenceType fieldType = (SubFormReferenceType) field.getType();
    FormClass subFormClass = catalog.getFormClass(fieldType.getClassId());
    FieldContext subFormContext = context.subForm(field, subFormClass);
    for (FormField subField : subFormClass.getFields()) {
        if (subField.getType() instanceof EnumType) {
            writeEnumItems(subFormContext, subField, ((EnumType) subField.getType()).getValues());
        } else {
            writeField(subFormContext, subField);
        }
    }
}
Also used : SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) FormClass(org.activityinfo.model.form.FormClass) EnumType(org.activityinfo.model.type.enumerated.EnumType) FormField(org.activityinfo.model.form.FormField)

Example 70 with FormClass

use of org.activityinfo.model.form.FormClass 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)

Aggregations

FormClass (org.activityinfo.model.form.FormClass)109 FormField (org.activityinfo.model.form.FormField)49 ResourceId (org.activityinfo.model.resource.ResourceId)41 Test (org.junit.Test)38 QuantityType (org.activityinfo.model.type.number.QuantityType)20 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)19 FormInstance (org.activityinfo.model.form.FormInstance)14 EnumType (org.activityinfo.model.type.enumerated.EnumType)12 JsonValue (org.activityinfo.json.JsonValue)11 FormTree (org.activityinfo.model.formTree.FormTree)10 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)10 ColumnSet (org.activityinfo.model.query.ColumnSet)8 QueryModel (org.activityinfo.model.query.QueryModel)8 CalculatedFieldType (org.activityinfo.model.type.expr.CalculatedFieldType)8 FieldValue (org.activityinfo.model.type.FieldValue)7 Quantity (org.activityinfo.model.type.number.Quantity)7 FormTreeBuilder (org.activityinfo.model.formTree.FormTreeBuilder)6 KeyGenerator (org.activityinfo.model.legacy.KeyGenerator)6 ColumnSetBuilder (org.activityinfo.store.query.server.ColumnSetBuilder)6 ColumnView (org.activityinfo.model.query.ColumnView)5