Search in sources :

Example 1 with XFormInstanceImpl

use of org.activityinfo.server.endpoint.odk.xform.XFormInstanceImpl 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 2 with XFormInstanceImpl

use of org.activityinfo.server.endpoint.odk.xform.XFormInstanceImpl in project activityinfo by bedatadriven.

the class XFormSubmissionResource method persist.

private void persist(AuthenticatedUser user, XFormInstance instance, AttachmentValue fieldValue) {
    Attachment attachment = fieldValue.getValues().get(0);
    if (attachment.getFilename() != null) {
        try {
            BodyPart bodyPart = ((XFormInstanceImpl) instance).findBodyPartByFilename(attachment.getFilename());
            String mimeType = bodyPart.getContentType();
            attachment.setMimeType(mimeType);
            blobFieldStorageService.put(user, bodyPart.getDisposition(), mimeType, new BlobId(attachment.getBlobId()), instance.getFormClassId(), bodyPart.getInputStream());
        } catch (MessagingException messagingException) {
            LOGGER.log(Level.SEVERE, "Unable to parse input", messagingException);
            throw new WebApplicationException(Response.status(BAD_REQUEST).build());
        } catch (IOException ioException) {
            LOGGER.log(Level.SEVERE, "Could not write attachment to GCS", ioException);
            throw new WebApplicationException(Response.status(SERVICE_UNAVAILABLE).build());
        }
    }
}
Also used : BodyPart(javax.mail.BodyPart) MessagingException(javax.mail.MessagingException) XFormInstanceImpl(org.activityinfo.server.endpoint.odk.xform.XFormInstanceImpl) Attachment(org.activityinfo.model.type.attachment.Attachment) IOException(java.io.IOException) BlobId(org.activityinfo.store.spi.BlobId)

Aggregations

XFormInstanceImpl (org.activityinfo.server.endpoint.odk.xform.XFormInstanceImpl)2 Optional (com.google.common.base.Optional)1 IOException (java.io.IOException)1 BodyPart (javax.mail.BodyPart)1 MessagingException (javax.mail.MessagingException)1 AuthenticatedUser (org.activityinfo.legacy.shared.AuthenticatedUser)1 FormClass (org.activityinfo.model.form.FormClass)1 FormField (org.activityinfo.model.form.FormField)1 FormInstance (org.activityinfo.model.form.FormInstance)1 KeyGenerator (org.activityinfo.model.legacy.KeyGenerator)1 ResourceId (org.activityinfo.model.resource.ResourceId)1 Attachment (org.activityinfo.model.type.attachment.Attachment)1 AttachmentValue (org.activityinfo.model.type.attachment.AttachmentValue)1 XFormInstance (org.activityinfo.server.endpoint.odk.xform.XFormInstance)1 BlobId (org.activityinfo.store.spi.BlobId)1 Element (org.w3c.dom.Element)1