Search in sources :

Example 96 with ResourceId

use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.

the class LookupKeySet method collectAncestors.

private void collectAncestors(Collection<ResourceId> formIds, Set<ResourceId> set) {
    for (ResourceId formId : formIds) {
        Collection<ResourceId> parents = parentMap.get(formId);
        set.addAll(parents);
        collectAncestors(parents, set);
    }
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId)

Example 97 with ResourceId

use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.

the class FormInstanceComparatorTest method testList.

private List<FormInstance> testList() {
    final ResourceId key1 = ResourceId.valueOf("key1");
    final ResourceId key2 = ResourceId.valueOf("key2");
    FormInstance instance1 = newInstance(1);
    instance1.set(key1, 3);
    instance1.set(key2, "d");
    FormInstance instance2 = newInstance(2);
    instance2.set(key1, 2);
    instance2.set(key2, "b");
    FormInstance instance3 = newInstance(3);
    instance3.set(key1, 1);
    instance3.set(key2, "c");
    return Lists.newArrayList(instance1, instance2, instance3);
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId)

Example 98 with ResourceId

use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.

the class RecordRef method fromQualifiedString.

public static RecordRef fromQualifiedString(@Nonnull String string) {
    int separatorPos = string.indexOf(SEPARATOR);
    if (separatorPos == -1) {
        throw new IllegalStateException("Malformed record ref: " + string);
    }
    ResourceId formId = ResourceId.valueOf(string.substring(0, separatorPos));
    ResourceId recordId = ResourceId.valueOf(string.substring(separatorPos + 1));
    return new RecordRef(formId, recordId);
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId)

Example 99 with ResourceId

use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.

the class FormResource method getMetadataResponse.

@GET
@NoCache
@Produces(JSON_CONTENT_TYPE)
public FormMetadata getMetadataResponse(@InjectParam DatabaseProviderImpl databaseProvider, @InjectParam AuthenticatedUser user, @QueryParam("localVersion") Long localVersion) {
    Optional<FormStorage> storage = backend.getStorage().getForm(formId);
    if (!storage.isPresent()) {
        return FormMetadata.notFound(formId);
    }
    ResourceId databaseId = storage.get().getFormClass().getDatabaseId();
    UserDatabaseMeta databaseMetadata;
    try {
        databaseMetadata = databaseProvider.getDatabaseMetadata(databaseId, user.getUserId());
    } catch (Exception e) {
        // We are initially using this just for locks,
        // not actually permissions, so just log the warning for now.
        LOGGER.log(Level.SEVERE, "Failed to retrieve metadata for database " + databaseId + " for user " + user.getUserId(), e);
        databaseMetadata = new UserDatabaseMeta.Builder().setDatabaseId(databaseId).setLabel("").setOwner(false).build();
    }
    FormPermissions permissions = backend.getFormSupervisor().getFormPermissions(formId);
    if (!permissions.isVisible()) {
        throw new WebApplicationException(Response.Status.FORBIDDEN);
    } else {
        // Workaround for sub form, which we don't yet have indexed to the
        // database in which they live.
        FormClass schema = storage.get().getFormClass();
        RecordLockSet locks;
        if (schema.isSubForm()) {
            locks = databaseMetadata.getEffectiveLocks(schema.getParentFormId().get());
        } else {
            locks = databaseMetadata.getEffectiveLocks(formId);
        }
        return new FormMetadata.Builder().setId(formId).setPermissions(permissions).setSchema(schema).setLocks(locks).setVersion(storage.get().cacheVersion()).build();
    }
}
Also used : VersionedFormStorage(org.activityinfo.store.spi.VersionedFormStorage) FormStorage(org.activityinfo.store.spi.FormStorage) ResourceId(org.activityinfo.model.resource.ResourceId) FormTreeBuilder(org.activityinfo.model.formTree.FormTreeBuilder) JsonFormTreeBuilder(org.activityinfo.model.formTree.JsonFormTreeBuilder) XlsFormBuilder(org.activityinfo.io.xlsform.XlsFormBuilder) RecordLockSet(org.activityinfo.model.database.RecordLockSet) UserDatabaseMeta(org.activityinfo.model.database.UserDatabaseMeta) NotFoundException(com.sun.jersey.api.NotFoundException) SQLException(java.sql.SQLException) InvalidUpdateException(org.activityinfo.store.query.server.InvalidUpdateException) ParseException(com.vividsolutions.jts.io.ParseException)

Example 100 with ResourceId

use of org.activityinfo.model.resource.ResourceId 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)

Aggregations

ResourceId (org.activityinfo.model.resource.ResourceId)198 Test (org.junit.Test)42 FormClass (org.activityinfo.model.form.FormClass)41 FormField (org.activityinfo.model.form.FormField)34 RecordRef (org.activityinfo.model.type.RecordRef)30 JsonValue (org.activityinfo.json.JsonValue)21 FormTree (org.activityinfo.model.formTree.FormTree)18 ReferenceValue (org.activityinfo.model.type.ReferenceValue)17 FieldValue (org.activityinfo.model.type.FieldValue)16 FormInstance (org.activityinfo.model.form.FormInstance)15 HashMap (java.util.HashMap)14 QuantityType (org.activityinfo.model.type.number.QuantityType)13 ColumnSet (org.activityinfo.model.query.ColumnSet)12 QueryModel (org.activityinfo.model.query.QueryModel)12 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)11 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)11 ReferenceType (org.activityinfo.model.type.ReferenceType)10 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)10 FormStorage (org.activityinfo.store.spi.FormStorage)10 ArrayList (java.util.ArrayList)9